39 #include "blocxx/BLOCXX_config.h"
41 #if !defined(BLOCXX_WIN32) && !defined(BLOCXX_NETWARE)
45 #ifdef BLOCXX_HAVE_UNISTD_H
51 #include <sys/types.h>
53 #ifdef BLOCXX_HAVE_SYS_SOCKET_H
54 #include <sys/socket.h>
57 #include <arpa/inet.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>
105 namespace BLOCXX_NAMESPACE
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))
296 if (ifc.ifc_buf != NULL)
298 delete [] ifc.ifc_buf;
302 BLOCXX_THROW(SocketException,
"NwIface::getInterfaceName");
308 #endif // #if !defined(BLOCXX_WIN32) && !defined(BLOCXX_NETWARE)
static unsigned long stringToAddress(const String &straddr)
void getInterfaceName(SocketHandle_t sockfd)
BLOCXX_COMMON_API int close(const FileHandle &hdl)
Close file handle.
bool sameNetwork(unsigned long addr)
This String class is an abstract data type that represents as NULL terminated string of characters...
unsigned long getNetmask()
String getNetmaskString()
unsigned long getIPAddress()
unsigned long getBroadcastAddress()
String getIPAddressString()
unsigned long m_bcastAddr
const char * c_str() const
String getBroadcastAddressString()
#define BLOCXX_THROW(exType, msg)
Throw an exception using FILE and LINE.
String inetAddrToString(UInt64 addr)