Close the file to which we're writing before exiting, so that:
[obnox/wireshark/wip.git] / tethereal.c
1 /* tethereal.c
2  *
3  * $Id: tethereal.c,v 1.91 2001/09/05 05:03:47 guy Exp $
4  *
5  * Ethereal - Network traffic analyzer
6  * By Gerald Combs <gerald@ethereal.com>
7  * Copyright 1998 Gerald Combs
8  *
9  * Text-mode variant, by Gilbert Ramirez <gram@xiexie.org>.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <locale.h>
35 #include <limits.h>
36
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40
41 #include <errno.h>
42
43 #ifdef HAVE_SYS_TYPES_H
44 #include <sys/types.h>
45 #endif
46
47 #ifdef HAVE_SYS_STAT_H
48 #include <sys/stat.h>
49 #endif
50
51 #ifdef HAVE_FCNTL_H
52 #include <fcntl.h>
53 #endif
54
55 #include <signal.h>
56
57 #ifdef HAVE_LIBPCAP
58 #include <pcap.h>
59 #endif
60
61 #ifdef HAVE_LIBZ
62 #include <zlib.h>       /* to get the libz version number */
63 #endif
64
65 #ifdef NEED_SNPRINTF_H
66 # include "snprintf.h"
67 #endif
68
69 #if defined(HAVE_UCD_SNMP_SNMP_H)
70 #ifdef HAVE_UCD_SNMP_VERSION_H
71 #include <ucd-snmp/version.h>
72 #endif /* HAVE_UCD_SNMP_VERSION_H */
73 #elif defined(HAVE_SNMP_SNMP_H)
74 #ifdef HAVE_SNMP_VERSION_H
75 #include <snmp/version.h>
76 #endif /* HAVE_SNMP_VERSION_H */
77 #endif /* SNMP */
78
79 #ifdef NEED_STRERROR_H
80 #include "strerror.h"
81 #endif
82
83 #ifdef NEED_GETOPT_H
84 #include "getopt.h"
85 #endif
86
87 #include <glib.h>
88 #include <epan.h>
89
90 #include "globals.h"
91 #include "timestamp.h"
92 #include "packet.h"
93 #include "file.h"
94 #include "prefs.h"
95 #include "column.h"
96 #include "print.h"
97 #include "resolv.h"
98 #include "util.h"
99 #include "conversation.h"
100 #include "reassemble.h"
101 #include "plugins.h"
102 #include "register.h"
103
104 #ifdef WIN32
105 #include "capture-wpcap.h"
106 #endif
107
108 static guint32 firstsec, firstusec;
109 static guint32 prevsec, prevusec;
110 static GString *comp_info_str;
111 static gboolean verbose;
112 static gboolean print_hex;
113 static gboolean line_buffered;
114
115 #ifdef HAVE_LIBPCAP
116 typedef struct _loop_data {
117   gint           linktype;
118   pcap_t        *pch;
119   wtap_dumper   *pdh;
120 } loop_data;
121
122 static loop_data ld;
123
124 static int capture(int, int);
125 static void capture_pcap_cb(u_char *, const struct pcap_pkthdr *,
126   const u_char *);
127 static void capture_cleanup(int);
128 #endif
129
130 typedef struct {
131   capture_file *cf;
132   wtap_dumper *pdh;
133 } cb_args_t;
134
135 static int load_cap_file(capture_file *, int);
136 static void wtap_dispatch_cb_write(u_char *, const struct wtap_pkthdr *, int,
137     union wtap_pseudo_header *, const u_char *);
138 static void show_capture_file_io_error(const char *, int, gboolean);
139 static void wtap_dispatch_cb_print(u_char *, const struct wtap_pkthdr *, int,
140     union wtap_pseudo_header *, const u_char *);
141
142 packet_info  pi;
143 capture_file cfile;
144 FILE        *data_out_file = NULL;
145 ts_type timestamp_type = RELATIVE;
146 #ifdef HAVE_LIBPCAP
147 static int promisc_mode = TRUE;
148 #endif
149
150 static void 
151 print_usage(void)
152 {
153   int i;
154
155   fprintf(stderr, "This is GNU t%s %s, compiled %s\n", PACKAGE, VERSION,
156         comp_info_str->str);
157 #ifdef HAVE_LIBPCAP
158   fprintf(stderr, "t%s [ -DvVhlp ] [ -c <count> ] [ -f <capture filter> ]\n", PACKAGE);
159   fprintf(stderr, "\t[ -F <capture file type> ] [ -i <interface> ] [ -n ] [ -N <resolving> ]\n");
160   fprintf(stderr, "\t[ -o <preference setting> ] ... [ -r <infile> ] [ -R <read filter> ]\n");
161   fprintf(stderr, "\t[ -s <snaplen> ] [ -t <time stamp format> ] [ -w <savefile> ] [ -x ]\n");
162 #else
163   fprintf(stderr, "t%s [ -vVhl ] [ -F <capture file type> ] [ -n ] [ -N <resolving> ]\n", PACKAGE);
164   fprintf(stderr, "\t[ -o <preference setting> ] ... [ -r <infile> ] [ -R <read filter> ]\n");
165   fprintf(stderr, "\t[ -t <time stamp format> ] [ -w <savefile> ] [ -x ]\n");
166 #endif
167   fprintf(stderr, "Valid file type arguments to the \"-F\" flag:\n");
168   for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
169     if (wtap_dump_can_open(i))
170       fprintf(stderr, "\t%s - %s\n",
171         wtap_file_type_short_string(i), wtap_file_type_string(i));
172   }
173   fprintf(stderr, "\tdefault is libpcap\n");
174 }
175
176 int
177 get_positive_int(const char *string, const char *name)
178 {
179   long number;
180   char *p;
181
182   number = strtol(string, &p, 10);
183   if (p == string || *p != '\0') {
184     fprintf(stderr, "tethereal: The specified %s \"%s\" is not a decimal number\n",
185             name, string);
186     exit(1);
187   }
188   if (number < 0) {
189     fprintf(stderr, "tethereal: The specified %s \"%s\" is a negative number\n",
190             name, string);
191     exit(1);
192   }
193   if (number > INT_MAX) {
194     fprintf(stderr, "tethereal: The specified %s \"%s\" is too large (greater than %d)\n",
195             name, string, INT_MAX);
196     exit(1);
197   }
198   return number;
199 }
200
201 int
202 main(int argc, char *argv[])
203 {
204   int                  opt, i;
205   extern char         *optarg;
206   gboolean             arg_error = FALSE;
207 #ifdef HAVE_LIBPCAP
208 #ifdef HAVE_PCAP_VERSION
209   extern char          pcap_version[];
210 #endif /* HAVE_PCAP_VERSION */
211 #endif /* HAVE_LIBPCAP */
212
213 #ifdef WIN32
214   WSADATA               wsaData;
215 #endif
216
217   char                *gpf_path, *pf_path;
218   int                  gpf_open_errno, pf_open_errno;
219   int                  err;
220 #ifdef HAVE_LIBPCAP
221   gboolean             capture_filter_specified = FALSE;
222   int                  packet_count = 0;
223   GList               *if_list, *if_entry;
224   gchar                err_str[PCAP_ERRBUF_SIZE];
225 #else
226   gboolean             capture_option_specified = FALSE;
227 #endif
228   int                  out_file_type = WTAP_FILE_PCAP;
229   gchar               *cf_name = NULL, *rfilter = NULL;
230   dfilter_t           *rfcode = NULL;
231   e_prefs             *prefs;
232   char                 badopt;
233
234   /* Register all dissectors; we must do this before checking for the
235      "-G" flag, as the "-G" flag dumps a list of fields registered
236      by the dissectors, and we must do it before we read the preferences,
237      in case any dissectors register preferences. */
238   epan_init(PLUGIN_DIR,register_all_protocols,register_all_protocol_handoffs);
239
240   /* Now register the preferences for any non-dissector modules.
241      We must do that before we read the preferences as well. */
242   prefs_register_modules();
243
244   /* If invoked with the "-G" flag, we dump out a glossary of
245      display filter symbols.
246
247      We do this here to mirror what happens in the GTK+ version, although
248      it's not necessary here. */
249   if (argc >= 2 && strcmp(argv[1], "-G") == 0) {
250     proto_registrar_dump();
251     exit(0);
252   }
253
254   /* Set the C-language locale to the native environment. */
255   setlocale(LC_ALL, "");
256
257   prefs = read_prefs(&gpf_open_errno, &gpf_path, &pf_open_errno, &pf_path);
258   if (gpf_path != NULL) {
259     fprintf(stderr, "Can't open global preferences file \"%s\": %s.\n", pf_path,
260         strerror(gpf_open_errno));
261   }
262   if (pf_path != NULL) {
263     fprintf(stderr, "Can't open your preferences file \"%s\": %s.\n", pf_path,
264         strerror(pf_open_errno));
265   }
266
267 #ifdef WIN32
268   /* Load Wpcap, if possible */
269   load_wpcap();
270 #endif
271     
272   /* Initialize the capture file struct */
273   cfile.plist           = NULL;
274   cfile.plist_end       = NULL;
275   cfile.wth             = NULL;
276   cfile.filename        = NULL;
277   cfile.user_saved      = FALSE;
278   cfile.is_tempfile     = FALSE;
279   cfile.rfcode          = NULL;
280   cfile.dfilter         = NULL;
281   cfile.dfcode          = NULL;
282 #ifdef HAVE_LIBPCAP
283   cfile.cfilter         = g_strdup("");
284 #endif
285   cfile.iface           = NULL;
286   cfile.save_file       = NULL;
287   cfile.save_file_fd    = -1;
288   cfile.snap            = WTAP_MAX_PACKET_SIZE;
289   cfile.count           = 0;
290   col_init(&cfile.cinfo, prefs->num_cols);
291
292   /* Assemble the compile-time options */
293   comp_info_str = g_string_new("");
294
295   g_string_append(comp_info_str, "with ");
296   g_string_sprintfa(comp_info_str,
297 #ifdef GLIB_MAJOR_VERSION
298     "GLib %d.%d.%d", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION,
299     GLIB_MICRO_VERSION);
300 #else
301     "GLib (version unknown)");
302 #endif
303
304 #ifdef HAVE_LIBPCAP
305   g_string_append(comp_info_str, ", with libpcap ");
306 #ifdef HAVE_PCAP_VERSION
307   g_string_append(comp_info_str, pcap_version);
308 #else /* HAVE_PCAP_VERSION */
309   g_string_append(comp_info_str, "(version unknown)");
310 #endif /* HAVE_PCAP_VERSION */
311 #else /* HAVE_LIBPCAP */
312   g_string_append(comp_info_str, ", without libpcap");
313 #endif /* HAVE_LIBPCAP */
314
315 #ifdef HAVE_LIBZ
316   g_string_append(comp_info_str, ", with libz ");
317 #ifdef ZLIB_VERSION
318   g_string_append(comp_info_str, ZLIB_VERSION);
319 #else /* ZLIB_VERSION */
320   g_string_append(comp_info_str, "(version unknown)");
321 #endif /* ZLIB_VERSION */
322 #else /* HAVE_LIBZ */
323   g_string_append(comp_info_str, ", without libz");
324 #endif /* HAVE_LIBZ */
325
326 /* Oh, this is pretty */
327 #if defined(HAVE_UCD_SNMP_SNMP_H)
328   g_string_append(comp_info_str, ", with UCD SNMP ");
329 #ifdef HAVE_UCD_SNMP_VERSION_H
330   g_string_append(comp_info_str, VersionInfo);
331 #else /* HAVE_UCD_SNMP_VERSION_H */
332   g_string_append(comp_info_str, "(version unknown)");
333 #endif /* HAVE_UCD_SNMP_VERSION_H */
334 #elif defined(HAVE_SNMP_SNMP_H)
335   g_string_append(comp_info_str, ", with CMU SNMP ");
336 #ifdef HAVE_SNMP_VERSION_H
337   g_string_append(comp_info_str, snmp_Version());
338 #else /* HAVE_SNMP_VERSION_H */
339   g_string_append(comp_info_str, "(version unknown)");
340 #endif /* HAVE_SNMP_VERSION_H */
341 #else /* no SNMP */
342   g_string_append(comp_info_str, ", without SNMP");
343 #endif
344     
345   /* Now get our args */
346   while ((opt = getopt(argc, argv, "c:Df:F:hi:lnN:o:pr:R:s:t:vw:Vx")) != EOF) {
347     switch (opt) {
348       case 'c':        /* Capture xxx packets */
349 #ifdef HAVE_LIBPCAP
350         packet_count = get_positive_int(optarg, "packet count");
351 #else
352         capture_option_specified = TRUE;
353         arg_error = TRUE;
354 #endif
355         break;
356       case 'D':        /* Print a list of capture devices */
357 #ifdef HAVE_LIBPCAP
358         if_list = get_interface_list(&err, err_str);
359         if (if_list == NULL) {
360             switch (err) {
361
362             case CANT_GET_INTERFACE_LIST:
363                 fprintf(stderr, "tethereal: Can't get list of interfaces: %s\n",
364                         err_str);
365                 break;
366
367             case NO_INTERFACES_FOUND:
368                 fprintf(stderr, "tethereal: There are no interfaces on which a capture can be done\n");
369                 break;
370             }
371             exit(2);
372         }
373         for (if_entry = g_list_first(if_list); if_entry != NULL;
374                 if_entry = g_list_next(if_entry))
375           printf("%s\n", (char *)if_entry->data);
376         free_interface_list(if_list);
377         exit(0);
378 #else
379         capture_option_specified = TRUE;
380         arg_error = TRUE;
381 #endif
382         break;
383       case 'f':
384 #ifdef HAVE_LIBPCAP
385         capture_filter_specified = TRUE;
386         cfile.cfilter = g_strdup(optarg);
387 #else
388         capture_option_specified = TRUE;
389         arg_error = TRUE;
390 #endif
391         break;
392       case 'F':
393         out_file_type = wtap_short_string_to_file_type(optarg);
394         if (out_file_type < 0) {
395           fprintf(stderr, "tethereal: \"%s\" is not a valid capture file type\n",
396                         optarg);
397           exit(1);
398         }
399         break;
400       case 'h':        /* Print help and exit */
401         print_usage();
402         exit(0);
403         break;
404       case 'i':        /* Use interface xxx */
405 #ifdef HAVE_LIBPCAP
406         cfile.iface = g_strdup(optarg);
407 #else
408         capture_option_specified = TRUE;
409         arg_error = TRUE;
410 #endif
411         break;
412       case 'l':        /* "Line-buffer" standard output */
413         /* This isn't line-buffering, strictly speaking, it's just
414            flushing the standard output after the information for
415            each packet is printed; however, that should be good
416            enough for all the purposes to which "-l" is put.
417
418            See the comment in "wtap_dispatch_cb_print()" for an
419            explanation of why we do that, and why we don't just
420            use "setvbuf()" to make the standard output line-buffered
421            (short version: in Windows, "line-buffered" is the same
422            as "fully-buffered", and the output buffer is only flushed
423            when it fills up). */
424         line_buffered = TRUE;
425         break;
426       case 'n':        /* No name resolution */
427         prefs->name_resolve = PREFS_RESOLV_NONE;
428         break;
429       case 'N':        /* Select what types of addresses/port #s to resolve */
430         if (prefs->name_resolve == PREFS_RESOLV_ALL)
431           prefs->name_resolve = PREFS_RESOLV_NONE;
432         badopt = string_to_name_resolve(optarg, &prefs->name_resolve);
433         if (badopt != '\0') {
434           fprintf(stderr, "tethereal: -N specifies unknown resolving option '%c'; valid options are 'm', 'n', and 't'\n",
435                         badopt);
436           exit(1);
437         }
438         break;
439       case 'o':        /* Override preference from command line */
440         switch (prefs_set_pref(optarg)) {
441
442         case PREFS_SET_SYNTAX_ERR:
443           fprintf(stderr, "tethereal: Invalid -o flag \"%s\"\n", optarg);
444           exit(1);
445           break;
446
447         case PREFS_SET_NO_SUCH_PREF:
448           fprintf(stderr, "tethereal: -o flag \"%s\" specifies unknown preference\n",
449                         optarg);
450           exit(1);
451           break;
452         }
453         break;
454       case 'p':        /* Don't capture in promiscuous mode */
455 #ifdef HAVE_LIBPCAP
456         promisc_mode = 0;
457 #else
458         capture_option_specified = TRUE;
459         arg_error = TRUE;
460 #endif
461         break;
462       case 'r':        /* Read capture file xxx */
463         cf_name = g_strdup(optarg);
464         break;
465       case 'R':        /* Read file filter */
466         rfilter = optarg;
467         break;
468       case 's':        /* Set the snapshot (capture) length */
469 #ifdef HAVE_LIBPCAP
470         cfile.snap = get_positive_int(optarg, "snapshot length");
471 #else
472         capture_option_specified = TRUE;
473         arg_error = TRUE;
474 #endif
475         break;
476       case 't':        /* Time stamp type */
477         if (strcmp(optarg, "r") == 0)
478           timestamp_type = RELATIVE;
479         else if (strcmp(optarg, "a") == 0)
480           timestamp_type = ABSOLUTE;
481         else if (strcmp(optarg, "ad") == 0)
482           timestamp_type = ABSOLUTE_WITH_DATE;
483         else if (strcmp(optarg, "d") == 0)
484           timestamp_type = DELTA;
485         else {
486           fprintf(stderr, "tethereal: Invalid time stamp type \"%s\"\n",
487             optarg);
488           fprintf(stderr, "It must be \"r\" for relative, \"a\" for absolute,\n");
489           fprintf(stderr, "\"ad\" for absolute with date, or \"d\" for delta.\n");
490           exit(1);
491         }
492         break;
493       case 'v':        /* Show version and exit */
494         printf("t%s %s, %s\n", PACKAGE, VERSION, comp_info_str->str);
495         exit(0);
496         break;
497       case 'w':        /* Write to capture file xxx */
498         cfile.save_file = g_strdup(optarg);
499         break;
500       case 'V':        /* Verbose */
501         verbose = TRUE;
502         break;
503       case 'x':        /* Print packet data in hex (and ASCII) */
504         print_hex = TRUE;
505         break;
506     }
507   }
508   
509   /* If no capture filter or read filter has been specified, and there are
510      still command-line arguments, treat them as the tokens of a capture
511      filter (if no "-r" flag was specified) or a read filter (if a "-r"
512      flag was specified. */
513   if (optind < argc) {
514     if (cf_name != NULL) {
515       if (rfilter != NULL) {
516         fprintf(stderr,
517 "tethereal: Read filters were specified both with \"-R\" and with additional command-line arguments\n");
518         exit(2);
519       }
520       rfilter = get_args_as_string(argc, argv, optind);
521     } else {
522 #ifdef HAVE_LIBPCAP
523       if (capture_filter_specified) {
524         fprintf(stderr,
525 "tethereal: Capture filters were specified both with \"-f\" and with additional command-line arguments\n");
526         exit(2);
527       }
528       cfile.cfilter = get_args_as_string(argc, argv, optind);
529 #else
530       capture_option_specified = TRUE;
531 #endif
532     }
533   }
534
535 #ifdef WIN32
536   /* Start windows sockets */
537   WSAStartup( MAKEWORD( 1, 1 ), &wsaData );
538 #endif
539
540   /* Notify all registered modules that have had any of their preferences
541      changed either from one of the preferences file or from the command
542      line that its preferences have changed. */
543   prefs_apply_all();
544
545 #ifndef HAVE_LIBPCAP
546   if (capture_option_specified)
547     fprintf(stderr, "This version of Tethereal was not built with support for capturing packets.\n");
548 #endif
549   if (arg_error)
550     print_usage();
551
552   /* Build the column format array */  
553   for (i = 0; i < cfile.cinfo.num_cols; i++) {
554     cfile.cinfo.col_fmt[i] = get_column_format(i);
555     cfile.cinfo.col_title[i] = g_strdup(get_column_title(i));
556     cfile.cinfo.fmt_matx[i] = (gboolean *) g_malloc0(sizeof(gboolean) *
557       NUM_COL_FMTS);
558     get_column_format_matches(cfile.cinfo.fmt_matx[i], cfile.cinfo.col_fmt[i]);
559     cfile.cinfo.col_data[i] = NULL;
560     if (cfile.cinfo.col_fmt[i] == COL_INFO)
561       cfile.cinfo.col_buf[i] = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_INFO_LEN);
562     else
563       cfile.cinfo.col_buf[i] = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_LEN);
564   }
565
566   if (cfile.snap < 1)
567     cfile.snap = WTAP_MAX_PACKET_SIZE;
568   else if (cfile.snap < MIN_PACKET_SIZE)
569     cfile.snap = MIN_PACKET_SIZE;
570   
571   if (rfilter != NULL) {
572     if (!dfilter_compile(rfilter, &rfcode)) {
573       fprintf(stderr, "tethereal: %s\n", dfilter_error_msg);
574       epan_cleanup();
575       exit(2);
576     }
577   }
578   cfile.rfcode = rfcode;
579   if (cf_name) {
580     err = open_cap_file(cf_name, FALSE, &cfile);
581     if (err != 0) {
582       epan_cleanup();
583       exit(2);
584     }
585     err = load_cap_file(&cfile, out_file_type);
586     if (err != 0) {
587       epan_cleanup();
588       exit(2);
589     }
590     cf_name[0] = '\0';
591   } else {
592     /* No capture file specified, so we're supposed to do a live capture;
593        do we have support for live captures? */
594 #ifdef HAVE_LIBPCAP
595
596 #ifdef _WIN32
597     if (!has_wpcap) {
598         fprintf(stderr, "tethereal: Could not load wpcap.dll.\n");
599         exit(2);
600     }
601 #endif
602
603     /* Yes; did the user specify an interface to use? */
604     if (cfile.iface == NULL) {
605         /* No - pick the first one from the list of interfaces. */
606         if_list = get_interface_list(&err, err_str);
607         if (if_list == NULL) {
608             switch (err) {
609
610             case CANT_GET_INTERFACE_LIST:
611                 fprintf(stderr, "tethereal: Can't get list of interfaces: %s\n",
612                         err_str);
613                 break;
614
615             case NO_INTERFACES_FOUND:
616                 fprintf(stderr, "tethereal: There are no interfaces on which a capture can be done\n");
617                 break;
618             }
619             exit(2);
620         }
621         cfile.iface = g_strdup(if_list->data);  /* first interface */
622         free_interface_list(if_list);
623     }
624     capture(packet_count, out_file_type);
625 #else
626     /* No - complain. */
627     fprintf(stderr, "This version of Tethereal was not built with support for capturing packets.\n");
628     exit(2);
629 #endif
630   }
631
632   epan_cleanup();
633
634   return 0;
635 }
636
637 #ifdef HAVE_LIBPCAP
638 /* Do the low-level work of a capture.
639    Returns TRUE if it succeeds, FALSE otherwise. */
640 static int
641 capture(int packet_count, int out_file_type)
642 {
643   gchar       err_str[PCAP_ERRBUF_SIZE];
644   bpf_u_int32 netnum, netmask;
645   struct bpf_program fcode;
646   void        (*oldhandler)(int);
647   int         err, inpkts;
648   char        errmsg[1024+1];
649 #ifndef _WIN32
650   static const char ppamsg[] = "can't find PPA for ";
651   char       *libpcap_warn;
652 #endif
653   struct pcap_stat stats;
654
655   /* Initialize the table of conversations. */
656   epan_conversation_init();
657
658   /* Initialize protocol-specific variables */
659   init_all_protocols();
660
661   /* Initialize the common data structures for fragment reassembly.
662      Must be done *after* "init_all_protocols()", as "init_all_protocols()"
663      may free up space for fragments, which it finds by using the
664      data structures that "reassemble_init()" frees. */
665   reassemble_init();
666
667   ld.linktype       = WTAP_ENCAP_UNKNOWN;
668   ld.pdh            = NULL;
669
670   /* Open the network interface to capture from it. */
671   ld.pch = pcap_open_live(cfile.iface, cfile.snap, promisc_mode, 1000, err_str);
672
673   if (ld.pch == NULL) {
674     /* Well, we couldn't start the capture. */
675 #ifdef _WIN32
676     /* On Win32 OSes, the capture devices are probably available to all
677        users; don't warn about permissions problems.
678
679        Do, however, warn that Token Ring and PPP devices aren't supported. */
680     snprintf(errmsg, sizeof errmsg,
681         "The capture session could not be initiated (%s).\n"
682         "Please check that you have the proper interface specified.\n"
683         "\n"
684         "Note that the driver Tethereal uses for packet capture on Windows\n"
685         "doesn't support capturing on Token Ring interfaces, and doesn't\n"
686         "support capturing on PPP/WAN interfaces in Windows NT/2000.\n",
687         err_str);
688 #else
689       /* If we got a "can't find PPA for XXX" message, warn the user (who
690          is running Ethereal on HP-UX) that they don't have a version
691          of libpcap that properly handles HP-UX (libpcap 0.6.x and later
692          versions, which properly handle HP-UX, say "can't find /dev/dlpi
693          PPA for XXX" rather than "can't find PPA for XXX"). */
694       if (strncmp(err_str, ppamsg, sizeof ppamsg - 1) == 0)
695         libpcap_warn =
696           "\n\n"
697           "You are running Tethereal with a version of the libpcap library\n"
698           "that doesn't handle HP-UX network devices well; this means that\n"
699           "Tethereal may not be able to capture packets.\n"
700           "\n"
701           "To fix this, you should install libpcap 0.6.2, or a later version\n"
702           "of libpcap, rather than libpcap 0.4 or 0.5.x.  It is available in\n"
703           "packaged binary form from the Software Porting And Archive Centre\n"
704           "for HP-UX; the Centre is at http://hpux.connect.org.uk/ - the page\n"
705           "at the URL lists a number of mirror sites.";
706       else
707         libpcap_warn = "";
708     snprintf(errmsg, sizeof errmsg,
709       "The capture session could not be initiated (%s).\n"
710       "Please check to make sure you have sufficient permissions, and that\n"
711       "you have the proper interface specified.%s", err_str, libpcap_warn);
712 #endif
713     goto error;
714   }
715
716   if (cfile.cfilter) {
717     /* A capture filter was specified; set it up. */
718     if (pcap_lookupnet (cfile.iface, &netnum, &netmask, err_str) < 0) {
719       /*
720        * Well, we can't get the netmask for this interface; it's used
721        * only for filters that check for broadcast IP addresses, so
722        * we just warn the user, and punt and use 0.
723        */
724       fprintf(stderr, 
725         "Warning:  Couldn't obtain netmask info (%s)\n.", err_str);
726       netmask = 0;
727     }
728     if (pcap_compile(ld.pch, &fcode, cfile.cfilter, 1, netmask) < 0) {
729       snprintf(errmsg, sizeof errmsg, "Unable to parse filter string (%s).",
730         pcap_geterr(ld.pch));
731       goto error;
732     }
733     if (pcap_setfilter(ld.pch, &fcode) < 0) {
734       snprintf(errmsg, sizeof errmsg, "Can't install filter (%s).",
735         pcap_geterr(ld.pch));
736       goto error;
737     }
738   }
739
740   ld.linktype = wtap_pcap_encap_to_wtap_encap(pcap_datalink(ld.pch));
741   if (cfile.save_file != NULL) {
742     /* Set up to write to the capture file. */
743     if (ld.linktype == WTAP_ENCAP_UNKNOWN) {
744       strcpy(errmsg, "The network you're capturing from is of a type"
745                " that Tethereal doesn't support.");
746       goto error;
747     }
748     ld.pdh = wtap_dump_open(cfile.save_file, out_file_type,
749                 ld.linktype, pcap_snapshot(ld.pch), &err);
750
751     if (ld.pdh == NULL) {
752       snprintf(errmsg, sizeof errmsg, file_open_error_message(errno, TRUE),
753                 cfile.save_file);
754       goto error;
755     }
756   }
757
758   /* Catch SIGINT and SIGTERM and, if we get either of them, clean up
759      and exit.
760      XXX - deal with signal semantics on various platforms.  Or just
761      use "sigaction()" and be done with it? */
762   signal(SIGTERM, capture_cleanup);
763   signal(SIGINT, capture_cleanup);
764 #if !defined(WIN32)
765   if ((oldhandler = signal(SIGHUP, capture_cleanup)) != SIG_DFL)
766     signal(SIGHUP, oldhandler);
767 #endif
768
769   /* Let the user know what interface was chosen. */
770   fprintf(stderr, "Capturing on %s\n", cfile.iface);
771   fflush(stderr);
772
773   inpkts = pcap_loop(ld.pch, packet_count, capture_pcap_cb, (u_char *) &ld);
774
775   if (cfile.save_file != NULL) {
776     /* We're saving to a file, which means we're printing packet counts
777        to the standard output.  Send a newline so that we move to the
778        line after the packet count. */
779     fprintf(stderr, "\n");
780   }
781
782   /* If we got an error while capturing, report it. */
783   if (inpkts < 0) {
784     fprintf(stderr, "tethereal: Error while capturing packets: %s\n",
785         pcap_geterr(ld.pch));
786   }
787
788   /* Get the capture statistics, and, if any packets were dropped, report
789      that. */
790   if (pcap_stats(ld.pch, &stats) >= 0) {
791     if (stats.ps_drop != 0) {
792       fprintf(stderr, "%u packets dropped\n", stats.ps_drop);
793     }
794   } else {
795     fprintf(stderr, "tethereal: Can't get packet-drop statistics: %s\n",
796         pcap_geterr(ld.pch));
797   }
798   pcap_close(ld.pch);
799
800   if (cfile.save_file != NULL) {
801     /* We're saving to a file; close the file. */
802     if (!wtap_dump_close(ld.pdh, &err))
803       show_capture_file_io_error(cfile.save_file, err, TRUE);
804   }
805
806   return TRUE;
807
808 error:
809   g_free(cfile.save_file);
810   cfile.save_file = NULL;
811   fprintf(stderr, "tethereal: %s\n", errmsg);
812   if (ld.pch != NULL)
813     pcap_close(ld.pch);
814
815   return FALSE;
816 }
817
818 static void
819 capture_pcap_cb(u_char *user, const struct pcap_pkthdr *phdr,
820   const u_char *pd)
821 {
822   struct wtap_pkthdr whdr;
823   loop_data *ld = (loop_data *) user;
824   cb_args_t args;
825
826   whdr.ts.tv_sec = phdr->ts.tv_sec;
827   whdr.ts.tv_usec = phdr->ts.tv_usec;
828   whdr.caplen = phdr->caplen;
829   whdr.len = phdr->len;
830   whdr.pkt_encap = ld->linktype;
831
832   args.cf = &cfile;
833   args.pdh = ld->pdh;
834   if (ld->pdh) {
835     wtap_dispatch_cb_write((u_char *)&args, &whdr, 0, NULL, pd);
836     fprintf(stderr, "\r%u ", cfile.count);
837     fflush(stdout);
838   } else {
839     wtap_dispatch_cb_print((u_char *)&args, &whdr, 0, NULL, pd);
840   }
841 }
842
843 static void
844 capture_cleanup(int signum)
845 {
846   int err;
847
848   fprintf(stderr, "\n");
849   pcap_close(ld.pch);
850   if (ld.pdh != NULL) {
851     if (!wtap_dump_close(ld.pdh, &err)) {
852       show_capture_file_io_error(cfile.save_file, err, TRUE);
853       exit(2);
854     }
855   }
856   exit(0);
857 }
858 #endif /* HAVE_LIBPCAP */
859
860 static int
861 load_cap_file(capture_file *cf, int out_file_type)
862 {
863   gint         linktype;
864   wtap_dumper *pdh;
865   int          err;
866   int          success;
867   cb_args_t    args;
868
869   linktype = wtap_file_encap(cf->wth);
870   if (cf->save_file != NULL) {
871     /* Set up to write to the capture file. */
872     pdh = wtap_dump_open(cf->save_file, out_file_type,
873                 linktype, wtap_snapshot_length(cf->wth), &err);
874
875     if (pdh == NULL) {
876       /* We couldn't set up to write to the capture file. */
877       switch (err) {
878
879       case WTAP_ERR_UNSUPPORTED_FILE_TYPE:
880         fprintf(stderr,
881                 "tethereal: Capture files can't be written in that format.\n");
882         break;
883
884       case WTAP_ERR_UNSUPPORTED_ENCAP:
885       case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
886         fprintf(stderr,
887 "tethereal: The capture file being read cannot be written in that format.\n");
888         break;
889
890       case WTAP_ERR_CANT_OPEN:
891         fprintf(stderr,
892 "tethereal: The file \"%s\" couldn't be created for some unknown reason.\n",
893                  cf->save_file);
894         break;
895
896       case WTAP_ERR_SHORT_WRITE:
897         fprintf(stderr,
898 "tethereal: A full header couldn't be written to the file \"%s\".\n",
899                 cf->save_file);
900         break;
901
902       default:
903         if (err < 0) {
904           fprintf(stderr,
905                 "tethereal: The file \"%s\" could not be opened: Error %d.\n",
906                 cf->save_file, err);
907         } else {
908           fprintf(stderr,
909                 "tethereal: The file \"%s\" could not be opened: %s\n.",
910                 cf->save_file, strerror(err));
911         }
912         break;
913       }
914       goto out;
915     }
916     args.cf = cf;
917     args.pdh = pdh;
918     success = wtap_loop(cf->wth, 0, wtap_dispatch_cb_write, (u_char *) &args,
919                         &err);
920
921     /* Now close the capture file. */
922     if (!wtap_dump_close(pdh, &err))
923       show_capture_file_io_error(cfile.save_file, err, TRUE);
924   } else {
925     args.cf = cf;
926     args.pdh = NULL;
927     success = wtap_loop(cf->wth, 0, wtap_dispatch_cb_print, (u_char *) &args,
928                         &err);
929   }
930   if (!success) {
931     /* Print up a message box noting that the read failed somewhere along
932        the line. */
933     switch (err) {
934
935     case WTAP_ERR_UNSUPPORTED_ENCAP:
936       fprintf(stderr,
937 "tethereal: \"%s\" is a capture file is for a network type that Tethereal doesn't support.\n",
938         cf->filename);
939       break;
940
941     case WTAP_ERR_CANT_READ:
942       fprintf(stderr,
943 "tethereal: An attempt to read from \"%s\" failed for some unknown reason.\n",
944         cf->filename);
945       break;
946
947     case WTAP_ERR_SHORT_READ:
948       fprintf(stderr,
949 "tethereal: \"%s\" appears to have been cut short in the middle of a packet.\n",
950         cf->filename);
951       break;
952
953     case WTAP_ERR_BAD_RECORD:
954       fprintf(stderr,
955 "tethereal: \"%s\" appears to be damaged or corrupt.\n",
956         cf->filename);
957       break;
958
959     default:
960       fprintf(stderr,
961 "tethereal: An error occurred while reading \"%s\": %s.\n",
962         cf->filename, wtap_strerror(err));
963       break;
964     }
965   }
966
967 out:
968   wtap_close(cf->wth);
969   cf->wth = NULL;
970
971   return err;
972 }
973
974 static void
975 fill_in_fdata(frame_data *fdata, capture_file *cf,
976         const struct wtap_pkthdr *phdr,
977         const union wtap_pseudo_header *pseudo_header, int offset)
978 {
979   int i;
980
981   fdata->next = NULL;
982   fdata->prev = NULL;
983   fdata->pfd = NULL;
984   fdata->data_src = NULL;
985   fdata->num = cf->count;
986   fdata->pkt_len = phdr->len;
987   fdata->cap_len = phdr->caplen;
988   fdata->file_off = offset;
989   fdata->cinfo = NULL;
990   fdata->lnk_t = phdr->pkt_encap;
991   fdata->abs_secs  = phdr->ts.tv_sec;
992   fdata->abs_usecs = phdr->ts.tv_usec;
993   fdata->flags.passed_dfilter = 0;
994   fdata->flags.encoding = CHAR_ASCII;
995   fdata->flags.visited = 0;
996   fdata->flags.marked = 0;
997
998   /* If we don't have the time stamp of the first packet in the
999      capture, it's because this is the first packet.  Save the time
1000      stamp of this packet as the time stamp of the first packet. */
1001   if (!firstsec && !firstusec) {
1002     firstsec  = fdata->abs_secs;
1003     firstusec = fdata->abs_usecs;
1004   }
1005
1006   /* If we don't have the time stamp of the previous displayed packet,
1007      it's because this is the first displayed packet.  Save the time
1008      stamp of this packet as the time stamp of the previous displayed
1009      packet. */
1010   if (!prevsec && !prevusec) {
1011     prevsec  = fdata->abs_secs;
1012     prevusec = fdata->abs_usecs;
1013   }
1014
1015   /* Get the time elapsed between the first packet and this packet. */
1016   compute_timestamp_diff(&fdata->rel_secs, &fdata->rel_usecs,
1017                 fdata->abs_secs, fdata->abs_usecs, firstsec, firstusec);
1018
1019   /* If it's greater than the current elapsed time, set the elapsed time
1020      to it (we check for "greater than" so as not to be confused by
1021      time moving backwards). */
1022   if (cf->esec < fdata->rel_secs
1023         || (cf->esec == fdata->rel_secs && cf->eusec < fdata->rel_usecs)) {
1024     cf->esec = fdata->rel_secs;
1025     cf->eusec = fdata->rel_usecs;
1026   }
1027   
1028   /* Get the time elapsed between the previous displayed packet and
1029      this packet. */
1030   compute_timestamp_diff(&fdata->del_secs, &fdata->del_usecs,
1031                 fdata->abs_secs, fdata->abs_usecs, prevsec, prevusec);
1032   prevsec = fdata->abs_secs;
1033   prevusec = fdata->abs_usecs;
1034
1035   fdata->cinfo = &cf->cinfo;
1036   for (i = 0; i < fdata->cinfo->num_cols; i++) {
1037     fdata->cinfo->col_buf[i][0] = '\0';
1038     fdata->cinfo->col_data[i] = fdata->cinfo->col_buf[i];
1039   }
1040 }
1041
1042 /* Free up all data attached to a "frame_data" structure. */
1043 static void
1044 clear_fdata(frame_data *fdata)
1045 {
1046   if (fdata->pfd)
1047     g_slist_free(fdata->pfd);
1048   if (fdata->data_src)
1049     g_slist_free(fdata->data_src);
1050 }
1051
1052 static void
1053 wtap_dispatch_cb_write(u_char *user, const struct wtap_pkthdr *phdr, int offset,
1054   union wtap_pseudo_header *pseudo_header, const u_char *buf)
1055 {
1056   cb_args_t    *args = (cb_args_t *) user;
1057   capture_file *cf = args->cf;
1058   wtap_dumper  *pdh = args->pdh;
1059   frame_data    fdata;
1060   proto_tree   *protocol_tree;
1061   int           err;
1062   gboolean      passed;
1063   epan_dissect_t *edt;
1064
1065   cf->count++;
1066   if (cf->rfcode) {
1067     fill_in_fdata(&fdata, cf, phdr, pseudo_header, offset);
1068     protocol_tree = proto_tree_create_root();
1069     edt = epan_dissect_new(pseudo_header, buf, &fdata, protocol_tree);
1070     passed = dfilter_apply_edt(cf->rfcode, edt);
1071   } else {
1072     protocol_tree = NULL;
1073     passed = TRUE;
1074     edt = NULL;
1075   }
1076   if (passed) {
1077     if (!wtap_dump(pdh, phdr, pseudo_header, buf, &err)) {
1078 #ifdef HAVE_LIBPCAP
1079       if (ld.pch != NULL) {
1080         /* We're capturing packets, so we're printing a count of packets
1081            captured; move to the line after the count. */
1082         fprintf(stderr, "\n");
1083       }
1084 #endif
1085       show_capture_file_io_error(cf->save_file, err, FALSE);
1086 #ifdef HAVE_LIBPCAP
1087       if (ld.pch != NULL)
1088         pcap_close(ld.pch);
1089 #endif
1090       wtap_dump_close(pdh, &err);
1091       exit(2);
1092     }
1093   }
1094   if (protocol_tree != NULL)
1095     proto_tree_free(protocol_tree);
1096   if (edt != NULL)
1097     epan_dissect_free(edt);
1098   if (cf->rfcode)
1099     clear_fdata(&fdata);
1100 }
1101
1102 static void
1103 show_capture_file_io_error(const char *fname, int err, gboolean is_close)
1104 {
1105   switch (err) {
1106
1107   case ENOSPC:
1108     fprintf(stderr,
1109 "tethereal: Not all the packets could be written to \"%s\" because there is "
1110 "no space left on the file system.\n",
1111         fname);
1112     break;
1113
1114 #ifdef EDQUOT
1115   case EDQUOT:
1116     fprintf(stderr,
1117 "tethereal: Not all the packets could be written to \"%s\" because you are "
1118 "too close to, or over your disk quota.\n",
1119         fname);
1120   break;
1121 #endif
1122
1123   case WTAP_ERR_CANT_CLOSE:
1124     fprintf(stderr,
1125 "tethereal: \"%s\" couldn't be closed for some unknown reason.\n",
1126         fname);
1127     break;
1128
1129   case WTAP_ERR_SHORT_WRITE:
1130     fprintf(stderr,
1131 "tethereal: Not all the packets could be written to \"%s\".\n",
1132         fname);
1133     break;
1134
1135   default:
1136     if (is_close) {
1137       fprintf(stderr,
1138 "tethereal: \"%s\" could not be closed: %s.\n",
1139         fname, wtap_strerror(err));
1140     } else {
1141       fprintf(stderr,
1142 "tethereal: An error occurred while writing to \"%s\": %s.\n",
1143         fname, wtap_strerror(err));
1144     }
1145     break;
1146   }
1147 }
1148
1149 static void
1150 wtap_dispatch_cb_print(u_char *user, const struct wtap_pkthdr *phdr, int offset,
1151   union wtap_pseudo_header *pseudo_header, const u_char *buf)
1152 {
1153   cb_args_t    *args = (cb_args_t *) user;
1154   capture_file *cf = args->cf;
1155   frame_data    fdata;
1156   proto_tree   *protocol_tree;
1157   gboolean      passed;
1158   print_args_t  print_args;
1159   epan_dissect_t *edt;
1160   int           i;
1161
1162   cf->count++;
1163
1164   /* The protocol tree will be "visible", i.e., printed, only if we're
1165      not printing a summary. */
1166   proto_tree_is_visible = verbose;
1167
1168   fill_in_fdata(&fdata, cf, phdr, pseudo_header, offset);
1169
1170   passed = TRUE;
1171   if (cf->rfcode || verbose)
1172     protocol_tree = proto_tree_create_root();
1173   else
1174     protocol_tree = NULL;
1175   edt = epan_dissect_new(pseudo_header, buf, &fdata, protocol_tree);
1176   if (cf->rfcode)
1177     passed = dfilter_apply_edt(cf->rfcode, edt);
1178   if (passed) {
1179     /* The packet passed the read filter. */
1180     if (verbose) {
1181       /* Print the information in the protocol tree. */
1182       print_args.to_file = TRUE;
1183       print_args.format = PR_FMT_TEXT;
1184       print_args.print_summary = FALSE;
1185       print_args.print_hex = print_hex;
1186       print_args.expand_all = TRUE;
1187       print_args.suppress_unmarked = FALSE;
1188       proto_tree_print(FALSE, &print_args, (GNode *)protocol_tree,
1189                         &fdata, stdout);
1190       if (!print_hex) {
1191         /* "print_hex_data()" will put out a leading blank line, as well
1192            as a trailing one; print one here, to separate the packets,
1193            only if "print_hex_data()" won't be called. */
1194         printf("\n");
1195       }
1196     } else {
1197       /* Just fill in the columns. */
1198       fill_in_columns(&fdata);
1199
1200       /* Now print them. */
1201       for (i = 0; i < cf->cinfo.num_cols; i++) {
1202         switch (cf->cinfo.col_fmt[i]) {
1203         case COL_NUMBER:
1204           /*
1205            * Don't print this if we're doing a live capture from a network
1206            * interface - if we're doing a live capture, you won't be
1207            * able to look at the capture in the future (it's not being
1208            * saved anywhere), so the frame numbers are unlikely to be
1209            * useful.
1210            *
1211            * (XXX - it might be nice to be able to save and print at
1212            * the same time, sort of like an "Update list of packets
1213            * in real time" capture in Ethereal.)
1214            */
1215           if (cf->iface != NULL)
1216             continue;
1217           printf("%3s", cf->cinfo.col_data[i]);
1218           break;
1219
1220         case COL_CLS_TIME:
1221         case COL_REL_TIME:
1222         case COL_ABS_TIME:
1223         case COL_ABS_DATE_TIME: /* XXX - wider */
1224           printf("%10s", cf->cinfo.col_data[i]);
1225           break;
1226
1227         case COL_DEF_SRC:
1228         case COL_RES_SRC:
1229         case COL_UNRES_SRC:
1230         case COL_DEF_DL_SRC:
1231         case COL_RES_DL_SRC:
1232         case COL_UNRES_DL_SRC:
1233         case COL_DEF_NET_SRC:
1234         case COL_RES_NET_SRC:
1235         case COL_UNRES_NET_SRC:
1236           printf("%12s", cf->cinfo.col_data[i]);
1237           break;
1238
1239         case COL_DEF_DST:
1240         case COL_RES_DST:
1241         case COL_UNRES_DST:
1242         case COL_DEF_DL_DST:
1243         case COL_RES_DL_DST:
1244         case COL_UNRES_DL_DST:
1245         case COL_DEF_NET_DST:
1246         case COL_RES_NET_DST:
1247         case COL_UNRES_NET_DST:
1248           printf("%-12s", cf->cinfo.col_data[i]);
1249           break;
1250
1251         default:
1252           printf("%s", cf->cinfo.col_data[i]);
1253           break;
1254         }
1255         if (i != cf->cinfo.num_cols - 1) {
1256           /*
1257            * This isn't the last column, so we need to print a
1258            * separator between this column and the next.
1259            *
1260            * If we printed a network source and are printing a
1261            * network destination of the same type next, separate
1262            * them with "->"; if we printed a network destination
1263            * and are printing a network source of the same type
1264            * next, separate them with "<-"; otherwise separate them
1265            * with a space.
1266            */
1267           switch (cf->cinfo.col_fmt[i]) {
1268
1269           case COL_DEF_SRC:
1270           case COL_RES_SRC:
1271           case COL_UNRES_SRC:
1272             switch (cf->cinfo.col_fmt[i + 1]) {
1273
1274             case COL_DEF_DST:
1275             case COL_RES_DST:
1276             case COL_UNRES_DST:
1277               printf(" -> ");
1278               break;
1279
1280             default:
1281               putchar(' ');
1282               break;
1283             }
1284             break;
1285
1286           case COL_DEF_DL_SRC:
1287           case COL_RES_DL_SRC:
1288           case COL_UNRES_DL_SRC:
1289             switch (cf->cinfo.col_fmt[i + 1]) {
1290
1291             case COL_DEF_DL_DST:
1292             case COL_RES_DL_DST:
1293             case COL_UNRES_DL_DST:
1294               printf(" -> ");
1295               break;
1296
1297             default:
1298               putchar(' ');
1299               break;
1300             }
1301             break;
1302
1303           case COL_DEF_NET_SRC:
1304           case COL_RES_NET_SRC:
1305           case COL_UNRES_NET_SRC:
1306             switch (cf->cinfo.col_fmt[i + 1]) {
1307
1308             case COL_DEF_NET_DST:
1309             case COL_RES_NET_DST:
1310             case COL_UNRES_NET_DST:
1311               printf(" -> ");
1312               break;
1313
1314             default:
1315               putchar(' ');
1316               break;
1317             }
1318             break;
1319
1320           case COL_DEF_DST:
1321           case COL_RES_DST:
1322           case COL_UNRES_DST:
1323             switch (cf->cinfo.col_fmt[i + 1]) {
1324
1325             case COL_DEF_SRC:
1326             case COL_RES_SRC:
1327             case COL_UNRES_SRC:
1328               printf(" <- ");
1329               break;
1330
1331             default:
1332               putchar(' ');
1333               break;
1334             }
1335             break;
1336
1337           case COL_DEF_DL_DST:
1338           case COL_RES_DL_DST:
1339           case COL_UNRES_DL_DST:
1340             switch (cf->cinfo.col_fmt[i + 1]) {
1341
1342             case COL_DEF_DL_SRC:
1343             case COL_RES_DL_SRC:
1344             case COL_UNRES_DL_SRC:
1345               printf(" <- ");
1346               break;
1347
1348             default:
1349               putchar(' ');
1350               break;
1351             }
1352             break;
1353
1354           case COL_DEF_NET_DST:
1355           case COL_RES_NET_DST:
1356           case COL_UNRES_NET_DST:
1357             switch (cf->cinfo.col_fmt[i + 1]) {
1358
1359             case COL_DEF_NET_SRC:
1360             case COL_RES_NET_SRC:
1361             case COL_UNRES_NET_SRC:
1362               printf(" <- ");
1363               break;
1364
1365             default:
1366               putchar(' ');
1367               break;
1368             }
1369             break;
1370
1371           default:
1372             putchar(' ');
1373             break;
1374           }
1375         }
1376       }
1377       putchar('\n');
1378     }
1379     if (print_hex) {
1380       print_hex_data(stdout, print_args.format, &fdata);
1381       putchar('\n');
1382     }
1383     fdata.cinfo = NULL;
1384   }
1385
1386   /* The ANSI C standard does not appear to *require* that a line-buffered
1387      stream be flushed to the host environment whenever a newline is
1388      written, it just says that, on such a stream, characters "are
1389      intended to be transmitted to or from the host environment as a
1390      block when a new-line character is encountered".
1391
1392      The Visual C++ 6.0 C implementation doesn't do what is intended;
1393      even if you set a stream to be line-buffered, it still doesn't
1394      flush the buffer at the end of every line.
1395
1396      So, if the "-l" flag was specified, we flush the standard output
1397      at the end of a packet.  This will do the right thing if we're
1398      printing packet summary lines, and, as we print the entire protocol
1399      tree for a single packet without waiting for anything to happen,
1400      it should be as good as line-buffered mode if we're printing
1401      protocol trees.  (The whole reason for the "-l" flag in either
1402      tcpdump or Tethereal is to allow the output of a live capture to
1403      be piped to a program or script and to have that script see the
1404      information for the packet as soon as it's printed, rather than
1405      having to wait until a standard I/O buffer fills up. */
1406   if (line_buffered)
1407     fflush(stdout);
1408   if (protocol_tree != NULL)
1409     proto_tree_free(protocol_tree);
1410
1411   epan_dissect_free(edt);
1412
1413   clear_fdata(&fdata);
1414
1415   proto_tree_is_visible = FALSE;
1416 }
1417
1418 char *
1419 file_open_error_message(int err, gboolean for_writing)
1420 {
1421   char *errmsg;
1422   static char errmsg_errno[1024+1];
1423
1424   switch (err) {
1425
1426   case WTAP_ERR_NOT_REGULAR_FILE:
1427     errmsg = "The file \"%s\" is a \"special file\" or socket or other non-regular file.";
1428     break;
1429
1430   case WTAP_ERR_FILE_UNKNOWN_FORMAT:
1431   case WTAP_ERR_UNSUPPORTED:
1432     /* Seen only when opening a capture file for reading. */
1433     errmsg = "The file \"%s\" is not a capture file in a format Tethereal understands.";
1434     break;
1435
1436   case WTAP_ERR_UNSUPPORTED_FILE_TYPE:
1437     /* Seen only when opening a capture file for writing. */
1438     errmsg = "Tethereal does not support writing capture files in that format.";
1439     break;
1440
1441   case WTAP_ERR_UNSUPPORTED_ENCAP:
1442   case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
1443     if (for_writing)
1444       errmsg = "Tethereal cannot save this capture in that format.";
1445     else
1446       errmsg = "The file \"%s\" is a capture for a network type that Tethereal doesn't support.";
1447     break;
1448
1449   case WTAP_ERR_BAD_RECORD:
1450     errmsg = "The file \"%s\" appears to be damaged or corrupt.";
1451     break;
1452
1453   case WTAP_ERR_CANT_OPEN:
1454     if (for_writing)
1455       errmsg = "The file \"%s\" could not be created for some unknown reason.";
1456     else
1457       errmsg = "The file \"%s\" could not be opened for some unknown reason.";
1458     break;
1459
1460   case WTAP_ERR_SHORT_READ:
1461     errmsg = "The file \"%s\" appears to have been cut short"
1462              " in the middle of a packet.";
1463     break;
1464
1465   case WTAP_ERR_SHORT_WRITE:
1466     errmsg = "A full header couldn't be written to the file \"%s\".";
1467     break;
1468
1469   case ENOENT:
1470     if (for_writing)
1471       errmsg = "The path to the file \"%s\" does not exist.";
1472     else
1473       errmsg = "The file \"%s\" does not exist.";
1474     break;
1475
1476   case EACCES:
1477     if (for_writing)
1478       errmsg = "You do not have permission to create or write to the file \"%s\".";
1479     else
1480       errmsg = "You do not have permission to read the file \"%s\".";
1481     break;
1482
1483   case EISDIR:
1484     errmsg = "\"%s\" is a directory (folder), not a file.";
1485     break;
1486
1487   default:
1488     snprintf(errmsg_errno, sizeof(errmsg_errno),
1489              "The file \"%%s\" could not be opened: %s.",
1490              wtap_strerror(err));
1491     errmsg = errmsg_errno;
1492     break;
1493   }
1494   return errmsg;
1495 }
1496
1497 int
1498 open_cap_file(char *fname, gboolean is_tempfile, capture_file *cf)
1499 {
1500   wtap       *wth;
1501   int         err;
1502   int         fd;
1503   struct stat cf_stat;
1504   char        err_msg[2048+1];
1505
1506   wth = wtap_open_offline(fname, &err, FALSE);
1507   if (wth == NULL)
1508     goto fail;
1509
1510   /* Find the size of the file. */
1511   fd = wtap_fd(wth);
1512   if (fstat(fd, &cf_stat) < 0) {
1513     err = errno;
1514     wtap_close(wth);
1515     goto fail;
1516   }
1517
1518   /* The open succeeded.  Fill in the information for this file. */
1519
1520   /* Initialize the table of conversations. */
1521   epan_conversation_init();
1522
1523   /* Initialize protocol-specific variables */
1524   init_all_protocols();
1525
1526   /* Initialize the common data structures for fragment reassembly.
1527      Must be done *after* "init_all_protocols()", as "init_all_protocols()"
1528      may free up space for fragments, which it finds by using the
1529      data structures that "reassemble_init()" frees. */
1530   reassemble_init();
1531
1532   cf->wth = wth;
1533   cf->filed = fd;
1534   cf->f_len = cf_stat.st_size;
1535
1536   /* Set the file name because we need it to set the follow stream filter.
1537      XXX - is that still true?  We need it for other reasons, though,
1538      in any case. */
1539   cf->filename = g_strdup(fname);
1540
1541   /* Indicate whether it's a permanent or temporary file. */
1542   cf->is_tempfile = is_tempfile;
1543
1544   /* If it's a temporary capture buffer file, mark it as not saved. */
1545   cf->user_saved = !is_tempfile;
1546
1547   cf->cd_t      = wtap_file_type(cf->wth);
1548   cf->count     = 0;
1549   cf->drops_known = FALSE;
1550   cf->drops     = 0;
1551   cf->esec      = 0;
1552   cf->eusec     = 0;
1553   cf->snap      = wtap_snapshot_length(cf->wth);
1554   cf->progbar_quantum = 0;
1555   cf->progbar_nextstep = 0;
1556   firstsec = 0, firstusec = 0;
1557   prevsec = 0, prevusec = 0;
1558  
1559   return (0);
1560
1561 fail:
1562   snprintf(err_msg, sizeof err_msg, file_open_error_message(err, FALSE), fname);
1563   fprintf(stderr, "tethereal: %s\n", err_msg);
1564   return (err);
1565 }