39 #include "blocxx/BLOCXX_config.h"
50 #if defined(BLOCXX_WIN32)
54 #ifndef BLOCXX_HAVE_GETHOSTBYNAME_R
61 #if !defined(BLOCXX_WIN32)
65 #include <sys/types.h>
68 #include <sys/socket.h>
69 #ifdef BLOCXX_HAVE_SYS_RESOURCE_H
70 #include <sys/resource.h>
73 #include <arpa/inet.h>
93 iaddr.sin_family = AF_INET;
94 iaddr.sin_addr.s_addr = addr;
96 #ifdef BLOCXX_HAVE_IPV6
97 char buf[INET6_ADDRSTRLEN];
98 String s(inet_ntop(iaddr.sin_family, &(iaddr.sin_addr), buf,
sizeof(buf)));
100 String s(inet_ntoa(iaddr.sin_addr));
106 #if defined(BLOCXX_WIN32)
118 TimeoutTimer timer(classTimeout);
120 DWORD timeout= timer.asDWORDMs();
122 if (networkEvents != -1L)
124 if(::WSAEventSelect(fd, eventArg, networkEvents) != 0)
127 Format(
"WSAEventSelect failed in waitForIO: %1",
137 events[1] = eventArg;
139 DWORD index = ::WaitForMultipleObjects(
154 index -= WAIT_OBJECT_0;
158 ::ResetEvent(eventArg);
171 switch(::WaitForSingleObject(eventArg, timeout))
174 ::ResetEvent(eventArg);
187 if(::WSAEventSelect(fd, eventArg, 0) != 0)
190 Format(
"Resetting socket with WSAEventSelect failed: %1",
194 ::ioctlsocket(fd, FIONBIO, &ioctlarg);
240 pipefd = lUPipe->getInputHandle();
262 if (selarray[1].readAvailable)
267 if (selarray[0].writeAvailable || selarray[0].readAvailable)
280 #ifndef BLOCXX_HAVE_GETHOSTBYNAME_R
283 namespace SocketUtils {
289 char hostName [2048];
290 if (gethostname (hostName,
sizeof(hostName)) == 0)
292 #ifndef BLOCXX_HAVE_GETHOSTBYNAME_R
295 if ((he = gethostbyname (hostName)) != 0)
305 hostent* host = &hostbuf;
306 #if (BLOCXX_GETHOSTBYNAME_R_ARGUMENTS == 6 || BLOCXX_GETHOSTBYNAME_R_ARGUMENTS == 5)
309 #elif (BLOCXX_GETHOSTBYNAME_R_ARGUMENTS == 3)
310 hostent_data hostdata;
313 #error Not yet supported: gethostbyname_r() with other argument counts.
319 for (
int i = 0;
i < 10 && (!worked || host == 0); ++
i)
321 #if (BLOCXX_GETHOSTBYNAME_R_ARGUMENTS == 6)
322 if (gethostbyname_r(hostName, &hostbuf, buf,
sizeof(buf),
323 &host, &h_err) != -1)
328 #elif (BLOCXX_GETHOSTBYNAME_R_ARGUMENTS == 5)
330 if ((host = gethostbyname_r(hostName, &hostbuf, buf,
sizeof(buf), &h_err))) {
334 #elif (BLOCXX_GETHOSTBYNAME_R_ARGUMENTS == 3)
335 if (gethostbyname_r(hostName, &hostbuf, &hostdata) == 0)
345 #error Not yet supported: gethostbyname_r() with other argument counts.
348 if (worked && host != 0)
369 struct hostent *hostentp;
370 char bfr[1024], ipaddrstr[128];
371 struct in_addr iaHost;
373 if(gethostname(bfr,
sizeof(bfr)-1) == SOCKET_ERROR)
376 Format(
"SocketUtils::getFullyQualifiedHostName: gethostname failed: %1(%2)",
386 if((hostentp = gethostbyname(bfr)) == NULL)
389 Format(
"SocketUtils::getFullyQualifiedHostName: gethostbyname"
390 " failed: %1(%2)", WSAGetLastError(),
394 if(strchr(hostentp->h_name,
'.'))
396 rv = hostentp->h_name;
401 addr.sin_family = AF_INET;
403 memcpy(&addr.sin_addr, hostentp->h_addr_list[0],
sizeof(addr.sin_addr));
404 #ifdef BLOCXX_HAVE_IPV6
405 char buf[INET6_ADDRSTRLEN];
406 rv = inet_ntop(addr.sin_family, &(addr.sin_addr), buf,
sizeof(buf));
408 rv = inet_ntoa(addr.sin_addr);
411 iaHost.s_addr = inet_addr(rv.
c_str());
412 if(iaHost.s_addr != INADDR_NONE)
414 hostentp = gethostbyaddr((
const char*)&iaHost,
415 sizeof(
struct in_addr), AF_INET);
418 if(strchr(hostentp->h_name,
'.'))
421 rv = hostentp->h_name;
Array<> wraps std::vector<> in COWReference<> adding ref counting and copy on write capability...
IntrusiveReference< U > cast_to() const
bool waitForWrite
Input parameter. Set it to true to indicate that waiting for write availability on s is desired...
String getFullyQualifiedHostName()
Get the fully qualified host name.
#define BLOCXX_ASSERT(CON)
BLOCXX_ASSERT works similar to the assert() macro, but instead of calling abort(), it throws an AssertionException.
This String class is an abstract data type that represents as NULL terminated string of characters...
String lastErrorMsg(bool socketError)
static Timeout relative(float seconds)
const int SELECT_TIMEOUT
The value returned from select when the timeout value has expired.
A timeout can be absolute, which means that it will happen at the specified DateTime.
const char * c_str() const
bool waitForRead
Input parameter. Set it to true to indicate that waiting for read availability on s is desired...
void push_back(const T &x)
Append an element to the end of the Array.
static ShutDownMechanism_t getShutDownMechanism()
#define BLOCXX_THROW(exType, msg)
Throw an exception using FILE and LINE.
int waitForIO(SocketHandle_t fd, int timeOutSecs, SocketFlags::EWaitDirectionFlag waitFlag)
Wait for input or output on a socket.
int selectRW(SelectObjectArray &selarray, UInt32 ms)
String inetAddrToString(UInt64 addr)