Introduce experimental new feature: GTK2 tree view based packet list
[metze/wireshark/wip.git] / capture.c
1 /* capture.c
2  * Routines for packet capture
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #ifdef HAVE_LIBPCAP
30
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38
39 #ifdef HAVE_FCNTL_H
40 #include <fcntl.h>
41 #endif
42
43 #ifdef HAVE_SYS_TYPES_H
44 #include <sys/types.h>
45 #endif
46
47 #ifdef HAVE_NETINET_IN_H
48 #include <netinet/in.h>
49 #endif
50
51 #ifdef HAVE_NETDB_H
52 #include <netdb.h>
53 #endif
54
55 #ifdef HAVE_ARPA_INET_H
56 #include <arpa/inet.h>
57 #endif
58
59 #ifdef HAVE_SYS_SOCKET_H
60 #include <sys/socket.h>         /* needed to define AF_ values on UNIX */
61 #endif
62
63 #ifdef HAVE_WINSOCK2_H
64 #include <winsock2.h>           /* needed to define AF_ values on Windows */
65 #endif
66
67 #ifdef NEED_INET_V6DEFS_H
68 # include "inet_v6defs.h"
69 #endif
70
71 #include <signal.h>
72 #include <errno.h>
73
74 #include <glib.h>
75
76 #include <epan/packet.h>
77 #include <epan/dfilter/dfilter.h>
78 #include "file.h"
79 #include "capture.h"
80 #include "capture_sync.h"
81 #include "capture_info.h"
82 #include "capture_ui_utils.h"
83 #include "util.h"
84 #include "capture-pcap-util.h"
85 #include "simple_dialog.h"
86 #include <epan/prefs.h>
87
88 #ifdef _WIN32
89 #include "capture-wpcap.h"
90 #endif
91 #include "ui_util.h"
92 #include "wsutil/file_util.h"
93 #include "log.h"
94
95 typedef struct if_stat_cache_item_s {
96     char *name;
97     struct pcap_stat ps;
98 } if_stat_cache_item_t;
99
100 struct if_stat_cache_s {
101     int stat_fd;
102     int fork_child;
103     GList *cache_list;  /* List of if_stat_chache_entry_t */
104 };
105
106 /* this callback mechanism should possibly be replaced by the g_signal_...() stuff (if I only would know how :-) */
107 typedef struct {
108     capture_callback_t cb_fct;
109     gpointer user_data;
110 } capture_callback_data_t;
111
112 static GList *capture_callbacks = NULL;
113
114 static void
115 capture_callback_invoke(int event, capture_options *capture_opts)
116 {
117     capture_callback_data_t *cb;
118     GList *cb_item = capture_callbacks;
119
120     /* there should be at least one interested */
121     g_assert(cb_item != NULL);
122
123     while(cb_item != NULL) {
124         cb = cb_item->data;
125         cb->cb_fct(event, capture_opts, cb->user_data);
126         cb_item = g_list_next(cb_item);
127     }
128 }
129
130
131 void
132 capture_callback_add(capture_callback_t func, gpointer user_data)
133 {
134     capture_callback_data_t *cb;
135
136     cb = g_malloc(sizeof(capture_callback_data_t));
137     cb->cb_fct = func;
138     cb->user_data = user_data;
139
140     capture_callbacks = g_list_append(capture_callbacks, cb);
141 }
142
143 void
144 capture_callback_remove(capture_callback_t func)
145 {
146     capture_callback_data_t *cb;
147     GList *cb_item = capture_callbacks;
148
149     while(cb_item != NULL) {
150         cb = cb_item->data;
151         if(cb->cb_fct == func) {
152             capture_callbacks = g_list_remove(capture_callbacks, cb);
153             g_free(cb);
154             return;
155         }
156         cb_item = g_list_next(cb_item);
157     }
158
159     g_assert_not_reached();
160 }
161
162 /**
163  * Start a capture.
164  *
165  * @return TRUE if the capture starts successfully, FALSE otherwise.
166  */
167 gboolean
168 capture_start(capture_options *capture_opts)
169 {
170   gboolean ret;
171
172
173   /* close the currently loaded capture file */
174   cf_close(capture_opts->cf);
175
176   g_assert(capture_opts->state == CAPTURE_STOPPED);
177   capture_opts->state = CAPTURE_PREPARING;
178
179   g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Start ...");
180
181   /* try to start the capture child process */
182   ret = sync_pipe_start(capture_opts);
183   if(!ret) {
184       if(capture_opts->save_file != NULL) {
185           g_free(capture_opts->save_file);
186           capture_opts->save_file = NULL;
187       }
188
189       g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Start failed!");
190       capture_opts->state = CAPTURE_STOPPED;
191   } else {
192       /* the capture child might not respond shortly after bringing it up */
193       /* (for example: it will block if no input arrives from an input capture pipe (e.g. mkfifo)) */
194
195       /* to prevent problems, bring the main GUI into "capture mode" right after a successful */
196       /* spawn/exec of the capture child, without waiting for any response from it */
197       capture_callback_invoke(capture_cb_capture_prepared, capture_opts);
198
199       if(capture_opts->show_info)
200         capture_info_open(capture_opts);
201   }
202
203   return ret;
204 }
205
206
207 void
208 capture_stop(capture_options *capture_opts)
209 {
210   g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Stop ...");
211
212   capture_callback_invoke(capture_cb_capture_stopping, capture_opts);
213
214   /* stop the capture child gracefully */
215   sync_pipe_stop(capture_opts);
216 }
217
218
219 void
220 capture_restart(capture_options *capture_opts)
221 {
222     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Restart");
223
224     capture_opts->restart = TRUE;
225     capture_stop(capture_opts);
226 }
227
228
229 void
230 capture_kill_child(capture_options *capture_opts)
231 {
232   g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_INFO, "Capture Kill");
233
234   /* kill the capture child */
235   sync_pipe_kill(capture_opts->fork_child);
236 }
237
238
239
240 /* We've succeeded in doing a (non real-time) capture; try to read it into a new capture file */
241 static gboolean
242 capture_input_read_all(capture_options *capture_opts, gboolean is_tempfile, gboolean drops_known,
243 guint32 drops)
244 {
245   int err;
246
247
248   /* Capture succeeded; attempt to open the capture file. */
249   if (cf_open(capture_opts->cf, capture_opts->save_file, is_tempfile, &err) != CF_OK) {
250     /* We're not doing a capture any more, so we don't have a save file. */
251     return FALSE;
252   }
253
254   /* Set the read filter to NULL. */
255   /* XXX - this is odd here; try to put it somewhere where it fits better */
256   cf_set_rfcode(capture_opts->cf, NULL);
257
258   /* Get the packet-drop statistics.
259
260      XXX - there are currently no packet-drop statistics stored
261      in libpcap captures, and that's what we're reading.
262
263      At some point, we will add support in Wiretap to return
264      packet-drop statistics for capture file formats that store it,
265      and will make "cf_read()" get those statistics from Wiretap.
266      We clear the statistics (marking them as "not known") in
267      "cf_open()", and "cf_read()" will only fetch them and mark
268      them as known if Wiretap supplies them, so if we get the
269      statistics now, after calling "cf_open()" but before calling
270      "cf_read()", the values we store will be used by "cf_read()".
271
272      If a future libpcap capture file format stores the statistics,
273      we'll put them into the capture file that we write, and will
274      thus not have to set them here - "cf_read()" will get them from
275      the file and use them. */
276   if (drops_known) {
277     cf_set_drops_known(capture_opts->cf, TRUE);
278
279     /* XXX - on some systems, libpcap doesn't bother filling in
280        "ps_ifdrop" - it doesn't even set it to zero - so we don't
281        bother looking at it.
282
283        Ideally, libpcap would have an interface that gave us
284        several statistics - perhaps including various interface
285        error statistics - and would tell us which of them it
286        supplies, allowing us to display only the ones it does. */
287     cf_set_drops(capture_opts->cf, drops);
288   }
289
290   /* read in the packet data */
291   switch (cf_read(capture_opts->cf)) {
292
293   case CF_READ_OK:
294   case CF_READ_ERROR:
295     /* Just because we got an error, that doesn't mean we were unable
296        to read any of the file; we handle what we could get from the
297        file. */
298     break;
299
300   case CF_READ_ABORTED:
301     /* User wants to quit program. Exit by leaving the main loop,
302        so that any quit functions we registered get called. */
303     main_window_nested_quit();
304     return FALSE;
305   }
306
307   /* if we didn't capture even a single packet, close the file again */
308   if(cf_get_packet_count(capture_opts->cf) == 0 && !capture_opts->restart) {
309     simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
310 "%sNo packets captured!%s\n"
311 "\n"
312 "As no data was captured, closing the %scapture file!\n"
313 "\n"
314 "\n"
315 "Help about capturing can be found at:\n"
316 "\n"
317 "       http://wiki.wireshark.org/CaptureSetup"
318 #ifdef _WIN32
319 "\n\n"
320 "Wireless (Wi-Fi/WLAN):\n"
321 "Try to switch off promiscuous mode in the Capture Options!"
322 #endif
323 "",
324     simple_dialog_primary_start(), simple_dialog_primary_end(),
325     (cf_is_tempfile(capture_opts->cf)) ? "temporary " : "");
326     cf_close(capture_opts->cf);
327   }
328   return TRUE;
329 }
330
331
332 /* capture child tells us we have a new (or the first) capture file */
333 gboolean
334 capture_input_new_file(capture_options *capture_opts, gchar *new_file)
335 {
336   gboolean is_tempfile;
337   int  err;
338
339
340   if(capture_opts->state == CAPTURE_PREPARING) {
341     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture started!");
342   }
343   g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "File: \"%s\"", new_file);
344
345   g_assert(capture_opts->state == CAPTURE_PREPARING || capture_opts->state == CAPTURE_RUNNING);
346
347   /* free the old filename */
348   if(capture_opts->save_file != NULL) {
349     /* we start a new capture file, close the old one (if we had one before). */
350     /* (we can only have an open capture file in real_time_mode!) */
351     if( ((capture_file *) capture_opts->cf)->state != FILE_CLOSED) {
352         capture_callback_invoke(capture_cb_capture_update_finished, capture_opts);
353         cf_finish_tail(capture_opts->cf, &err);
354         cf_close(capture_opts->cf);
355     }
356     g_free(capture_opts->save_file);
357     is_tempfile = FALSE;
358     cf_set_tempfile(capture_opts->cf, FALSE);
359   } else {
360     /* we didn't have a save_file before; must be a tempfile */
361     is_tempfile = TRUE;
362     cf_set_tempfile(capture_opts->cf, TRUE);
363   }
364
365   /* save the new filename */
366   capture_opts->save_file = g_strdup(new_file);
367
368   /* if we are in real-time mode, open the new file now */
369   if(capture_opts->real_time_mode) {
370     /* Attempt to open the capture file and set up to read from it. */
371     switch(cf_start_tail(capture_opts->cf, capture_opts->save_file, is_tempfile, &err)) {
372     case CF_OK:
373       break;
374     case CF_ERROR:
375       /* Don't unlink (delete) the save file - leave it around,
376          for debugging purposes. */
377       g_free(capture_opts->save_file);
378       capture_opts->save_file = NULL;
379       return FALSE;
380     }
381   }
382
383   if(capture_opts->show_info) {
384     if (!capture_info_new_file(new_file))
385       return FALSE;
386   }
387
388   if(capture_opts->real_time_mode) {
389     capture_callback_invoke(capture_cb_capture_update_started, capture_opts);
390   } else {
391     capture_callback_invoke(capture_cb_capture_fixed_started, capture_opts);
392   }
393   capture_opts->state = CAPTURE_RUNNING;
394
395   return TRUE;
396 }
397
398
399 /* capture child tells us we have new packets to read */
400 void
401 capture_input_new_packets(capture_options *capture_opts, int to_read)
402 {
403   int  err;
404
405
406   g_assert(capture_opts->save_file);
407
408   if(capture_opts->real_time_mode) {
409     /* Read from the capture file the number of records the child told us it added. */
410     switch (cf_continue_tail(capture_opts->cf, to_read, &err)) {
411
412     case CF_READ_OK:
413     case CF_READ_ERROR:
414       /* Just because we got an error, that doesn't mean we were unable
415          to read any of the file; we handle what we could get from the
416          file.
417
418          XXX - abort on a read error? */
419          capture_callback_invoke(capture_cb_capture_update_continue, capture_opts);
420       break;
421
422     case CF_READ_ABORTED:
423       /* Kill the child capture process; the user wants to exit, and we
424          shouldn't just leave it running. */
425       capture_kill_child(capture_opts);
426       break;
427     }
428   } else {
429     /* increase the capture file packet counter by the number of incoming packets */
430     cf_set_packet_count(capture_opts->cf,
431         cf_get_packet_count(capture_opts->cf) + to_read);
432
433     capture_callback_invoke(capture_cb_capture_fixed_continue, capture_opts);
434   }
435
436   /* update the main window so we get events (e.g. from the stop toolbar button) */
437   main_window_update();
438
439   if(capture_opts->show_info)
440     capture_info_new_packets(to_read);
441 }
442
443
444 /* Capture child told us how many dropped packets it counted.
445  */
446 void
447 capture_input_drops(capture_options *capture_opts, guint32 dropped)
448 {
449   g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_INFO, "%u packet%s dropped", dropped, plurality(dropped, "", "s"));
450
451   g_assert(capture_opts->state == CAPTURE_RUNNING);
452
453   cf_set_drops_known(capture_opts->cf, TRUE);
454   cf_set_drops(capture_opts->cf, dropped);
455 }
456
457
458 /* Capture child told us that an error has occurred while starting/running
459    the capture.
460    The buffer we're handed has *two* null-terminated strings in it - a
461    primary message and a secondary message, one right after the other.
462    The secondary message might be a null string.
463  */
464 void
465 capture_input_error_message(capture_options *capture_opts, char *error_msg, char *secondary_error_msg)
466 {
467   gchar *safe_error_msg;
468   gchar *safe_secondary_error_msg;
469
470   g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Error message from child: \"%s\", \"%s\"",
471         error_msg, secondary_error_msg);
472
473   g_assert(capture_opts->state == CAPTURE_PREPARING || capture_opts->state == CAPTURE_RUNNING);
474
475   safe_error_msg = simple_dialog_format_message(error_msg);
476   if (*secondary_error_msg != '\0') {
477     /* We have both primary and secondary messages. */
478     safe_secondary_error_msg = simple_dialog_format_message(secondary_error_msg);
479     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s%s%s\n\n%s",
480                   simple_dialog_primary_start(), safe_error_msg,
481                   simple_dialog_primary_end(), safe_secondary_error_msg);
482     g_free(safe_secondary_error_msg);
483   } else {
484     /* We have only a primary message. */
485     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s%s%s",
486                   simple_dialog_primary_start(), safe_error_msg,
487                   simple_dialog_primary_end());
488   }
489   g_free(safe_error_msg);
490
491   /* the capture child will close the sync_pipe if required, nothing to do for now */
492 }
493
494
495
496 /* Capture child told us that an error has occurred while parsing a
497    capture filter when starting/running the capture.
498  */
499 void
500 capture_input_cfilter_error_message(capture_options *capture_opts, char *error_message)
501 {
502   dfilter_t   *rfcode = NULL;
503   gchar *safe_cfilter = simple_dialog_format_message(capture_opts->cfilter);
504   gchar *safe_cfilter_error_msg = simple_dialog_format_message(error_message);
505
506   g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture filter error message from child: \"%s\"", error_message);
507
508   g_assert(capture_opts->state == CAPTURE_PREPARING || capture_opts->state == CAPTURE_RUNNING);
509
510   /* Did the user try a display filter? */
511   if (dfilter_compile(capture_opts->cfilter, &rfcode) && rfcode != NULL) {
512     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
513       "%sInvalid capture filter: \"%s\"!%s\n"
514       "\n"
515       "That string looks like a valid display filter; however, it isn't a valid\n"
516       "capture filter (%s).\n"
517       "\n"
518       "Note that display filters and capture filters don't have the same syntax,\n"
519       "so you can't use most display filter expressions as capture filters.\n"
520       "\n"
521       "See the User's Guide for a description of the capture filter syntax.",
522       simple_dialog_primary_start(), safe_cfilter,
523       simple_dialog_primary_end(), safe_cfilter_error_msg);
524       dfilter_free(rfcode);
525   } else {
526     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
527       "%sInvalid capture filter: \"%s\"!%s\n"
528       "\n"
529       "That string isn't a valid capture filter (%s).\n"
530       "See the User's Guide for a description of the capture filter syntax.",
531       simple_dialog_primary_start(), safe_cfilter,
532       simple_dialog_primary_end(), safe_cfilter_error_msg);
533   }
534   g_free(safe_cfilter_error_msg);
535   g_free(safe_cfilter);
536
537   /* the capture child will close the sync_pipe if required, nothing to do for now */
538 }
539
540
541 /* capture child closed its side of the pipe, do the required cleanup */
542 void
543 capture_input_closed(capture_options *capture_opts)
544 {
545     int  err;
546     int  packet_count_save;
547
548     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture stopped!");
549     g_assert(capture_opts->state == CAPTURE_PREPARING || capture_opts->state == CAPTURE_RUNNING);
550
551     /* if we didn't start the capture, do a fake start. */
552     /* (happens if we got an error message - we won't get a filename then). */
553     if(capture_opts->state == CAPTURE_PREPARING) {
554         if(capture_opts->real_time_mode) {
555             capture_callback_invoke(capture_cb_capture_update_started, capture_opts);
556         } else {
557             capture_callback_invoke(capture_cb_capture_fixed_started, capture_opts);
558         }
559     }
560
561     if(capture_opts->real_time_mode) {
562         cf_read_status_t status;
563
564         /* Read what remains of the capture file. */
565         status = cf_finish_tail(capture_opts->cf, &err);
566
567         /* XXX: If -Q (quit-after-cap) then cf->count clr'd below so save it first */
568         packet_count_save = cf_get_packet_count(capture_opts->cf);
569         /* Tell the GUI we are not doing a capture any more.
570                    Must be done after the cf_finish_tail(), so file lengths are 
571                    correctly displayed */
572         capture_callback_invoke(capture_cb_capture_update_finished, capture_opts);
573
574         /* Finish the capture. */
575         switch (status) {
576
577         case CF_READ_OK:
578             if ((packet_count_save == 0) && !capture_opts->restart) {
579                 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
580 "%sNo packets captured!%s\n"
581 "\n"
582 "As no data was captured, closing the %scapture file!\n"
583 "\n"
584 "\n"
585 "Help about capturing can be found at:\n"
586 "\n"
587 "       http://wiki.wireshark.org/CaptureSetup"
588 #ifdef _WIN32
589 "\n\n"
590 "Wireless (Wi-Fi/WLAN):\n"
591 "Try to switch off promiscuous mode in the Capture Options!"
592 #endif
593 "",
594               simple_dialog_primary_start(), simple_dialog_primary_end(),
595               cf_is_tempfile(capture_opts->cf) ? "temporary " : "");
596               cf_close(capture_opts->cf);
597             }
598             break;
599         case CF_READ_ERROR:
600           /* Just because we got an error, that doesn't mean we were unable
601              to read any of the file; we handle what we could get from the
602              file. */
603           break;
604
605         case CF_READ_ABORTED:
606           /* Exit by leaving the main loop, so that any quit functions
607              we registered get called. */
608           main_window_quit();
609         }
610
611     } else {
612         /* first of all, we are not doing a capture any more */
613         capture_callback_invoke(capture_cb_capture_fixed_finished, capture_opts);
614
615         /* this is a normal mode capture and if no error happened, read in the capture file data */
616         if(capture_opts->save_file != NULL) {
617             capture_input_read_all(capture_opts, cf_is_tempfile(capture_opts->cf),
618                 cf_get_drops_known(capture_opts->cf), cf_get_drops(capture_opts->cf));
619         }
620     }
621
622     if(capture_opts->show_info)
623       capture_info_close();
624
625     capture_opts->state = CAPTURE_STOPPED;
626
627     /* if we couldn't open a capture file, there's nothing more for us to do */
628     if(capture_opts->save_file == NULL) {
629         cf_close(capture_opts->cf);
630         return;
631     }
632
633     /* does the user wants to restart the current capture? */
634     if(capture_opts->restart) {
635         capture_opts->restart = FALSE;
636
637         ws_unlink(capture_opts->save_file);
638
639         /* if it was a tempfile, throw away the old filename (so it will become a tempfile again) */
640         if(cf_is_tempfile(capture_opts->cf)) {
641             g_free(capture_opts->save_file);
642             capture_opts->save_file = NULL;
643         }
644
645         /* ... and start the capture again */
646         capture_start(capture_opts);
647     } else {
648         /* We're not doing a capture any more, so we don't have a save file. */
649         g_free(capture_opts->save_file);
650         capture_opts->save_file = NULL;
651     }
652 }
653
654 /**
655  * Fetch the interface list from a child process (dumpcap).
656  *
657  * @return A GList containing if_info_t structs if successful, NULL (with err and possibly err_str set) otherwise.
658  *
659  */
660
661 /* XXX - We parse simple text output to get our interface list.  Should
662  * we use "real" data serialization instead, e.g. via XML? */
663 GList *
664 capture_interface_list(int *err, char **err_str)
665 {
666     GList     *if_list = NULL;
667     int        i, j;
668     gchar     *msg;
669     gchar    **raw_list, **if_parts, **addr_parts;
670     gchar     *name;
671     if_info_t *if_info;
672     if_addr_t *if_addr;
673
674     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface List ...");
675
676     /* Try to get our interface list */
677     *err = sync_interface_list_open(&msg);
678     if (*err != 0) {
679         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface List failed!");
680         if (err_str) {
681             *err_str = msg;
682         } else {
683             g_free(msg);
684         }
685         return NULL;
686     }
687
688     /* Split our lines */
689 #ifdef _WIN32
690     raw_list = g_strsplit(msg, "\r\n", 0);
691 #else
692     raw_list = g_strsplit(msg, "\n", 0);
693 #endif
694     g_free(msg);
695
696     for (i = 0; raw_list[i] != NULL; i++) {
697         if_parts = g_strsplit(raw_list[i], "\t", 4);
698         if (if_parts[0] == NULL || if_parts[1] == NULL || if_parts[2] == NULL ||
699                 if_parts[3] == NULL) {
700             g_strfreev(if_parts);
701             continue;
702         }
703
704         /* Number followed by the name, e.g "1. eth0" */
705         name = strchr(if_parts[0], ' ');
706         if (name) {
707             name++;
708         } else {
709             g_strfreev(if_parts);
710             continue;
711         }
712
713         if_info = g_malloc0(sizeof(if_info_t));
714         if_info->name = g_strdup(name);
715         if (strlen(if_parts[1]) > 0)
716             if_info->description = g_strdup(if_parts[1]);
717         addr_parts = g_strsplit(if_parts[2], ",", 0);
718         for (j = 0; addr_parts[j] != NULL; j++) {
719             if_addr = g_malloc0(sizeof(if_addr_t));
720             if (inet_pton(AF_INET, addr_parts[j], &if_addr->ip_addr.ip4_addr)) {
721                 if_addr->type = AT_IPv4;
722             } else if (inet_pton(AF_INET6, addr_parts[j],
723                     &if_addr->ip_addr.ip6_addr)) {
724                 if_addr->type = AT_IPv6;
725             } else {
726                 g_free(if_addr);
727                 if_addr = NULL;
728             }
729             if (if_addr) {
730                 if_info->ip_addr = g_slist_append(if_info->ip_addr, if_addr);
731             }
732         }
733         if (strcmp(if_parts[3], "loopback") == 0)
734             if_info->loopback = TRUE;
735         g_strfreev(if_parts);
736         g_strfreev(addr_parts);
737         if_list = g_list_append(if_list, if_info);
738     }
739     g_strfreev(raw_list);
740
741     /* Check to see if we built a list */
742     if (if_list == NULL) {
743         *err = NO_INTERFACES_FOUND;
744         if (err_str)
745             *err_str = g_strdup("No interfaces found");
746     }
747     return if_list;
748 }
749
750 /* XXX - We parse simple text output to get our interface list.  Should
751  * we use "real" data serialization instead, e.g. via XML? */
752 GList *
753 capture_pcap_linktype_list(const gchar *ifname, char **err_str)
754 {
755     GList     *linktype_list = NULL;
756     int        err, i;
757     gchar     *msg;
758     gchar    **raw_list, **lt_parts;
759     data_link_info_t *data_link_info;
760
761     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface List ...");
762
763     /* Try to get our interface list */
764     err = sync_linktype_list_open(ifname, &msg);
765     if (err != 0) {
766         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface List failed!");
767         if (err_str) {
768             *err_str = msg;
769         } else {
770             g_free(msg);
771         }
772         return NULL;
773     }
774
775     /* Split our lines */
776 #ifdef _WIN32
777     raw_list = g_strsplit(msg, "\r\n", 0);
778 #else
779     raw_list = g_strsplit(msg, "\n", 0);
780 #endif
781     g_free(msg);
782
783     for (i = 0; raw_list[i] != NULL; i++) {
784         /* ...and what if the interface name has a tab in it, Mr. Clever Programmer? */
785         lt_parts = g_strsplit(raw_list[i], "\t", 3);
786         if (lt_parts[0] == NULL || lt_parts[1] == NULL || lt_parts[2] == NULL) {
787             g_strfreev(lt_parts);
788             continue;
789         }
790
791         data_link_info = g_malloc(sizeof (data_link_info_t));
792         data_link_info->dlt = (int) strtol(lt_parts[0], NULL, 10);
793         data_link_info->name = g_strdup(lt_parts[1]);
794         if (strcmp(lt_parts[2], "(not supported)") != 0)
795             data_link_info->description = g_strdup(lt_parts[2]);
796         else
797             data_link_info->description = NULL;
798
799         linktype_list = g_list_append(linktype_list, data_link_info);
800     }
801     g_strfreev(raw_list);
802
803     /* Check to see if we built a list */
804     if (linktype_list == NULL) {
805         if (err_str)
806             *err_str = NULL;
807     }
808     return linktype_list;
809 }
810
811 if_stat_cache_t *
812 capture_stat_start(GList *if_list) {
813     int stat_fd, fork_child;
814     gchar *msg;
815     if_stat_cache_t *sc = NULL;
816     GList *if_entry;
817     if_info_t *if_info;
818     if_stat_cache_item_t *sc_item;
819
820     /* Fire up dumpcap. */
821     /*
822      * XXX - on systems with BPF, the number of BPF devices limits the
823      * number of devices on which you can capture simultaneously.
824      *
825      * This means that
826      *
827      *  1) this might fail if you run out of BPF devices
828      *
829      * and
830      *
831      *  2) opening every interface could leave too few BPF devices
832      *     for *other* programs.
833      *
834      * It also means the system could end up getting a lot of traffic
835      * that it has to pass through the networking stack and capture
836      * mechanism, so opening all the devices and presenting packet
837      * counts might not always be a good idea.
838      */
839      if (sync_interface_stats_open(&stat_fd, &fork_child, &msg) == 0) {
840         sc = g_malloc(sizeof(if_stat_cache_t));
841         sc->stat_fd = stat_fd;
842         sc->fork_child = fork_child;
843         sc->cache_list = NULL;
844
845         /* Initialize the cache */
846         for (if_entry = if_list; if_entry != NULL; if_entry = g_list_next(if_entry)) {
847             if_info = if_entry->data;
848             sc_item = g_malloc0(sizeof(if_stat_cache_item_t));
849             sc_item->name = g_strdup(if_info->name);
850             sc->cache_list = g_list_append(sc->cache_list, sc_item);
851         }
852     }
853     return sc;
854 }
855
856 #define MAX_STAT_LINE_LEN 500
857
858 static void
859 capture_stat_cache_update(if_stat_cache_t *sc) {
860     gchar stat_line[MAX_STAT_LINE_LEN];
861     gchar **stat_parts;
862     GList *sc_entry;
863     if_stat_cache_item_t *sc_item;
864
865     if (!sc)
866         return;
867
868     while (sync_pipe_gets_nonblock(sc->stat_fd, stat_line, MAX_STAT_LINE_LEN) > 0) {
869         g_strstrip(stat_line);
870         stat_parts = g_strsplit(stat_line, "\t", 3);
871         if (stat_parts[0] == NULL || stat_parts[1] == NULL ||
872             stat_parts[2] == NULL) {
873             g_strfreev(stat_parts);
874             continue;
875         }
876         for (sc_entry = sc->cache_list; sc_entry != NULL; sc_entry = g_list_next(sc_entry)) {
877             sc_item = sc_entry->data;
878             if (strcmp(sc_item->name, stat_parts[0]) == 0) {
879                 sc_item->ps.ps_recv = (u_int) strtoul(stat_parts[1], NULL, 10);
880                 sc_item->ps.ps_drop = (u_int) strtoul(stat_parts[2], NULL, 10);
881             }
882         }
883         g_strfreev(stat_parts);
884     }
885 }
886
887 gboolean
888 capture_stats(if_stat_cache_t *sc, char *ifname, struct pcap_stat *ps) {
889     GList *sc_entry;
890     if_stat_cache_item_t *sc_item;
891
892     if (!sc || !ifname || !ps) {
893         return FALSE;
894     }
895
896     capture_stat_cache_update(sc);
897     for (sc_entry = sc->cache_list; sc_entry != NULL; sc_entry = g_list_next(sc_entry)) {
898         sc_item = sc_entry->data;
899         if (strcmp(sc_item->name, ifname) == 0) {
900             memcpy(ps, &sc_item->ps, sizeof(struct pcap_stat));
901             return TRUE;
902         }
903     }
904     return FALSE;
905 }
906
907 void
908 capture_stat_stop(if_stat_cache_t *sc) {
909     GList *sc_entry;
910     if_stat_cache_item_t *sc_item;
911     gchar *msg;
912
913     if (!sc)
914         return;
915
916     sync_interface_stats_close(&sc->stat_fd, &sc->fork_child, &msg);
917
918     for (sc_entry = sc->cache_list; sc_entry != NULL; sc_entry = g_list_next(sc_entry)) {
919         sc_item = sc_entry->data;
920         g_free(sc_item->name);
921         g_free(sc_item);
922     }
923     g_free(sc);
924 }
925
926 #endif /* HAVE_LIBPCAP */