]>
Commit | Line | Data |
---|---|---|
1a0ca1e1 FC |
1 | /* |
2 | * ARP table | |
3 | * | |
4 | * Copyright (c) 2011 AdaCore | |
5 | * | |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
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. | |
23 | */ | |
24 | ||
25 | #include "slirp.h" | |
26 | ||
5a371a2e | 27 | void arp_table_add(Slirp *slirp, uint32_t ip_addr, uint8_t ethaddr[ETH_ALEN]) |
1a0ca1e1 | 28 | { |
5a371a2e | 29 | const uint32_t broadcast_addr = |
1a0ca1e1 FC |
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 | ||
959e4147 | 40 | if (ip_addr == 0 || ip_addr == 0xffffffff || ip_addr == broadcast_addr) { |
1a0ca1e1 FC |
41 | /* Do not register broadcast addresses */ |
42 | return; | |
43 | } | |
44 | ||
45 | /* Search for an entry */ | |
46 | for (i = 0; i < ARP_TABLE_SIZE; i++) { | |
47 | if (arptbl->table[i].ar_sip == ip_addr) { | |
48 | /* Update the entry */ | |
49 | memcpy(arptbl->table[i].ar_sha, ethaddr, ETH_ALEN); | |
50 | return; | |
51 | } | |
52 | } | |
53 | ||
54 | /* No entry found, create a new one */ | |
55 | arptbl->table[arptbl->next_victim].ar_sip = ip_addr; | |
56 | memcpy(arptbl->table[arptbl->next_victim].ar_sha, ethaddr, ETH_ALEN); | |
57 | arptbl->next_victim = (arptbl->next_victim + 1) % ARP_TABLE_SIZE; | |
58 | } | |
59 | ||
5a371a2e | 60 | bool arp_table_search(Slirp *slirp, uint32_t ip_addr, |
1a0ca1e1 FC |
61 | uint8_t out_ethaddr[ETH_ALEN]) |
62 | { | |
5a371a2e | 63 | const uint32_t broadcast_addr = |
1a0ca1e1 FC |
64 | ~slirp->vnetwork_mask.s_addr | slirp->vnetwork_addr.s_addr; |
65 | ArpTable *arptbl = &slirp->arp_table; | |
66 | int i; | |
67 | ||
68 | DEBUG_CALL("arp_table_search"); | |
5a371a2e | 69 | DEBUG_ARG("ip = 0x%x", ip_addr); |
1a0ca1e1 | 70 | |
1a0ca1e1 | 71 | /* If broadcast address */ |
5a371a2e | 72 | if (ip_addr == 0xffffffff || ip_addr == broadcast_addr) { |
1a0ca1e1 FC |
73 | /* return Ethernet broadcast address */ |
74 | memset(out_ethaddr, 0xff, ETH_ALEN); | |
75 | return 1; | |
76 | } | |
77 | ||
78 | for (i = 0; i < ARP_TABLE_SIZE; i++) { | |
5a371a2e | 79 | if (arptbl->table[i].ar_sip == ip_addr) { |
1a0ca1e1 FC |
80 | memcpy(out_ethaddr, arptbl->table[i].ar_sha, ETH_ALEN); |
81 | DEBUG_ARGS((dfd, " found hw addr = %02x:%02x:%02x:%02x:%02x:%02x\n", | |
82 | out_ethaddr[0], out_ethaddr[1], out_ethaddr[2], | |
83 | out_ethaddr[3], out_ethaddr[4], out_ethaddr[5])); | |
84 | return 1; | |
85 | } | |
86 | } | |
87 | ||
88 | return 0; | |
89 | } |