* Track MAC addresses in the client cache (when we can find them); so if we have an expired cache entry, but the MAC hasn't changed, we can assume the original ID is still valid.
This commit is contained in:
35
getifaddr.c
35
getifaddr.c
@ -127,3 +127,38 @@ getifhwaddr(const char * ifname, char * buf, int len)
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
get_remote_mac(struct in_addr ip_addr, unsigned char * mac)
|
||||
{
|
||||
struct in_addr arp_ent;
|
||||
FILE * arp;
|
||||
char remote_ip[16];
|
||||
int matches, hwtype, flags;
|
||||
memset(mac, 0xFF, 6);
|
||||
|
||||
arp = fopen("/proc/net/arp", "r");
|
||||
if( !arp )
|
||||
return 1;
|
||||
while( !feof(arp) )
|
||||
{
|
||||
matches = fscanf(arp, "%s 0x%X 0x%X %hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
|
||||
remote_ip, &hwtype, &flags,
|
||||
&mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
|
||||
if( matches != 9 )
|
||||
continue;
|
||||
inet_pton(AF_INET, remote_ip, &arp_ent);
|
||||
if( ip_addr.s_addr == arp_ent.s_addr )
|
||||
break;
|
||||
mac[0] = 0xFF;
|
||||
}
|
||||
fclose(arp);
|
||||
|
||||
if( mac[0] == 0xFF )
|
||||
{
|
||||
memset(mac, 0xFF, 6);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user