Browse code

Grab MAC addresses on OpenBSD

Shawn Webb authored on 2013/11/01 21:02:49
Showing 1 changed files
... ...
@@ -19,9 +19,13 @@
19 19
 #include <sys/ioctl.h>
20 20
 #endif
21 21
 
22
-#if defined(SIOCGIFHWADDR)
22
+#if defined(HAVE_GETIFADDRS)
23 23
 #include <net/if.h>
24
+#include <net/if_dl.h>
24 25
 #include <ifaddrs.h>
26
+#endif
27
+
28
+#if defined(SIOCGIFHWADDR)
25 29
 #include <linux/sockios.h>
26 30
 #endif
27 31
 
... ...
@@ -74,16 +78,21 @@ struct device *get_device_entry(struct device *devices, size_t *ndevices, const
74 74
     return devices;
75 75
 }
76 76
 
77
-#if HAVE_GETIFADDRS && !HAVE_SYSCTLBYNAME && defined(SIOCGIFHWADDR)
77
+#if HAVE_GETIFADDRS
78 78
 struct device *get_devices(void)
79 79
 {
80
-    struct ifaddrs *addrs, *addr;
80
+    struct ifaddrs *addrs=NULL, *addr;
81 81
     struct device *devices=NULL, *device=NULL;
82 82
     size_t ndevices=0, i;
83 83
     void *p;
84 84
     uint8_t *mac;
85 85
     int sock;
86
+
87
+#if defined(SIOCGIFHWADDR)
86 88
     struct ifreq ifr;
89
+#else
90
+    struct sockaddr_dl *sdl;
91
+#endif
87 92
 
88 93
     if (getifaddrs(&addrs))
89 94
         return NULL;
... ...
@@ -92,14 +101,40 @@ struct device *get_devices(void)
92 92
         if (!(addr->ifa_addr))
93 93
             continue;
94 94
 
95
+#if defined(AF_PACKET)
95 96
         if (addr->ifa_addr->sa_family != AF_PACKET)
96 97
             continue;
98
+#elif defined(AF_LINK)
99
+        if (addr->ifa_addr->sa_family != AF_LINK)
100
+            continue;
101
+#else
102
+        break; /* We don't support anything else */
103
+#endif
97 104
 
98 105
         devices = get_device_entry(devices, &ndevices, addr->ifa_name);
99 106
         if (!(devices)) {
100 107
             freeifaddrs(addrs);
101 108
             return NULL;
102 109
         }
110
+
111
+#if !defined(SIOCGIFHWADDR)
112
+        for (device = devices; device < devices + ndevices; device++) {
113
+            if (!(strcmp(device->name, addr->ifa_name))) {
114
+                sdl = (struct sockaddr_dl *)(addr->ifa_addr);
115
+
116
+#if defined(LLADDR)
117
+                mac = LLADDR(sdl);
118
+#else
119
+                mac = ((uint8_t *)(sdl->sdl_data + sdl->sdl_nlen));
120
+#endif
121
+                for (i=0; i<6; i++)
122
+                    snprintf(device->mac+strlen(device->mac), sizeof(device->mac)-strlen(device->mac)-1, "%02x:", mac[i]);
123
+
124
+                cli_warnmsg("MAC for device %s: %s\n", device->name, device->mac);
125
+                break;
126
+            }
127
+        }
128
+#endif
103 129
     }
104 130
 
105 131
     if (addrs) {
... ...
@@ -107,22 +142,23 @@ struct device *get_devices(void)
107 107
         addrs = NULL;
108 108
     }
109 109
 
110
-    sock = socket(AF_INET, SOCK_DGRAM, 0);
111
-    if (sock < 0)
112
-        goto err;
113
-
110
+#if defined(SIOCGIFHWADDR)
114 111
     for (device = devices; device < devices + (ndevices); device++) {
115 112
         memset(&ifr, 0x00, sizeof(struct ifreq));
116 113
         strcpy(ifr.ifr_name, device->name);
114
+        sock = socket(AF_INET, SOCK_DGRAM, 0);
115
+        if (sock < 0)
116
+            goto err;
117 117
         if (ioctl(sock, SIOCGIFHWADDR, &ifr)) {
118 118
             close(sock);
119 119
             goto err;
120 120
         }
121
-
122 121
         mac = ((uint8_t *)(ifr.ifr_ifru.ifru_hwaddr.sa_data));
122
+
123 123
         for (i=0; i<6; i++)
124 124
             snprintf(device->mac+strlen(device->mac), sizeof(device->mac)-strlen(device->mac)-1, "%02x:", mac[i]);
125 125
     }
126
+#endif
126 127
 
127 128
     close(sock);
128 129