--- uuid_mac.c 2004/01/10 17:01:22 1.1
+++ uuid_mac.c 2004/01/10 18:50:24 1.2
@@ -40,13 +40,6 @@
#include <sys/time.h>
#include <sys/types.h>
-#ifdef HAVE_IFADDRS_H
-#include <ifaddrs.h>
-#endif
-#ifdef HAVE_NET_IF_DL_H
-#include <net/if_dl.h>
-#endif
-
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
@@ -59,9 +52,21 @@
#ifdef HAVE_NET_IF_H
#include <net/if.h>
#endif
+#ifdef HAVE_NET_IF_DL_H
+#include <net/if_dl.h>
+#endif
+#ifdef HAVE_IF_ARP_H
+#include <net/if_arp.h>
+#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
+#ifdef HAVE_ARPA_INET_H
+#include <arpa/inet.h>
+#endif
+#ifdef HAVE_IFADDRS_H
+#include <ifaddrs.h>
+#endif
#ifndef FALSE
#define FALSE 0
@@ -79,7 +84,7 @@
return FALSE;
#if defined(HAVE_IFADDRS_H) && defined(HAVE_NET_IF_DL_H) && defined(HAVE_GETIFADDRS)
- /* use getifaddrs(3) on BSD class platforms */
+ /* use getifaddrs(3) on BSD class platforms (xxxBSD, MacOS X, etc) */
{
struct ifaddrs *ifap;
struct ifaddrs *ifap_head;
@@ -87,7 +92,7 @@
unsigned char *ucp;
int i;
- if (getifaddrs(&ifap_head) != 0)
+ if (getifaddrs(&ifap_head) < 0)
return FALSE;
for (ifap = ifap_head; ifap != NULL; ifap = ifap->ifa_next) {
if (ifap->ifa_addr != NULL && ifap->ifa_addr->sa_family == AF_LINK) {
@@ -95,7 +100,7 @@
ucp = (unsigned char *)(sdl->sdl_data + sdl->sdl_nlen);
if (ucp != NULL && sdl->sdl_alen > 0) {
for (i = 0; i < MAC_LEN && i < sdl->sdl_alen; i++, ucp++)
- data_ptr[i] = (*ucp & 0xff);
+ data_ptr[i] = (unsigned char)(*ucp & 0xff);
freeifaddrs(ifap_head);
return TRUE;
}
@@ -105,6 +110,57 @@
}
#endif
+#if defined(HAVE_NET_IF_H) && defined(SIOCGIFHWADDR)
+ /* use SIOCGIFHWADDR ioctl(2) on Linux class platforms */
+ {
+ struct ifreq ifr;
+ struct sockaddr *sa;
+ int s;
+
+ if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
+ return FALSE;
+ sprintf(ifr.ifr_name, "eth0");
+ if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) {
+ close(s);
+ return FALSE;
+ }
+ sa = (struct sockaddr *)&ifr.ifr_addr;
+ for (i = 0; i < MAC_LEN; i++)
+ data_ptr[i] = (unsigned char)(sa->sa_data[i] & 0xff);
+ close(s);
+ return TRUE;
+ }
+#endif
+
+#if defined(SIOCGARP)
+ /* use SIOCGARP ioctl(2) on SVR4 class platforms (Solaris, etc) */
+ {
+ char hostname[MAXHOSTNAMELEN];
+ struct hostent *he;
+ struct arpreq ar;
+ struct sockaddr_in *sa;
+
+ if (gethostname(hostname, sizeof(hostname)) < 0)
+ return FALSE;
+ if ((he = gethostbyname(hostname)) < 0)
+ return FALSE;
+ if ((s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
+ return FALSE;
+ memset(&ar, '\0', sizeof(ar));
+ sa = (struct sockaddr_in *)&(ar.arp_pa);
+ sa->sin_family = AF_INET;
+ memcpy(&(sa->sin_addr), *(he->h_addr_list), sizeof(struct in_addr));
+ if (ioctl(s, SIOCGARP, &ar) < 0) {
+ close(s);
+ return FALSE;
+ }
+ for (i = 0; i < MAC_LEN; i++)
+ data_ptr[i] = (unsigned char)(ar.arp_ha.sa_data[i] & 0xff);
+ close(s);
+ return TRUE;
+ }
+#endif
+
return FALSE;
}
|