Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
folium-app
GitHub Repository: folium-app/Folium
Path: blob/a-new-beginning/SharedDependencies/Sources/libslirp/arp_table.c
2 views
1
/* SPDX-License-Identifier: MIT */
2
/*
3
* ARP table
4
*
5
* Copyright (c) 2011 AdaCore
6
*
7
* Permission is hereby granted, free of charge, to any person obtaining a copy
8
* of this software and associated documentation files (the "Software"), to deal
9
* in the Software without restriction, including without limitation the rights
10
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
* copies of the Software, and to permit persons to whom the Software is
12
* furnished to do so, subject to the following conditions:
13
*
14
* The above copyright notice and this permission notice shall be included in
15
* all copies or substantial portions of the Software.
16
*
17
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
* THE SOFTWARE.
24
*/
25
26
#include "slirp.h"
27
28
#include <string.h>
29
30
void arp_table_add(Slirp *slirp, uint32_t ip_addr,
31
const uint8_t ethaddr[ETH_ALEN])
32
{
33
const uint32_t broadcast_addr =
34
~slirp->vnetwork_mask.s_addr | slirp->vnetwork_addr.s_addr;
35
ArpTable *arptbl = &slirp->arp_table;
36
int i;
37
char ethaddr_str[ETH_ADDRSTRLEN];
38
char addr[INET_ADDRSTRLEN];
39
40
DEBUG_CALL("arp_table_add");
41
DEBUG_ARG("ip = %s", inet_ntop(AF_INET, &(struct in_addr){ .s_addr = ip_addr },
42
addr, sizeof(addr)));
43
DEBUG_ARG("hw addr = %s", slirp_ether_ntoa(ethaddr, ethaddr_str,
44
sizeof(ethaddr_str)));
45
46
if (ip_addr == 0 || ip_addr == 0xffffffff || ip_addr == broadcast_addr) {
47
/* Do not register broadcast addresses */
48
return;
49
}
50
51
/* Search for an entry */
52
for (i = 0; i < ARP_TABLE_SIZE; i++) {
53
if (arptbl->table[i].ar_sip == ip_addr) {
54
/* Update the entry */
55
memcpy(arptbl->table[i].ar_sha, ethaddr, ETH_ALEN);
56
return;
57
}
58
}
59
60
/* No entry found, create a new one */
61
arptbl->table[arptbl->next_victim].ar_sip = ip_addr;
62
memcpy(arptbl->table[arptbl->next_victim].ar_sha, ethaddr, ETH_ALEN);
63
arptbl->next_victim = (arptbl->next_victim + 1) % ARP_TABLE_SIZE;
64
}
65
66
bool arp_table_search(Slirp *slirp, uint32_t ip_addr,
67
uint8_t out_ethaddr[ETH_ALEN])
68
{
69
const uint32_t broadcast_addr =
70
~slirp->vnetwork_mask.s_addr | slirp->vnetwork_addr.s_addr;
71
ArpTable *arptbl = &slirp->arp_table;
72
int i;
73
char ethaddr_str[ETH_ADDRSTRLEN];
74
char addr[INET_ADDRSTRLEN];
75
76
DEBUG_CALL("arp_table_search");
77
DEBUG_ARG("ip = %s", inet_ntop(AF_INET, &(struct in_addr){ .s_addr = ip_addr },
78
addr, sizeof(addr)));
79
80
/* If broadcast address */
81
if (ip_addr == 0 || ip_addr == 0xffffffff || ip_addr == broadcast_addr) {
82
/* return Ethernet broadcast address */
83
memset(out_ethaddr, 0xff, ETH_ALEN);
84
return 1;
85
}
86
87
for (i = 0; i < ARP_TABLE_SIZE; i++) {
88
if (arptbl->table[i].ar_sip == ip_addr) {
89
memcpy(out_ethaddr, arptbl->table[i].ar_sha, ETH_ALEN);
90
DEBUG_ARG("found hw addr = %s",
91
slirp_ether_ntoa(out_ethaddr, ethaddr_str,
92
sizeof(ethaddr_str)));
93
return 1;
94
}
95
}
96
97
return 0;
98
}
99
100