1
2 #include <stdio.h>
3 #include <errno.h>
4 #include <syscall.h>
5 #include <sys/nic.h>
6
7 #include <net/ethernet.h>
8
9 static int nicNo = 1;
10 static char buf[4096];
11 static MBuf mbuf;
12
13 void
dumpPacket()14 dumpPacket()
15 {
16 struct ether_header *hdr = (struct ether_header *)&buf;
17 char srcMac[18];
18 char dstMac[18];
19
20 sprintf(srcMac, "%02x:%02x:%02x:%02x:%02x:%02x",
21 hdr->ether_shost[0], hdr->ether_shost[1], hdr->ether_shost[2],
22 hdr->ether_shost[3], hdr->ether_shost[4], hdr->ether_shost[5]);
23 sprintf(dstMac, "%02x:%02x:%02x:%02x:%02x:%02x",
24 hdr->ether_dhost[0], hdr->ether_dhost[1], hdr->ether_dhost[2],
25 hdr->ether_dhost[3], hdr->ether_dhost[4], hdr->ether_dhost[5]);
26
27 printf("From %s to %s of type %04x\n", srcMac, dstMac, hdr->ether_type);
28 }
29
30 void
readPacket(NIC * nic)31 readPacket(NIC *nic)
32 {
33 uint64_t status;
34
35 mbuf.vaddr = (uint64_t)&buf;
36 mbuf.maddr = 0;
37 mbuf.len = 4096;
38 mbuf.type = MBUF_TYPE_NULL;
39 mbuf.flags = 0;
40 mbuf.status = MBUF_STATUS_NULL;
41
42 status = OSNICRecv(nicNo, &mbuf);
43 if (status != 0) {
44 printf("OSNICRecv failed!\n");
45 return;
46 }
47
48 if (mbuf.status == MBUF_STATUS_FAILED) {
49 printf("Failed to read packet!\n");
50 return;
51 }
52
53 dumpPacket();
54 }
55
56 int
main(int argc,const char * argv[])57 main(int argc, const char *argv[])
58 {
59 uint64_t status;
60 NIC nic;
61
62 printf("Ethernet Dump Tool\n");
63
64 status = OSNICStat(nicNo, &nic);
65 if (status == ENOENT) {
66 printf("nic%d not present\n", nicNo);
67 return 1;
68 }
69
70 printf("Listening to nic%d\n", (int)nic.nicNo);
71
72 while (1) {
73 readPacket(&nic);
74 }
75 }
76
77