CURDIR?= $(shell /bin/pwd)
SRCDIR?= .
+M5_SRCDIR?= $(SRCDIR)/../..
-BASE_SRCDIR?= $(SRCDIR)/../../base
-SIM_SRCDIR?= $(SRCDIR)/../../sim
+vpath % $(M5_SRCDIR)/base
+vpath % $(M5_SRCDIR)/sim
-vpath % $(BASE_SRCDIR)
-vpath % $(SIM_SRCDIR)
-
-INCLDIRS= -I. -I$(BASE_SRCDIR) -I$(SIM_SRCDIR) -I- -I/usr/local/include
+INCLDIRS= -I. -I$(M5_SRCDIR) -I- -I/usr/local/include
CCFLAGS= -g -O0 -MMD $(INCLDIRS)
default: m5tap
m5tap: tap.o cprintf.o
$(CXX) $(LFLAGS) -o $@ $^ -lpcap -L/usr/local/lib -ldnet
+install: m5tap
+ $(SUDO) install -o root -m 555 m5tap /usr/local/bin
clean:
@rm -f m5tap *.o *.d *~ .#*
int verbose = 0;
#define DPRINTF(args...) do { \
- if (verbose > 1) \
+ if (verbose >= 1) \
cprintf(args); \
} while (0)
#define DDUMP(args...) do { \
- if (verbose > 2) \
+ if (verbose >= 2) \
dump((const u_char *)args); \
} while (0)
{
int fd = ::socket(PF_INET, SOCK_STREAM, 0);
if (fd < 0)
- panic("Can't create socket!");
+ panic("Can't create socket!\n");
if (reuse) {
int i = 1;
if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&i,
sizeof(i)) < 0)
- panic("setsockopt() SO_REUSEADDR failed!");
+ panic("setsockopt() SO_REUSEADDR failed!\n");
}
return fd;
sockaddr.sin_port = htons(port);
int ret = ::bind(fd, (struct sockaddr *)&sockaddr, sizeof (sockaddr));
if (ret == -1)
- panic("bind() failed!");
+ panic("bind() failed!\n");
if (::listen(fd, 1) == -1)
- panic("listen() failed!");
+ panic("listen() failed!\n");
}
// Open a connection. Accept will block, so if you don't want it to,
socklen_t slen = sizeof (sockaddr);
int sfd = ::accept(fd, (struct sockaddr *)&sockaddr, &slen);
if (sfd == -1)
- panic("accept() failed!");
+ panic("accept() failed!\n");
if (nodelay) {
int i = 1;
sockaddr.sin_port = htons(port);
if (::connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) != 0)
- panic("could not connect to %s on port %d", host, port);
+ panic("could not connect to %s on port %d\n", host, port);
DPRINTF("connected to %s on port %d\n", host, port);
}
}
char errbuf[PCAP_ERRBUF_SIZE];
+ memset(errbuf, 0, sizeof errbuf);
pcap_t *pcap = pcap_open_live(device, 1500, 1, -1, errbuf);
if (pcap == NULL)
panic("pcap_open_live failed: %s\n", errbuf);
bpf_program program;
bpf_u_int32 localnet, netmask;
- if (!pcap_lookupnet(device, &localnet, &netmask, errbuf))
+ if (pcap_lookupnet(device, &localnet, &netmask, errbuf) == -1)
panic("pcap_lookupnet failed: %s\n", errbuf);
if (pcap_compile(pcap, &program, filter, 1, netmask) == -1)
panic("pcap_setfilter failed\n");
eth_t *ethernet = eth_open(device);
+ if (!ethernet)
+ panic("cannot open the ethernet device for writing\n");
pollfd pfds[3];
pfds[0].fd = Socket(true);
int32_t buffer_offset = 0;
int32_t data_len = 0;
+ DPRINTF("Begin poll loop\n");
while (!quit) {
int ret = ::poll(pfds, npfds, INFTIM);
if (ret < 0)
if (listening)
npfds--;
- else
+ else {
+ DPRINTF("Calling it quits because of poll error\n");
quit = true;
+ }
}
if (client_pfd)
client_pfd->revents = 0;
}
-
}
delete [] buffer;