1 /*
2 **  OSSP uuid - Universally Unique Identifier
3 **  Copyright (c) 2004-2008 Ralf S. Engelschall <rse@engelschall.com>
4 **  Copyright (c) 2004-2008 The OSSP Project <http://www.ossp.org/>
5 **
6 **  This file is part of OSSP uuid, a library for the generation
7 **  of UUIDs which can found at http://www.ossp.org/pkg/lib/uuid/
8 **
9 **  Permission to use, copy, modify, and distribute this software for
10 **  any purpose with or without fee is hereby granted, provided that
11 **  the above copyright notice and this permission notice appear in all
12 **  copies.
13 **
14 **  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
15 **  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 **  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 **  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
18 **  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 **  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 **  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
21 **  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 **  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 **  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
24 **  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 **  SUCH DAMAGE.
26 **
27 **  uuid_mac.c: Media Access Control (MAC) resolver implementation
28 */
29 
30 /* own headers (part (1/2) */
31 #include "uuid_ac.h"
32 
33 /* system headers */
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <time.h>
40 #ifdef HAVE_SYS_TIME_H
41 #include <sys/time.h>
42 #endif
43 #ifdef HAVE_SYS_TYPES_H
44 #include <sys/types.h>
45 #endif
46 #ifdef HAVE_SYS_PARAM_H
47 #include <sys/param.h>
48 #endif
49 #ifdef HAVE_SYS_IOCTL_H
50 #include <sys/ioctl.h>
51 #endif
52 #ifdef HAVE_SYS_SOCKET_H
53 #include <sys/socket.h>
54 #endif
55 #ifdef HAVE_SYS_SOCKIO_H
56 #include <sys/sockio.h>
57 #endif
58 #ifdef HAVE_NETDB_H
59 #include <netdb.h>
60 #endif
61 #ifdef HAVE_NET_IF_H
62 #include <net/if.h>
63 #endif
64 #ifdef HAVE_NET_IF_DL_H
65 #include <net/if_dl.h>
66 #endif
67 #ifdef HAVE_NET_IF_ARP_H
68 #include <net/if_arp.h>
69 #endif
70 #ifdef HAVE_NETINET_IN_H
71 #include <netinet/in.h>
72 #endif
73 #ifdef HAVE_ARPA_INET_H
74 #include <arpa/inet.h>
75 #endif
76 #ifdef HAVE_IFADDRS_H
77 #include <ifaddrs.h>
78 #endif
79 
80 /* own headers (part (1/2) */
81 #include "uuid_mac.h"
82 
83 #ifndef FALSE
84 #define FALSE 0
85 #endif
86 #ifndef TRUE
87 #define TRUE (/*lint -save -e506*/ !FALSE /*lint -restore*/)
88 #endif
89 
90 /* return the Media Access Control (MAC) address of
91    the FIRST network interface card (NIC) */
mac_address(unsigned char * data_ptr,size_t data_len)92 int mac_address(unsigned char *data_ptr, size_t data_len)
93 {
94     /* sanity check arguments */
95     if (data_ptr == NULL || data_len < MAC_LEN)
96         return FALSE;
97 
98 #if defined(HAVE_IFADDRS_H) && defined(HAVE_NET_IF_DL_H) && defined(HAVE_GETIFADDRS)
99     /* use getifaddrs(3) on BSD class platforms (xxxBSD, MacOS X, etc) */
100     {
101         struct ifaddrs *ifap;
102         struct ifaddrs *ifap_head;
103         const struct sockaddr_dl *sdl;
104         unsigned char *ucp;
105         int i;
106 
107         if (getifaddrs(&ifap_head) < 0)
108             return FALSE;
109         for (ifap = ifap_head; ifap != NULL; ifap = ifap->ifa_next) {
110             if (ifap->ifa_addr != NULL && ifap->ifa_addr->sa_family == AF_LINK) {
111                 sdl = (const struct sockaddr_dl *)(void *)ifap->ifa_addr;
112                 ucp = (unsigned char *)(sdl->sdl_data + sdl->sdl_nlen);
113                 if (sdl->sdl_alen > 0) {
114                     for (i = 0; i < MAC_LEN && i < sdl->sdl_alen; i++, ucp++)
115                         data_ptr[i] = (unsigned char)(*ucp & 0xff);
116                     freeifaddrs(ifap_head);
117                     return TRUE;
118                 }
119             }
120         }
121         freeifaddrs(ifap_head);
122     }
123 #endif
124 
125 #if defined(HAVE_NET_IF_H) && defined(SIOCGIFHWADDR)
126     /* use SIOCGIFHWADDR ioctl(2) on Linux class platforms */
127     {
128         struct ifreq ifr;
129         struct sockaddr *sa;
130         int s;
131         int i;
132 
133         if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
134             return FALSE;
135         sprintf(ifr.ifr_name, "eth0");
136         if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) {
137             close(s);
138             return FALSE;
139         }
140         sa = (struct sockaddr *)&ifr.ifr_addr;
141         for (i = 0; i < MAC_LEN; i++)
142             data_ptr[i] = (unsigned char)(sa->sa_data[i] & 0xff);
143         close(s);
144         return TRUE;
145     }
146 #endif
147 
148 #if defined(SIOCGARP)
149     /* use SIOCGARP ioctl(2) on SVR4 class platforms (Solaris, etc) */
150     {
151         char hostname[MAXHOSTNAMELEN];
152         struct hostent *he;
153         struct arpreq ar;
154         struct sockaddr_in *sa;
155         int s;
156         int i;
157 
158         if (gethostname(hostname, sizeof(hostname)) < 0)
159             return FALSE;
160         if ((he = gethostbyname(hostname)) == NULL)
161             return FALSE;
162         if ((s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
163             return FALSE;
164         memset(&ar, 0, sizeof(ar));
165         sa = (struct sockaddr_in *)((void *)&(ar.arp_pa));
166         sa->sin_family = AF_INET;
167         memcpy(&(sa->sin_addr), *(he->h_addr_list), sizeof(struct in_addr));
168         if (ioctl(s, SIOCGARP, &ar) < 0) {
169             close(s);
170             return FALSE;
171         }
172         close(s);
173         if (!(ar.arp_flags & ATF_COM))
174             return FALSE;
175         for (i = 0; i < MAC_LEN; i++)
176             data_ptr[i] = (unsigned char)(ar.arp_ha.sa_data[i] & 0xff);
177         return TRUE;
178     }
179 #endif
180 
181     return FALSE;
182 }
183 
184