If we have pcap_open, call it instead of pcap_open_live, otherwise we might
[obnox/wireshark/wip.git] / text2pcap.c
index 4d710abdd5e44e2cf2a1f774553105d52ee8eeb6..829b205dbc2a801395989bbeb3448841f215803b 100644 (file)
 # include "config.h"
 #endif
 
-#include <ctype.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
 /*
  * Just make sure we include the prototype for strptime as well
  * (needed for glibc 2.2) but make sure we do this only if not
 #ifndef __USE_XOPEN
 #  define __USE_XOPEN
 #endif
+#ifndef _XOPEN_SOURCE
+#  define _XOPEN_SOURCE
+#endif
+
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <wiretap/file_util.h>
 
 #include <time.h>
 #include <glib.h>
@@ -177,6 +181,7 @@ static unsigned long num_packets_written = 0;
 static gint32 ts_sec  = 0;
 static guint32 ts_usec = 0;
 static char *ts_fmt = NULL;
+static struct tm timecode_default;
 
 /* Input file */
 static const char *input_filename;
@@ -581,7 +586,7 @@ write_current_packet (void)
             HDR_UDP.length = g_htons(proto_length);
 
            HDR_UDP.checksum = 0;
-           u = g_ntohs(in_checksum(&pseudoh, sizeof(pseudoh))) + 
+           u = g_ntohs(in_checksum(&pseudoh, sizeof(pseudoh))) +
                    g_ntohs(in_checksum(&HDR_UDP, sizeof(HDR_UDP))) +
                    g_ntohs(in_checksum(packet_buf, curr_offset));
            HDR_UDP.checksum = g_htons((u & 0xffff) + (u>>16));
@@ -599,7 +604,7 @@ write_current_packet (void)
            HDR_TCP.window = g_htons(0x2000);
 
            HDR_TCP.checksum = 0;
-           u = g_ntohs(in_checksum(&pseudoh, sizeof(pseudoh))) + 
+           u = g_ntohs(in_checksum(&pseudoh, sizeof(pseudoh))) +
                    g_ntohs(in_checksum(&HDR_TCP, sizeof(HDR_TCP))) +
                    g_ntohs(in_checksum(packet_buf, curr_offset));
            HDR_TCP.checksum = g_htons((u & 0xffff) + (u>>16));
@@ -699,14 +704,14 @@ append_to_preamble(char *str)
     if (toklen != 0) {
         if (packet_preamble_len + toklen > PACKET_PREAMBLE_MAX_LEN)
             return;    /* no room to add the token to the preamble */
-        strcpy(&packet_preamble[packet_preamble_len], str);
+        strncpy(&packet_preamble[packet_preamble_len], str, PACKET_PREAMBLE_MAX_LEN - packet_preamble_len);
         packet_preamble_len += toklen;
        if (debug >= 2) {
                char *c;
                char xs[PACKET_PREAMBLE_MAX_LEN];
-               strcpy(xs, packet_preamble);
+               strncpy(xs, packet_preamble, PACKET_PREAMBLE_MAX_LEN);
                while ((c = strchr(xs, '\r')) != NULL) *c=' ';
-               fprintf (stderr, "[[append_to_preamble: \"%s\"]]", xs); 
+               fprintf (stderr, "[[append_to_preamble: \"%s\"]]", xs);
        }
     }
 }
@@ -732,20 +737,11 @@ parse_preamble (void)
            return;
 
        /*
-        * Initialize to the Epoch, just in case not all fields
-        * of the date and time are specified
-        * (or date & time is not parsed or fails to parse below).
+        * Initialize to today localtime, just in case not all fields
+        * of the date and time are specified.
         */
-       timecode.tm_sec   = 0;
-       timecode.tm_min   = 0;
-       timecode.tm_hour  = 0;
-       timecode.tm_mday  = 1;
-       timecode.tm_mon   = 0;
-       timecode.tm_year  = 70;
-       timecode.tm_wday  = 0;
-       timecode.tm_yday  = 0;
-       timecode.tm_isdst = -1;
-       ts_sec  = (gint32)mktime( &timecode );
+
+       timecode = timecode_default;
        ts_usec = 0;
 
        /*
@@ -761,9 +757,29 @@ parse_preamble (void)
                subsecs = strptime( packet_preamble, ts_fmt, &timecode );
                if (subsecs != NULL) {
                        /* Get the long time from the tm structure */
+                        /*  (will return -1 if failure)            */
                        ts_sec  = (gint32)mktime( &timecode );
+               } else
+                       ts_sec = -1;    /* we failed to parse it */
 
-                       /* Get subsecs only if strptime succeeded above  */
+               /* This will ensure incorrectly parsed dates get set to zero */
+               if ( -1 == ts_sec )
+               {
+                       /* Sanitize - remove all '\r' */
+                       char *c;
+                       while ((c = strchr(packet_preamble, '\r')) != NULL) *c=' ';
+                       fprintf (stderr, "Failure processing time \"%s\" using time format \"%s\"\n   (defaulting to Jan 1,1970 00:00:00 GMT)\n",
+                                packet_preamble, ts_fmt);
+                       if (debug >= 2) {
+                               fprintf(stderr, "timecode: %02d/%02d/%d %02d:%02d:%02d %d\n",
+                                       timecode.tm_mday, timecode.tm_mon, timecode.tm_year,
+                                       timecode.tm_hour, timecode.tm_min, timecode.tm_sec, timecode.tm_isdst);
+                       }
+                       ts_sec  = 0;  /* Jan 1,1970: 00:00 GMT; tshark/wireshark will display date/time as adjusted by timezone */
+                       ts_usec = 0;
+               }
+               else
+               {
                        /* Parse subseconds */
                        ts_usec = strtol(subsecs, &p, 10);
                        if (subsecs == p) {
@@ -792,18 +808,12 @@ parse_preamble (void)
                                                ts_usec *= 10;
                                }
                        }
-                } else { /* subsecs == NULL: strptime failed to parse */
-                       /* Sanitize - remove all '\r' */
-                       char *c;
-                       while ((c = strchr(packet_preamble, '\r')) != NULL) *c=' ';
-                       fprintf (stderr, "Failure parsing time \"%s\" using time format \"%s\"\n   (defaulting to Jan 1,1970 00:00:00)\n",
-                                packet_preamble, ts_fmt);
                }
        }
        if (debug >= 2) {
                char *c;
                while ((c = strchr(packet_preamble, '\r')) != NULL) *c=' ';
-               fprintf(stderr, "[[parse_preamble: \"%s\"]]\n", packet_preamble); 
+               fprintf(stderr, "[[parse_preamble: \"%s\"]]\n", packet_preamble);
                fprintf(stderr, "Format(%s), time(%u), subsecs(%u)\n", ts_fmt, ts_sec, ts_usec);
        }
 
@@ -1014,7 +1024,7 @@ usage (void)
             "      <output-filename> specifies output filename (use - for standard output)\n"
             "\n"
             "Input:\n"
-            "  -o hex|oct             parse offsets as (h)ex or (o)ctal, default is hex\n"
+            "  -o hex|oct|dec         parse offsets as (h)ex, (o)ctal or (d)ecimal, default is hex\n"
             "  -t <timefmt>           treats the text before the packet as a date/time code;\n"
             "                         the specified argument is a format string of the sort \n"
             "                         supported by strptime.\n"
@@ -1023,6 +1033,8 @@ usage (void)
             "                         NOTE: The subsecond component delimiter must be given\n"
             "                          (.) but no pattern is required; the remaining number\n"
             "                          is assumed to be fractions of a second.\n"
+            "                         NOTE: Date/time fields from the current date/time are\n"
+            "                         used as the default for unspecified fields.\n"
             "\n"
             "Output:\n"
             "  -l <typenum>           link-layer type number. Default is 1 (Ethernet). \n"
@@ -1084,11 +1096,15 @@ parse_options (int argc, char *argv[])
         case 'l': pcap_link_type = strtol(optarg, NULL, 0); break;
         case 'm': max_offset = strtol(optarg, NULL, 0); break;
         case 'o':
-            if (optarg[0]!='h' && optarg[0] != 'o') {
+            if (optarg[0]!='h' && optarg[0] != 'o' && optarg[0] != 'd') {
                 fprintf(stderr, "Bad argument for '-o': %s\n", optarg);
                 usage();
             }
-            offset_base = (optarg[0]=='o') ? 8 : 16;
+                       switch(optarg[0]) {
+                       case 'o': offset_base = 8; break;
+                       case 'h': offset_base = 16; break;
+                       case 'd': offset_base = 10; break;
+                       }
             break;
         case 'e':
             hdr_ethernet = TRUE;
@@ -1248,7 +1264,7 @@ parse_options (int argc, char *argv[])
 
     if (strcmp(argv[optind], "-")) {
         input_filename = strdup(argv[optind]);
-        input_file = fopen(input_filename, "rb");
+        input_file = eth_fopen(input_filename, "rb");
         if (!input_file) {
             fprintf(stderr, "Cannot open file [%s] for reading: %s\n",
                     input_filename, strerror(errno));
@@ -1261,7 +1277,7 @@ parse_options (int argc, char *argv[])
 
     if (strcmp(argv[optind+1], "-")) {
         output_filename = strdup(argv[optind+1]);
-        output_file = fopen(output_filename, "wb");
+        output_file = eth_fopen(output_filename, "wb");
         if (!output_file) {
             fprintf(stderr, "Cannot open file [%s] for writing: %s\n",
                     output_filename, strerror(errno));
@@ -1289,6 +1305,7 @@ parse_options (int argc, char *argv[])
     }
 
     ts_sec = (gint32) time(0);         /* initialize to current time */
+    timecode_default = *localtime((time_t *)&ts_sec);
 
     /* Display summary of our state */
     if (!quiet) {
@@ -1326,8 +1343,9 @@ int main(int argc, char *argv[])
     if (debug)
         fprintf(stderr, "\n-------------------------\n");
     if (!quiet) {
-    fprintf(stderr, "Read %ld potential packets, wrote %ld packets\n",
-            num_packets_read, num_packets_written);
+    fprintf(stderr, "Read %ld potential packet%s, wrote %ld packet%s\n",
+            num_packets_read,    (num_packets_read==1)   ?"":"s",
+            num_packets_written, (num_packets_written==1)?"":"s");
     }
     return 0;
 }