Skip to content

Commit

Permalink
add support for writing packets to a file rather then the network.
Browse files Browse the repository at this point in the history
This allows merging of pcaps as well as all the fancy packet mods.
  • Loading branch information
synfinatic committed Jul 16, 2003
1 parent 35bf8af commit 8b213fe
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 23 deletions.
9 changes: 8 additions & 1 deletion Docs/CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
$Id: CHANGELOG,v 1.12 2003/06/07 18:20:37 aturner Exp $
$Id: CHANGELOG,v 1.13 2003/07/16 22:30:39 aturner Exp $

06/??/2003: Version 1.5.alpha4
- Split do_packets.c & do_packets() -> edit_packet.c & rewrite_l2()
- Don't die when packet > MTU, just skip
- Fix a ptr bug in do_packets() w/ the ethernet header
- Merge Ctrl-C fix from 1.4.4 for libnet_adv_write_link()
in do_packets.c
- Rewrite flowreplay design document
- Fix an integer overflow in packet_stats() in tcpreplay.c
- tcpreplay's -2 now accepts a hex string rather then a filename
- tcpreplay now can output to a file (-w <file>)

06/06/2003: Version 1.5.alpha3
- Add support for Linux Cooked Sockets (SLL) format rewriting
Expand Down
32 changes: 19 additions & 13 deletions do_packets.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $Id: do_packets.c,v 1.34 2003/06/16 21:01:40 aturner Exp $ */
/* $Id: do_packets.c,v 1.35 2003/07/16 22:30:39 aturner Exp $ */

/*
* Copyright (c) 2001, 2002, 2003 Aaron Turner, Matt Bing.
Expand Down Expand Up @@ -243,19 +243,25 @@ do_packets(pcap_t * pcap, u_int32_t linktype, int l2enabled, char *l2data, int l
do_sleep((struct timeval *)&pkthdr.ts, &last, pkthdr.caplen);

/* Physically send the packet */
do {
ret = libnet_adv_write_link(l, pktdata, pkthdr.caplen);
if (ret == -1) {
/* Make note of failed writes due to full buffers */
if (errno == ENOBUFS) {
failed++;
if (options.savepcap != NULL) {
/* write to a file */
pcap_dump((u_char *)options.savedumper, &pkthdr, pktdata);
} else {
/* write packet out on network */
do {
ret = libnet_adv_write_link(l, pktdata, pkthdr.caplen);
if (ret == -1) {
/* Make note of failed writes due to full buffers */
if (errno == ENOBUFS) {
failed++;
}
else {
errx(1, "libnet_adv_write_link(): %s", strerror(errno));
}
}
else {
errx(1, "libnet_adv_write_link(): %s", strerror(errno));
}
}
/* keep trying if fail, unless user Ctrl-C's */
} while (ret == -1 && !didsig);
/* keep trying if fail, unless user Ctrl-C's */
} while (ret == -1 && !didsig);
}

bytes_sent += pkthdr.caplen;
pkts_sent++;
Expand Down
9 changes: 8 additions & 1 deletion man/tcpreplay.8
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.\" $Id: tcpreplay.8,v 1.9 2003/06/17 18:27:01 aturner Exp $
.\" $Id: tcpreplay.8,v 1.10 2003/07/16 22:30:39 aturner Exp $
.TH TCPREPLAY 8
.SH NAME
tcpreplay \- replay packets from capture files
Expand All @@ -21,6 +21,9 @@ tcpreplay \- replay packets from capture files
] [
.B \-j
.I intf2
|
.B \-w
.I file
] [
.B \-J
.I intf mac
Expand Down Expand Up @@ -176,6 +179,10 @@ Verbose mode, currently unused.
.B \-V
Print version info and exit.
.TP
.B -w
Specify the output file to write all the packets to instead of the network.
You still must specify the primary interface via -i.
.TP
.B \-x or "include"
Specifies which packets from the capture file(s) to send. Can be one of:
.br
Expand Down
29 changes: 23 additions & 6 deletions tcpreplay.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $Id: tcpreplay.c,v 1.63 2003/06/17 18:52:56 aturner Exp $ */
/* $Id: tcpreplay.c,v 1.64 2003/07/16 22:30:39 aturner Exp $ */

/*
* Copyright (c) 2001, 2002, 2003 Aaron Turner, Matt Bing.
Expand Down Expand Up @@ -88,10 +88,10 @@ main(int argc, char *argv[])

#ifdef DEBUG
while ((ch =
getopt(argc, argv, "d:c:C:f:Fhi:I:j:J:l:m:Mp:Pr:Rs:t:u:Vvx:X:?2:")) != -1)
getopt(argc, argv, "d:c:C:f:Fhi:I:j:J:l:m:Mp:Pr:Rs:t:u:Vvw:x:X:?2:")) != -1)
#else
while ((ch =
getopt(argc, argv, "c:C:f:Fhi:I:j:J:l:m:Mp:Pr:Rs:t:u:Vvx:X:?2:")) != -1)
getopt(argc, argv, "c:C:f:Fhi:I:j:J:l:m:Mp:Pr:Rs:t:u:Vvw:x:X:?2:")) != -1)
#endif
switch (ch) {
case 'c': /* cache file */
Expand Down Expand Up @@ -177,6 +177,15 @@ main(int argc, char *argv[])
case 'v': /* verbose */
options.verbose++;
break;
case 'w': /* write packets to file */
if ((options.savepcap = pcap_open_dead(DLT_EN10MB, 0)) == NULL)
errx(1, "error setting output file linktype");

if ((options.savedumper = pcap_dump_open(options.savepcap, optarg)) == NULL)
errx(1, "pcap_dump_open() error: %s", pcap_geterr(options.savepcap));

warnx("saving packets in %s", optarg);
break;
case 'u': /* untruncate packet */
if (strcmp("pad", optarg) == 0) {
options.trunc = PAD_PACKET;
Expand Down Expand Up @@ -239,15 +248,18 @@ main(int argc, char *argv[])
if (!strcmp("-", argv[i]))
errx(1, "stdin must be the only file specified");

if (intf == NULL)
errx(1, "Must specify interface");
if (intf == NULL)
errx(1, "Must specify a primary interface");

if ((intf2 == NULL) && (cache_file != NULL))
errx(1, "Needs secondary interface with cache");

if ((intf2 != NULL) && (!options.cidr && (cache_file == NULL)))
errx(1, "Needs cache or cidr match with secondary interface");

if ((intf2 != NULL) && (options.savepcap != NULL))
errx(1, "You can't specify an output file and dual-interfaces");

if (options.seed != 0) {
srand(options.seed);
options.seed = random();
Expand Down Expand Up @@ -289,7 +301,8 @@ main(int argc, char *argv[])
errx(1, "Can't open %s: %s", intf2, ebuf);
}

warnx("sending on %s %s", intf, intf2 == NULL ? "" : intf2);
if (options.savepcap == NULL)
warnx("sending on %s %s", intf, intf2 == NULL ? "" : intf2);

/* init the signal handlers */
init_signal_handlers();
Expand Down Expand Up @@ -322,6 +335,9 @@ main(int argc, char *argv[])
if (bytes_sent > 0)
packet_stats();

if (options.savepcap != NULL)
pcap_dump_close(options.savedumper);

return 0;
}

Expand Down Expand Up @@ -704,6 +720,7 @@ usage()
"-u pad|trunc\t\tPad/Truncate packets which are larger than the snaplen\n"
"-v\t\t\tVerbose\n"
"-V\t\t\tVersion\n"
"-w <file>\t\tWrite packets to file\n"
"-x <match>\t\tOnly send the packets specified\n"
"-X <match>\t\tSend all the packets except those specified\n"
"-2 <datafile>\t\tLayer 2 data\n"
Expand Down
7 changes: 5 additions & 2 deletions tcpreplay.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $Id: tcpreplay.h,v 1.34 2003/06/05 16:48:16 aturner Exp $ */
/* $Id: tcpreplay.h,v 1.35 2003/07/16 22:30:39 aturner Exp $ */

/*
* Copyright (c) 2001, 2002, 2003 Aaron Turner.
Expand All @@ -12,6 +12,7 @@

#include "config.h"
#include <libnet.h>
#include <pcap.h>
#include <sys/time.h>

#include "timer.h"
Expand All @@ -37,7 +38,9 @@ typedef struct libnet_ethernet_hdr eth_hdr_t;
/* run-time options */
struct options {
LIBNET *intf1;
LIBNET *intf2;
LIBNET *intf2;
pcap_t *savepcap;
pcap_dumper_t *savedumper;
char intf1_mac[6];
char intf2_mac[6];
float rate;
Expand Down

0 comments on commit 8b213fe

Please sign in to comment.