]> The Tcpdump Group git mirrors - tcpdump/blobdiff - tcpdump.c
Check for a TCP header length that's too short, report more information
[tcpdump] / tcpdump.c
index 12160045906301a52362c2312a687a232ccb2876..3f175c28fa15f5d082f82748aff1c16aa445d0ea 100644 (file)
--- a/tcpdump.c
+++ b/tcpdump.c
@@ -30,7 +30,7 @@ static const char copyright[] _U_ =
     "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
 The Regents of the University of California.  All rights reserved.\n";
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.216.2.3 2003-11-18 08:53:40 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/tcpdump/tcpdump.c,v 1.216.2.12 2004-06-15 00:01:52 guy Exp $ (LBL)";
 #endif
 
 /*
@@ -205,6 +205,9 @@ static struct printer printers[] = {
 #endif
 #ifdef DLT_ENC
        { enc_if_print,         DLT_ENC },
+#endif
+#ifdef DLT_APPLE_IP_OVER_IEEE1394
+       { ap1394_if_print,      DLT_APPLE_IP_OVER_IEEE1394 },
 #endif
        { NULL,                 0 },
 };
@@ -256,7 +259,8 @@ show_dlts_and_exit(pcap_t *pd)
        while (--n_dlts >= 0) {
                dlt_name = pcap_datalink_val_to_name(dlts[n_dlts]);
                if (dlt_name != NULL) {
-                       (void) fprintf(stderr, "  %s", dlt_name);
+                       (void) fprintf(stderr, "  %s (%s)", dlt_name,
+                           pcap_datalink_val_to_description(dlts[n_dlts]));
 
                        /*
                         * OK, does tcpdump handle that type?
@@ -493,6 +497,7 @@ main(int argc, char **argv)
                                      program_name, optarg);
                        (void)fprintf(stderr, "(no libsmi support)\n");
 #endif
+                       break;
 
                case 'O':
                        Oflag = 0;
@@ -610,7 +615,7 @@ main(int argc, char **argv)
                        /* NOTREACHED */
                }
 
-       if (tflag > 0)
+       if (tflag > 0 || tflag == -3)
                thiszone = gmt2local(0);
 
        if (RFileName != NULL) {
@@ -634,10 +639,15 @@ main(int argc, char **argv)
                        error("%s", ebuf);
                dlt = pcap_datalink(pd);
                dlt_name = pcap_datalink_val_to_name(dlt);
-               if (dlt_name == NULL)
-                       dlt_name = "???";
-                printf("reading from file %s, link-type %u (%s)\n",
-                      RFileName, dlt, dlt_name);
+               if (dlt_name == NULL) {
+                       fprintf(stderr, "reading from file %s, link-type %u\n",
+                           RFileName, dlt);
+               } else {
+                       fprintf(stderr,
+                           "reading from file %s, link-type %s (%s)\n",
+                           RFileName, dlt_name,
+                           pcap_datalink_val_to_description(dlt));
+               }
                localnet = 0;
                netmask = 0;
                if (fflag != 0)
@@ -649,11 +659,8 @@ main(int argc, char **argv)
                                error("%s", ebuf);
                }
 #ifdef WIN32
-               if(IsTextUnicode(device,  
-                       wcslen((short*)device),                // Device always ends with a double \0, so this way to determine its 
-                                                                                                       // length should be always valid
-                       NULL))
-               {
+               if(strlen(device) == 1) //we assume that an ASCII string is always longer than 1 char
+               {                                               //a Unicode string has a \0 as second byte (so strlen() is 1)
                        fprintf(stderr, "%s: listening on %ws\n", program_name, device);
                }
                else
@@ -783,10 +790,14 @@ main(int argc, char **argv)
                        (void)fprintf(stderr, "%s: ", program_name);
                dlt = pcap_datalink(pd);
                dlt_name = pcap_datalink_val_to_name(dlt);
-               if (dlt_name == NULL)
-                       dlt_name = "???";
-               (void)fprintf(stderr, "listening on %s, link-type %u (%s), capture size %u bytes\n",
-                   device, dlt, dlt_name, snaplen);
+               if (dlt_name == NULL) {
+                       (void)fprintf(stderr, "listening on %s, link-type %u, capture size %u bytes\n",
+                           device, dlt, snaplen);
+               } else {
+                       (void)fprintf(stderr, "listening on %s, link-type %s (%s), capture size %u bytes\n",
+                           device, dlt_name,
+                           pcap_datalink_val_to_description(dlt), snaplen);
+               }
                (void)fflush(stderr);
        }
 #endif /* WIN32 */
@@ -930,13 +941,18 @@ dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *s
         * file could put it over Cflag.
         */
        if (ftell((FILE *)dump_info->p) > Cflag) {
+               /*
+                * Close the current file and open a new one.
+                */
+               pcap_dump_close(dump_info->p);
+               if (cnt >= 1000)
+                       error("too many output files");
                name = (char *) malloc(strlen(dump_info->WFileName) + 4);
                if (name == NULL)
                        error("dump_packet_and_trunc: malloc");
                strcpy(name, dump_info->WFileName);
                swebitoa(cnt, name + strlen(dump_info->WFileName));
                cnt++;
-               pcap_dump_close(dump_info->p);
                dump_info->p = pcap_dump_open(dump_info->pd, name);
                free(name);
                if (dump_info->p == NULL)
@@ -1001,7 +1017,7 @@ print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
                        /*
                         * Include the link-layer header.
                         */
-                       default_print_unaligned(sp, h->caplen);
+                       default_print(sp, h->caplen);
                } else {
                        /*
                         * Don't include the link-layer header - and if
@@ -1009,7 +1025,7 @@ print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
                         * print nothing.
                         */
                        if (h->caplen > hdrlen)
-                               default_print_unaligned(sp + hdrlen,
+                               default_print(sp + hdrlen,
                                    h->caplen - hdrlen);
                }
        }
@@ -1021,32 +1037,6 @@ print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
                info(0);
 }
 
-/* Like default_print() but data need not be aligned */
-void
-default_print_unaligned(register const u_char *cp, register u_int length)
-{
-       register u_int i, s;
-       register int nshorts;
-
-       if (Xflag) {
-               ascii_print(cp, length);
-               return;
-       }
-       nshorts = (u_int) length / sizeof(u_short);
-       i = 0;
-       while (--nshorts >= 0) {
-               if ((i++ % 8) == 0)
-                       (void)printf("\n\t\t\t");
-               s = *cp++;
-               (void)printf(" %02x%02x", s, *cp++);
-       }
-       if (length & 1) {
-               if ((i % 8) == 0)
-                       (void)printf("\n\t\t\t");
-               (void)printf(" %02x", *cp);
-       }
-}
-
 #ifdef WIN32
        /*
         * XXX - there should really be libpcap calls to get the version
@@ -1069,7 +1059,7 @@ default_print_unaligned(register const u_char *cp, register u_int length)
        char WDversion[]="current-cvs.tcpdump.org";
        char version[]="current-cvs.tcpdump.org";
        char pcap_version[]="current-cvs.tcpdump.org";
-       char Wpcap_version[]="3.0 alpha";
+       char Wpcap_version[]="3.1";
 #endif
 
 /*
@@ -1078,7 +1068,7 @@ default_print_unaligned(register const u_char *cp, register u_int length)
 void
 default_print(register const u_char *bp, register u_int length)
 {
-       default_print_unaligned(bp, length);
+    ascii_print("\n\t", bp, length); /* pass on lf and identation string */
 }
 
 #ifdef SIGINFO
@@ -1104,8 +1094,12 @@ usage(void)
 #endif /* HAVE_PCAP_LIB_VERSION */
 
 #ifdef HAVE_PCAP_LIB_VERSION
+#ifdef WIN32
+       (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);
+#else /* WIN32 */
        (void)fprintf(stderr, "%s version %s\n", program_name, version);
-       (void)fprintf(stderr, "%s\n", pcap_lib_version());
+#endif /* WIN32 */
+       (void)fprintf(stderr, "%s\n",pcap_lib_version());
 #else /* HAVE_PCAP_LIB_VERSION */
 #ifdef WIN32
        (void)fprintf(stderr, "%s version %s, based on tcpdump version %s\n", program_name, WDversion, version);