-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmac2ifname.c
102 lines (82 loc) · 2.28 KB
/
mac2ifname.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/if_link.h>
#include <net/if.h>
#include <string.h>
static int
get_mac_by_ifname(const char *ifname, uint8_t *hwaddr)
{
struct ifreq ifr;
int sock;
if ((NULL == ifname) || (NULL == hwaddr)) {
return -1;
}
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
return -1;
}
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) {
perror("ioctl(SIOCGIFHWADDR)");
return -1;
}
memcpy(hwaddr, ifr.ifr_ifru.ifru_hwaddr.sa_data, IFHWADDRLEN);
close(sock);
return 0;
}
int main(int argc, char *argv[])
{
struct ifaddrs *ifaddr, *ifa;
unsigned int scanfbuf[6];
uint8_t mac_addr_one[6];
uint8_t mac_addr_two[6];
int n;
if (argc < 2) {
printf(" usage: mac2ifname MAC_ADDRESS\n");
return -1;
}
n = sscanf(argv[1], "%x:%x:%x:%x:%x:%x",
&scanfbuf[0],
&scanfbuf[1],
&scanfbuf[2],
&scanfbuf[3],
&scanfbuf[4],
&scanfbuf[5]);
if (n != 6) {
printf(" invalid MAC address %s\n", argv[1]);
return -1;
}
for (n = 0; n < 6; n++) {
mac_addr_one[n] = (uint8_t)scanfbuf[n];
}
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
exit(EXIT_FAILURE);
}
/* Walk through linked list, maintaining head pointer so we
can free list later */
for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
if (ifa->ifa_addr == NULL)
continue;
get_mac_by_ifname(ifa->ifa_name, mac_addr_two);
#if 0
printf("%s %02x:%02x:%02x:%02x:%02x:%02x\n", ifa->ifa_name,
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3],
mac_addr[4], mac_addr[5]);
#endif
if (memcmp(mac_addr_one, mac_addr_two, sizeof(mac_addr_one)) == 0) {
printf("%s\n", ifa->ifa_name);
freeifaddrs(ifaddr);
return 0;
}
}
freeifaddrs(ifaddr);
return -1;
}