+ 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;
+ struct ifconf ifc;
+ char buf[1024];
+ int n, i;
+ unsigned char *a;
+#ifdef AF_LINK
+ struct sockaddr_dl *sdlp;
+#endif
+
+//
+// BSD 4.4 defines the size of an ifreq to be
+// max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
+// However, under earlier systems, sa_len isn't present, so the size is
+// just sizeof(struct ifreq)
+// We should investiage the use of SIZEOF_ADDR_IFREQ
+//
+#ifdef HAVE_SA_LEN
+#ifndef max
+#define max(a,b) ((a) > (b) ? (a) : (b))
+#endif
+#define ifreq_size(i) max(sizeof(struct ifreq),\
+ sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
+#else
+#define ifreq_size(i) sizeof(struct ifreq)
+#endif // HAVE_SA_LEN
+
+ if( (sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0 )
+ {
+ return -1;
+ }
+ memset(buf, 0, sizeof(buf));
+ ifc.ifc_len = sizeof(buf);
+ ifc.ifc_buf = buf;
+ if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0)
+ {
+ close(sd);
+ return -1;
+ }
+ n = ifc.ifc_len;
+ for (i = 0; i < n; i+= ifreq_size(*ifrp) )
+ {
+ ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
+ strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
+#ifdef SIOCGIFHWADDR
+ if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
+ continue;
+ 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;
+#else
+#ifdef AF_LINK
+ sdlp = (struct sockaddr_dl *) &ifrp->ifr_addr;
+ if ((sdlp->sdl_family != AF_LINK) || (sdlp->sdl_alen != 6))
+ continue;
+ a = (unsigned char *) &sdlp->sdl_data[sdlp->sdl_nlen];
+#else
+ /*
+ * XXX we don't have a way of getting the hardware
+ * address
+ */
+ close(sd);
+ return -1;
+#endif // AF_LINK
+#endif // SIOCGENADDR
+#endif // SIOCGIFHWADDR
+ if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5]) continue;
+
+ if (addr)
+ {
+ memcpy(addr, a, 6);
+ close(sd);
+ return 0;
+ }
+ }
+ close(sd);
+#endif
+ return -1;
+#endif //__sun
+
+}
+
+std::string Util::GetMACAddress()
+{
+ // 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;
+
+ long stat = GetMacAddrSys(addr);
+ if (0 == stat)
+ {
+ //printf( "MAC address = ");
+ for (int i=0; i<6; ++i)
+ {
+ //printf("%2.2x", addr[i]);
+ macaddr += Format("%2.2x", addr[i]);
+ }
+ //printf( "\n");
+ return macaddr;
+ }
+ else
+ {
+ //printf( "No MAC address !\n" );
+ return "";
+ }
+}
+
+/**
+ * \ingroup Util
+ * \brief Return the IP adress of the machine writting the DICOM image
+ */
+std::string Util::GetIPAddress()
+{
+ // This is a rip from
+ // http://www.codeguru.com/Cpp/I-N/internet/network/article.php/c3445/
+#ifndef HOST_NAME_MAX
+ // SUSv2 guarantees that `Host names are limited to 255 bytes'.
+ // POSIX 1003.1-2001 guarantees that `Host names (not including the
+ // terminating NUL) are limited to HOST_NAME_MAX bytes'.
+# define HOST_NAME_MAX 255
+ // In this case we should maybe check the string was not truncated.
+ // But I don't known how to check that...
+#if defined(_MSC_VER) || defined(__BORLANDC__)
+ // with WinSock DLL we need to initialise the WinSock before using gethostname
+ WORD wVersionRequested = MAKEWORD(1,0);
+ WSADATA WSAData;
+ int err = WSAStartup(wVersionRequested,&WSAData);
+ if (err != 0)
+ {
+ // Tell the user that we could not find a usable
+ // WinSock DLL.
+ WSACleanup();
+ return "127.0.0.1";
+ }
+#endif
+
+#endif //HOST_NAME_MAX
+
+ std::string str;
+ char szHostName[HOST_NAME_MAX+1];
+ int r = gethostname(szHostName, HOST_NAME_MAX);
+
+ if( r == 0 )
+ {
+ // Get host adresses
+ struct hostent *pHost = gethostbyname(szHostName);
+
+ for( int i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
+ {
+ for( int j = 0; j<pHost->h_length; j++ )
+ {
+ if( j > 0 ) str += ".";
+
+ str += Util::Format("%u",
+ (unsigned int)((unsigned char*)pHost->h_addr_list[i])[j]);
+ }
+ // str now contains one local IP address
+
+#if defined(_MSC_VER) || defined(__BORLANDC__)
+ WSACleanup();
+#endif
+
+ }
+ }
+ // If an error occur r == -1
+ // Most of the time it will return 127.0.0.1...
+ return str;
+}
+
+/**
+ * \ingroup Util
+ * \brief Creates a new UID. As stipulate in the DICOM ref
+ * each time a DICOM image is create it should have
+ * a unique identifier (URI)
+ */
+std::string Util::CreateUniqueUID(const std::string &root)
+{
+ // The code works as follow:
+ // echo "gdcm" | od -b
+ // 0000000 147 144 143 155 012
+ // Therefore we return
+ // radical + 147.144.143.155 + IP + time()
+ std::string radical = root;
+ if( !root.size() ) //anything better ?
+ {
+ radical = "0.0."; // Is this really usefull ?
+ }
+ // else
+ // A root was specified use it to forge our new UID:
+ radical += "147.144.143.155"; // gdcm
+ radical += ".";
+ radical += Util::GetIPAddress();
+ radical += ".";
+ radical += Util::GetCurrentDate();
+ radical += ".";
+ radical += Util::GetCurrentTime();
+
+ return radical;
+}
+
+template <class T>
+std::ostream &binary_write(std::ostream &os, const T &val)
+{
+ return os.write(reinterpret_cast<const char*>(&val), sizeof val);
+}
+
+std::ostream &binary_write(std::ostream &os, const uint16_t &val)
+{
+#ifdef GDCM_WORDS_BIGENDIAN
+ uint16_t swap;
+ swap = ((( val << 8 ) & 0x0ff00 ) | (( val >> 8 ) & 0x00ff ) );
+ return os.write(reinterpret_cast<const char*>(&swap), 2);
+#else
+ return os.write(reinterpret_cast<const char*>(&val), 2);
+#endif //GDCM_WORDS_BIGENDIAN
+}
+
+std::ostream &binary_write(std::ostream &os, const uint32_t &val)
+{
+#ifdef GDCM_WORDS_BIGENDIAN
+ uint32_t swap;
+ swap = ( ((val<<24) & 0xff000000) | ((val<<8) & 0x00ff0000) |
+ ((val>>8) & 0x0000ff00) | ((val>>24) & 0x000000ff) );
+ return os.write(reinterpret_cast<const char*>(&swap), 4);
+#else
+ return os.write(reinterpret_cast<const char*>(&val), 4);
+#endif //GDCM_WORDS_BIGENDIAN
+}
+
+std::ostream &binary_write(std::ostream &os, const char *val)
+{
+ return os.write(val, strlen(val));
+}
+
+std::ostream &binary_write(std::ostream &os, std::string const &val)
+{
+ return os.write(val.c_str(), val.size());
+}
+
+} // end namespace gdcm
+