-
Notifications
You must be signed in to change notification settings - Fork 2
/
arp.c
97 lines (85 loc) · 1.93 KB
/
arp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/if.h>
#include <linux/if_ether.h>
#include <linux/if_tun.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "list.h"
struct arp_hdr {
uint16_t hwtype;
uint16_t protype;
unsigned char hwsize;
unsigned char prosize;
uint16_t opcode;
unsigned char data[];
} __attribute__((packed));
struct arp_ipv4 {
unsigned char smac[6];
uint32_t sip;
unsigned char dmac[6];
uint32_t dip;
} __attribute__((packed));
struct arp_cache {
uint16_t proto;
uint32_t ip;
unsigned char mac[6];
list_head_t next;
};
static LIST_HEAD(head);
#define ETHERNET 0x0001
#define IPV4 0x0800
#define ARP_REQUEST 0x0001
#define ARP_REPLY 0x0001
static uint32_t myip = 0x0200000a;
static char mymac[] = {0x00, 0x50, 0x43, 0x78, 0x89, 0x9a};
int arp_in(struct arp_hdr *hdr)
{
assert(hdr);
if (ntohs(hdr->hwtype) != ETHERNET)
return -EINVAL;
if (ntohs(hdr->protype) != IPV4)
return -EINVAL;
struct arp_ipv4 *arpd = (struct arp_ipv4 *) hdr->data;
bool merge_flag = false;
struct arp_cache *arpc;
list_for_each_entry(arpc, &head, next) {
if (arpc->ip == arpd->sip) {
memcpy(arpc->mac, arpd->smac, 6);
merge_flag = true;
break;
}
}
if (arpd->dip == myip) {
if (!merge_flag) {
arpc = (struct arp_cache *) malloc(sizeof(struct arp_cache));
if (!arpc)
return -ENOMEM;
arpc->ip = arpd->sip;
arpc->proto = hdr->protype;
memcpy(arpc->mac, arpd->smac, 6);
list_add(&arpc->next, &head);
}
if (ntohs(hdr->opcode) == ARP_REQUEST) {
hdr->opcode = htons(ARP_REPLY);
uint32_t tmp = arpd->dip;
arpd->dip = arpd->sip;
arpd->sip = tmp;
memcpy(arpd->dmac, arpd->smac, 6);
memcpy(arpd->smac, mymac, 6);
}
}
return 0;
}