CS350 COS
COS
Loading...
Searching...
No Matches
ethinject.c
Go to the documentation of this file.
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
11static int nicNo = 1;
12static char buf[4096];
13static MBuf mbuf;
14
15void
17{
18 uint64_t status;
19
21 mbuf.maddr = 0;
22 mbuf.len = 64;
24 mbuf.flags = 0;
26
27 status = OSNICSend(nicNo, &mbuf);
28 if (status != 0) {
29 printf("OSNICSend failed!\n");
30 return;
31 }
32
34 printf("Failed to write packet!\n");
35 return;
36 }
37}
38
39int
40main(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
#define ENOENT
Definition: errno.h:15
uint8_t ether_shost[ETHER_ADDR_LEN]
Definition: ethernet.h:15
uint8_t ether_dhost[ETHER_ADDR_LEN]
Definition: ethernet.h:14
static int nicNo
Definition: ethinject.c:11
static char buf[4096]
Definition: ethinject.c:12
static MBuf mbuf
Definition: ethinject.c:13
int main(int argc, const char *argv[])
Definition: ethinject.c:40
void writePacket(NIC *nic)
Definition: ethinject.c:16
int OSNICStat(uint64_t nicNo, NIC *nic)
Definition: syscall.c:137
int OSNICSend(uint64_t nicNo, MBuf *mbuf)
Definition: syscall.c:143
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
Definition: nic.h:11
uint64_t nicNo
Definition: nic.h:13
unsigned long uint64_t
Definition: types.h:13
unsigned int sleep(unsigned int seconds)
Definition: process.c:12