tests: test-announce-self: fix memory leak
[qemu/ar7.git] / slirp / src / arp_table.c
blob58eafdcfd8f322cbc246b29f708eea3f2630040d
1 /*
2 * ARP table
4 * Copyright (c) 2011 AdaCore
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "slirp.h"
27 #include <string.h>
29 void arp_table_add(Slirp *slirp, uint32_t ip_addr, uint8_t ethaddr[ETH_ALEN])
31 const uint32_t broadcast_addr =
32 ~slirp->vnetwork_mask.s_addr | slirp->vnetwork_addr.s_addr;
33 ArpTable *arptbl = &slirp->arp_table;
34 int i;
36 DEBUG_CALL("arp_table_add");
37 DEBUG_ARG("ip = %s", inet_ntoa((struct in_addr){.s_addr = ip_addr}));
38 DEBUG_ARG("hw addr = %02x:%02x:%02x:%02x:%02x:%02x",
39 ethaddr[0], ethaddr[1], ethaddr[2],
40 ethaddr[3], ethaddr[4], ethaddr[5]);
42 if (ip_addr == 0 || ip_addr == 0xffffffff || ip_addr == broadcast_addr) {
43 /* Do not register broadcast addresses */
44 return;
47 /* Search for an entry */
48 for (i = 0; i < ARP_TABLE_SIZE; i++) {
49 if (arptbl->table[i].ar_sip == ip_addr) {
50 /* Update the entry */
51 memcpy(arptbl->table[i].ar_sha, ethaddr, ETH_ALEN);
52 return;
56 /* No entry found, create a new one */
57 arptbl->table[arptbl->next_victim].ar_sip = ip_addr;
58 memcpy(arptbl->table[arptbl->next_victim].ar_sha, ethaddr, ETH_ALEN);
59 arptbl->next_victim = (arptbl->next_victim + 1) % ARP_TABLE_SIZE;
62 bool arp_table_search(Slirp *slirp, uint32_t ip_addr,
63 uint8_t out_ethaddr[ETH_ALEN])
65 const uint32_t broadcast_addr =
66 ~slirp->vnetwork_mask.s_addr | slirp->vnetwork_addr.s_addr;
67 ArpTable *arptbl = &slirp->arp_table;
68 int i;
70 DEBUG_CALL("arp_table_search");
71 DEBUG_ARG("ip = %s", inet_ntoa((struct in_addr){.s_addr = ip_addr}));
73 /* If broadcast address */
74 if (ip_addr == 0xffffffff || ip_addr == broadcast_addr) {
75 /* return Ethernet broadcast address */
76 memset(out_ethaddr, 0xff, ETH_ALEN);
77 return 1;
80 for (i = 0; i < ARP_TABLE_SIZE; i++) {
81 if (arptbl->table[i].ar_sip == ip_addr) {
82 memcpy(out_ethaddr, arptbl->table[i].ar_sha, ETH_ALEN);
83 DEBUG_ARG("found hw addr = %02x:%02x:%02x:%02x:%02x:%02x",
84 out_ethaddr[0], out_ethaddr[1], out_ethaddr[2],
85 out_ethaddr[3], out_ethaddr[4], out_ethaddr[5]);
86 return 1;
90 return 0;