12 # include <winsock2.h> 13 # include <ws2tcpip.h> 33 # define EAGAIN WSATRY_AGAIN 34 # define EINTR WSAEINTR 35 # define EINPROGRESS WSAEINPROGRESS 36 # define EWOULDBLOCK WSAEWOULDBLOCK 37 # define ETIMEDOUT WSAETIMEDOUT 42 # include <sys/types.h> 43 # include <sys/socket.h> 44 # include <netinet/in.h> 50 # include <arpa/inet.h> 57 #if defined(__APPLE__) || defined(__MACH__) 59 # define MSG_NOSIGNAL SO_NOSIGPIPE 73 static bool wsInit =
false;
76 WORD wVersionRequested = MAKEWORD( 2, 0 );
78 WSAStartup(wVersionRequested, &wsaData);
95 return (err == EINPROGRESS || err == EAGAIN || err == EWOULDBLOCK || err == EINTR);
110 #if defined(_WINDOWS) 123 #if defined(_WINDOWS) 124 unsigned long flag = 1;
125 return (ioctlsocket((SOCKET)fd, FIONBIO, &flag) == 0);
127 return (fcntl(fd, F_SETFL, O_NONBLOCK) == 0);
137 return (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (
const char *)&sflag,
sizeof(sflag)) == 0);
147 memset(&ss, 0,
sizeof(ss));
151 sockaddr_in6 *address = (sockaddr_in6 *)&ss;
152 ss_len =
sizeof(sockaddr_in6);
154 address->sin6_family = AF_INET6;
155 address->sin6_addr = in6addr_any;
156 address->sin6_port = htons((u_short) port);
160 sockaddr_in *address = (sockaddr_in *)&ss;
161 ss_len =
sizeof(sockaddr_in);
163 address->sin_family = AF_INET;
164 address->sin_addr.s_addr = htonl(INADDR_ANY);
165 address->sin_port = htons((u_short) port);
168 return (::
bind(fd, (sockaddr*)&ss, ss_len) == 0);
176 return (::
listen(fd, backlog) == 0);
183 struct sockaddr_in addr;
184 #if defined(_WINDOWS) 189 addrlen =
sizeof(addr);
193 return (
int)
::accept(fd, (
struct sockaddr*)&addr, &addrlen);
204 memset(&ss, 0,
sizeof(ss));
206 struct addrinfo* addr;
207 struct addrinfo hints;
208 memset(&hints, 0,
sizeof(hints));
209 hints.ai_family = AF_UNSPEC;
210 int getaddr_err = getaddrinfo(host.c_str(), NULL, &hints, &addr);
211 if (0 != getaddr_err) {
212 if(getaddr_err == EAI_SYSTEM) {
215 XmlRpcUtil::error(
"Couldn't find an %s address for [%s]: %s\n",
s_use_ipv6_ ?
"AF_INET6" :
"AF_INET", host.c_str(), gai_strerror(getaddr_err));
221 struct addrinfo* it = addr;
223 for (; it; it = it->ai_next)
227 sockaddr_in *address = (sockaddr_in *)&ss;
228 ss_len =
sizeof(sockaddr_in);
230 memcpy(address, it->ai_addr, it->ai_addrlen);
231 address->sin_family = it->ai_family;
232 address->sin_port = htons((u_short) port);
234 XmlRpcUtil::log(5,
"found host as %s\n", inet_ntoa(address->sin_addr));
240 sockaddr_in6 *address = (sockaddr_in6 *)&ss;
241 ss_len =
sizeof(sockaddr_in6);
243 memcpy(address, it->ai_addr, it->ai_addrlen);
244 address->sin6_family = it->ai_family;
245 address->sin6_port = htons((u_short) port);
249 XmlRpcUtil::log(5,
"found ipv6 host as %s\n", inet_ntop(AF_INET6, (
void*)&(address->sin6_addr), buf,
sizeof(buf)));
265 int result =
::connect(fd, (sockaddr*)&ss, ss_len);
270 #if defined(_WINDOWS) 271 if (error != EWOULDBLOCK) {
273 if (error != EINPROGRESS) {
291 const int READ_SIZE = 4096;
292 char readBuf[READ_SIZE];
294 bool wouldBlock =
false;
297 while ( ! wouldBlock && ! *eof) {
298 #if defined(_WINDOWS) 299 int n = recv(fd, readBuf, READ_SIZE-1, 0);
301 int n = read(fd, readBuf, READ_SIZE-1);
307 s.append(readBuf, n);
324 int nToWrite = int(s.length()) - *bytesSoFar;
325 char *sp =
const_cast<char*
>(s.c_str()) + *bytesSoFar;
326 bool wouldBlock =
false;
328 while ( nToWrite > 0 && ! wouldBlock ) {
329 #if defined(_WINDOWS) 330 int n = send(fd, sp, nToWrite, 0);
332 int n = write(fd, sp, nToWrite);
334 XmlRpcUtil::log(5,
"XmlRpcSocket::nbWrite: send/write returned %d.", n);
354 #if defined(_WINDOWS) 355 return WSAGetLastError();
375 strerror_s(err,60,error);
377 snprintf(err,
sizeof(err),
"%s",strerror(error));
379 return std::string(err);
385 socklen_t ss_len =
sizeof(ss);
386 if(getsockname(socket, (sockaddr *)&ss, &ss_len) == 0) {
387 sockaddr_in *sin = (sockaddr_in *)&ss;
388 sockaddr_in6 *sin6 = (sockaddr_in6 *)&ss;
390 switch (ss.ss_family)
393 return ntohs(sin->sin_port);
395 return ntohs(sin6->sin6_port);
static bool setReuseAddr(int socket)
static int get_port(int socket)
static int accept(int socket)
Accept a client connection request.
static void error(const char *fmt,...)
Dump error messages somewhere.
static bool nbRead(int socket, std::string &s, bool *eof)
Read text from the specified socket. Returns false on error.
static std::string getErrorMsg()
Returns message corresponding to last error.
static bool nbWrite(int socket, const std::string &s, int *bytesSoFar)
Write text to the specified socket. Returns false on error.
static bool bind(int socket, int port)
Bind to a specified port.
static int getError()
Returns last errno.
static bool connect(int socket, const std::string &host, int port)
Connect a socket to a server (from a client)
static bool listen(int socket, int backlog)
Set socket in listen mode.
static int socket()
Creates a stream (TCP) socket. Returns -1 on failure.
static void close(int socket)
Closes a socket.
static bool setNonBlocking(int socket)
Sets a stream (TCP) socket to perform non-blocking IO. Returns false on failure.
static bool nonFatalError()
static void log(int level, const char *fmt,...)
Dump messages somewhere.