misc: Fix building the m5tap utility.

Use printf instead of cprintf to avoid having to have .o files from gem5.
Stop disabling optimization. Placate the style checker by rearranging the
header include order. Include some missing standard headers. Switch from
the deprecated -I- gcc option to using -iquote. Make the "program"
variable a const char *. When checking the return value of getopt, don't
check against the char c which may mask the -1 return value. Instead,
store the return value in an int, and then later cast it to a char when
it's actually consumed.

Change-Id: Ibec51927d0cdbd98db8e53081be2c4931e20333c
Reviewed-on: https://gem5-review.googlesource.com/3600
Reviewed-by: Nathan Binkert <nate@binkert.org>
Maintainer: Nathan Binkert <nate@binkert.org>
This commit is contained in:
Gabe Black
2017-06-01 16:53:36 -07:00
parent 201e86fbeb
commit 0010088c43
2 changed files with 32 additions and 42 deletions

View File

@@ -29,20 +29,13 @@
CC= gcc CC= gcc
CXX= g++ CXX= g++
CURDIR?= $(shell /bin/pwd) INCLDIRS= -iquote . -I /usr/local/include
SRCDIR?= . CCFLAGS= -g -MMD $(INCLDIRS)
M5_SRCDIR?= $(SRCDIR)/../..
vpath % $(M5_SRCDIR)/base
vpath % $(M5_SRCDIR)/sim
INCLDIRS= -I. -I$(M5_SRCDIR) -I- -I/usr/local/include
CCFLAGS= -g -O0 -MMD $(INCLDIRS)
default: m5tap default: m5tap
m5tap: tap.o cprintf.o m5tap: tap.o
$(CXX) $(LFLAGS) -o $@ $^ -lpcap -L/usr/local/lib -ldnet $(CXX) $(LFLAGS) -o $@ $^ -lpcap
install: m5tap install: m5tap
$(SUDO) install -o root -m 555 m5tap /usr/local/bin $(SUDO) install -o root -m 555 m5tap /usr/local/bin

View File

@@ -30,36 +30,37 @@
extern "C" { extern "C" {
#include <pcap.h> #include <pcap.h>
} }
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <dnet.h>
#include <fcntl.h> #include <fcntl.h>
#include <libgen.h> #include <libgen.h>
#include <netdb.h> #include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <poll.h> #include <poll.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <cerrno> #include <cerrno>
#include <csignal> #include <csignal>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <list> #include <list>
#include <string> #include <string>
#include "base/cprintf.hh"
#define panic(arg...) \ #define panic(arg...) \
do { cprintf("Panic: " arg); exit(1); } while (0) do { printf("Panic: " arg); exit(1); } while (0)
char *program = "ethertap"; const char *program = "ethertap";
void void
usage() usage()
{ {
cprintf( printf(
"usage: \n" "usage: \n"
"\t%s [-b bufsize] [-d] [-f filter] [-p port] [-v] <device> <host>\n" "\t%s [-b bufsize] [-d] [-f filter] [-p port] [-v] <device> <host>\n"
"\t%s [-b bufsize] [-d] [-f filter] [-l] [-p port] [-v] <device>\n", "\t%s [-b bufsize] [-d] [-f filter] [-l] [-p port] [-v] <device>\n",
@@ -70,7 +71,7 @@ usage()
int verbose = 0; int verbose = 0;
#define DPRINTF(args...) do { \ #define DPRINTF(args...) do { \
if (verbose >= 1) \ if (verbose >= 1) \
cprintf(args); \ printf(args); \
} while (0) } while (0)
#define DDUMP(args...) do { \ #define DDUMP(args...) do { \
@@ -84,26 +85,26 @@ dump(const u_char *data, int len)
int c, i, j; int c, i, j;
for (i = 0; i < len; i += 16) { for (i = 0; i < len; i += 16) {
cprintf("%08x ", i); printf("%08x ", i);
c = len - i; c = len - i;
if (c > 16) c = 16; if (c > 16) c = 16;
for (j = 0; j < c; j++) { for (j = 0; j < c; j++) {
cprintf("%02x ", data[i + j] & 0xff); printf("%02x ", data[i + j] & 0xff);
if ((j & 0xf) == 7 && j > 0) if ((j & 0xf) == 7 && j > 0)
cprintf(" "); printf(" ");
} }
for (; j < 16; j++) for (; j < 16; j++)
cprintf(" "); printf(" ");
cprintf(" "); printf(" ");
for (j = 0; j < c; j++) { for (j = 0; j < c; j++) {
int ch = data[i + j] & 0x7f; int ch = data[i + j] & 0x7f;
cprintf("%c", (char)(isprint(ch) ? ch : ' ')); printf("%c", (char)(isprint(ch) ? ch : ' '));
} }
cprintf("\n"); printf("\n");
if (c < 16) if (c < 16)
break; break;
@@ -178,7 +179,7 @@ Connect(int fd, const std::string &host, int port)
struct hostent *hp; struct hostent *hp;
hp = ::gethostbyname(host.c_str()); hp = ::gethostbyname(host.c_str());
if (!hp) if (!hp)
panic("Host %s not found\n", host); panic("Host %s not found\n", host.c_str());
sockaddr.sin_family = hp->h_addrtype; sockaddr.sin_family = hp->h_addrtype;
memcpy(&sockaddr.sin_addr, hp->h_addr, hp->h_length); memcpy(&sockaddr.sin_addr, hp->h_addr, hp->h_length);
@@ -186,9 +187,9 @@ Connect(int fd, const std::string &host, int port)
sockaddr.sin_port = htons(port); sockaddr.sin_port = htons(port);
if (::connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) != 0) if (::connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) != 0)
panic("could not connect to %s on port %d\n", host, port); panic("could not connect to %s on port %d\n", host.c_str(), port);
DPRINTF("connected to %s on port %d\n", host, port); DPRINTF("connected to %s on port %d\n", host.c_str(), port);
} }
class Ethernet class Ethernet
@@ -221,7 +222,6 @@ class PCap : public Ethernet
{ {
private: private:
pcap_t *pcap; pcap_t *pcap;
eth_t *ethernet;
public: public:
PCap(char *device, char *filter = NULL); PCap(char *device, char *filter = NULL);
@@ -253,17 +253,12 @@ PCap::PCap(char *device, char *filter)
panic("pcap_setfilter failed\n"); panic("pcap_setfilter failed\n");
} }
ethernet = eth_open(device);
if (!ethernet)
panic("cannot open the ethernet device for writing\n");
fd = pcap_fileno(pcap); fd = pcap_fileno(pcap);
} }
PCap::~PCap() PCap::~PCap()
{ {
pcap_close(pcap); pcap_close(pcap);
eth_close(ethernet);
} }
bool bool
@@ -281,7 +276,7 @@ PCap::read(const char *&data, int &len)
bool bool
PCap::write(const char *data, int len) PCap::write(const char *data, int len)
{ {
eth_send(ethernet, data, len); return pcap_inject(pcap, data, len) == len;
} }
Tap::Tap(char *device) Tap::Tap(char *device)
@@ -335,7 +330,9 @@ main(int argc, char *argv[])
program = basename(argv[0]); program = basename(argv[0]);
while ((c = getopt(argc, argv, "b:df:lp:tv")) != -1) { int ret;
while ((ret = getopt(argc, argv, "b:df:lp:tv")) != -1) {
char c = ret;
switch (c) { switch (c) {
case 'b': case 'b':
bufsize = atoi(optarg); bufsize = atoi(optarg);
@@ -435,7 +432,7 @@ main(int argc, char *argv[])
DPRINTF("Begin poll loop\n"); DPRINTF("Begin poll loop\n");
while (!quit) { while (!quit) {
int ret = ::poll(pfds, npfds, INFTIM); int ret = ::poll(pfds, npfds, -1);
if (ret < 0) if (ret < 0)
continue; continue;