Program: gdcm
Module: $RCSfile: gdcmUtil.cxx,v $
Language: C++
- Date: $Date: 2005/01/15 20:24:02 $
- Version: $Revision: 1.92 $
+ Date: $Date: 2005/01/15 22:40:23 $
+ Version: $Revision: 1.96 $
Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
l'Image). All rights reserved. See Doc/License.txt or
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
-#include <fcntl.h>
-//#include <errno.h>
#include <sys/types.h>
-#include <sys/time.h>
-#include <sys/stat.h>
-#include <sys/file.h>
+#endif
+
+// How do I do that in CMake ?
+#ifdef __APPLE__
+#define HAVE_SA_LEN
+#define CMAKE_HAVE_NET_IF_DL_H
+#define CMAKE_HAVE_NETINET_IN_H
+#define CMAKE_HAVE_NET_IF_H
+#endif //APPLE
+
#ifdef CMAKE_HAVE_SYS_IOCTL_H
#include <sys/ioctl.h> // For SIOCGIFCONF on Linux
#endif
#ifdef CMAKE_HAVE_NET_IF_DL_H
#include <net/if_dl.h>
#endif
-
-// How do I do that in CMake ?
-#ifdef __APPLE__
-#define HAVE_SA_LEN
-#endif //APPLE
-
-#endif //_WIN32
+#if defined(CMAKE_HAVE_NET_IF_ARP_H) && defined(__sun)
+// This is absolutely necesseray on SunOS
+#include <net/if_arp.h>
+#endif
namespace gdcm
{
SNMP_FreeVarBind(&varBind[0]);
SNMP_FreeVarBind(&varBind[1]);
return 0;
-#else
+#endif //Win32 version
+
+
// implementation for POSIX system
+#ifdef __sun
+ //The POSIX version is broken anyway on Solaris, plus would require full
+ //root power
+ int i;
+ struct arpreq parpreq;
+ struct sockaddr_in sa, *psa;
+ struct in_addr inaddr;
+ struct hostent *phost;
+ char hostname[MAXHOSTNAMELEN];
+ unsigned char *ptr;
+ char **paddrs;
+ int sock, status=0;
+
+ gethostname(hostname, MAXHOSTNAMELEN);
+ phost = gethostbyname(hostname);
+ paddrs = phost->h_addr_list;
+
+ //memcpy(&inaddr.s_addr, *paddrs, sizeof(inaddr.s_addr));
+ sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+
+ if(sock == -1)
+ {
+ perror("sock");
+ return -1;
+ }
+ memset(&parpreq, 0, sizeof(struct arpreq));
+ psa = (struct sockaddr_in *) &parpreq.arp_pa;
+ memset(psa, 0, sizeof(struct sockaddr_in));
+ psa->sin_family = AF_INET;
+ memcpy(&psa->sin_addr, *paddrs, sizeof(struct in_addr));
+
+ status = ioctl(sock, SIOCGARP, &parpreq);
+
+ if(status == -1)
+ {
+ perror("SIOCGARP");
+ //exit(-1);
+ return -1;
+ }
+
+ memcpy(addr, parpreq.arp_ha.sa_data, 6);
+// printf("MAC Address: %x:%x:%x:%x:%x:%x\n",
+//
+// parpreq.arp_ha.sa_data[0],
+// parpreq.arp_ha.sa_data[1],
+// parpreq.arp_ha.sa_data[2],
+// parpreq.arp_ha.sa_data[3],
+// parpreq.arp_ha.sa_data[4],
+//
+// parpreq.arp_ha.sa_data[5]);
+
+ return 0;
+#else
#ifdef CMAKE_HAVE_NET_IF_H
int sd;
struct ifreq ifr, *ifrp;
a = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
#else
#ifdef SIOCGENADDR
+ // In theory this call should also work on Sun Solaris, but apparently
+ // SIOCGENADDR is not implemented properly thus the call
+ // ioctl(sd, SIOCGENADDR, &ifr) always returns errno=2
+ // (No such file or directory)
+ // Furthermore the DLAPI seems to require full root access
if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
continue;
a = (unsigned char *) ifr.ifr_enaddr;
close(sd);
#endif
return -1;
-#endif //_WIN32
+#endif //__sun
}
std::string Util::GetMACAddress()
{
- // This is a rip from: http://cplus.kompf.de/macaddr.html for Linux/CYGWIN, HPUX and AIX
- // and http://tangentsoft.net/wskfaq/examples/src/snmpmac.cpp for windows version
- // and http://groups-beta.google.com/group/sol.lists.freebsd.hackers/msg/0d0f862e05fce6c0 for the FreeBSD version
- // and http://developer.apple.com/samplecode/GetPrimaryMACAddress/GetPrimaryMACAddress.html for MacOSX version
+ // This code is the result of a long internet search to find something
+ // as compact as possible (not OS independant). We only have to separate
+ // 3 OS: Win32, SunOS and 'real' POSIX
+ // http://groups-beta.google.com/group/comp.unix.solaris/msg/ad36929d783d63be
+ // http://bdn.borland.com/article/0,1410,26040,00.html
u_char addr[6];
std::string macaddr;