Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
folium-app
GitHub Repository: folium-app/Folium
Path: blob/a-new-beginning/SharedDependencies/Sources/libslirp/ndp_table.c
2 views
1
/* SPDX-License-Identifier: BSD-3-Clause */
2
/*
3
* Copyright (c) 2013
4
* Guillaume Subiron, Yann Bordenave, Serigne Modou Wagne.
5
*/
6
7
#include "slirp.h"
8
9
void ndp_table_add(Slirp *slirp, struct in6_addr ip_addr,
10
uint8_t ethaddr[ETH_ALEN])
11
{
12
char addrstr[INET6_ADDRSTRLEN];
13
NdpTable *ndp_table = &slirp->ndp_table;
14
int i;
15
char ethaddr_str[ETH_ADDRSTRLEN];
16
17
inet_ntop(AF_INET6, &(ip_addr), addrstr, INET6_ADDRSTRLEN);
18
19
DEBUG_CALL("ndp_table_add");
20
DEBUG_ARG("ip = %s", addrstr);
21
DEBUG_ARG("hw addr = %s", slirp_ether_ntoa(ethaddr, ethaddr_str,
22
sizeof(ethaddr_str)));
23
24
if (IN6_IS_ADDR_MULTICAST(&ip_addr) || in6_zero(&ip_addr)) {
25
/* Do not register multicast or unspecified addresses */
26
DEBUG_CALL(" abort: do not register multicast or unspecified address");
27
return;
28
}
29
30
/* Search for an entry */
31
for (i = 0; i < NDP_TABLE_SIZE; i++) {
32
if (in6_equal(&ndp_table->table[i].ip_addr, &ip_addr)) {
33
DEBUG_CALL(" already in table: update the entry");
34
/* Update the entry */
35
memcpy(ndp_table->table[i].eth_addr, ethaddr, ETH_ALEN);
36
return;
37
}
38
}
39
40
/* No entry found, create a new one */
41
DEBUG_CALL(" create new entry");
42
/* Save the first entry, it is the guest. */
43
if (in6_zero(&ndp_table->guest_in6_addr)) {
44
ndp_table->guest_in6_addr = ip_addr;
45
}
46
ndp_table->table[ndp_table->next_victim].ip_addr = ip_addr;
47
memcpy(ndp_table->table[ndp_table->next_victim].eth_addr, ethaddr,
48
ETH_ALEN);
49
ndp_table->next_victim = (ndp_table->next_victim + 1) % NDP_TABLE_SIZE;
50
}
51
52
bool ndp_table_search(Slirp *slirp, struct in6_addr ip_addr,
53
uint8_t out_ethaddr[ETH_ALEN])
54
{
55
char addrstr[INET6_ADDRSTRLEN];
56
NdpTable *ndp_table = &slirp->ndp_table;
57
int i;
58
char ethaddr_str[ETH_ADDRSTRLEN];
59
60
inet_ntop(AF_INET6, &(ip_addr), addrstr, INET6_ADDRSTRLEN);
61
62
DEBUG_CALL("ndp_table_search");
63
DEBUG_ARG("ip = %s", addrstr);
64
65
/* If unspecified address */
66
if (in6_zero(&ip_addr)) {
67
/* return Ethernet broadcast address */
68
memset(out_ethaddr, 0xff, ETH_ALEN);
69
return 1;
70
}
71
72
/* Multicast address: fec0::abcd:efgh/8 -> 33:33:ab:cd:ef:gh */
73
if (IN6_IS_ADDR_MULTICAST(&ip_addr)) {
74
out_ethaddr[0] = 0x33;
75
out_ethaddr[1] = 0x33;
76
out_ethaddr[2] = ip_addr.s6_addr[12];
77
out_ethaddr[3] = ip_addr.s6_addr[13];
78
out_ethaddr[4] = ip_addr.s6_addr[14];
79
out_ethaddr[5] = ip_addr.s6_addr[15];
80
DEBUG_ARG("multicast addr = %s",
81
slirp_ether_ntoa(out_ethaddr, ethaddr_str,
82
sizeof(ethaddr_str)));
83
return 1;
84
}
85
86
for (i = 0; i < NDP_TABLE_SIZE; i++) {
87
if (in6_equal(&ndp_table->table[i].ip_addr, &ip_addr)) {
88
memcpy(out_ethaddr, ndp_table->table[i].eth_addr, ETH_ALEN);
89
DEBUG_ARG("found hw addr = %s",
90
slirp_ether_ntoa(out_ethaddr, ethaddr_str,
91
sizeof(ethaddr_str)));
92
return 1;
93
}
94
}
95
96
DEBUG_CALL(" ip not found in table");
97
return 0;
98
}
99
100