If the expected "next offset" doesn't match the offset we read, it may

merely mean that we mistakenly treated stuff from the text-dump part of
the file we're reading as if it were hex byte data (e.g., if the first
non-white-space part of the text dump was a 2-digit hex number).  If the
offset we read is less than the expected next offset, assume that's the
problem, and throw away enough extra bytes to make the offset we read
the expected next offset.

"getopt()" will never, for any option that the "getopt()" string says
takes an argument, leave "optarg" null; if no argument was specified,
it'll return an error, so there's no need to check for a null "optarg".

svn path=/trunk/; revision=4250
This commit is contained in:
Guy Harris 2001-11-24 07:52:05 +00:00
parent 0bc13df91e
commit f84bce97cc
1 changed files with 36 additions and 18 deletions

View File

@ -6,14 +6,12 @@
* *
* (c) Copyright 2001 Ashok Narayanan <ashokn@cisco.com> * (c) Copyright 2001 Ashok Narayanan <ashokn@cisco.com>
* *
* $Id: text2pcap.c,v 1.4 2001/08/01 03:22:14 guy Exp $ * $Id: text2pcap.c,v 1.5 2001/11/24 07:52:05 guy Exp $
* *
* Ethereal - Network traffic analyzer * Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com> * By Gerald Combs <gerald@ethereal.com>
* Copyright 1998 Gerald Combs * Copyright 1998 Gerald Combs
* *
*
*
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 * as published by the Free Software Foundation; either version 2
@ -282,6 +280,15 @@ write_byte (char *str)
curr_offset ++; curr_offset ++;
} }
/*----------------------------------------------------------------------
* Remove bytes from the current packet
*/
static int
unwrite_bytes (unsigned long nbytes)
{
curr_offset -= nbytes;
}
/*---------------------------------------------------------------------- /*----------------------------------------------------------------------
* Compute one's complement checksum (from RFC1071) * Compute one's complement checksum (from RFC1071)
*/ */
@ -484,12 +491,26 @@ parse_token (token_t token, char *str)
start_new_packet(); start_new_packet();
state = READ_OFFSET; state = READ_OFFSET;
} else if (num != curr_offset) { } else if (num != curr_offset) {
/* Bad offset; switch to INIT state */ /*
if (debug>=1) * The offset we read isn't the one we expected.
fprintf(stderr, "Inconsistent offset. Expecting %0lX, got %0lX. Ignoring rest of packet\n", * This may only mean that we mistakenly interpreted
curr_offset, num); * some text as byte values (e.g., if the text dump
write_current_packet(); * of packet data included a number with spaces around
state = INIT; * it). If the offset is less than what we expected,
* assume that's the problem, and throw away the putative
* extra byte values.
*/
if (num < curr_offset) {
unwrite_bytes(curr_offset - num);
state = READ_OFFSET;
} else {
/* Bad offset; switch to INIT state */
if (debug>=1)
fprintf(stderr, "Inconsistent offset. Expecting %0lX, got %0lX. Ignoring rest of packet\n",
curr_offset, num);
write_current_packet();
state = INIT;
}
} else } else
state = READ_OFFSET; state = READ_OFFSET;
break; break;
@ -614,27 +635,24 @@ parse_options (int argc, char *argv[])
case 'q': quiet = TRUE; debug = FALSE; break; case 'q': quiet = TRUE; debug = FALSE; break;
case 'l': pcap_link_type = atoi(optarg); break; case 'l': pcap_link_type = atoi(optarg); break;
case 'o': case 'o':
if (!optarg || (optarg[0]!='h' && optarg[0] != 'o')) { if (optarg[0]!='h' && optarg[0] != 'o') {
fprintf(stderr, "Bad argument for '-e': %s\n", fprintf(stderr, "Bad argument for '-e': %s\n", optarg);
optarg ? optarg : "");
help(argv[0]); help(argv[0]);
} }
offset_base = (optarg[0]=='o') ? 8 : 16; offset_base = (optarg[0]=='o') ? 8 : 16;
break; break;
case 'e': case 'e':
hdr_ethernet = TRUE; hdr_ethernet = TRUE;
if (!optarg || sscanf(optarg, "%lx", &hdr_ethernet_proto) < 1) { if (sscanf(optarg, "%lx", &hdr_ethernet_proto) < 1) {
fprintf(stderr, "Bad argument for '-e': %s\n", fprintf(stderr, "Bad argument for '-e': %s\n", optarg);
optarg ? optarg : "");
help(argv[0]); help(argv[0]);
} }
break; break;
case 'i': case 'i':
hdr_ip = TRUE; hdr_ip = TRUE;
if (!optarg || sscanf(optarg, "%ld", &hdr_ip_proto) < 1) { if (sscanf(optarg, "%ld", &hdr_ip_proto) < 1) {
fprintf(stderr, "Bad argument for '-i': %s\n", fprintf(stderr, "Bad argument for '-i': %s\n", optarg);
optarg ? optarg : "");
help(argv[0]); help(argv[0]);
} }
hdr_ethernet = TRUE; hdr_ethernet = TRUE;