# 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>
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;
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));
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));
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);
}
}
}
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;
/*
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) {
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);
}
" <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"
" 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"
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;
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));
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));
}
ts_sec = (gint32) time(0); /* initialize to current time */
+ timecode_default = *localtime((time_t *)&ts_sec);
/* Display summary of our state */
if (!quiet) {
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;
}