00001
00002
00003
00004
00005 #include "XmlRpcSocket.h"
00006 #include "XmlRpcUtil.h"
00007
00008 #ifndef MAKEDEPEND
00009
00010 #if defined(_WINDOWS)
00011 # include <stdio.h>
00012 # include <winsock2.h>
00013 # include <ws2tcpip.h>
00014
00015
00016
00017
00018 #ifdef EAGAIN
00019 #undef EAGAIN
00020 #endif
00021 #ifdef EINTR
00022 #undef EINTR
00023 #endif
00024 #ifdef EINPROGRESS
00025 #undef EINPROGRESS
00026 #endif
00027 #ifdef EWOULDBLOCK
00028 #undef EWOULDBLOCK
00029 #endif
00030 #ifdef ETIMEDOUT
00031 #undef ETIMEDOUT
00032 #endif
00033 # define EAGAIN WSATRY_AGAIN
00034 # define EINTR WSAEINTR
00035 # define EINPROGRESS WSAEINPROGRESS
00036 # define EWOULDBLOCK WSAEWOULDBLOCK
00037 # define ETIMEDOUT WSAETIMEDOUT
00038 #else
00039 extern "C" {
00040 # include <unistd.h>
00041 # include <stdio.h>
00042 # include <sys/types.h>
00043 # include <sys/socket.h>
00044 # include <netinet/in.h>
00045 # include <netdb.h>
00046 # include <errno.h>
00047 # include <fcntl.h>
00048 # include <string.h>
00049 # include <stdlib.h>
00050 # include <arpa/inet.h>
00051 }
00052 #endif // _WINDOWS
00053
00054 #endif // MAKEDEPEND
00055
00056
00057 #if defined(__APPLE__) || defined(__MACH__)
00058 # ifndef MSG_NOSIGNAL
00059 # define MSG_NOSIGNAL SO_NOSIGPIPE
00060 # endif
00061 #endif
00062
00063
00064 using namespace XmlRpc;
00065
00066
00067 bool XmlRpcSocket::s_use_ipv6_ = false;
00068
00069 #if defined(_WINDOWS)
00070
00071 static void initWinSock()
00072 {
00073 static bool wsInit = false;
00074 if (! wsInit)
00075 {
00076 WORD wVersionRequested = MAKEWORD( 2, 0 );
00077 WSADATA wsaData;
00078 WSAStartup(wVersionRequested, &wsaData);
00079 wsInit = true;
00080 }
00081 }
00082
00083 #else
00084
00085 #define initWinSock()
00086
00087 #endif // _WINDOWS
00088
00089
00090
00091 static inline bool
00092 nonFatalError()
00093 {
00094 int err = XmlRpcSocket::getError();
00095 return (err == EINPROGRESS || err == EAGAIN || err == EWOULDBLOCK || err == EINTR);
00096 }
00097
00098 int
00099 XmlRpcSocket::socket()
00100 {
00101 initWinSock();
00102 return (int) ::socket(s_use_ipv6_ ? AF_INET6 : AF_INET, SOCK_STREAM, 0);
00103 }
00104
00105
00106 void
00107 XmlRpcSocket::close(int fd)
00108 {
00109 XmlRpcUtil::log(4, "XmlRpcSocket::close: fd %d.", fd);
00110 #if defined(_WINDOWS)
00111 closesocket(fd);
00112 #else
00113 ::close(fd);
00114 #endif // _WINDOWS
00115 }
00116
00117
00118
00119
00120 bool
00121 XmlRpcSocket::setNonBlocking(int fd)
00122 {
00123 #if defined(_WINDOWS)
00124 unsigned long flag = 1;
00125 return (ioctlsocket((SOCKET)fd, FIONBIO, &flag) == 0);
00126 #else
00127 return (fcntl(fd, F_SETFL, O_NONBLOCK) == 0);
00128 #endif // _WINDOWS
00129 }
00130
00131
00132 bool
00133 XmlRpcSocket::setReuseAddr(int fd)
00134 {
00135
00136 int sflag = 1;
00137 return (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&sflag, sizeof(sflag)) == 0);
00138 }
00139
00140
00141
00142 bool
00143 XmlRpcSocket::bind(int fd, int port)
00144 {
00145 sockaddr_storage ss;
00146 socklen_t ss_len;
00147 memset(&ss, 0, sizeof(ss));
00148
00149 if (s_use_ipv6_)
00150 {
00151 sockaddr_in6 *address = (sockaddr_in6 *)&ss;
00152 ss_len = sizeof(sockaddr_in6);
00153
00154 address->sin6_family = AF_INET6;
00155 address->sin6_addr = in6addr_any;
00156 address->sin6_port = htons((u_short) port);
00157 }
00158 else
00159 {
00160 sockaddr_in *address = (sockaddr_in *)&ss;
00161 ss_len = sizeof(sockaddr_in);
00162
00163 address->sin_family = AF_INET;
00164 address->sin_addr.s_addr = htonl(INADDR_ANY);
00165 address->sin_port = htons((u_short) port);
00166 }
00167
00168 return (::bind(fd, (sockaddr*)&ss, ss_len) == 0);
00169 }
00170
00171
00172
00173 bool
00174 XmlRpcSocket::listen(int fd, int backlog)
00175 {
00176 return (::listen(fd, backlog) == 0);
00177 }
00178
00179
00180 int
00181 XmlRpcSocket::accept(int fd)
00182 {
00183 struct sockaddr_in addr;
00184 #if defined(_WINDOWS)
00185 int
00186 #else
00187 socklen_t
00188 #endif
00189 addrlen = sizeof(addr);
00190
00191
00192
00193 return (int) ::accept(fd, (struct sockaddr*)&addr, &addrlen);
00194 }
00195
00196
00197
00198
00199 bool
00200 XmlRpcSocket::connect(int fd, std::string& host, int port)
00201 {
00202 sockaddr_storage ss;
00203 socklen_t ss_len;
00204 memset(&ss, 0, sizeof(ss));
00205
00206 struct addrinfo* addr;
00207 struct addrinfo hints;
00208 memset(&hints, 0, sizeof(hints));
00209 hints.ai_family = AF_UNSPEC;
00210 if (getaddrinfo(host.c_str(), NULL, &hints, &addr) != 0)
00211 {
00212 fprintf(stderr, "Couldn't find an %s address for [%s]\n", s_use_ipv6_ ? "AF_INET6" : "AF_INET", host.c_str());
00213 return false;
00214 }
00215
00216 bool found = false;
00217 struct addrinfo* it = addr;
00218
00219 for (; it; it = it->ai_next)
00220 {
00221 if (!s_use_ipv6_ && it->ai_family == AF_INET)
00222 {
00223 sockaddr_in *address = (sockaddr_in *)&ss;
00224 ss_len = sizeof(sockaddr_in);
00225
00226 memcpy(address, it->ai_addr, it->ai_addrlen);
00227 address->sin_family = it->ai_family;
00228 address->sin_port = htons((u_short) port);
00229
00230 XmlRpcUtil::log(5, "found host as %s\n", inet_ntoa(address->sin_addr));
00231 found = true;
00232 break;
00233 }
00234 if (s_use_ipv6_ && it->ai_family == AF_INET6)
00235 {
00236 sockaddr_in6 *address = (sockaddr_in6 *)&ss;
00237 ss_len = sizeof(sockaddr_in6);
00238
00239 memcpy(address, it->ai_addr, it->ai_addrlen);
00240 address->sin6_family = it->ai_family;
00241 address->sin6_port = htons((u_short) port);
00242
00243 char buf[128];
00244
00245 XmlRpcUtil::log(5, "found ipv6 host as %s\n", inet_ntop(AF_INET6, (void*)&(address->sin6_addr), buf, sizeof(buf)));
00246 found = true;
00247 break;
00248 }
00249
00250 }
00251
00252 if (!found)
00253 {
00254 printf("Couldn't find an %s address for [%s]\n", s_use_ipv6_ ? "AF_INET6" : "AF_INET", host.c_str());
00255 freeaddrinfo(addr);
00256 return false;
00257 }
00258
00259
00260
00261 int result = ::connect(fd, (sockaddr*)&ss, ss_len);
00262 if (result != 0 ) {
00263 int error = getError();
00264 if ( (error != EINPROGRESS) && error != EWOULDBLOCK) {
00265 printf("::connect error = %d\n", getError());
00266 }
00267 }
00268
00269 freeaddrinfo(addr);
00270
00271 return result == 0 || nonFatalError();
00272 }
00273
00274
00275
00276
00277 bool
00278 XmlRpcSocket::nbRead(int fd, std::string& s, bool *eof)
00279 {
00280 const int READ_SIZE = 4096;
00281 char readBuf[READ_SIZE];
00282
00283 bool wouldBlock = false;
00284 *eof = false;
00285
00286 while ( ! wouldBlock && ! *eof) {
00287 #if defined(_WINDOWS)
00288 int n = recv(fd, readBuf, READ_SIZE-1, 0);
00289 #else
00290 int n = read(fd, readBuf, READ_SIZE-1);
00291 #endif
00292 XmlRpcUtil::log(5, "XmlRpcSocket::nbRead: read/recv returned %d.", n);
00293
00294 if (n > 0) {
00295 readBuf[n] = 0;
00296 s.append(readBuf, n);
00297 } else if (n == 0) {
00298 *eof = true;
00299 } else if (nonFatalError()) {
00300 wouldBlock = true;
00301 } else {
00302 return false;
00303 }
00304 }
00305 return true;
00306 }
00307
00308
00309
00310 bool
00311 XmlRpcSocket::nbWrite(int fd, std::string& s, int *bytesSoFar)
00312 {
00313 int nToWrite = int(s.length()) - *bytesSoFar;
00314 char *sp = const_cast<char*>(s.c_str()) + *bytesSoFar;
00315 bool wouldBlock = false;
00316
00317 while ( nToWrite > 0 && ! wouldBlock ) {
00318 #if defined(_WINDOWS)
00319 int n = send(fd, sp, nToWrite, 0);
00320 #else
00321 int n = write(fd, sp, nToWrite);
00322 #endif
00323 XmlRpcUtil::log(5, "XmlRpcSocket::nbWrite: send/write returned %d.", n);
00324
00325 if (n > 0) {
00326 sp += n;
00327 *bytesSoFar += n;
00328 nToWrite -= n;
00329 } else if (nonFatalError()) {
00330 wouldBlock = true;
00331 } else {
00332 return false;
00333 }
00334 }
00335 return true;
00336 }
00337
00338
00339
00340 int
00341 XmlRpcSocket::getError()
00342 {
00343 #if defined(_WINDOWS)
00344 return WSAGetLastError();
00345 #else
00346 return errno;
00347 #endif
00348 }
00349
00350
00351
00352 std::string
00353 XmlRpcSocket::getErrorMsg()
00354 {
00355 return getErrorMsg(getError());
00356 }
00357
00358
00359 std::string
00360 XmlRpcSocket::getErrorMsg(int error)
00361 {
00362 char err[60];
00363 #ifdef _MSC_VER
00364 strerror_s(err,60,error);
00365 #else
00366 snprintf(err,sizeof(err),"%s",strerror(error));
00367 #endif
00368 return std::string(err);
00369 }
00370
00371 int XmlRpcSocket::get_port(int socket)
00372 {
00373 sockaddr_storage ss;
00374 socklen_t ss_len = sizeof(ss);
00375 getsockname(socket, (sockaddr *)&ss, &ss_len);
00376
00377 sockaddr_in *sin = (sockaddr_in *)&ss;
00378 sockaddr_in6 *sin6 = (sockaddr_in6 *)&ss;
00379
00380 switch (ss.ss_family)
00381 {
00382 case AF_INET:
00383 return ntohs(sin->sin_port);
00384 case AF_INET6:
00385 return ntohs(sin6->sin6_port);
00386 }
00387 return 0;
00388 }
00389