Skip to content

Commit

Permalink
fix over optimization bug introduced in last checkin.
Browse files Browse the repository at this point in the history
must set ioctl() before every write()
refs appneta#505
  • Loading branch information
synfinatic committed Apr 24, 2012
1 parent 2bef172 commit cd0ab87
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions src/common/sendpacket.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ static struct tcpr_ether_addr *sendpacket_get_hwaddr_pcap(sendpacket_t *) _U_;
#endif

static void sendpacket_seterr(sendpacket_t *sp, const char *fmt, ...);
static sendpacket_t * sendpacket_open_khial(const char *, char *, tcpr_dir_t) _U_;
static sendpacket_t * sendpacket_open_khial(const char *, char *) _U_;
static struct tcpr_ether_addr * sendpacket_get_hwaddr_khial(sendpacket_t *) _U_;

/* You need to define didsig in your main .c file. Set to 1 if CTRL-C was pressed */
Expand All @@ -246,7 +246,7 @@ extern volatile int didsig;
int
sendpacket(sendpacket_t *sp, const u_char *data, size_t len, struct pcap_pkthdr *pkthdr)
{
int retcode;
int retcode, val;
u_char buffer[10000]; /* 10K bytes, enough for jumbo frames + pkthdr */

assert(sp);
Expand All @@ -265,6 +265,23 @@ sendpacket(sendpacket_t *sp, const u_char *data, size_t len, struct pcap_pkthdr
memcpy(buffer, pkthdr, sizeof(struct pcap_pkthdr));
memcpy(buffer + sizeof(struct pcap_pkthdr), data, len);

/* tell the kernel module which direction the traffic is going */
if (sp->cache_dir == TCPR_DIR_C2S) { /* aka PRIMARY */
val = KHIAL_DIRECTION_RX;
if (ioctl(sp->handle.fd, KHIAL_SET_DIRECTION, (void *)&val) < 0) {
sendpacket_seterr(sp, "Error setting direction on %s: %s (%d)",
sp->device, strerror(errno), errno);
return -1;
}
} else if (sp->cache_dir == TCPR_DIR_S2C) {
val = KHIAL_DIRECTION_TX;
if (ioctl(sp->handle.fd, KHIAL_SET_DIRECTION, (void *)&val) < 0) {
sendpacket_seterr(sp, "Error setting direction on %s: %s (%d)",
sp->device, strerror(errno), errno);
return -1;
}
}

/* write the pkthdr + packet data all at once */
retcode = write(sp->handle.fd, (void *)buffer, sizeof(struct pcap_pkthdr) + len);
retcode -= sizeof(struct pcap_pkthdr); /* only record packet bytes we sent, not pcap data too */
Expand Down Expand Up @@ -452,7 +469,7 @@ sendpacket_open(const char *device, char *errbuf, tcpr_dir_t direction)
if (stat(device, &sdata) == 0) {
if (((sdata.st_mode & S_IFMT) == S_IFCHR)) {

sp = sendpacket_open_khial(device, errbuf, direction);
sp = sendpacket_open_khial(device, errbuf);
} else {
err(1, "%s is not a valid Tcpreplay character device");
}
Expand Down Expand Up @@ -1112,11 +1129,10 @@ sendpacket_get_method(sendpacket_t *sp)
* your kernel via a custom driver
*/
static sendpacket_t *
sendpacket_open_khial(const char *device, char *errbuf, tcpr_dir_t direction)
sendpacket_open_khial(const char *device, char *errbuf)
{
int mysocket;
sendpacket_t *sp;
int val;

assert(device);
assert(errbuf);
Expand All @@ -1131,24 +1147,6 @@ sendpacket_open_khial(const char *device, char *errbuf, tcpr_dir_t direction)
sp->handle.fd = mysocket;
sp->handle_type = SP_TYPE_KHIAL;

/* tell the kernel module which direction the traffic is going */
if (direction == TCPR_DIR_C2S) { /* aka PRIMARY */
val = KHIAL_DIRECTION_RX;
if (ioctl(sp->handle.fd, KHIAL_SET_DIRECTION, (void *)&val) < 0) {
sendpacket_seterr(sp, "Error setting direction on %s: %s (%d)",
sp->device, strerror(errno), errno);
return -1;
}
} else if (direction == TCPR_DIR_S2C) {
val = KHIAL_DIRECTION_TX;
if (ioctl(sp->handle.fd, KHIAL_SET_DIRECTION, (void *)&val) < 0) {
sendpacket_seterr(sp, "Error setting direction on %s: %s (%d)",
sp->device, strerror(errno), errno);
return -1;
}
} else {
errx(1, "Invalid direction passed to sendpacket_open_khial(): %d", (int)direction);
}

return sp;
}
Expand Down

0 comments on commit cd0ab87

Please sign in to comment.