]>
Commit | Line | Data |
---|---|---|
6f248ce1 TL |
1 | #define _GNU_SOURCE |
2 | ||
3 | #include <stdio.h> | |
4 | #include <strings.h> | |
5 | #include <err.h> | |
6 | #include <fnmatch.h> | |
7 | #include <net/if.h> | |
8 | #include <net/if_dl.h> | |
9 | ||
10 | #include "archcommon.h" | |
11 | ||
12 | static bool match_mac(const char *iface, const char *pattern) { | |
13 | for (struct ifaddrs *ifa = ifap; ifa; ifa = ifa->ifa_next) { | |
14 | if (ifa->ifa_addr->sa_family != AF_LINK) | |
15 | continue; | |
16 | ||
17 | struct sockaddr_dl *dl = (struct sockaddr_dl *)ifa->ifa_addr; | |
18 | if (dl->sdl_alen != 6) | |
19 | continue; | |
20 | ||
21 | if (strcmp(ifa->ifa_name, iface)) | |
22 | continue; | |
23 | ||
24 | unsigned char *ll = (unsigned char *)LLADDR(dl); | |
25 | char buf[18]; | |
26 | snprintf(buf, sizeof buf, "%02x:%02x:%02x:%02x:%02x:%02x", ll[0], ll[1], ll[2], ll[3], ll[4], ll[5]); | |
27 | return fnmatch(pattern, buf, FNM_EXTMATCH) == 0; | |
28 | } | |
29 | ||
30 | return false; | |
31 | } | |
32 | ||
33 | bool variable_match(const char *iface, const char *variable, const char *pattern) { | |
34 | if (!strcasecmp(variable, "mac")) | |
35 | return match_mac(iface, pattern); | |
36 | ||
37 | if (!strcasecmp(variable, "name")) | |
38 | return fnmatch(pattern, iface, FNM_EXTMATCH) == 0; | |
39 | ||
40 | warnx("Unknown or unsupported pattern variable %s", variable); | |
41 | return false; | |
42 | } |