| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | #include <config.h> |
|---|
| 24 | |
|---|
| 25 | #include "safeerrno.h" |
|---|
| 26 | #include "safefcntl.h" |
|---|
| 27 | |
|---|
| 28 | #include "tcpclient.h" |
|---|
| 29 | #include <xapian/error.h> |
|---|
| 30 | |
|---|
| 31 | #include <string.h> |
|---|
| 32 | #ifndef __WIN32__ |
|---|
| 33 | # include <netdb.h> |
|---|
| 34 | # include <netinet/in.h> |
|---|
| 35 | # include <netinet/tcp.h> |
|---|
| 36 | # include <sys/socket.h> |
|---|
| 37 | # include "safesysselect.h" |
|---|
| 38 | #endif |
|---|
| 39 | |
|---|
| 40 | #include "utils.h" |
|---|
| 41 | |
|---|
| 42 | std::string |
|---|
| 43 | TcpClient::get_tcpcontext(const std::string & hostname, int port) |
|---|
| 44 | { |
|---|
| 45 | return "remote:tcp(" + hostname + ":" + om_tostring(port) + ")"; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | int |
|---|
| 49 | TcpClient::open_socket(const std::string & hostname, int port, |
|---|
| 50 | int msecs_timeout_connect) |
|---|
| 51 | { |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | struct hostent *host = gethostbyname(hostname.c_str()); |
|---|
| 57 | |
|---|
| 58 | if (host == 0) { |
|---|
| 59 | throw Xapian::NetworkError(std::string("Couldn't resolve host ") + hostname, |
|---|
| 60 | get_tcpcontext(hostname, port), |
|---|
| 61 | #ifdef __WIN32__ |
|---|
| 62 | socket_errno() |
|---|
| 63 | #else |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | (h_errno < 0 ? errno : -h_errno) |
|---|
| 70 | #endif |
|---|
| 71 | ); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | int socketfd = socket(PF_INET, SOCK_STREAM, 0); |
|---|
| 75 | |
|---|
| 76 | if (socketfd < 0) { |
|---|
| 77 | throw Xapian::NetworkError("Couldn't create socket", get_tcpcontext(hostname, port), socket_errno()); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | struct sockaddr_in remaddr; |
|---|
| 81 | remaddr.sin_family = AF_INET; |
|---|
| 82 | remaddr.sin_port = htons(port); |
|---|
| 83 | memcpy(&remaddr.sin_addr, host->h_addr, sizeof(remaddr.sin_addr)); |
|---|
| 84 | |
|---|
| 85 | #ifdef __WIN32__ |
|---|
| 86 | ULONG enabled = 1; |
|---|
| 87 | int rc = ioctlsocket(socketfd, FIONBIO, &enabled); |
|---|
| 88 | #else |
|---|
| 89 | int rc = fcntl(socketfd, F_SETFL, O_NDELAY); |
|---|
| 90 | #endif |
|---|
| 91 | if (rc < 0) { |
|---|
| 92 | int saved_errno = socket_errno(); |
|---|
| 93 | close(socketfd); |
|---|
| 94 | throw Xapian::NetworkError("Couldn't set O_NDELAY", get_tcpcontext(hostname, port), saved_errno); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | { |
|---|
| 98 | int optval = 1; |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | if (setsockopt(socketfd, IPPROTO_TCP, TCP_NODELAY, |
|---|
| 102 | reinterpret_cast<char *>(&optval), |
|---|
| 103 | sizeof(optval)) < 0) { |
|---|
| 104 | int saved_errno = socket_errno(); |
|---|
| 105 | close(socketfd); |
|---|
| 106 | throw Xapian::NetworkError("Couldn't set TCP_NODELAY", get_tcpcontext(hostname, port), saved_errno); |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | int retval = connect(socketfd, reinterpret_cast<sockaddr *>(&remaddr), |
|---|
| 111 | sizeof(remaddr)); |
|---|
| 112 | |
|---|
| 113 | if (retval < 0) { |
|---|
| 114 | #ifdef __WIN32__ |
|---|
| 115 | if (WSAGetLastError() != WSAEWOULDBLOCK) { |
|---|
| 116 | #else |
|---|
| 117 | if (socket_errno() != EINPROGRESS) { |
|---|
| 118 | #endif |
|---|
| 119 | int saved_errno = socket_errno(); |
|---|
| 120 | close(socketfd); |
|---|
| 121 | throw Xapian::NetworkError("Couldn't connect", get_tcpcontext(hostname, port), saved_errno); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | fd_set fdset; |
|---|
| 126 | FD_ZERO(&fdset); |
|---|
| 127 | FD_SET(socketfd, &fdset); |
|---|
| 128 | |
|---|
| 129 | struct timeval tv; |
|---|
| 130 | tv.tv_sec = msecs_timeout_connect / 1000; |
|---|
| 131 | tv.tv_usec = msecs_timeout_connect % 1000 * 1000; |
|---|
| 132 | |
|---|
| 133 | retval = select(socketfd + 1, 0, &fdset, &fdset, &tv); |
|---|
| 134 | |
|---|
| 135 | if (retval == 0) { |
|---|
| 136 | close(socketfd); |
|---|
| 137 | throw Xapian::NetworkTimeoutError("Couldn't connect", get_tcpcontext(hostname, port), ETIMEDOUT); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | int err = 0; |
|---|
| 141 | SOCKLEN_T len = sizeof(err); |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | retval = getsockopt(socketfd, SOL_SOCKET, SO_ERROR, |
|---|
| 146 | reinterpret_cast<char *>(&err), &len); |
|---|
| 147 | |
|---|
| 148 | if (retval < 0) { |
|---|
| 149 | int saved_errno = socket_errno(); |
|---|
| 150 | close(socketfd); |
|---|
| 151 | throw Xapian::NetworkError("Couldn't get socket options", get_tcpcontext(hostname, port), saved_errno); |
|---|
| 152 | } |
|---|
| 153 | if (err) { |
|---|
| 154 | close(socketfd); |
|---|
| 155 | throw Xapian::NetworkError("Couldn't connect", get_tcpcontext(hostname, port), err); |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | #ifdef __WIN32__ |
|---|
| 160 | enabled = 0; |
|---|
| 161 | ioctlsocket(socketfd, FIONBIO, &enabled); |
|---|
| 162 | #else |
|---|
| 163 | fcntl(socketfd, F_SETFL, 0); |
|---|
| 164 | #endif |
|---|
| 165 | return socketfd; |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | TcpClient::~TcpClient() |
|---|
| 169 | { |
|---|
| 170 | do_close(); |
|---|
| 171 | } |
|---|