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#include "slirp.h"
26
27void arp_table_add(Slirp *slirp, uint32_t ip_addr, uint8_t ethaddr[ETH_ALEN])
28{
29 const uint32_t broadcast_addr =
30 ~slirp->vnetwork_mask.s_addr | slirp->vnetwork_addr.s_addr;
31 ArpTable *arptbl = &slirp->arp_table;
32 int i;
33
34 DEBUG_CALL("arp_table_add");
35 DEBUG_ARG("ip = 0x%x", ip_addr);
36 DEBUG_ARGS((dfd, " hw addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
37 ethaddr[0], ethaddr[1], ethaddr[2],
38 ethaddr[3], ethaddr[4], ethaddr[5]));
39
40
41 if ((ip_addr & htonl(~(0xf << 28))) == 0) {
42 return;
43 }
44
45 if (ip_addr == 0xffffffff || ip_addr == broadcast_addr) {
46
47 return;
48 }
49
50
51 for (i = 0; i < ARP_TABLE_SIZE; i++) {
52 if (arptbl->table[i].ar_sip == ip_addr) {
53
54 memcpy(arptbl->table[i].ar_sha, ethaddr, ETH_ALEN);
55 return;
56 }
57 }
58
59
60 arptbl->table[arptbl->next_victim].ar_sip = ip_addr;
61 memcpy(arptbl->table[arptbl->next_victim].ar_sha, ethaddr, ETH_ALEN);
62 arptbl->next_victim = (arptbl->next_victim + 1) % ARP_TABLE_SIZE;
63}
64
65bool arp_table_search(Slirp *slirp, uint32_t ip_addr,
66 uint8_t out_ethaddr[ETH_ALEN])
67{
68 const uint32_t broadcast_addr =
69 ~slirp->vnetwork_mask.s_addr | slirp->vnetwork_addr.s_addr;
70 ArpTable *arptbl = &slirp->arp_table;
71 int i;
72
73 DEBUG_CALL("arp_table_search");
74 DEBUG_ARG("ip = 0x%x", ip_addr);
75
76
77 assert((ip_addr & htonl(~(0xf << 28))) != 0);
78
79
80 if (ip_addr == 0xffffffff || ip_addr == broadcast_addr) {
81
82 memset(out_ethaddr, 0xff, ETH_ALEN);
83 return 1;
84 }
85
86 for (i = 0; i < ARP_TABLE_SIZE; i++) {
87 if (arptbl->table[i].ar_sip == ip_addr) {
88 memcpy(out_ethaddr, arptbl->table[i].ar_sha, ETH_ALEN);
89 DEBUG_ARGS((dfd, " found hw addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
90 out_ethaddr[0], out_ethaddr[1], out_ethaddr[2],
91 out_ethaddr[3], out_ethaddr[4], out_ethaddr[5]));
92 return 1;
93 }
94 }
95
96 return 0;
97}
98