1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-16 04:39:03 +00:00

Rename do_arp_cache to do_mac_cache.

It is no longer specific to ARP; it also caches IPv6 ND results.
This commit is contained in:
david
2011-06-14 00:42:39 +00:00
parent fdef8d7663
commit 35d920cda9
3 changed files with 38 additions and 38 deletions

View File

@@ -459,27 +459,27 @@ int ip_is_reserved(struct in_addr *ip)
} }
/* A trivial functon that maintains a cache of IP to MAC Address /* A trivial functon that maintains a cache of IP to MAC Address
entries. If the command is ARPCACHE_GET, this func looks for the entries. If the command is MACCACHE_GET, this func looks for the
IPv4 address in ss and fills in the 'mac' parameter and returns IPv4 address in ss and fills in the 'mac' parameter and returns
true if it is found. Otherwise (not found), the function returns true if it is found. Otherwise (not found), the function returns
false. If the command is ARPCACHE_SET, the function adds an entry false. If the command is MACCACHE_SET, the function adds an entry
with the given ip (ss) and mac address. An existing entry for the with the given ip (ss) and mac address. An existing entry for the
IP ss will be overwritten with the new MAC address. true is always IP ss will be overwritten with the new MAC address. true is always
returned for the set command. */ returned for the set command. */
#define ARPCACHE_GET 1 #define MACCACHE_GET 1
#define ARPCACHE_SET 2 #define MACCACHE_SET 2
static int do_arp_cache(int command, struct sockaddr_storage *ss, u8 *mac) { static int do_mac_cache(int command, struct sockaddr_storage *ss, u8 *mac) {
struct ArpCache { struct MacCache {
struct sockaddr_storage ip; struct sockaddr_storage ip;
u8 mac[6]; u8 mac[6];
}; };
static struct ArpCache *Cache = NULL; static struct MacCache *Cache = NULL;
static int ArpCapacity = 0; static int MacCapacity = 0;
static int ArpCacheSz = 0; static int MacCacheSz = 0;
int i; int i;
if (command == ARPCACHE_GET) { if (command == MACCACHE_GET) {
for (i = 0; i < ArpCacheSz; i++) { for (i = 0; i < MacCacheSz; i++) {
if (sockaddr_storage_cmp(&Cache[i].ip, ss) == 0) { if (sockaddr_storage_cmp(&Cache[i].ip, ss) == 0) {
memcpy(mac, Cache[i].mac, 6); memcpy(mac, Cache[i].mac, 6);
return 1; return 1;
@@ -487,17 +487,17 @@ static int do_arp_cache(int command, struct sockaddr_storage *ss, u8 *mac) {
} }
return 0; return 0;
} }
assert(command == ARPCACHE_SET); assert(command == MACCACHE_SET);
if (ArpCacheSz == ArpCapacity) { if (MacCacheSz == MacCapacity) {
if (ArpCapacity == 0) if (MacCapacity == 0)
ArpCapacity = 32; MacCapacity = 32;
else else
ArpCapacity <<= 2; MacCapacity <<= 2;
Cache = (struct ArpCache *) safe_realloc(Cache, ArpCapacity * sizeof(struct ArpCache)); Cache = (struct MacCache *) safe_realloc(Cache, MacCapacity * sizeof(struct MacCache));
} }
/* Ensure that it isn't already there ... */ /* Ensure that it isn't already there ... */
for (i = 0; i < ArpCacheSz; i++) { for (i = 0; i < MacCacheSz; i++) {
if (sockaddr_storage_cmp(&Cache[i].ip, ss) == 0) { if (sockaddr_storage_cmp(&Cache[i].ip, ss) == 0) {
memcpy(Cache[i].mac, mac, 6); memcpy(Cache[i].mac, mac, 6);
return 1; return 1;
@@ -507,26 +507,26 @@ static int do_arp_cache(int command, struct sockaddr_storage *ss, u8 *mac) {
/* Add it to the end of the list */ /* Add it to the end of the list */
memcpy(&Cache[i].ip, ss, sizeof(struct sockaddr_storage)); memcpy(&Cache[i].ip, ss, sizeof(struct sockaddr_storage));
memcpy(Cache[i].mac, mac, 6); memcpy(Cache[i].mac, mac, 6);
ArpCacheSz++; MacCacheSz++;
return 1; return 1;
} }
/* A couple of trivial functions that maintain a cache of IP to MAC /* A couple of trivial functions that maintain a cache of IP to MAC
* Address entries. Function arp_cache_get() looks for the IPv4 address * Address entries. Function mac_cache_get() looks for the IPv4 address
* in ss and fills in the 'mac' parameter and returns true if it is * in ss and fills in the 'mac' parameter and returns true if it is
* found. Otherwise (not found), the function returns false. * found. Otherwise (not found), the function returns false.
* Function arp_cache_set() adds an entry with the given ip (ss) and * Function mac_cache_set() adds an entry with the given ip (ss) and
* mac address. An existing entry for the IP ss will be overwritten * mac address. An existing entry for the IP ss will be overwritten
* with the new MAC address. arp_cache_set() always returns true. * with the new MAC address. mac_cache_set() always returns true.
* WARNING: The caller must ensure that the supplied "ss" is of family * WARNING: The caller must ensure that the supplied "ss" is of family
* AF_INET. Otherwise the function will return 0 and there would be * AF_INET. Otherwise the function will return 0 and there would be
* no way for the caller to tell tell the difference between an error * no way for the caller to tell tell the difference between an error
* or a cache miss.*/ * or a cache miss.*/
int arp_cache_get(struct sockaddr_storage *ss, u8 *mac){ int mac_cache_get(struct sockaddr_storage *ss, u8 *mac){
return do_arp_cache(ARPCACHE_GET, ss, mac); return do_mac_cache(MACCACHE_GET, ss, mac);
} }
int arp_cache_set(struct sockaddr_storage *ss, u8 *mac){ int mac_cache_set(struct sockaddr_storage *ss, u8 *mac){
return do_arp_cache(ARPCACHE_SET, ss, mac); return do_mac_cache(MACCACHE_SET, ss, mac);
} }
/* Standard BSD internet checksum routine. Uses libdnet helper functions. */ /* Standard BSD internet checksum routine. Uses libdnet helper functions. */

View File

@@ -183,14 +183,14 @@ int ip_is_reserved(struct in_addr *ip);
/* A couple of trivial functions that maintain a cache of IP to MAC /* A couple of trivial functions that maintain a cache of IP to MAC
* Address entries. Function arp_cache_get() looks for the IPv4 address * Address entries. Function mac_cache_get() looks for the IPv4 address
* in ss and fills in the 'mac' parameter and returns true if it is * in ss and fills in the 'mac' parameter and returns true if it is
* found. Otherwise (not found), the function returns false. * found. Otherwise (not found), the function returns false.
* Function arp_cache_set() adds an entry with the given ip (ss) and * Function mac_cache_set() adds an entry with the given ip (ss) and
* mac address. An existing entry for the IP ss will be overwritten * mac address. An existing entry for the IP ss will be overwritten
* with the new MAC address. arp_cache_set() always returns true. */ * with the new MAC address. mac_cache_set() always returns true. */
int arp_cache_get(struct sockaddr_storage *ss, u8 *mac); int mac_cache_get(struct sockaddr_storage *ss, u8 *mac);
int arp_cache_set(struct sockaddr_storage *ss, u8 *mac); int mac_cache_set(struct sockaddr_storage *ss, u8 *mac);
const void *ip_get_data(const void *packet, unsigned int *len, const void *ip_get_data(const void *packet, unsigned int *len,
struct abstract_ip_hdr *hdr); struct abstract_ip_hdr *hdr);

View File

@@ -1797,7 +1797,7 @@ bool setTargetNextHopMAC(Target *target) {
} }
/* First, let us check the Nmap arp cache ... */ /* First, let us check the Nmap arp cache ... */
if (arp_cache_get(&targetss, mac)) { if (mac_cache_get(&targetss, mac)) {
target->setNextHopMACAddress(mac); target->setNextHopMACAddress(mac);
return true; return true;
} }
@@ -1806,7 +1806,7 @@ bool setTargetNextHopMAC(Target *target) {
a = arp_open(); a = arp_open();
addr_ston((sockaddr *) & targetss, &ae.arp_pa); addr_ston((sockaddr *) & targetss, &ae.arp_pa);
if (arp_get(a, &ae) == 0) { if (arp_get(a, &ae) == 0) {
arp_cache_set(&targetss, ae.arp_ha.addr_eth.data); mac_cache_set(&targetss, ae.arp_ha.addr_eth.data);
target->setNextHopMACAddress(ae.arp_ha.addr_eth.data); target->setNextHopMACAddress(ae.arp_ha.addr_eth.data);
arp_close(a); arp_close(a);
return true; return true;
@@ -1819,7 +1819,7 @@ bool setTargetNextHopMAC(Target *target) {
if (target->af() == AF_INET){ if (target->af() == AF_INET){
if (doArp(target->deviceFullName(), target->SrcMACAddress(), if (doArp(target->deviceFullName(), target->SrcMACAddress(),
&srcss, &targetss, mac, PacketTrace::traceArp)) { &srcss, &targetss, mac, PacketTrace::traceArp)) {
arp_cache_set(&targetss, mac); mac_cache_set(&targetss, mac);
target->setNextHopMACAddress(mac); target->setNextHopMACAddress(mac);
return true; return true;
} }
@@ -1827,7 +1827,7 @@ bool setTargetNextHopMAC(Target *target) {
else if (target->af() == AF_INET6){ else if (target->af() == AF_INET6){
if (doND(target->deviceFullName(), target->SrcMACAddress(), if (doND(target->deviceFullName(), target->SrcMACAddress(),
&srcss, &targetss, mac, PacketTrace::traceND)) { &srcss, &targetss, mac, PacketTrace::traceND)) {
arp_cache_set(&targetss, mac); mac_cache_set(&targetss, mac);
target->setNextHopMACAddress(mac); target->setNextHopMACAddress(mac);
return true; return true;
} }
@@ -1846,14 +1846,14 @@ bool getNextHopMAC(char *iface, u8 *srcmac, struct sockaddr_storage *srcss,
struct arp_entry ae; struct arp_entry ae;
/* Nmap's ARP cache */ /* Nmap's ARP cache */
if (arp_cache_get(dstss, dstmac)) if (mac_cache_get(dstss, dstmac))
return true; return true;
/* System ARP cache */ /* System ARP cache */
a = arp_open(); a = arp_open();
addr_ston((sockaddr *) dstss, &ae.arp_pa); addr_ston((sockaddr *) dstss, &ae.arp_pa);
if (arp_get(a, &ae) == 0) { if (arp_get(a, &ae) == 0) {
arp_cache_set(dstss, ae.arp_ha.addr_eth.data); mac_cache_set(dstss, ae.arp_ha.addr_eth.data);
memcpy(dstmac, ae.arp_ha.addr_eth.data, 6); memcpy(dstmac, ae.arp_ha.addr_eth.data, 6);
arp_close(a); arp_close(a);
return true; return true;
@@ -1862,7 +1862,7 @@ bool getNextHopMAC(char *iface, u8 *srcmac, struct sockaddr_storage *srcss,
/* Send ARP */ /* Send ARP */
if (doArp(iface, srcmac, srcss, dstss, dstmac, PacketTrace::traceArp)) { if (doArp(iface, srcmac, srcss, dstss, dstmac, PacketTrace::traceArp)) {
arp_cache_set(dstss, dstmac); mac_cache_set(dstss, dstmac);
return true; return true;
} }