Skip to content

Commit

Permalink
switch from using #define CACHE_* to enum tcpr_dir. I may of just int…
Browse files Browse the repository at this point in the history
…roduced

a "bug" by reversing which interface C2S/S2C packets go out.  However, if so
it's just a change in functionality since this is a rewrite.

Anyways, this work was really for making things clearer as part of the DLT
code rewrite.  refs appneta#82

Also this updates the copyright/URL for the man pages.  fixes appneta#93
  • Loading branch information
synfinatic committed Nov 6, 2006
1 parent 5d08ec9 commit cb33a05
Show file tree
Hide file tree
Showing 24 changed files with 135 additions and 87 deletions.
2 changes: 1 addition & 1 deletion src/bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ live_callback(struct live_data_t *livedata, struct pcap_pkthdr *pkthdr,


/* what is our cache mode? */
cache_mode = livedata->source == PCAP_INT1 ? CACHE_PRIMARY : CACHE_SECONDARY;
cache_mode = livedata->source == PCAP_INT1 ? TCPR_DIR_C2S : TCPR_DIR_S2C;

/* Rewrite any Layer 2 data and copy the data to our local buffer */
if ((newl2len = rewrite_l2(livedata->pcap, &pkthdr, pktdata, cache_mode))
Expand Down
25 changes: 13 additions & 12 deletions src/common/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,13 @@ new_cache(void)
* adds the cache data for a packet to the given cachedata
*/

int
add_cache(tcpr_cache_t ** cachedata, const int send, const int interface)
tcpr_dir_t
add_cache(tcpr_cache_t ** cachedata, const int send, const tcpr_dir_t interface)
{
tcpr_cache_t *lastcache = NULL;
u_char *byte = NULL;
u_int32_t bit, result;
u_int32_t bit;
tcpr_dir_t result = TCPR_DIR_ERROR;
COUNTER index;
#ifdef DEBUG
char bitstring[9] = EIGHT_ZEROS;
Expand Down Expand Up @@ -304,15 +305,15 @@ add_cache(tcpr_cache_t ** cachedata, const int send, const int interface)
dbgx(2, "set send bit: byte " COUNTER_SPEC " = 0x%x", index, *byte);

/* if true, set low order bit. else, do squat */
if (interface == CACHE_PRIMARY) {
if (interface == TCPR_DIR_C2S) {
*byte += (u_char)(1 << (bit - 1));

dbgx(2, "set interface bit: byte " COUNTER_SPEC " = 0x%x", index, *byte);
result = CACHE_PRIMARY;
result = TCPR_DIR_C2S;
}
else {
dbgx(2, "don't set interface bit: byte " COUNTER_SPEC " = 0x%x", index, *byte);
result = CACHE_SECONDARY;
result = TCPR_DIR_S2C;
}
dbgx(3, "Current cache byte: %c%c%c%c%c%c%c%c",

Expand All @@ -329,7 +330,7 @@ add_cache(tcpr_cache_t ** cachedata, const int send, const int interface)
}
else {
dbg(1, "not setting send bit");
result = CACHE_NOSEND;
result = TCPR_DIR_NOSEND;
}

return result;
Expand All @@ -339,7 +340,7 @@ add_cache(tcpr_cache_t ** cachedata, const int send, const int interface)
/*
* returns the action for a given packet based on the CACHE
*/
int
tcpr_dir_t
check_cache(char *cachedata, COUNTER packetid)
{
COUNTER index = 0;
Expand All @@ -356,19 +357,19 @@ check_cache(char *cachedata, COUNTER packetid)
cachedata[index], (cachedata[index] & (char)(1 << bit)));

if (!(cachedata[index] & (char)(1 << bit))) {
return CACHE_NOSEND;
return TCPR_DIR_NOSEND;
}

/* go back a bit to get the interface */
bit--;
if (cachedata[index] & (char)(1 << bit)) {
return CACHE_PRIMARY;
return TCPR_DIR_C2S;
}
else {
return CACHE_SECONDARY;
return TCPR_DIR_S2C;
}

return CACHE_ERROR;
return TCPR_DIR_ERROR;
}

/*
Expand Down
19 changes: 15 additions & 4 deletions src/common/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,27 @@ struct tcpr_cache_file_hdr_s {

typedef struct tcpr_cache_file_hdr_s tcpr_cache_file_hdr_t;

enum tcpr_dir_e {
TCPR_DIR_ERROR = -1,
TCPR_DIR_NOSEND = 0,
TCPR_DIR_C2S = 1, /* aka PRIMARY */
TCPR_DIR_S2C = 2 /* aka SECONDARY */
};
typedef enum tcpr_dir_e tcpr_dir_t;


COUNTER write_cache(tcpr_cache_t *, const int, COUNTER, char *);
int add_cache(tcpr_cache_t **, const int, const int);
tcpr_dir_t add_cache(tcpr_cache_t **, const int, const tcpr_dir_t);
COUNTER read_cache(char **, const char *, char **);
int check_cache(char *, COUNTER);
tcpr_dir_t check_cache(char *, COUNTER);

/* return values for check_cache */
/* return values for check_cache
#define CACHE_ERROR -1
#define CACHE_NOSEND 0 /* equal to NULL */
#define CACHE_NOSEND 0 // NULL
#define CACHE_PRIMARY 1
#define CACHE_SECONDARY 2
*/


/* macro to change a bitstring to 8 bits */
#define BIT_STR(x) x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]
Expand Down
22 changes: 11 additions & 11 deletions src/common/cidr.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,18 +376,18 @@ parse_cidr_map(tcpr_cidrmap_t **cidrmap, const char *optarg)

/*
* checks to see if the ip address is in the cidr
* returns CACHE_PRIMARY for true, CACHE_SECONDARY for false
* returns TCPR_DIR_C2S for true, TCPR_DIR_S2C for false
*/

int
tcpr_dir_t
ip_in_cidr(const tcpr_cidr_t * mycidr, const unsigned long ip)
{
unsigned long ipaddr = 0, network = 0, mask = 0;
int ret;

/* always return 1 if 0.0.0.0/0 */
if (mycidr->masklen == 0 && mycidr->network == 0)
return CACHE_PRIMARY;
return TCPR_DIR_C2S;

mask = ~0; /* turn on all the bits */

Expand All @@ -406,25 +406,25 @@ ip_in_cidr(const tcpr_cidr_t * mycidr, const unsigned long ip)
get_addr2name4(ip, RESOLVE),
get_addr2name4(htonl(network), RESOLVE), mycidr->masklen);

ret = CACHE_PRIMARY;
ret = TCPR_DIR_C2S;
}
else {

dbgx(1, "The ip %s is not inside of %s/%d",
get_addr2name4(ip, RESOLVE),
get_addr2name4(htonl(network), RESOLVE), mycidr->masklen);

ret = CACHE_SECONDARY;
ret = TCPR_DIR_S2C;
}
return ret;
}

/*
* iterates over cidrdata to find if a given ip matches
* returns CACHE_PRIMARY for true, CACHE_SECONDARY for false
* returns TCPR_DIR_C2S for true, TCPR_DIR_S2C for false
*/

int
tcpr_dir_t
check_ip_cidr(tcpr_cidr_t * cidrdata, const unsigned long ip)
{
tcpr_cidr_t *mycidr;
Expand All @@ -433,7 +433,7 @@ check_ip_cidr(tcpr_cidr_t * cidrdata, const unsigned long ip)
* this actually should happen occasionally, so don't put an assert here
*/
if (cidrdata == NULL) {
return CACHE_SECONDARY;
return TCPR_DIR_S2C;
}

mycidr = cidrdata;
Expand All @@ -442,9 +442,9 @@ check_ip_cidr(tcpr_cidr_t * cidrdata, const unsigned long ip)
while (1) {

/* if match, return 1 */
if (ip_in_cidr(mycidr, ip) == CACHE_PRIMARY) {
if (ip_in_cidr(mycidr, ip) == TCPR_DIR_C2S) {
dbgx(3, "Found %s in cidr", get_addr2name4(ip, RESOLVE));
return CACHE_PRIMARY;
return TCPR_DIR_C2S;
}
/* check for next record */
if (mycidr->next != NULL) {
Expand All @@ -457,7 +457,7 @@ check_ip_cidr(tcpr_cidr_t * cidrdata, const unsigned long ip)

/* if we get here, no match */
dbgx(3, "Didn't find %s in cidr", get_addr2name4(ip, RESOLVE));
return CACHE_SECONDARY;
return TCPR_DIR_S2C;
}


Expand Down
6 changes: 4 additions & 2 deletions src/common/cidr.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "cache.h"

#ifndef __CIDR_H__
#define __CIDR_H__

Expand All @@ -48,8 +50,8 @@ struct tcpr_cidrmap_s {
};
typedef struct tcpr_cidrmap_s tcpr_cidrmap_t;

int ip_in_cidr(const tcpr_cidr_t *, const unsigned long);
int check_ip_cidr(tcpr_cidr_t *, const unsigned long);
tcpr_dir_t ip_in_cidr(const tcpr_cidr_t *, const unsigned long);
tcpr_dir_t check_ip_cidr(tcpr_cidr_t *, const unsigned long);
int parse_cidr(tcpr_cidr_t **, char *, char *delim);
int parse_cidr_map(tcpr_cidrmap_t **, const char *);
int parse_endpoints(tcpr_cidrmap_t **, tcpr_cidrmap_t **, const char *);
Expand Down
11 changes: 5 additions & 6 deletions src/common/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,8 @@ parse_list(tcpr_list_t ** listdata, char *ourstr)

/*
* Checks to see if the given integer exists in the LIST.
* Returns 1 for true, 0 for false
*/
int
tcpr_dir_t
check_list(tcpr_list_t * list, COUNTER value)
{
tcpr_list_t *current;
Expand All @@ -166,15 +165,15 @@ check_list(tcpr_list_t * list, COUNTER value)
do {
if ((current->min != 0) && (current->max != 0)) {
if ((value >= current->min) && (value <= current->max))
return CACHE_PRIMARY;
return TCPR_DIR_C2S;
}
else if (current->min == 0) {
if (value <= current->max)
return CACHE_PRIMARY;
return TCPR_DIR_C2S;
}
else if (current->max == 0) {
if (value >= current->min)
return CACHE_PRIMARY;
return TCPR_DIR_C2S;
}

if (current->next != NULL) {
Expand All @@ -186,7 +185,7 @@ check_list(tcpr_list_t * list, COUNTER value)

} while (current != NULL);

return CACHE_SECONDARY;
return TCPR_DIR_S2C;

}

Expand Down
10 changes: 8 additions & 2 deletions src/flowreplay_opts.def
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ autogen definitions options;


copyright = {
date = "2004-2005";
date = "2004-2006";
owner = "Aaron Turner";
type = "bsd";
author = <<- EOText
Copyright 2000-2005 Aaron Turner
Copyright 2000-2006 Aaron Turner

For support please use the [email protected] mailing list.

The latest version of this software is always available from:
http://tcpreplay.synfin.net/
EOText;
};

Expand Down Expand Up @@ -44,6 +47,9 @@ Please note that flowreplay is currently in *alpha*. As such, it is still
very much a work in progress and currently will not work for most uses. If
you have the skill and interest to help make flowreplay work better, please
contact Aaron Turner.

For more details, please see the Tcpreplay Manual at:
http://tcpreplay.synfin.net/wiki/manual
EODetail;

flag = {
Expand Down
14 changes: 7 additions & 7 deletions src/mac.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ dualmac2hex(const char *dualmac, u_char *first, u_char *second, int len)
/*
* Figures out if a MAC is listed in a comma delimited
* string of MAC addresses.
* returns CACHE_PRIMARY if listed
* returns CACHE_SECONDARY if not listed
* returns TCPR_DIR_C2S if listed
* returns TCPR_DIR_S2C if not listed
*/
int
tcpr_dir_t
macinstring(const char *macstring, const u_char *mac)
{
char *tok, *tempstr, *ourstring;
u_char tempmac[6];
int len = 6, ret = CACHE_SECONDARY;
int len = 6, ret = TCPR_DIR_S2C;

ourstring = safe_strdup(macstring);

Expand All @@ -131,7 +131,7 @@ macinstring(const char *macstring, const u_char *mac)
mac2hex(tempstr, tempmac, len);
if (memcmp(mac, tempmac, len) == 0) {
dbgx(3, "Packet matches: " MAC_FORMAT " sending out primary.\n", MAC_STR(tempmac));
ret = CACHE_PRIMARY;
ret = TCPR_DIR_C2S;
goto EXIT_MACINSTRING;
}
} else {
Expand All @@ -141,7 +141,7 @@ macinstring(const char *macstring, const u_char *mac)
while ((tempstr = strtok_r(NULL, ",", &tok)) != NULL) {
mac2hex(tempstr, tempmac, len);
if (memcmp(mac, tempmac, len) == 0) {
ret = CACHE_PRIMARY;
ret = TCPR_DIR_C2S;
dbgx(3, "Packet matches: " MAC_FORMAT " sending out primary.\n", MAC_STR(tempmac));
goto EXIT_MACINSTRING;
}
Expand All @@ -150,7 +150,7 @@ macinstring(const char *macstring, const u_char *mac)
EXIT_MACINSTRING:
free(ourstring);
#ifdef DEBUG
if (ret == CACHE_SECONDARY)
if (ret == TCPR_DIR_S2C)
dbg(3, "Packet doesn't match any MAC addresses sending out secondary.\n");
#endif
return ret;
Expand Down
2 changes: 1 addition & 1 deletion src/mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

void mac2hex(const char *mac, u_char *dst, int len);
int dualmac2hex(const char *dualmac, u_char *first, u_char *second, int len);
int macinstring(const char *macstring, const u_char *mac);
tcpr_dir_t macinstring(const char *macstring, const u_char *mac);

#endif /* __MAC_H__ */

Expand Down
10 changes: 5 additions & 5 deletions src/send_packets.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ send_packets(pcap_t *pcap)
sp = (sendpacket_t *) cache_mode(options.cachedata, packetnum);

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

Expand Down Expand Up @@ -170,15 +170,15 @@ cache_mode(char *cachedata, COUNTER packet_num)
err(1, "Exceeded number of packets in cache file.");

result = check_cache(cachedata, packet_num);
if (result == CACHE_NOSEND) {
if (result == TCPR_DIR_NOSEND) {
dbgx(2, "Cache: Not sending packet " COUNTER_SPEC ".", packet_num);
return CACHE_NOSEND;
return TCPR_DIR_NOSEND;
}
else if (result == CACHE_PRIMARY) {
else if (result == TCPR_DIR_C2S) {
dbgx(2, "Cache: Sending packet " COUNTER_SPEC " out primary interface.", packet_num);
sp = options.intf1;
}
else if (result == CACHE_SECONDARY) {
else if (result == TCPR_DIR_S2C) {
dbgx(2, "Cache: Sending packet " COUNTER_SPEC " out secondary interface.", packet_num);
sp = options.intf2;
}
Expand Down
8 changes: 7 additions & 1 deletion src/tcpbridge_opts.def
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ copyright = {
owner = "Aaron Turner";
type = "bsd";
author = <<- EOText
Copyright 2000-2005 Aaron Turner
Copyright 2000-2006 Aaron Turner

For support please use the [email protected] mailing list.

The latest version of this software is always available from:
http://tcpreplay.synfin.net/
EOText;
};

Expand Down Expand Up @@ -50,6 +53,9 @@ The basic operation of tcpbridge is to be a network bridge between two
subnets. All packets received on one interface are sent via the other.

Optionally, packets can be edited in a variety of ways according to your needs.

For more details, please see the Tcpreplay Manual at:
http://tcpreplay.synfin.net/wiki/manual
EODetail;

man-doc = <<- EOMan
Expand Down
Loading

0 comments on commit cb33a05

Please sign in to comment.