Skip to content

Commit

Permalink
work on removing libnet requirement, refs appneta#29
Browse files Browse the repository at this point in the history
  • Loading branch information
synfinatic committed Jul 29, 2006
1 parent 7357845 commit 5b089b5
Show file tree
Hide file tree
Showing 24 changed files with 141 additions and 98 deletions.
2 changes: 1 addition & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ tcpbridge_opts.c: tcpbridge_opts.def
@AUTOGEN@ tcpbridge_opts.def

noinst_HEADERS = tcpreplay.h tcpprep.h flowreplay.h bridge.h \
defines.h dlt_names.h tree.h mac.h \
defines.h tree.h mac.h \
send_packets.h signal_handler.h common.h tcpreplay_opts.h \
tcprewrite.h tcprewrite_opts.h tcpprep_opts.h \
tcpprep_opts.def tcprewrite_opts.def tcpreplay_opts.def \
Expand Down
29 changes: 11 additions & 18 deletions src/bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#include <string.h>
#include <netinet/in.h>
#include <time.h>
#include <errno.h>
#include <stdlib.h>

#include "tcpbridge.h"
#include "bridge.h"
Expand Down Expand Up @@ -193,13 +195,13 @@ live_callback(struct live_data_t *livedata, struct pcap_pkthdr *pkthdr,
const u_char * nextpkt)
{
ipv4_hdr_t *ip_hdr = NULL;
libnet_t *l = NULL;
sendpacket_t *sp = NULL;
static u_char *pktdata = NULL; /* full packet buffer */
#ifdef FORCE_ALIGN
u_char *ipbuff = NULL; /* IP header and above buffer */
#endif
static int first_time = 1;
int ret, newl2len, cache_mode;
int newl2len, cache_mode;
static unsigned long packetnum = 0;
struct macsrc_t *node, finder; /* rb tree nodes */
#ifdef DEBUG
Expand Down Expand Up @@ -243,12 +245,12 @@ live_callback(struct live_data_t *livedata, struct pcap_pkthdr *pkthdr,
#endif

/* first, is this a packet sent locally? If so, ignore it */
if ((memcmp(libnet_get_hwaddr(options.send1), &finder.key,
if ((memcmp(sendpacket_get_hwaddr(options.sp1), &finder.key,
ETHER_ADDR_LEN)) == 0) {
dbgx(1, "Packet matches the MAC of %s, skipping.", options.intf1);
return (1);
}
else if ((memcmp(libnet_get_hwaddr(options.send2), &finder.key,
else if ((memcmp(sendpacket_get_hwaddr(options.sp2), &finder.key,
ETHER_ADDR_LEN)) == 0) {
dbgx(1, "Packet matches the MAC of %s, skipping.", options.intf2);
return (1);
Expand Down Expand Up @@ -292,12 +294,12 @@ live_callback(struct live_data_t *livedata, struct pcap_pkthdr *pkthdr,
if (node->source == PCAP_INT1) {
dbgx(2, "Packet source was %s... sending out on %s", options.intf1,
options.intf2);
l = options.send2;
sp = options.sp2;
}
else if (node->source == PCAP_INT2) {
dbgx(2, "Packet source was %s... sending out on %s", options.intf2,
options.intf1);
l = options.send1;
sp = options.sp1;
} else {
errx(1, "wtf? our node->source != PCAP_INT1 and != PCAP_INT2: %c",
node->source);
Expand Down Expand Up @@ -350,18 +352,9 @@ live_callback(struct live_data_t *livedata, struct pcap_pkthdr *pkthdr,
/*
* write packet out on the 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));
}
}
} while (ret == -1 && !didsig);
if (sendpacket(sp, pktdata, pkthdr->caplen) < pkthdr->caplen) {
errx(1, "Unable to send packet out %s: %s", sp->device, sendpacket_geterr(sp));
}

bytes_sent += pkthdr->caplen;
pkts_sent++;
Expand Down
2 changes: 1 addition & 1 deletion src/bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct macsrc_t {
RB_ENTRY(macsrc_t) node;
u_char key[ETHER_ADDR_LEN];
u_char source; /* interface device name we first saw the source MAC */
libnet_t *libnet; /* libnet handle to send packets out */
sendpacket_t *sp; /* sendpacket handle to send packets out */
};

/* pri and secondary pcap interfaces */
Expand Down
1 change: 1 addition & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "common/tcpdump.h"
#include "common/timer.h"
#include "common/abort.h"
#include "common/sendpacket.h"

const char *svn_version(void); /* svn_version.c */

Expand Down
11 changes: 10 additions & 1 deletion src/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
/* Define to 1 if you have the <arpa/inet.h> header file. */
#undef HAVE_ARPA_INET_H

/* Do we have BPF device support? */
#undef HAVE_BPF

/* Define to 1 if you have the `canonicalize_file_name' function. */
#undef HAVE_CANONICALIZE_FILE_NAME

Expand Down Expand Up @@ -103,6 +106,9 @@
/* Define to 1 if you have the <libgen.h> header file. */
#undef HAVE_LIBGEN_H

/* Enable libnet support */
#undef HAVE_LIBNET

/* Define to 1 if you have the `nsl' library (-lnsl). */
#undef HAVE_LIBNSL

Expand Down Expand Up @@ -149,7 +155,7 @@
/* Does libpcap have pcap_sendpacket? */
#undef HAVE_PCAP_SENDPACKET

/* Do we have Linux PF_PACKET? */
/* Do we have Linux PF_PACKET socket support? */
#undef HAVE_PF_PACKET

/* Define to 1 if you have the `poll' function. */
Expand Down Expand Up @@ -289,6 +295,9 @@
/* Define to 1 if you have the `vprintf' function. */
#undef HAVE_VPRINTF

/* Define to 1 if you have the `vsnprintf' function. */
#undef HAVE_VSNPRINTF

/* What is the path (if any) to the pcap-bpf.h header? */
#undef INCLUDE_PCAP_BPF_HEADER

Expand Down
41 changes: 27 additions & 14 deletions src/defines.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,35 @@
#define COUNTER_SPEC "%lu"
#endif

#include "lib/strlcpy.h"
#include "common/list.h"
#include "common/cidr.h"

#include "@LNETINC@"
#ifdef INCLUDE_PCAP_BPF_H_FILE
#ifdef HAVE_BPF
#include <net/bpf.h>
#define PCAP_DONT_INCLUDE_PCAP_BPF_H 1
#endif

#if defined INCLUDE_PCAP_BPF_H_FILE && !defined PCAP_DONT_INCLUDE_PCAP_BPF_H
#include "@PCAP_BPF_H_FILE@"
#define PCAP_DONT_INCLUDE_PCAP_BPF_H /* don't re-include it in pcap.h */
#define PCAP_DONT_INCLUDE_PCAP_BPF_H 1 /* don't re-include it in pcap.h */
#endif

#include "@LPCAPINC@"
#ifdef HAVE_LIBNIDS
#include "@LNIDSINC@"
#endif

/* The release version of libnet 1.1.1 changed DNS */
#ifdef TCPR_DNSV4_H
#define TCPR_DNS_H TCPR_DNSV4_H
#else
#define TCPR_DNS_H TCPR_UDP_DNSV4_H
#include "lib/strlcpy.h"
#include "common/list.h"
#include "common/cidr.h"

/*
* net/bpf.h doesn't include DLT types, but pcap-bpf.h does.
* Unfortunately, pcap-bpf.h also includes things in net/bpf.h
* while also missing some key things (wow, that sucks)
* The result is that I stole the DLT types from pcap-bpf.h and
* put them in here.
*/
#include "common/dlt_names.h"


#ifdef HAVE_LIBNET
#include "@LNETINC@"
#endif

typedef struct tcpr_ipv4_hdr ipv4_hdr_t;
Expand Down Expand Up @@ -98,6 +108,9 @@ typedef struct tcpr_speed_s tcpr_speed_t;

#define MAX_SNAPLEN 65535 /* tell libpcap to capture the entire packet */

#define DNS_RESOLVE 1
#define DNS_DONT_RESOLVE 0

#define RESOLVE 0 /* disable dns lookups */
#define BPF_OPTIMIZE 1 /* default is to optimize bpf program */
#define PCAP_TIMEOUT 100 /* 100ms pcap_open_live timeout */
Expand Down
1 change: 0 additions & 1 deletion src/flowreplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ struct flowreplay_opt_s {
struct in_addr targetaddr; /* target host */
tcpr_cidr_t *clients;
tcpr_cidr_t *servers;
libnet_t *l;
struct timeval timeout;
u_int32_t pernodebufflim;
u_int32_t totalbufflim;
Expand Down
5 changes: 5 additions & 0 deletions src/flowreplay_opts.def
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ config-header = "config.h";
include = "#include \"defines.h\"\n"
"#include \"flowreplay.h\"\n"
"#include \"common.h\"\n"
"#include \"config.h\"\n"
"extern char pcap_version[];\n"
"extern flowreplay_opt_t options;\n";

Expand Down Expand Up @@ -262,7 +263,11 @@ flag = {
#endif
fprintf(stderr, "\n");
fprintf(stderr, "Copyright 2001-2006 by Aaron Turner <[email protected]>\n");
#ifdef HAVE_LIBNET
fprintf(stderr, "Compiled against libnet: %s\n", LIBNET_VERSION);
#else
fprintf(stderr, "Not compiled with libnet.\n");
#endif
fprintf(stderr, "Compiled against libpcap: %s\n", pcap_version);
#ifdef ENABLE_64BITS
fprintf(stderr, "64 bit packet counters: enabled\n");
Expand Down
4 changes: 4 additions & 0 deletions src/mac.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
#include "config.h"
#include "defines.h"
#include "common.h"
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <ctype.h>

#include "mac.h"

Expand Down
45 changes: 17 additions & 28 deletions src/send_packets.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
#include <signal.h>
#include <string.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>

#include "tcpreplay.h"

Expand All @@ -62,7 +65,7 @@ extern tcpdump_t tcpdump;
extern int debug;
#endif

static void do_sleep(struct timeval *time, struct timeval *last, int len, libnet_t *l);
static void do_sleep(struct timeval *time, struct timeval *last, int len, sendpacket_t *sp);


/*
Expand All @@ -76,7 +79,7 @@ send_packets(pcap_t *pcap)
COUNTER packetnum = 0;
struct pcap_pkthdr pkthdr;
const u_char *pktdata = NULL;
libnet_t *l = options.intf1;
sendpacket_t *sp = options.intf1;
int ret; /* libnet return code */
u_int32_t pktlen;

Expand Down Expand Up @@ -117,10 +120,10 @@ send_packets(pcap_t *pcap)
/* Dual nic processing */
if (options.intf2 != NULL) {

l = (libnet_t *) cache_mode(options.cachedata, packetnum);
sp = (sendpacket_t *) cache_mode(options.cachedata, packetnum);

/* sometimes we should not send the packet */
if (l == CACHE_NOSEND)
if (sp == CACHE_NOSEND)
continue;
}

Expand All @@ -134,25 +137,11 @@ send_packets(pcap_t *pcap)
* we have to cast the ts, since OpenBSD sucks
* had to be special and use bpf_timeval
*/
do_sleep((struct timeval *)&pkthdr.ts, &last, pktlen, l);
do_sleep((struct timeval *)&pkthdr.ts, &last, pktlen, sp);

/* write packet out on network */
do {
ret = libnet_adv_write_link(l, pktdata, pktlen);
if (ret == -1) {
/* Make note of failed writes due to full buffers */
if (errno == ENOBUFS) {
failed++;
} else {
errx(1, "Unable to send packet: %s", strerror(errno));
}
}

/* keep trying if fail, unless user Ctrl-C's */
} while (ret == -1 && !didsig);

bytes_sent += pktlen;
pkts_sent++;
if (sendpacket(sp, pktdata, pktlen) < pktlen)
errx(1, "Unable to send packet: %s", sendpacket_geterr(sp));

/*
* track the time of the "last packet sent". Again, because of OpenBSD
Expand All @@ -174,7 +163,7 @@ send_packets(pcap_t *pcap)
void *
cache_mode(char *cachedata, COUNTER packet_num)
{
void *l = NULL;
void *sp = NULL;
int result;

if (packet_num > options.cache_packets)
Expand All @@ -187,17 +176,17 @@ cache_mode(char *cachedata, COUNTER packet_num)
}
else if (result == CACHE_PRIMARY) {
dbgx(2, "Cache: Sending packet " COUNTER_SPEC " out primary interface.", packet_num);
l = options.intf1;
sp = options.intf1;
}
else if (result == CACHE_SECONDARY) {
dbgx(2, "Cache: Sending packet " COUNTER_SPEC " out secondary interface.", packet_num);
l = options.intf2;
sp = options.intf2;
}
else {
err(1, "check_cache() returned an error. Aborting...");
}

return l;
return sp;
}


Expand All @@ -206,7 +195,7 @@ cache_mode(char *cachedata, COUNTER packet_num)
* calculate the appropriate amount of time to sleep and do so.
*/
static void
do_sleep(struct timeval *time, struct timeval *last, int len, libnet_t *l)
do_sleep(struct timeval *time, struct timeval *last, int len, sendpacket_t *sp)
{
static struct timeval didsleep = { 0, 0 };
static struct timeval start = { 0, 0 };
Expand Down Expand Up @@ -287,7 +276,7 @@ do_sleep(struct timeval *time, struct timeval *last, int len, libnet_t *l)
/* do we skip prompting for a key press? */
if (send == 0) {
printf("**** How many packets do you wish to send? (next packet out %s): ",
l == options.intf1 ? options.intf1_name : options.intf2_name);
sp == options.intf1 ? options.intf1_name : options.intf2_name);
fflush(NULL);
poller[0].fd = STDIN_FILENO;
poller[0].events = POLLIN;
Expand Down Expand Up @@ -320,7 +309,7 @@ do_sleep(struct timeval *time, struct timeval *last, int len, libnet_t *l)

/* decrement our send counter */
printf("Sending packet out: %s\n",
l == options.intf1 ? options.intf1_name : options.intf2_name);
sp == options.intf1 ? options.intf1_name : options.intf2_name);
send --;

/* leave do_sleep() */
Expand Down
1 change: 1 addition & 0 deletions src/signal_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include "tcpreplay.h"
#include "signal_handler.h"
Expand Down
Loading

0 comments on commit 5b089b5

Please sign in to comment.