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)
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);
216 Select::SelectObject so(fd);
219 so.waitForRead =
true;
223 so.waitForWrite =
true;
227 so.waitForRead =
true;
228 so.waitForWrite =
true;
231 selarray.push_back(so);
238 lUPipe = foo.cast_to<PosixUnnamedPipe>();
240 pipefd = lUPipe->getInputHandle();
244 so = Select::SelectObject(pipefd);
245 so.waitForRead =
true;
246 selarray.push_back(so);
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)
301 BLOCXX_THROW(SocketException, Format(
"SocketUtils::getFullyQualifiedHostName: gethostbyname failed: %1", h_errno).c_str());
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)
354 BLOCXX_THROW(SocketException, Format(
"SocketUtils::getFullyQualifiedHostName: gethostbyname_r(%1) failed: %2", hostName, h_err).c_str());
360 BLOCXX_THROW(SocketException, Format(
"SocketUtils::getFullyQualifiedHostName: gethostname failed: %1(%2)", errno, strerror(errno)).c_str());
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;