Commit 978bfd37 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Add a function to determine an interface's link-layer address.

parent c7535333
......@@ -58,6 +58,45 @@ kernel_ll_addresses(char *ifname, int ifindex,
return j;
}
/* Determine an interface's hardware address, in modified EUI-64 format */
int
if_eui64(char *ifname, int ifindex, unsigned char *eui)
{
int s, rc;
struct ifreq req;
unsigned char *mac;
s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
if(s < 0) return -1;
memset(&req, 0, sizeof(req));
strncpy(req.ifr_name, ifname, sizeof(req.ifr_name));
rc = ioctl(s, SIOCGIFHWADDR, &req);
if(rc < 0) {
int saved_errno = errno;
close(s);
errno = saved_errno;
return -1;
}
close(s);
mac = (unsigned char *)req.ifr_hwaddr.sa_data;
/* Check not group and global */
if((mac[0] & 1) != 0 || (mac[0] & 2) != 0) {
errno = ENOENT;
return -1;
}
eui[0] = mac[0] ^ 2;
eui[1] = mac[1];
eui[2] = mac[2];
eui[3] = 0xFF;
eui[4] = 0xFE;
eui[5] = mac[3];
eui[6] = mac[4];
eui[7] = mac[5];
return 1;
}
/* Like gettimeofday, but should return monotonic time. If POSIX clocks
are not available, falls back to gettimeofday. */
int
......
......@@ -61,4 +61,5 @@ int kernel_addresses(char *ifname, int ifindex,
struct kernel_route *routes, int maxroutes);
int kernel_ll_addresses(char *ifname, int ifindex,
unsigned char (*addresses)[16], int maxaddr);
int if_eui64(char *ifname, int ifindex, unsigned char *eui);
int gettime(struct timeval *tv);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment