CS350 COS
COS
Loading...
Searching...
No Matches
ethdump.c
Go to the documentation of this file.
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
9static int nicNo = 1;
10static char buf[4096];
11static MBuf mbuf;
12
13void
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
30void
32{
33 uint64_t status;
34
36 mbuf.maddr = 0;
37 mbuf.len = 4096;
39 mbuf.flags = 0;
41
42 status = OSNICRecv(nicNo, &mbuf);
43 if (status != 0) {
44 printf("OSNICRecv failed!\n");
45 return;
46 }
47
49 printf("Failed to read packet!\n");
50 return;
51 }
52
53 dumpPacket();
54}
55
56int
57main(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
#define ENOENT
Definition: errno.h:15
static int nicNo
Definition: ethdump.c:9
void dumpPacket()
Definition: ethdump.c:14
static char buf[4096]
Definition: ethdump.c:10
static MBuf mbuf
Definition: ethdump.c:11
int main(int argc, const char *argv[])
Definition: ethdump.c:57
void readPacket(NIC *nic)
Definition: ethdump.c:31
uint8_t ether_shost[ETHER_ADDR_LEN]
Definition: ethernet.h:15
uint16_t ether_type
Definition: ethernet.h:16
uint8_t ether_dhost[ETHER_ADDR_LEN]
Definition: ethernet.h:14
int OSNICStat(uint64_t nicNo, NIC *nic)
Definition: syscall.c:137
int OSNICRecv(uint64_t nicNo, MBuf *mbuf)
Definition: syscall.c:149
uint32_t status
Definition: mbuf.h:20
uint32_t type
Definition: mbuf.h:18
uint64_t vaddr
Definition: mbuf.h:15
#define MBUF_STATUS_NULL
Definition: mbuf.h:10
uint32_t flags
Definition: mbuf.h:19
uint64_t maddr
Definition: mbuf.h:16
uint32_t len
Definition: mbuf.h:17
#define MBUF_STATUS_FAILED
Definition: mbuf.h:12
#define MBUF_TYPE_NULL
Definition: mbuf.h:5
Definition: mbuf.h:14
int printf(const char *fmt,...)
Definition: printf.c:212
int sprintf(char *str, const char *fmt,...)
Definition: printf.c:256
Definition: nic.h:11
uint64_t nicNo
Definition: nic.h:13
unsigned long uint64_t
Definition: types.h:13