where it's not necessary, remove #include "compat_macros.h"
[obnox/wireshark/wip.git] / gtk / follow_udp.c
1 /* follow_udp.c
2  * UDP specific routines for following traffic streams
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
23  * USA.
24  */
25
26 #include "config.h"
27
28 #include <gtk/gtk.h>
29
30 #include <string.h>
31
32 #include <epan/addr_resolv.h>
33 #include <epan/epan_dissect.h>
34 #include <epan/follow.h>
35 #include <epan/ipproto.h>
36 #include <epan/strutil.h>
37 #include <epan/tap.h>
38 #include "follow_stream.h"
39 #include <globals.h>
40 #include <gtk/keys.h>
41 #include <gtk/main.h>
42 #include <simple_dialog.h>
43
44 #include "follow_udp.h"
45
46
47 static int
48 udp_queue_packet_data(void *tapdata, packet_info *pinfo,
49                       epan_dissect_t *edt _U_, const void *data)
50 {
51         follow_record_t *follow_record;
52         follow_info_t *follow_info = tapdata;
53         const tvbuff_t *next_tvb = data;
54         
55         follow_record = g_malloc(sizeof(follow_record_t));
56
57         follow_record->data = g_byte_array_sized_new(next_tvb->length);
58         follow_record->data = g_byte_array_append(follow_record->data,
59                                                   next_tvb->real_data,
60                                                   next_tvb->length);
61
62         if (follow_info->client_port == 0) {
63                 follow_info->client_port = pinfo->srcport;
64                 COPY_ADDRESS(&follow_info->client_ip, &pinfo->src);
65         }
66
67         if (ADDRESSES_EQUAL(&follow_info->client_ip, &pinfo->src) &&
68             follow_info->client_port == pinfo->srcport)
69                 follow_record->is_server = FALSE;
70         else 
71                 follow_record->is_server = TRUE;
72
73         /* update stream counter */
74         follow_info->bytes_written[follow_record->is_server] +=
75                 follow_record->data->len;
76
77         follow_info->payload = g_list_append(follow_info->payload,
78                                              follow_record);
79         return 0;
80 }
81
82
83 /* Follow the UDP stream, if any, to which the last packet that we called
84    a dissection routine on belongs (this might be the most recently
85    selected packet, or it might be the last packet in the file). */
86 void
87 follow_udp_stream_cb(GtkWidget *w, gpointer data _U_)
88 {
89         GtkWidget *filter_te;
90         gchar *follow_filter;
91         const gchar *previous_filter;
92         int filter_out_filter_len, previous_filter_len;
93         const char *hostname0, *hostname1;
94         char *port0, *port1;
95         gchar *server_to_client_string = NULL;
96         gchar *client_to_server_string = NULL;
97         gchar *both_directions_string = NULL;
98         follow_stats_t stats;
99         follow_info_t *follow_info;
100         GString *msg;
101
102         /* we got udp so we can follow */
103         if(cfile.edt->pi.ipproto != IP_PROTO_UDP) {
104                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
105                               "Error following stream.  Please make\n"
106                               "sure you have a UDP packet selected.");
107                 return;
108         }
109         
110         follow_info = g_new0(follow_info_t, 1);
111         follow_info->follow_type = FOLLOW_UDP;
112
113         /* Create a new filter that matches all packets in the UDP stream,
114            and set the display filter entry accordingly */
115         follow_filter = build_follow_filter(&cfile.edt->pi);
116         if (!follow_filter)
117                 {
118                         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
119                                       "Error creating filter for this stream.\n"
120                                       "A network layer header is needed");
121                         return;
122                 }
123
124         /* Set the display filter entry accordingly */
125         filter_te = g_object_get_data(G_OBJECT(w), E_DFILTER_TE_KEY);
126
127         /* needed in follow_filter_out_stream(), is there a better way? */
128         follow_info->filter_te = filter_te;
129
130         /* save previous filter, const since we're not supposed to alter */
131         previous_filter =
132                 (const gchar *)gtk_entry_get_text(GTK_ENTRY(filter_te));
133
134         /* allocate our new filter. API claims g_malloc terminates program on failure */
135         /* my calc for max alloc needed is really +10 but when did a few extra bytes hurt ? */
136         previous_filter_len = previous_filter?strlen(previous_filter):0;
137         filter_out_filter_len = strlen(follow_filter) + previous_filter_len + 16;
138         follow_info->filter_out_filter = (gchar *)g_malloc(filter_out_filter_len);
139
140         /* append the negation */
141         if(previous_filter_len) {
142                 g_snprintf(follow_info->filter_out_filter, filter_out_filter_len,
143                            "%s and !(%s)", previous_filter, follow_filter);
144         } else {
145                 g_snprintf(follow_info->filter_out_filter, filter_out_filter_len,
146                            "!(%s)", follow_filter);
147         }
148
149         /* data will be passed via tap callback*/
150         msg = register_tap_listener("udp_follow", follow_info, follow_filter,
151                                     NULL, udp_queue_packet_data, NULL);
152         if (msg) {
153                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
154                               "Can't register udp_follow tap: %s\n",
155                               msg->str);
156                 return;
157         }
158
159         gtk_entry_set_text(GTK_ENTRY(filter_te), follow_filter);
160
161         /* Run the display filter so it goes in effect - even if it's the
162            same as the previous display filter. */
163         main_filter_packets(&cfile, follow_filter, TRUE);
164
165         /* Free the filter string, as we're done with it. */
166         g_free(follow_filter);
167
168         remove_tap_listener(follow_info);
169
170         /* Stream to show */
171         follow_stats(&stats);
172
173         if (stats.is_ipv6) {
174                 struct e_in6_addr ipaddr;
175                 memcpy(&ipaddr, stats.ip_address[0], 16);
176                 hostname0 = get_hostname6(&ipaddr);
177                 memcpy(&ipaddr, stats.ip_address[0], 16);
178                 hostname1 = get_hostname6(&ipaddr);
179         } else {
180                 guint32 ipaddr;
181                 memcpy(&ipaddr, stats.ip_address[0], 4);
182                 hostname0 = get_hostname(ipaddr);
183                 memcpy(&ipaddr, stats.ip_address[1], 4);
184                 hostname1 = get_hostname(ipaddr);
185         }
186     
187         port0 = get_udp_port(stats.port[0]);
188         port1 = get_udp_port(stats.port[1]);
189     
190         follow_info->is_ipv6 = stats.is_ipv6;
191
192         /* Both Stream Directions */
193         both_directions_string = g_strdup_printf("Entire conversation (%u bytes)", follow_info->bytes_written[0] + follow_info->bytes_written[1]);
194     
195         if(follow_info->client_port == stats.port[0]) {
196                 server_to_client_string =
197                         g_strdup_printf("%s:%s --> %s:%s (%u bytes)",
198                                         hostname0, port0,
199                                         hostname1, port1,
200                                         follow_info->bytes_written[0]);
201                 
202                 client_to_server_string =
203                         g_strdup_printf("%s:%s --> %s:%s (%u bytes)",
204                                         hostname1, port1,
205                                         hostname0, port0,
206                                         follow_info->bytes_written[1]);
207         } else {
208                 server_to_client_string =
209                         g_strdup_printf("%s:%s --> %s:%s (%u bytes)",
210                                         hostname1, port1,
211                                         hostname0, port0,
212                                         follow_info->bytes_written[0]);
213                 
214                 client_to_server_string =
215                         g_strdup_printf("%s:%s --> %s:%s (%u bytes)",
216                                         hostname0, port0,
217                                         hostname1, port1,
218                                         follow_info->bytes_written[1]);
219         }
220
221         follow_stream("Follow UDP Stream", follow_info, both_directions_string,
222                       server_to_client_string, client_to_server_string);
223
224         g_free(both_directions_string);
225         g_free(server_to_client_string);
226         g_free(client_to_server_string);
227 }
228
229 #define FLT_BUF_SIZE 1024
230
231 /*
232  * XXX - the routine pointed to by "print_line" doesn't get handed lines,
233  * it gets handed bufferfuls.  That's fine for "follow_write_raw()"
234  * and "follow_add_to_gtk_text()", but, as "follow_print_text()" calls
235  * the "print_line()" routine from "print.c", and as that routine might
236  * genuinely expect to be handed a line (if, for example, it's using
237  * some OS or desktop environment's printing API, and that API expects
238  * to be handed lines), "follow_print_text()" should probably accumulate
239  * lines in a buffer and hand them "print_line()".  (If there's a
240  * complete line in a buffer - i.e., there's nothing of the line in
241  * the previous buffer or the next buffer - it can just hand that to
242  * "print_line()" after filtering out non-printables, as an
243  * optimization.)
244  *
245  * This might or might not be the reason why C arrays display
246  * correctly but get extra blank lines very other line when printed.
247  */
248 frs_return_t
249 follow_read_udp_stream(follow_info_t *follow_info,
250                        gboolean (*print_line)(char *, size_t, gboolean, void *),
251                        void *arg)
252 {
253         int iplen;
254         guint32 global_client_pos = 0, global_server_pos = 0;
255         guint32 server_packet_count = 0;
256         guint32 client_packet_count = 0;
257         guint32 *global_pos;
258         gboolean skip;
259         GList* cur;
260         frs_return_t frs_return;
261         follow_record_t *follow_record;
262         char *buffer;
263
264         iplen = (follow_info->is_ipv6) ? 16 : 4;
265     
266         for (cur = follow_info->payload; cur; cur = g_list_next(cur)) {
267                 follow_record = cur->data;
268                 skip = FALSE;
269                 if (!follow_record->is_server) {
270                         global_pos = &global_client_pos;
271                         if(follow_info->show_stream == FROM_SERVER) {
272                                 skip = TRUE;
273                         }
274                 } else {
275                         global_pos = &global_server_pos;
276                         if (follow_info->show_stream == FROM_CLIENT) {
277                                 skip = TRUE;
278                         }
279                 }
280
281                 if (!skip) {
282                         buffer = g_memdup(follow_record->data->data,
283                                           follow_record->data->len);
284             
285                         frs_return = follow_show(follow_info, print_line,
286                                                  buffer,
287                                                  follow_record->data->len,
288                                                  follow_record->is_server, arg,
289                                                  global_pos, 
290                                                  &server_packet_count,
291                                                  &client_packet_count);
292                         g_free(buffer);
293                         if(frs_return == FRS_PRINT_ERROR)
294                                 return frs_return;
295                 }
296         }
297
298         return FRS_OK;
299 }