]> The Tcpdump Group git mirrors - tcpdump/blobdiff - tcpdump.c
Merge remote-tracking branch 'upstream/master'
[tcpdump] / tcpdump.c
index 5f3bde5661cfe32993a630a1a8dccffafda6379a..c43ab8549548f620f2715f707027477ba540f1a5 100644 (file)
--- a/tcpdump.c
+++ b/tcpdump.c
@@ -589,6 +589,8 @@ static void
 MakeFilename(char *buffer, char *orig_name, int cnt, int max_chars)
 {
         char *filename = malloc(NAME_MAX + 1);
+        if (filename == NULL)
+            error("Makefilename: malloc);
 
         /* Process with strftime if Gflag is set. */
         if (Gflag != 0) {
@@ -630,6 +632,40 @@ static int tcpdump_printf(netdissect_options *ndo _U_,
   return ret;
 }
 
+struct print_info get_print_info(int type) {
+       struct print_info printinfo;
+
+       printinfo.ndo_type = 1;
+       printinfo.ndo = gndo;
+       printinfo.p.ndo_printer = lookup_ndo_printer(type);
+       if (printinfo.p.ndo_printer == NULL) {
+               printinfo.p.printer = lookup_printer(type);
+               printinfo.ndo_type = 0;
+               if (printinfo.p.printer == NULL) {
+                       gndo->ndo_dltname = pcap_datalink_val_to_name(type);
+                       if (gndo->ndo_dltname != NULL)
+                               error("packet printing is not supported for link type %s: use -w",
+                                     gndo->ndo_dltname);
+                       else
+                               error("packet printing is not supported for link type %d: use -w", type);
+               }
+       }
+       return (printinfo);
+}
+
+char *get_next_file(FILE *VFile, char *ptr) {
+       char *ret;
+
+       ret = fgets(ptr, NAME_MAX, VFile);
+       if (!ret)
+               return NULL;
+
+       if (ptr[strlen(ptr) - 1] == '\n')
+               ptr[strlen(ptr) - 1] = '\0';
+
+       return ret;
+}
+
 int
 main(int argc, char **argv)
 {
@@ -639,6 +675,7 @@ main(int argc, char **argv)
        pcap_handler callback;
        int type;
        int dlt;
+       int new_dlt;
        const char *dlt_name;
        struct bpf_program fcode;
 #ifndef WIN32
@@ -1116,19 +1153,16 @@ main(int argc, char **argv)
 #endif /* WIN32 */
                if (VFileName != NULL) {
                        if (VFileName[0] == '-' && VFileName[1] == '\0')
-                               VFile = fopen("/dev/stdin", "r");
+                               VFile = stdin;
                        else
                                VFile = fopen(VFileName, "r");
 
                        if (VFile == NULL)
                                error("Unable to open file: %s\n", strerror(errno));
 
-                       ret = fgets(VFileLine, NAME_MAX, VFile);
+                       ret = get_next_file(VFile, VFileLine);
                        if (!ret)
-                               error("Nothing in %s\n", VFile);
-
-                       if (VFileLine[strlen(VFileLine) - 1] == '\n')
-                               VFileLine[strlen(VFileLine) - 1] = '\0';
+                               error("Nothing in %s\n", VFileName);
                        RFileName = VFileLine;
                }
 
@@ -1413,21 +1447,7 @@ main(int argc, char **argv)
 #endif
        } else {
                type = pcap_datalink(pd);
-                printinfo.ndo_type = 1;
-                printinfo.ndo = gndo;
-               printinfo.p.ndo_printer = lookup_ndo_printer(type);
-                if (printinfo.p.ndo_printer == NULL) {
-                        printinfo.p.printer = lookup_printer(type);
-                        printinfo.ndo_type = 0;
-                        if (printinfo.p.printer == NULL) {
-                                gndo->ndo_dltname = pcap_datalink_val_to_name(type);
-                                if (gndo->ndo_dltname != NULL)
-                                        error("packet printing is not supported for link type %s: use -w",
-                                              gndo->ndo_dltname);
-                                else
-                                        error("packet printing is not supported for link type %d: use -w", type);
-                        }
-                }
+               printinfo = get_print_info(type);
                callback = print_packet;
                pcap_userdata = (u_char *)&printinfo;
        }
@@ -1511,24 +1531,25 @@ main(int argc, char **argv)
                }
                pcap_close(pd);
                if (VFileName != NULL) {
-                       ret = fgets(VFileLine, NAME_MAX, VFile);
+                       ret = get_next_file(VFile, VFileLine);
                        if (ret) {
-                               if (VFileLine[strlen(VFileLine) - 1] == '\n')
-                                       VFileLine[strlen(VFileLine) - 1] = '\0';
                                RFileName = VFileLine;
                                pd = pcap_open_offline(RFileName, ebuf);
                                if (pd == NULL)
                                        error("%s", ebuf);
-                               dlt = pcap_datalink(pd);
-                               dlt_name = pcap_datalink_val_to_name(dlt);
+                               new_dlt = pcap_datalink(pd);
+                               if (WFileName && new_dlt != dlt)
+                                       error("%s: new dlt does not match original", RFileName);
+                               printinfo = get_print_info(new_dlt);
+                               dlt_name = pcap_datalink_val_to_name(new_dlt);
                                if (dlt_name == NULL) {
                                        fprintf(stderr, "reading from file %s, link-type %u\n",
-                                       RFileName, dlt);
+                                       RFileName, new_dlt);
                                } else {
                                        fprintf(stderr,
                                        "reading from file %s, link-type %s (%s)\n",
                                        RFileName, dlt_name,
-                                       pcap_datalink_val_to_description(dlt));
+                                       pcap_datalink_val_to_description(new_dlt));
                                }
                                if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
                                        error("%s", pcap_geterr(pd));