add some comments
[tridge/junkcode.git] / aix_getmac.c
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <errno.h>
4 #include <sys/ndd_var.h>
5 #include <sys/kinfo.h>
6
7 /*
8   get ethernet MAC address on AIX
9  */
10 static int aix_get_mac_addr(const char *device_name, uint8_t mac[6])
11 {
12         size_t ksize;
13         struct kinfo_ndd *ndd;
14         int count, i;
15
16         ksize = getkerninfo(KINFO_NDD, 0, 0, 0);
17         if (ksize == 0) {
18                 errno = ENOSYS;
19                 return -1;
20         }
21
22         ndd = (struct kinfo_ndd *)malloc(ksize);
23         if (ndd == NULL) {
24                 errno = ENOMEM;
25                 return -1;
26         }
27
28         if (getkerninfo(KINFO_NDD, ndd, &ksize, 0) == -1) {
29                 errno = ENOSYS;
30                 return -1;
31         }
32
33         count= ksize/sizeof(struct kinfo_ndd);
34         for (i=0;i<count;i++) {
35                 if ((ndd[i].ndd_type == NDD_ETHER || 
36                      ndd[i].ndd_type == NDD_ISO88023) &&
37                     ndd[i].ndd_addrlen == 6 &&
38                     (strcmp(ndd[i].ndd_alias, device_name) == 0 ||
39                      strcmp(ndd[i].ndd_name, device_name == 0))) {
40                         memcpy(mac, ndd[i].ndd_addr, 6);
41                         free(ndd);
42                         return 0;
43                 }
44         }
45         free(ndd);
46         errno = ENOENT;
47         return -1;
48 }
49
50
51 int main(int argc, char *argv[])
52 {
53         unsigned char mac[6];
54         int i, ret;
55
56         if (argc != 2) {
57                 printf("Usage: aix_getmac <interface>\n");
58                 exit(1);
59         }
60         ret = aix_get_mac_addr(argv[1], mac);
61         if (ret == -1) {
62                 perror("aix_getmac");
63                 exit(1);
64         }
65         printf("%02x:%02x:%02x:%02x:%02x:%02x\n", 
66                mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
67 }