Add the missing files from Balint Reczey's patch for bug 2233.
[obnox/wireshark/wip.git] / text2pcap.c
index ef5cac70f653876a4b5be76908a5903064960089..e8d1252f204ed4c1272aee5a1b9175e87b96feb4 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));
@@ -701,12 +706,20 @@ append_to_preamble(char *str)
             return;    /* no room to add the token to the preamble */
         strcpy(&packet_preamble[packet_preamble_len], str);
         packet_preamble_len += toklen;
+       if (debug >= 2) {
+               char *c;
+               char xs[PACKET_PREAMBLE_MAX_LEN];
+               strcpy(xs, packet_preamble);
+               while ((c = strchr(xs, '\r')) != NULL) *c=' ';
+               fprintf (stderr, "[[append_to_preamble: \"%s\"]]", xs);
+       }
     }
 }
 
 /*----------------------------------------------------------------------
  * Parse the preamble to get the timecode.
  */
+
 static void
 parse_preamble (void)
 {
@@ -723,7 +736,12 @@ parse_preamble (void)
        if (ts_fmt == NULL)
            return;
 
-       ts_sec  = 0;
+       /*
+        * Initialize to today localtime, just in case not all fields
+        * of the date and time are specified.
+        */
+
+       timecode = timecode_default;
        ts_usec = 0;
 
        /*
@@ -735,35 +753,29 @@ parse_preamble (void)
         * This should cover line breaks etc that get counted.
         */
        if ( strlen(packet_preamble) > 2 ) {
-               /*
-                * Initialize to the Epoch, 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;
-
                /* Get Time leaving subseconds */
                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 {
-                       fprintf (stderr, "Failed to parse '%s' using time format '%s'; using 00:00:00 (midnight) as default\n",
-                                       packet_preamble, ts_fmt);
+               } else
                        ts_sec = -1;    /* we failed to parse it */
-                }
 
                /* This will ensure incorrectly parsed dates get set to zero */
                if ( -1 == ts_sec )
                {
-                       ts_sec  = 0;
+                       /* 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
@@ -798,10 +810,14 @@ parse_preamble (void)
                        }
                }
        }
+       if (debug >= 2) {
+               char *c;
+               while ((c = strchr(packet_preamble, '\r')) != NULL) *c=' ';
+               fprintf(stderr, "[[parse_preamble: \"%s\"]]\n", packet_preamble);
+               fprintf(stderr, "Format(%s), time(%u), subsecs(%u)\n", ts_fmt, ts_sec, ts_usec);
+       }
 
 
-       /*printf("Format(%s), time(%u), subsecs(%u)\n\n", ts_fmt, ts_sec, ts_usec);*/
-
        /* Clear Preamble */
        packet_preamble_len = 0;
 }
@@ -1017,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"
@@ -1242,7 +1260,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));
@@ -1255,7 +1273,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));
@@ -1283,6 +1301,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) {