3 * $Id: tethereal.c,v 1.95 2001/10/26 18:28:16 gram Exp $
5 * Ethereal - Network traffic analyzer
6 * By Gerald Combs <gerald@ethereal.com>
7 * Copyright 1998 Gerald Combs
9 * Text-mode variant, by Gilbert Ramirez <gram@xiexie.org>.
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.
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.
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.
43 #ifdef HAVE_SYS_TYPES_H
44 #include <sys/types.h>
47 #ifdef HAVE_SYS_STAT_H
62 #include <zlib.h> /* to get the libz version number */
65 #ifdef NEED_SNPRINTF_H
66 # include "snprintf.h"
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 */
79 #ifdef NEED_STRERROR_H
91 #include "timestamp.h"
99 #include "conversation.h"
100 #include "reassemble.h"
102 #include "register.h"
105 #include "capture-wpcap.h"
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;
116 typedef struct _loop_data {
124 static int capture(int, int);
125 static void capture_pcap_cb(u_char *, const struct pcap_pkthdr *,
127 static void capture_cleanup(int);
135 static int load_cap_file(capture_file *, int);
136 static void wtap_dispatch_cb_write(u_char *, const struct wtap_pkthdr *, long,
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 *, long,
140 union wtap_pseudo_header *, const u_char *);
144 FILE *data_out_file = NULL;
145 ts_type timestamp_type = RELATIVE;
147 static int promisc_mode = TRUE;
155 fprintf(stderr, "This is GNU t%s %s, compiled %s\n", PACKAGE, VERSION,
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");
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");
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));
173 fprintf(stderr, "\tdefault is libpcap\n");
177 get_positive_int(const char *string, const char *name)
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",
189 fprintf(stderr, "tethereal: The specified %s \"%s\" is a negative number\n",
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);
202 main(int argc, char *argv[])
206 gboolean arg_error = FALSE;
208 #ifdef HAVE_PCAP_VERSION
209 extern char pcap_version[];
210 #endif /* HAVE_PCAP_VERSION */
211 #endif /* HAVE_LIBPCAP */
219 int gpf_open_errno, pf_open_errno;
222 gboolean capture_filter_specified = FALSE;
223 int packet_count = 0;
224 GList *if_list, *if_entry;
225 gchar err_str[PCAP_ERRBUF_SIZE];
227 gboolean capture_option_specified = FALSE;
229 int out_file_type = WTAP_FILE_PCAP;
230 gchar *cf_name = NULL, *rfilter = NULL;
231 dfilter_t *rfcode = NULL;
235 /* Register all dissectors; we must do this before checking for the
236 "-G" flag, as the "-G" flag dumps a list of fields registered
237 by the dissectors, and we must do it before we read the preferences,
238 in case any dissectors register preferences. */
239 epan_init(PLUGIN_DIR,register_all_protocols,register_all_protocol_handoffs);
241 /* Now register the preferences for any non-dissector modules.
242 We must do that before we read the preferences as well. */
243 prefs_register_modules();
245 /* If invoked with the "-G" flag, we dump out a glossary of
246 display filter symbols.
248 We do this here to mirror what happens in the GTK+ version, although
249 it's not necessary here. */
250 if (argc >= 2 && strcmp(argv[1], "-G") == 0) {
251 proto_registrar_dump();
255 /* Set the C-language locale to the native environment. */
256 setlocale(LC_ALL, "");
258 prefs = read_prefs(&gpf_open_errno, &gpf_path, &pf_open_errno, &pf_path);
259 if (gpf_path != NULL) {
260 fprintf(stderr, "Can't open global preferences file \"%s\": %s.\n", pf_path,
261 strerror(gpf_open_errno));
263 if (pf_path != NULL) {
264 fprintf(stderr, "Can't open your preferences file \"%s\": %s.\n", pf_path,
265 strerror(pf_open_errno));
269 /* Load Wpcap, if possible */
273 /* Initialize the capture file struct */
275 cfile.plist_end = NULL;
277 cfile.filename = NULL;
278 cfile.user_saved = FALSE;
279 cfile.is_tempfile = FALSE;
281 cfile.dfilter = NULL;
284 cfile.cfilter = g_strdup("");
287 cfile.save_file = NULL;
288 cfile.save_file_fd = -1;
289 cfile.snap = WTAP_MAX_PACKET_SIZE;
291 col_init(&cfile.cinfo, prefs->num_cols);
293 /* Assemble the compile-time options */
294 comp_info_str = g_string_new("");
296 g_string_append(comp_info_str, "with ");
297 g_string_sprintfa(comp_info_str,
298 #ifdef GLIB_MAJOR_VERSION
299 "GLib %d.%d.%d", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION,
302 "GLib (version unknown)");
306 g_string_append(comp_info_str, ", with libpcap ");
307 #ifdef HAVE_PCAP_VERSION
308 g_string_append(comp_info_str, pcap_version);
309 #else /* HAVE_PCAP_VERSION */
310 g_string_append(comp_info_str, "(version unknown)");
311 #endif /* HAVE_PCAP_VERSION */
312 #else /* HAVE_LIBPCAP */
313 g_string_append(comp_info_str, ", without libpcap");
314 #endif /* HAVE_LIBPCAP */
317 g_string_append(comp_info_str, ", with libz ");
319 g_string_append(comp_info_str, ZLIB_VERSION);
320 #else /* ZLIB_VERSION */
321 g_string_append(comp_info_str, "(version unknown)");
322 #endif /* ZLIB_VERSION */
323 #else /* HAVE_LIBZ */
324 g_string_append(comp_info_str, ", without libz");
325 #endif /* HAVE_LIBZ */
327 /* Oh, this is pretty */
328 #if defined(HAVE_UCD_SNMP_SNMP_H)
329 g_string_append(comp_info_str, ", with UCD SNMP ");
330 #ifdef HAVE_UCD_SNMP_VERSION_H
331 g_string_append(comp_info_str, VersionInfo);
332 #else /* HAVE_UCD_SNMP_VERSION_H */
333 g_string_append(comp_info_str, "(version unknown)");
334 #endif /* HAVE_UCD_SNMP_VERSION_H */
335 #elif defined(HAVE_SNMP_SNMP_H)
336 g_string_append(comp_info_str, ", with CMU SNMP ");
337 #ifdef HAVE_SNMP_VERSION_H
338 g_string_append(comp_info_str, snmp_Version());
339 #else /* HAVE_SNMP_VERSION_H */
340 g_string_append(comp_info_str, "(version unknown)");
341 #endif /* HAVE_SNMP_VERSION_H */
343 g_string_append(comp_info_str, ", without SNMP");
346 /* Now get our args */
347 while ((opt = getopt(argc, argv, "c:Df:F:hi:lnN:o:pr:R:s:t:vw:Vx")) != EOF) {
349 case 'c': /* Capture xxx packets */
351 packet_count = get_positive_int(optarg, "packet count");
353 capture_option_specified = TRUE;
357 case 'D': /* Print a list of capture devices */
359 if_list = get_interface_list(&err, err_str);
360 if (if_list == NULL) {
363 case CANT_GET_INTERFACE_LIST:
364 fprintf(stderr, "tethereal: Can't get list of interfaces: %s\n",
368 case NO_INTERFACES_FOUND:
369 fprintf(stderr, "tethereal: There are no interfaces on which a capture can be done\n");
374 for (if_entry = g_list_first(if_list); if_entry != NULL;
375 if_entry = g_list_next(if_entry))
376 printf("%s\n", (char *)if_entry->data);
377 free_interface_list(if_list);
380 capture_option_specified = TRUE;
386 capture_filter_specified = TRUE;
387 cfile.cfilter = g_strdup(optarg);
389 capture_option_specified = TRUE;
394 out_file_type = wtap_short_string_to_file_type(optarg);
395 if (out_file_type < 0) {
396 fprintf(stderr, "tethereal: \"%s\" is not a valid capture file type\n",
401 case 'h': /* Print help and exit */
405 case 'i': /* Use interface xxx */
407 cfile.iface = g_strdup(optarg);
409 capture_option_specified = TRUE;
413 case 'l': /* "Line-buffer" standard output */
414 /* This isn't line-buffering, strictly speaking, it's just
415 flushing the standard output after the information for
416 each packet is printed; however, that should be good
417 enough for all the purposes to which "-l" is put.
419 See the comment in "wtap_dispatch_cb_print()" for an
420 explanation of why we do that, and why we don't just
421 use "setvbuf()" to make the standard output line-buffered
422 (short version: in Windows, "line-buffered" is the same
423 as "fully-buffered", and the output buffer is only flushed
424 when it fills up). */
425 line_buffered = TRUE;
427 case 'n': /* No name resolution */
428 prefs->name_resolve = PREFS_RESOLV_NONE;
430 case 'N': /* Select what types of addresses/port #s to resolve */
431 if (prefs->name_resolve == PREFS_RESOLV_ALL)
432 prefs->name_resolve = PREFS_RESOLV_NONE;
433 badopt = string_to_name_resolve(optarg, &prefs->name_resolve);
434 if (badopt != '\0') {
435 fprintf(stderr, "tethereal: -N specifies unknown resolving option '%c'; valid options are 'm', 'n', and 't'\n",
440 case 'o': /* Override preference from command line */
441 switch (prefs_set_pref(optarg)) {
443 case PREFS_SET_SYNTAX_ERR:
444 fprintf(stderr, "tethereal: Invalid -o flag \"%s\"\n", optarg);
448 case PREFS_SET_NO_SUCH_PREF:
449 fprintf(stderr, "tethereal: -o flag \"%s\" specifies unknown preference\n",
455 case 'p': /* Don't capture in promiscuous mode */
459 capture_option_specified = TRUE;
463 case 'r': /* Read capture file xxx */
464 cf_name = g_strdup(optarg);
466 case 'R': /* Read file filter */
469 case 's': /* Set the snapshot (capture) length */
471 cfile.snap = get_positive_int(optarg, "snapshot length");
473 capture_option_specified = TRUE;
477 case 't': /* Time stamp type */
478 if (strcmp(optarg, "r") == 0)
479 timestamp_type = RELATIVE;
480 else if (strcmp(optarg, "a") == 0)
481 timestamp_type = ABSOLUTE;
482 else if (strcmp(optarg, "ad") == 0)
483 timestamp_type = ABSOLUTE_WITH_DATE;
484 else if (strcmp(optarg, "d") == 0)
485 timestamp_type = DELTA;
487 fprintf(stderr, "tethereal: Invalid time stamp type \"%s\"\n",
489 fprintf(stderr, "It must be \"r\" for relative, \"a\" for absolute,\n");
490 fprintf(stderr, "\"ad\" for absolute with date, or \"d\" for delta.\n");
494 case 'v': /* Show version and exit */
495 printf("t%s %s, %s\n", PACKAGE, VERSION, comp_info_str->str);
498 case 'w': /* Write to capture file xxx */
499 cfile.save_file = g_strdup(optarg);
501 case 'V': /* Verbose */
504 case 'x': /* Print packet data in hex (and ASCII) */
510 /* If no capture filter or read filter has been specified, and there are
511 still command-line arguments, treat them as the tokens of a capture
512 filter (if no "-r" flag was specified) or a read filter (if a "-r"
513 flag was specified. */
515 if (cf_name != NULL) {
516 if (rfilter != NULL) {
518 "tethereal: Read filters were specified both with \"-R\" and with additional command-line arguments\n");
521 rfilter = get_args_as_string(argc, argv, optind);
524 if (capture_filter_specified) {
526 "tethereal: Capture filters were specified both with \"-f\" and with additional command-line arguments\n");
529 cfile.cfilter = get_args_as_string(argc, argv, optind);
531 capture_option_specified = TRUE;
537 /* Start windows sockets */
538 WSAStartup( MAKEWORD( 1, 1 ), &wsaData );
541 /* Notify all registered modules that have had any of their preferences
542 changed either from one of the preferences file or from the command
543 line that its preferences have changed. */
547 if (capture_option_specified)
548 fprintf(stderr, "This version of Tethereal was not built with support for capturing packets.\n");
553 /* Build the column format array */
554 for (i = 0; i < cfile.cinfo.num_cols; i++) {
555 cfile.cinfo.col_fmt[i] = get_column_format(i);
556 cfile.cinfo.col_title[i] = g_strdup(get_column_title(i));
557 cfile.cinfo.fmt_matx[i] = (gboolean *) g_malloc0(sizeof(gboolean) *
559 get_column_format_matches(cfile.cinfo.fmt_matx[i], cfile.cinfo.col_fmt[i]);
560 cfile.cinfo.col_data[i] = NULL;
561 if (cfile.cinfo.col_fmt[i] == COL_INFO)
562 cfile.cinfo.col_buf[i] = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_INFO_LEN);
564 cfile.cinfo.col_buf[i] = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_LEN);
568 cfile.snap = WTAP_MAX_PACKET_SIZE;
569 else if (cfile.snap < MIN_PACKET_SIZE)
570 cfile.snap = MIN_PACKET_SIZE;
572 if (rfilter != NULL) {
573 if (!dfilter_compile(rfilter, &rfcode)) {
574 fprintf(stderr, "tethereal: %s\n", dfilter_error_msg);
579 cfile.rfcode = rfcode;
581 err = open_cap_file(cf_name, FALSE, &cfile);
586 err = load_cap_file(&cfile, out_file_type);
593 /* No capture file specified, so we're supposed to do a live capture;
594 do we have support for live captures? */
599 fprintf(stderr, "tethereal: Could not load wpcap.dll.\n");
604 /* Yes; did the user specify an interface to use? */
605 if (cfile.iface == NULL) {
606 /* No - pick the first one from the list of interfaces. */
607 if_list = get_interface_list(&err, err_str);
608 if (if_list == NULL) {
611 case CANT_GET_INTERFACE_LIST:
612 fprintf(stderr, "tethereal: Can't get list of interfaces: %s\n",
616 case NO_INTERFACES_FOUND:
617 fprintf(stderr, "tethereal: There are no interfaces on which a capture can be done\n");
622 cfile.iface = g_strdup(if_list->data); /* first interface */
623 free_interface_list(if_list);
625 capture(packet_count, out_file_type);
628 fprintf(stderr, "This version of Tethereal was not built with support for capturing packets.\n");
639 /* Do the low-level work of a capture.
640 Returns TRUE if it succeeds, FALSE otherwise. */
642 capture(int packet_count, int out_file_type)
644 gchar open_err_str[PCAP_ERRBUF_SIZE];
645 gchar lookup_net_err_str[PCAP_ERRBUF_SIZE];
646 bpf_u_int32 netnum, netmask;
647 struct bpf_program fcode;
648 void (*oldhandler)(int);
652 static const char ppamsg[] = "can't find PPA for ";
655 struct pcap_stat stats;
657 /* Initialize the table of conversations. */
658 epan_conversation_init();
660 /* Initialize protocol-specific variables */
661 init_all_protocols();
663 /* Initialize the common data structures for fragment reassembly.
664 Must be done *after* "init_all_protocols()", as "init_all_protocols()"
665 may free up space for fragments, which it finds by using the
666 data structures that "reassemble_init()" frees. */
669 ld.linktype = WTAP_ENCAP_UNKNOWN;
672 /* Open the network interface to capture from it.
673 Some versions of libpcap may put warnings into the error buffer
674 if they succeed; to tell if that's happened, we have to clear
675 the error buffer, and check if it's still a null string. */
676 open_err_str[0] = '\0';
677 ld.pch = pcap_open_live(cfile.iface, cfile.snap, promisc_mode, 1000,
680 if (ld.pch == NULL) {
681 /* Well, we couldn't start the capture. */
683 /* On Win32 OSes, the capture devices are probably available to all
684 users; don't warn about permissions problems.
686 Do, however, warn that Token Ring and PPP devices aren't supported. */
687 snprintf(errmsg, sizeof errmsg,
688 "The capture session could not be initiated (%s).\n"
689 "Please check that you have the proper interface specified.\n"
691 "Note that the driver Tethereal uses for packet capture on Windows\n"
692 "doesn't support capturing on Token Ring interfaces, and doesn't\n"
693 "support capturing on PPP/WAN interfaces in Windows NT/2000.\n",
696 /* If we got a "can't find PPA for XXX" message, warn the user (who
697 is running Ethereal on HP-UX) that they don't have a version
698 of libpcap that properly handles HP-UX (libpcap 0.6.x and later
699 versions, which properly handle HP-UX, say "can't find /dev/dlpi
700 PPA for XXX" rather than "can't find PPA for XXX"). */
701 if (strncmp(open_err_str, ppamsg, sizeof ppamsg - 1) == 0)
704 "You are running Tethereal with a version of the libpcap library\n"
705 "that doesn't handle HP-UX network devices well; this means that\n"
706 "Tethereal may not be able to capture packets.\n"
708 "To fix this, you should install libpcap 0.6.2, or a later version\n"
709 "of libpcap, rather than libpcap 0.4 or 0.5.x. It is available in\n"
710 "packaged binary form from the Software Porting And Archive Centre\n"
711 "for HP-UX; the Centre is at http://hpux.connect.org.uk/ - the page\n"
712 "at the URL lists a number of mirror sites.";
715 snprintf(errmsg, sizeof errmsg,
716 "The capture session could not be initiated (%s).\n"
717 "Please check to make sure you have sufficient permissions, and that\n"
718 "you have the proper interface specified.%s", open_err_str, libpcap_warn);
724 /* A capture filter was specified; set it up. */
725 if (pcap_lookupnet(cfile.iface, &netnum, &netmask, lookup_net_err_str) < 0) {
727 * Well, we can't get the netmask for this interface; it's used
728 * only for filters that check for broadcast IP addresses, so
729 * we just warn the user, and punt and use 0.
732 "Warning: Couldn't obtain netmask info (%s)\n.", lookup_net_err_str);
735 if (pcap_compile(ld.pch, &fcode, cfile.cfilter, 1, netmask) < 0) {
736 snprintf(errmsg, sizeof errmsg, "Unable to parse filter string (%s).",
737 pcap_geterr(ld.pch));
740 if (pcap_setfilter(ld.pch, &fcode) < 0) {
741 snprintf(errmsg, sizeof errmsg, "Can't install filter (%s).",
742 pcap_geterr(ld.pch));
747 ld.linktype = wtap_pcap_encap_to_wtap_encap(pcap_datalink(ld.pch));
748 if (cfile.save_file != NULL) {
749 /* Set up to write to the capture file. */
750 if (ld.linktype == WTAP_ENCAP_UNKNOWN) {
751 strcpy(errmsg, "The network you're capturing from is of a type"
752 " that Tethereal doesn't support.");
755 ld.pdh = wtap_dump_open(cfile.save_file, out_file_type,
756 ld.linktype, pcap_snapshot(ld.pch), &err);
758 if (ld.pdh == NULL) {
759 snprintf(errmsg, sizeof errmsg, file_open_error_message(errno, TRUE),
765 /* Does "open_err_str" contain a non-empty string? If so, "pcap_open_live()"
766 returned a warning; print it, but keep capturing. */
767 if (open_err_str[0] != '\0')
768 fprintf(stderr, "tethereal: WARNING: %s.\n", open_err_str);
770 /* Catch SIGINT and SIGTERM and, if we get either of them, clean up
772 XXX - deal with signal semantics on various platforms. Or just
773 use "sigaction()" and be done with it? */
774 signal(SIGTERM, capture_cleanup);
775 signal(SIGINT, capture_cleanup);
777 if ((oldhandler = signal(SIGHUP, capture_cleanup)) != SIG_DFL)
778 signal(SIGHUP, oldhandler);
781 /* Let the user know what interface was chosen. */
782 fprintf(stderr, "Capturing on %s\n", cfile.iface);
785 inpkts = pcap_loop(ld.pch, packet_count, capture_pcap_cb, (u_char *) &ld);
787 if (cfile.save_file != NULL) {
788 /* We're saving to a file, which means we're printing packet counts
789 to the standard output. Send a newline so that we move to the
790 line after the packet count. */
791 fprintf(stderr, "\n");
794 /* If we got an error while capturing, report it. */
796 fprintf(stderr, "tethereal: Error while capturing packets: %s\n",
797 pcap_geterr(ld.pch));
800 /* Get the capture statistics, and, if any packets were dropped, report
802 if (pcap_stats(ld.pch, &stats) >= 0) {
803 if (stats.ps_drop != 0) {
804 fprintf(stderr, "%u packets dropped\n", stats.ps_drop);
807 fprintf(stderr, "tethereal: Can't get packet-drop statistics: %s\n",
808 pcap_geterr(ld.pch));
812 if (cfile.save_file != NULL) {
813 /* We're saving to a file; close the file. */
814 if (!wtap_dump_close(ld.pdh, &err))
815 show_capture_file_io_error(cfile.save_file, err, TRUE);
821 g_free(cfile.save_file);
822 cfile.save_file = NULL;
823 fprintf(stderr, "tethereal: %s\n", errmsg);
831 capture_pcap_cb(u_char *user, const struct pcap_pkthdr *phdr,
834 struct wtap_pkthdr whdr;
835 loop_data *ld = (loop_data *) user;
838 whdr.ts.tv_sec = phdr->ts.tv_sec;
839 whdr.ts.tv_usec = phdr->ts.tv_usec;
840 whdr.caplen = phdr->caplen;
841 whdr.len = phdr->len;
842 whdr.pkt_encap = ld->linktype;
847 wtap_dispatch_cb_write((u_char *)&args, &whdr, 0, NULL, pd);
848 fprintf(stderr, "\r%u ", cfile.count);
851 wtap_dispatch_cb_print((u_char *)&args, &whdr, 0, NULL, pd);
856 capture_cleanup(int signum)
860 fprintf(stderr, "\n");
862 if (ld.pdh != NULL) {
863 if (!wtap_dump_close(ld.pdh, &err)) {
864 show_capture_file_io_error(cfile.save_file, err, TRUE);
870 #endif /* HAVE_LIBPCAP */
873 load_cap_file(capture_file *cf, int out_file_type)
881 linktype = wtap_file_encap(cf->wth);
882 if (cf->save_file != NULL) {
883 /* Set up to write to the capture file. */
884 pdh = wtap_dump_open(cf->save_file, out_file_type,
885 linktype, wtap_snapshot_length(cf->wth), &err);
888 /* We couldn't set up to write to the capture file. */
891 case WTAP_ERR_UNSUPPORTED_FILE_TYPE:
893 "tethereal: Capture files can't be written in that format.\n");
896 case WTAP_ERR_UNSUPPORTED_ENCAP:
897 case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
899 "tethereal: The capture file being read cannot be written in that format.\n");
902 case WTAP_ERR_CANT_OPEN:
904 "tethereal: The file \"%s\" couldn't be created for some unknown reason.\n",
908 case WTAP_ERR_SHORT_WRITE:
910 "tethereal: A full header couldn't be written to the file \"%s\".\n",
917 "tethereal: The file \"%s\" could not be opened: Error %d.\n",
921 "tethereal: The file \"%s\" could not be opened: %s\n.",
922 cf->save_file, strerror(err));
930 success = wtap_loop(cf->wth, 0, wtap_dispatch_cb_write, (u_char *) &args,
933 /* Now close the capture file. */
934 if (!wtap_dump_close(pdh, &err))
935 show_capture_file_io_error(cfile.save_file, err, TRUE);
939 success = wtap_loop(cf->wth, 0, wtap_dispatch_cb_print, (u_char *) &args,
943 /* Print up a message box noting that the read failed somewhere along
947 case WTAP_ERR_UNSUPPORTED_ENCAP:
949 "tethereal: \"%s\" is a capture file is for a network type that Tethereal doesn't support.\n",
953 case WTAP_ERR_CANT_READ:
955 "tethereal: An attempt to read from \"%s\" failed for some unknown reason.\n",
959 case WTAP_ERR_SHORT_READ:
961 "tethereal: \"%s\" appears to have been cut short in the middle of a packet.\n",
965 case WTAP_ERR_BAD_RECORD:
967 "tethereal: \"%s\" appears to be damaged or corrupt.\n",
973 "tethereal: An error occurred while reading \"%s\": %s.\n",
974 cf->filename, wtap_strerror(err));
987 fill_in_fdata(frame_data *fdata, capture_file *cf,
988 const struct wtap_pkthdr *phdr,
989 const union wtap_pseudo_header *pseudo_header, long offset)
996 fdata->data_src = NULL;
997 fdata->num = cf->count;
998 fdata->pkt_len = phdr->len;
999 fdata->cap_len = phdr->caplen;
1000 fdata->file_off = offset;
1001 fdata->cinfo = NULL;
1002 fdata->lnk_t = phdr->pkt_encap;
1003 fdata->abs_secs = phdr->ts.tv_sec;
1004 fdata->abs_usecs = phdr->ts.tv_usec;
1005 fdata->flags.passed_dfilter = 0;
1006 fdata->flags.encoding = CHAR_ASCII;
1007 fdata->flags.visited = 0;
1008 fdata->flags.marked = 0;
1010 /* If we don't have the time stamp of the first packet in the
1011 capture, it's because this is the first packet. Save the time
1012 stamp of this packet as the time stamp of the first packet. */
1013 if (!firstsec && !firstusec) {
1014 firstsec = fdata->abs_secs;
1015 firstusec = fdata->abs_usecs;
1018 /* If we don't have the time stamp of the previous displayed packet,
1019 it's because this is the first displayed packet. Save the time
1020 stamp of this packet as the time stamp of the previous displayed
1022 if (!prevsec && !prevusec) {
1023 prevsec = fdata->abs_secs;
1024 prevusec = fdata->abs_usecs;
1027 /* Get the time elapsed between the first packet and this packet. */
1028 compute_timestamp_diff(&fdata->rel_secs, &fdata->rel_usecs,
1029 fdata->abs_secs, fdata->abs_usecs, firstsec, firstusec);
1031 /* If it's greater than the current elapsed time, set the elapsed time
1032 to it (we check for "greater than" so as not to be confused by
1033 time moving backwards). */
1034 if ((gint32)cf->esec < fdata->rel_secs
1035 || ((gint32)cf->esec == fdata->rel_secs && (gint32)cf->eusec < fdata->rel_usecs)) {
1036 cf->esec = fdata->rel_secs;
1037 cf->eusec = fdata->rel_usecs;
1040 /* Get the time elapsed between the previous displayed packet and
1042 compute_timestamp_diff(&fdata->del_secs, &fdata->del_usecs,
1043 fdata->abs_secs, fdata->abs_usecs, prevsec, prevusec);
1044 prevsec = fdata->abs_secs;
1045 prevusec = fdata->abs_usecs;
1047 fdata->cinfo = &cf->cinfo;
1048 for (i = 0; i < fdata->cinfo->num_cols; i++) {
1049 fdata->cinfo->col_buf[i][0] = '\0';
1050 fdata->cinfo->col_data[i] = fdata->cinfo->col_buf[i];
1054 /* Free up all data attached to a "frame_data" structure. */
1056 clear_fdata(frame_data *fdata)
1059 g_slist_free(fdata->pfd);
1060 if (fdata->data_src)
1061 g_slist_free(fdata->data_src);
1065 wtap_dispatch_cb_write(u_char *user, const struct wtap_pkthdr *phdr,
1066 long offset, union wtap_pseudo_header *pseudo_header, const u_char *buf)
1068 cb_args_t *args = (cb_args_t *) user;
1069 capture_file *cf = args->cf;
1070 wtap_dumper *pdh = args->pdh;
1072 proto_tree *protocol_tree;
1075 epan_dissect_t *edt;
1079 fill_in_fdata(&fdata, cf, phdr, pseudo_header, offset);
1080 protocol_tree = proto_tree_create_root();
1081 edt = epan_dissect_new(pseudo_header, buf, &fdata, protocol_tree);
1082 passed = dfilter_apply_edt(cf->rfcode, edt);
1084 protocol_tree = NULL;
1089 if (!wtap_dump(pdh, phdr, pseudo_header, buf, &err)) {
1091 if (ld.pch != NULL) {
1092 /* We're capturing packets, so we're printing a count of packets
1093 captured; move to the line after the count. */
1094 fprintf(stderr, "\n");
1097 show_capture_file_io_error(cf->save_file, err, FALSE);
1102 wtap_dump_close(pdh, &err);
1106 if (protocol_tree != NULL)
1107 proto_tree_free(protocol_tree);
1109 epan_dissect_free(edt);
1111 clear_fdata(&fdata);
1115 show_capture_file_io_error(const char *fname, int err, gboolean is_close)
1121 "tethereal: Not all the packets could be written to \"%s\" because there is "
1122 "no space left on the file system.\n",
1129 "tethereal: Not all the packets could be written to \"%s\" because you are "
1130 "too close to, or over your disk quota.\n",
1135 case WTAP_ERR_CANT_CLOSE:
1137 "tethereal: \"%s\" couldn't be closed for some unknown reason.\n",
1141 case WTAP_ERR_SHORT_WRITE:
1143 "tethereal: Not all the packets could be written to \"%s\".\n",
1150 "tethereal: \"%s\" could not be closed: %s.\n",
1151 fname, wtap_strerror(err));
1154 "tethereal: An error occurred while writing to \"%s\": %s.\n",
1155 fname, wtap_strerror(err));
1162 wtap_dispatch_cb_print(u_char *user, const struct wtap_pkthdr *phdr,
1163 long offset, union wtap_pseudo_header *pseudo_header, const u_char *buf)
1165 cb_args_t *args = (cb_args_t *) user;
1166 capture_file *cf = args->cf;
1168 proto_tree *protocol_tree;
1170 print_args_t print_args;
1171 epan_dissect_t *edt;
1176 /* The protocol tree will be "visible", i.e., printed, only if we're
1177 not printing a summary. */
1178 proto_tree_is_visible = verbose;
1180 fill_in_fdata(&fdata, cf, phdr, pseudo_header, offset);
1183 if (cf->rfcode || verbose)
1184 protocol_tree = proto_tree_create_root();
1186 protocol_tree = NULL;
1187 edt = epan_dissect_new(pseudo_header, buf, &fdata, protocol_tree);
1189 passed = dfilter_apply_edt(cf->rfcode, edt);
1191 /* The packet passed the read filter. */
1193 /* Print the information in the protocol tree. */
1194 print_args.to_file = TRUE;
1195 print_args.format = PR_FMT_TEXT;
1196 print_args.print_summary = FALSE;
1197 print_args.print_hex = print_hex;
1198 print_args.expand_all = TRUE;
1199 print_args.suppress_unmarked = FALSE;
1200 proto_tree_print(FALSE, &print_args, (GNode *)protocol_tree,
1203 /* "print_hex_data()" will put out a leading blank line, as well
1204 as a trailing one; print one here, to separate the packets,
1205 only if "print_hex_data()" won't be called. */
1209 /* Just fill in the columns. */
1210 fill_in_columns(&fdata);
1212 /* Now print them. */
1213 for (i = 0; i < cf->cinfo.num_cols; i++) {
1214 switch (cf->cinfo.col_fmt[i]) {
1217 * Don't print this if we're doing a live capture from a network
1218 * interface - if we're doing a live capture, you won't be
1219 * able to look at the capture in the future (it's not being
1220 * saved anywhere), so the frame numbers are unlikely to be
1223 * (XXX - it might be nice to be able to save and print at
1224 * the same time, sort of like an "Update list of packets
1225 * in real time" capture in Ethereal.)
1227 if (cf->iface != NULL)
1229 printf("%3s", cf->cinfo.col_data[i]);
1235 case COL_ABS_DATE_TIME: /* XXX - wider */
1236 printf("%10s", cf->cinfo.col_data[i]);
1242 case COL_DEF_DL_SRC:
1243 case COL_RES_DL_SRC:
1244 case COL_UNRES_DL_SRC:
1245 case COL_DEF_NET_SRC:
1246 case COL_RES_NET_SRC:
1247 case COL_UNRES_NET_SRC:
1248 printf("%12s", cf->cinfo.col_data[i]);
1254 case COL_DEF_DL_DST:
1255 case COL_RES_DL_DST:
1256 case COL_UNRES_DL_DST:
1257 case COL_DEF_NET_DST:
1258 case COL_RES_NET_DST:
1259 case COL_UNRES_NET_DST:
1260 printf("%-12s", cf->cinfo.col_data[i]);
1264 printf("%s", cf->cinfo.col_data[i]);
1267 if (i != cf->cinfo.num_cols - 1) {
1269 * This isn't the last column, so we need to print a
1270 * separator between this column and the next.
1272 * If we printed a network source and are printing a
1273 * network destination of the same type next, separate
1274 * them with "->"; if we printed a network destination
1275 * and are printing a network source of the same type
1276 * next, separate them with "<-"; otherwise separate them
1279 switch (cf->cinfo.col_fmt[i]) {
1284 switch (cf->cinfo.col_fmt[i + 1]) {
1298 case COL_DEF_DL_SRC:
1299 case COL_RES_DL_SRC:
1300 case COL_UNRES_DL_SRC:
1301 switch (cf->cinfo.col_fmt[i + 1]) {
1303 case COL_DEF_DL_DST:
1304 case COL_RES_DL_DST:
1305 case COL_UNRES_DL_DST:
1315 case COL_DEF_NET_SRC:
1316 case COL_RES_NET_SRC:
1317 case COL_UNRES_NET_SRC:
1318 switch (cf->cinfo.col_fmt[i + 1]) {
1320 case COL_DEF_NET_DST:
1321 case COL_RES_NET_DST:
1322 case COL_UNRES_NET_DST:
1335 switch (cf->cinfo.col_fmt[i + 1]) {
1349 case COL_DEF_DL_DST:
1350 case COL_RES_DL_DST:
1351 case COL_UNRES_DL_DST:
1352 switch (cf->cinfo.col_fmt[i + 1]) {
1354 case COL_DEF_DL_SRC:
1355 case COL_RES_DL_SRC:
1356 case COL_UNRES_DL_SRC:
1366 case COL_DEF_NET_DST:
1367 case COL_RES_NET_DST:
1368 case COL_UNRES_NET_DST:
1369 switch (cf->cinfo.col_fmt[i + 1]) {
1371 case COL_DEF_NET_SRC:
1372 case COL_RES_NET_SRC:
1373 case COL_UNRES_NET_SRC:
1392 print_hex_data(stdout, print_args.format, &fdata);
1398 /* The ANSI C standard does not appear to *require* that a line-buffered
1399 stream be flushed to the host environment whenever a newline is
1400 written, it just says that, on such a stream, characters "are
1401 intended to be transmitted to or from the host environment as a
1402 block when a new-line character is encountered".
1404 The Visual C++ 6.0 C implementation doesn't do what is intended;
1405 even if you set a stream to be line-buffered, it still doesn't
1406 flush the buffer at the end of every line.
1408 So, if the "-l" flag was specified, we flush the standard output
1409 at the end of a packet. This will do the right thing if we're
1410 printing packet summary lines, and, as we print the entire protocol
1411 tree for a single packet without waiting for anything to happen,
1412 it should be as good as line-buffered mode if we're printing
1413 protocol trees. (The whole reason for the "-l" flag in either
1414 tcpdump or Tethereal is to allow the output of a live capture to
1415 be piped to a program or script and to have that script see the
1416 information for the packet as soon as it's printed, rather than
1417 having to wait until a standard I/O buffer fills up. */
1420 if (protocol_tree != NULL)
1421 proto_tree_free(protocol_tree);
1423 epan_dissect_free(edt);
1425 clear_fdata(&fdata);
1427 proto_tree_is_visible = FALSE;
1431 file_open_error_message(int err, gboolean for_writing)
1434 static char errmsg_errno[1024+1];
1438 case WTAP_ERR_NOT_REGULAR_FILE:
1439 errmsg = "The file \"%s\" is a \"special file\" or socket or other non-regular file.";
1442 case WTAP_ERR_FILE_UNKNOWN_FORMAT:
1443 case WTAP_ERR_UNSUPPORTED:
1444 /* Seen only when opening a capture file for reading. */
1445 errmsg = "The file \"%s\" is not a capture file in a format Tethereal understands.";
1448 case WTAP_ERR_UNSUPPORTED_FILE_TYPE:
1449 /* Seen only when opening a capture file for writing. */
1450 errmsg = "Tethereal does not support writing capture files in that format.";
1453 case WTAP_ERR_UNSUPPORTED_ENCAP:
1454 case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
1456 errmsg = "Tethereal cannot save this capture in that format.";
1458 errmsg = "The file \"%s\" is a capture for a network type that Tethereal doesn't support.";
1461 case WTAP_ERR_BAD_RECORD:
1462 errmsg = "The file \"%s\" appears to be damaged or corrupt.";
1465 case WTAP_ERR_CANT_OPEN:
1467 errmsg = "The file \"%s\" could not be created for some unknown reason.";
1469 errmsg = "The file \"%s\" could not be opened for some unknown reason.";
1472 case WTAP_ERR_SHORT_READ:
1473 errmsg = "The file \"%s\" appears to have been cut short"
1474 " in the middle of a packet.";
1477 case WTAP_ERR_SHORT_WRITE:
1478 errmsg = "A full header couldn't be written to the file \"%s\".";
1483 errmsg = "The path to the file \"%s\" does not exist.";
1485 errmsg = "The file \"%s\" does not exist.";
1490 errmsg = "You do not have permission to create or write to the file \"%s\".";
1492 errmsg = "You do not have permission to read the file \"%s\".";
1496 errmsg = "\"%s\" is a directory (folder), not a file.";
1500 snprintf(errmsg_errno, sizeof(errmsg_errno),
1501 "The file \"%%s\" could not be opened: %s.",
1502 wtap_strerror(err));
1503 errmsg = errmsg_errno;
1510 open_cap_file(char *fname, gboolean is_tempfile, capture_file *cf)
1515 struct stat cf_stat;
1516 char err_msg[2048+1];
1518 wth = wtap_open_offline(fname, &err, FALSE);
1522 /* Find the size of the file. */
1524 if (fstat(fd, &cf_stat) < 0) {
1530 /* The open succeeded. Fill in the information for this file. */
1532 /* Initialize the table of conversations. */
1533 epan_conversation_init();
1535 /* Initialize protocol-specific variables */
1536 init_all_protocols();
1538 /* Initialize the common data structures for fragment reassembly.
1539 Must be done *after* "init_all_protocols()", as "init_all_protocols()"
1540 may free up space for fragments, which it finds by using the
1541 data structures that "reassemble_init()" frees. */
1546 cf->f_len = cf_stat.st_size;
1548 /* Set the file name because we need it to set the follow stream filter.
1549 XXX - is that still true? We need it for other reasons, though,
1551 cf->filename = g_strdup(fname);
1553 /* Indicate whether it's a permanent or temporary file. */
1554 cf->is_tempfile = is_tempfile;
1556 /* If it's a temporary capture buffer file, mark it as not saved. */
1557 cf->user_saved = !is_tempfile;
1559 cf->cd_t = wtap_file_type(cf->wth);
1561 cf->drops_known = FALSE;
1565 cf->snap = wtap_snapshot_length(cf->wth);
1566 cf->progbar_quantum = 0;
1567 cf->progbar_nextstep = 0;
1568 firstsec = 0, firstusec = 0;
1569 prevsec = 0, prevusec = 0;
1574 snprintf(err_msg, sizeof err_msg, file_open_error_message(err, FALSE), fname);
1575 fprintf(stderr, "tethereal: %s\n", err_msg);