nbd: Permit simple error to NBD_CMD_BLOCK_STATUS
[qemu/ericb.git] / slirp / src / arp_table.c
blob9d7a59eb2c2ff053356ac0f8ba4c7a25edc32f35
1 /* SPDX-License-Identifier: MIT */
2 /*
3 * ARP table
5 * Copyright (c) 2011 AdaCore
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
26 #include "slirp.h"
28 #include <string.h>
30 void arp_table_add(Slirp *slirp, uint32_t ip_addr, uint8_t ethaddr[ETH_ALEN])
32 const uint32_t broadcast_addr =
33 ~slirp->vnetwork_mask.s_addr | slirp->vnetwork_addr.s_addr;
34 ArpTable *arptbl = &slirp->arp_table;
35 int i;
37 DEBUG_CALL("arp_table_add");
38 DEBUG_ARG("ip = %s", inet_ntoa((struct in_addr){.s_addr = ip_addr}));
39 DEBUG_ARG("hw addr = %02x:%02x:%02x:%02x:%02x:%02x",
40 ethaddr[0], ethaddr[1], ethaddr[2],
41 ethaddr[3], ethaddr[4], ethaddr[5]);
43 if (ip_addr == 0 || ip_addr == 0xffffffff || ip_addr == broadcast_addr) {
44 /* Do not register broadcast addresses */
45 return;
48 /* Search for an entry */
49 for (i = 0; i < ARP_TABLE_SIZE; i++) {
50 if (arptbl->table[i].ar_sip == ip_addr) {
51 /* Update the entry */
52 memcpy(arptbl->table[i].ar_sha, ethaddr, ETH_ALEN);
53 return;
57 /* No entry found, create a new one */
58 arptbl->table[arptbl->next_victim].ar_sip = ip_addr;
59 memcpy(arptbl->table[arptbl->next_victim].ar_sha, ethaddr, ETH_ALEN);
60 arptbl->next_victim = (arptbl->next_victim + 1) % ARP_TABLE_SIZE;
63 bool arp_table_search(Slirp *slirp, uint32_t ip_addr,
64 uint8_t out_ethaddr[ETH_ALEN])
66 const uint32_t broadcast_addr =
67 ~slirp->vnetwork_mask.s_addr | slirp->vnetwork_addr.s_addr;
68 ArpTable *arptbl = &slirp->arp_table;
69 int i;
71 DEBUG_CALL("arp_table_search");
72 DEBUG_ARG("ip = %s", inet_ntoa((struct in_addr){.s_addr = ip_addr}));
74 /* If broadcast address */
75 if (ip_addr == 0xffffffff || ip_addr == broadcast_addr) {
76 /* return Ethernet broadcast address */
77 memset(out_ethaddr, 0xff, ETH_ALEN);
78 return 1;
81 for (i = 0; i < ARP_TABLE_SIZE; i++) {
82 if (arptbl->table[i].ar_sip == ip_addr) {
83 memcpy(out_ethaddr, arptbl->table[i].ar_sha, ETH_ALEN);
84 DEBUG_ARG("found hw addr = %02x:%02x:%02x:%02x:%02x:%02x",
85 out_ethaddr[0], out_ethaddr[1], out_ethaddr[2],
86 out_ethaddr[3], out_ethaddr[4], out_ethaddr[5]);
87 return 1;
91 return 0;