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