Skip to content

Commit

Permalink
rewrite read_l2data() to parse a comma seperated hex string as the l2…
Browse files Browse the repository at this point in the history
… data
  • Loading branch information
synfinatic committed Jun 17, 2003
1 parent 1220774 commit 3ce8ab3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
12 changes: 6 additions & 6 deletions man/tcpreplay.8
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.\" $Id: tcpreplay.8,v 1.8 2003/06/16 19:33:28 aturner Exp $
.\" $Id: tcpreplay.8,v 1.9 2003/06/17 18:27:01 aturner Exp $
.TH TCPREPLAY 8
.SH NAME
tcpreplay \- replay packets from capture files
Expand Down Expand Up @@ -64,7 +64,7 @@ CIDR...
.B \-X
.I exclude
] [
.B \-2 l2datafile
.B \-2 l2data
]
.I file ...
.SH DESCRIPTION
Expand Down Expand Up @@ -207,10 +207,10 @@ P:<list> - Must be one of the listed packets where the list corresponds to
packet number in the capture file. Ex: -X P:1-5,9,15 would send all packets except 1
through 5, 9 and 15.
.TP
.B \-2 or l2datafile
Specifies a file containing the Layer 2 header to use instead of the Layer 2 header
in the packet. Useful for converting between 802.x types or adding a header when
the pcap file doesn't contain a header (as in the case of DLT_RAW). Currently
.B \-2 or l2data
Specifies a string of comma seperated numbers in hex to be used instead of the Layer
2 header in the packet. Useful for converting between 802.x types or adding a header
when the pcap file doesn't contain a header (as in the case of DLT_RAW). Currently
this only supports the following pcap(3) types: DLT_EN10MB and DLT_RAW.
.SH SIGNALS
.I Tcpreplay
Expand Down
56 changes: 35 additions & 21 deletions tcpreplay.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $Id: tcpreplay.c,v 1.61 2003/06/16 19:28:40 aturner Exp $ */
/* $Id: tcpreplay.c,v 1.62 2003/06/17 18:27:01 aturner Exp $ */

/*
* Copyright (c) 2001, 2002, 2003 Aaron Turner, Matt Bing.
Expand Down Expand Up @@ -40,7 +40,7 @@ volatile int didsig;
int include_exclude_mode = 0;
CIDR *xX_cidr = NULL;
LIST *xX_list = NULL;
char *l2data = NULL;
char l2data[L2DATALEN] = "";
int l2len = LIBNET_ETH_H;
int maxpacket = 0;

Expand All @@ -61,7 +61,7 @@ void version();
void mac2hex(const char *, char *, int);
void configfile(char *);
int argv_create(char *, int, char **);
int read_l2data(char *, char **);
int read_l2data(char *, char *);

int
main(int argc, char *argv[])
Expand Down Expand Up @@ -222,7 +222,7 @@ main(int argc, char *argv[])
break;
case '2': /* layer 2 header file */
l2enabled = 1;
l2len = read_l2data(optarg, &l2data);
l2len = read_l2data(optarg, l2data);
break;
default:
usage();
Expand Down Expand Up @@ -477,25 +477,39 @@ argv_create(char *p, int argc, char *argv[])
}

int
read_l2data(char *file, char **l2data)
read_l2data(char *l2string, char *l2data)
{
int fd, bytes;

*l2data = (char *)malloc(L2DATALEN);
bzero(*l2data, L2DATALEN);


if ((fd = open(file, O_RDONLY)) == -1)
errx(1, "Could not open layer 2 data file %s: %s", file, strerror(errno));

if ((bytes = read(fd, *l2data, L2DATALEN)) < 0)
errx(1, "Error reading layer 2 data file: %s", strerror(errno));
int numbytes = 0;
unsigned int value;
char *l2byte;
u_char databyte;

memset(l2data, '\0', L2DATALEN);

/* data is hex, comma seperated, byte by byte */

/* get the first byte */
l2byte = strtok(l2string, ",");
sscanf(l2byte, "%x", &value);
if (value > 0xff)
errx(1, "Invalid hex byte passed to -2: %s", l2byte);
databyte = (u_char)value;
memcpy(&l2data[numbytes], &databyte, 1);

/* get remaining bytes */
while ((l2byte = strtok(NULL, ",")) != NULL) {
numbytes ++;
sscanf(l2byte, "%x", &value);
if (value > 0xff)
errx(1, "Invalid hex byte passed to -2: %s", l2byte);
databyte = (u_char)value;
memcpy(&l2data[numbytes], &databyte, 1);
}

if (close(fd) == -1)
errx(1, "Error closing layer 2 data file: %s", strerror(errno));
numbytes ++;

dbg(1, "Read %d bytes of layer 2 data", bytes);
return bytes;
dbg(1, "Read %d bytes of layer 2 data", numbytes);
return(numbytes);
}


Expand Down Expand Up @@ -535,7 +549,7 @@ configfile(char *file)
#endif
}
else if (ARGS("l2datafile", 2)) {
l2len = read_l2data(argv[1], &l2data);
l2len = read_l2data(argv[1], l2data);
}
else if (ARGS("intf", 2)) {
intf = strdup(argv[1]);
Expand Down

0 comments on commit 3ce8ab3

Please sign in to comment.