39#include "blocxx/BLOCXX_config.h"
41#if !defined(BLOCXX_WIN32) && !defined(BLOCXX_NETWARE)
45#ifdef BLOCXX_HAVE_UNISTD_H
53#ifdef BLOCXX_HAVE_SYS_SOCKET_H
54 #include <sys/socket.h>
60#ifdef BLOCXX_GNU_LINUX
61 #include <sys/ioctl.h>
65#elif defined (BLOCXX_OPENSERVER)
69 #include <netinet/in.h>
71 #include <arpa/inet.h>
76#elif defined (BLOCXX_DARWIN)
78 #include <sys/ioctl.h>
81 #ifdef BLOCXX_HAVE_STROPTS_H
86 #include <netinet/in.h>
88 #if defined (BLOCXX_HAVE_SYS_SOCKIO_H)
89 #include <sys/sockio.h>
113 struct sockaddr_in *sin(0);
114 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
119 bzero(&ifr,
sizeof(ifr));
120 strncpy(ifr.ifr_name,
m_name.c_str(),
sizeof(ifr.ifr_name));
123 if (ioctl(s, SIOCGIFADDR, &ifr) < 0)
129 sin =
reinterpret_cast<struct sockaddr_in *
>(&ifr.ifr_addr);
130 m_addr = sin->sin_addr.s_addr;
134 if (ioctl(s, SIOCGIFBRDADDR, &ifr) < 0)
140 sin =
reinterpret_cast<struct sockaddr_in*
>(&ifr.ifr_broadaddr);
144 if (ioctl(s, SIOCGIFNETMASK, &ifr) < 0)
150#ifdef BLOCXX_GNU_LINUX
151 sin =
reinterpret_cast<struct sockaddr_in *
>(&ifr.ifr_netmask);
153 sin =
reinterpret_cast<struct sockaddr_in *
>(&ifr.ifr_broadaddr);
224 return inet_addr(straddr.
c_str());
233 struct ifreq *ifr(0);
234 struct ifreq ifrcopy;
238 const char* appliesTo(0);
242 ifc.ifc_len =
sizeof(
struct ifreq) * numreqs;
243 if (ifc.ifc_buf == NULL)
245 ifc.ifc_buf = new char[ifc.ifc_len];
249 p = new char[ifc.ifc_len];
250 memmove(p, ifc.ifc_buf, oldlen);
251 delete [] ifc.ifc_buf;
254 oldlen = ifc.ifc_len;
255 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
258 appliesTo =
"ioctl:SIOCGIFCONF";
261 if (ifc.ifc_len ==
static_cast<int>(
sizeof(
struct ifreq) * numreqs))
272 appliesTo =
"No interfaces found";
274 for (n = 0; n < ifc.ifc_len; n +=
sizeof(
struct ifreq))
277 if (ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy) < 0)
280 appliesTo =
"ioctl:SIOCGIFFLAGS";
283#ifdef BLOCXX_GNU_LINUX
284 if ((ifrcopy.ifr_flags & IFF_UP) && !(ifrcopy.ifr_flags & (IFF_LOOPBACK | IFF_DYNAMIC)))
286 if ((ifrcopy.ifr_flags & IFF_UP))
289 m_name = ifr->ifr_name;
296 if (ifc.ifc_buf != NULL)
298 delete [] ifc.ifc_buf;
#define BLOCXX_THROW(exType, msg)
Throw an exception using FILE and LINE.
String getIPAddressString()
unsigned long m_bcastAddr
unsigned long getBroadcastAddress()
String getBroadcastAddressString()
String getNetmaskString()
unsigned long getNetmask()
void getInterfaceName(SocketHandle_t sockfd)
bool sameNetwork(unsigned long addr)
static unsigned long stringToAddress(const String &straddr)
unsigned long getIPAddress()
This String class is an abstract data type that represents as NULL terminated string of characters.
const char * c_str() const
String inetAddrToString(UInt64 addr)