39 #include "blocxx/BLOCXX_config.h" 50 #if !defined(BLOCXX_WIN32) 52 #include <netinet/in.h> 53 #include <arpa/inet.h> 54 #include <sys/param.h> 55 #include <sys/utsname.h> 69 const char *inet_ntop(
int af,
const void *src,
char *dst,
socklen_t cnt)
73 struct sockaddr_in in;
74 memset(&in, 0,
sizeof(in));
75 in.sin_family = AF_INET;
76 memcpy(&in.sin_addr, src,
sizeof(
struct in_addr));
77 getnameinfo((
struct sockaddr *)&in,
sizeof(
struct sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
80 else if (af == AF_INET6)
82 struct sockaddr_in6 in;
83 memset(&in, 0,
sizeof(in));
84 in.sin6_family = AF_INET6;
85 memcpy(&in.sin6_addr, src,
sizeof(
struct in_addr6));
86 getnameinfo((
struct sockaddr *)&in,
sizeof(
struct sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
98 #if !defined(BLOCXX_WIN32) 116 #ifdef BLOCXX_SOLARIS 118 offsetof(
struct sockaddr_un, sun_path);
119 #elif defined BLOCXX_OPENUNIX 122 offsetof(
struct sockaddr_un, sun_path);
123 #elif defined BLOCXX_AIX || defined BLOCXX_DARWIN 127 offsetof(
struct sockaddr_un, sun_path) + 1;
128 #elif defined BLOCXX_FREEBSD 139 #endif // #if !defined(BLOCXX_WIN32) 147 #ifndef BLOCXX_HAVE_GETHOSTBYNAME_R 156 #ifdef BLOCXX_HAVE_IPV6 159 memset(&sa, 0,
sizeof(sa));
160 if( inet_pton(AF_INET6, hostName.
c_str(), (
void*)&(reinterpret_cast<sockaddr_in6*>(&sa)->sin6_addr)))
162 reinterpret_cast<sockaddr_in6*
>(&sa)->sin6_family = AF_INET6;
163 reinterpret_cast<sockaddr_in6*
>(&sa)->sin6_port = htons(port);
170 #if defined(BLOCXX_HAVE_GETHOSTBYNAME_R) && defined(BLOCXX_GETHOSTBYNAME_R_ARGUMENTS) 172 hostent* host = &hostbuf;
173 #if (BLOCXX_GETHOSTBYNAME_R_ARGUMENTS == 6) 176 if (gethostbyname_r(hostName.
c_str(), &hostbuf, buf,
sizeof(buf),
177 &host, &h_err) == -1)
181 #elif (BLOCXX_GETHOSTBYNAME_R_ARGUMENTS == 5) 186 host = gethostbyname_r(hostName.
c_str(), &hostbuf, buf,
sizeof(buf), &h_err);
188 #elif (BLOCXX_GETHOSTBYNAME_R_ARGUMENTS == 3) 189 hostent_data hostdata;
190 if (gethostbyname_r(hostName.
c_str(), &hostbuf, &hostdata) != 0)
196 #error Not yet supported: gethostbyname_r() with other argument counts. 200 #if defined(BLOCXX_NCR) 201 hostent* host = gethostbyname(const_cast<char *>(hostName.
c_str()));
203 hostent* host = gethostbyname(hostName.
c_str());
212 memcpy(&addr, host->h_addr_list[0],
sizeof(addr));
224 #if !defined(BLOCXX_WIN32) 232 #endif // !defined(BLOCXX_WIN32) 238 UInt16 nativePort,
const String& hostName)
241 memset(&addr, 0,
sizeof(addr));
242 reinterpret_cast<sockaddr_in*
>(&addr)->sin_family = AF_INET;
243 reinterpret_cast<sockaddr_in*
>(&addr)->sin_port =
hton16(nativePort);
244 reinterpret_cast<sockaddr_in*
>(&addr)->sin_addr = nativeForm;
258 #if !defined(BLOCXX_WIN32) 274 #if !defined(BLOCXX_WIN32) 289 addr.s_addr =
hton32(INADDR_ANY);
292 gethostname(buf,
sizeof(buf));
296 #if defined(BLOCXX_HAVE_GETHOSTBYNAME_R) && defined(BLOCXX_GETHOSTBYNAME_R_ARGUMENTS) 298 hostent* hent = &hostbuf;
299 #if (BLOCXX_GETHOSTBYNAME_R_ARGUMENTS == 6) 300 char local_buf[2048];
302 if (gethostbyname_r(buf, &hostbuf, local_buf,
sizeof(local_buf),
303 &hent, &h_err) == -1)
307 #elif (BLOCXX_GETHOSTBYNAME_R_ARGUMENTS == 5) 309 char local_buf[2048];
312 hent = gethostbyname_r(buf, &hostbuf, local_buf,
sizeof(local_buf), &h_err);
314 #elif (BLOCXX_GETHOSTBYNAME_R_ARGUMENTS == 3) 315 hostent_data hostdata;
316 if (gethostbyname_r(buf, &hostbuf, &hostdata) != 0)
322 #error Not yet supported: gethostbyname_r() with other argument counts. 326 hostent* hent = gethostbyname(buf);
328 if (hent && hent->h_name && (strlen(hent->h_name) > 0))
330 hname =
String(hent->h_name);
343 #ifdef BLOCXX_HAVE_IPV6 344 char buf[INET6_ADDRSTRLEN];
359 #if !defined(BLOCXX_WIN32) 370 #endif // !defined(BLOCXX_WIN32) 377 #ifdef BLOCXX_HAVE_IPV6 391 #if !defined(BLOCXX_WIN32) 398 #endif // !defined(BLOCXX_WIN32) 428 memset(&addr, 0,
sizeof(addr));
429 reinterpret_cast<sockaddr_in*
>(&addr)->sin_family = AF_INET;
432 #if !defined(BLOCXX_WIN32) 433 else if (type ==
UDS)
436 memset(&addr, 0,
sizeof(addr));
437 addr.sun_family = AF_UNIX;
sockaddr_un UnixSocketAddress_t
const String toString() const
Returns the IP address and the port with a colon in between.
static SocketAddress allocEmptyAddress(AddressType type)
Allocate an empty SocketAddress.
static SocketAddress getAnyLocalHost(UInt16 port=0)
Do a DNS lookup on a hostname and return a list of all addresses that map to that hostname...
const SocketAddress_t * getNativeForm() const
size_t getNativeFormSize() const
const String getAddress() const
Returns the IP address of the host.
#define BLOCXX_ASSERT(CON)
BLOCXX_ASSERT works similar to the assert() macro, but instead of calling abort(), it throws an AssertionException.
UnixSocketAddress_t m_UDSNativeAddress
This String class is an abstract data type that represents as NULL terminated string of characters...
#define BLOCXX_DEFINE_EXCEPTION_WITH_ID(NAME)
Define a new exception class named <NAME>Exception that derives from Exception.
const UnixSocketAddress_t * getUnixAddress() const
Get a pointer to the UnixSocketAddress_t precondition: getType() == UDS.
static SocketAddress getUDS(const String &filename)
static SocketAddress getFromNativeForm(const InetAddress_t &nativeForm, UInt16 nativePort, const String &hostname)
static SocketAddress getByName(const String &host, unsigned short port=0)
Do a DNS lookup on a hostname and return an SocketAddress for that host.
void assignFromNativeForm(const UnixSocketAddress_t *address, size_t len)
InetSocketAddress_t m_inetNativeAddress
static const char *const ALL_LOCAL_ADDRESSES
const char * c_str() const
const InetSocketAddress_t * getInetAddress() const
Get a pointer to the InetSocketAddress_t precondition: getType() == INET.
size_t indexOf(char ch, size_t fromIndex=0) const
Find the first occurence of a given character in this String object.
sockaddr_in InetSocketAddress_t
#define BLOCXX_THROW(exType, msg)
Throw an exception using FILE and LINE.
const String getName() const
Returns the hostname (FQDN) of the address.
UInt16 getPort() const
Get the port associated with the address.