blob: 32fc2e77cd513cbbe92ec27679238038293200a6 [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
Marc Slemkoe03da182006-07-21 21:32:36 +00007#include <config.h>
Mark Sleee8540632006-05-30 09:24:40 +00008#include <sys/socket.h>
9#include <arpa/inet.h>
10#include <netinet/in.h>
11#include <netinet/tcp.h>
12#include <netdb.h>
13#include <unistd.h>
14#include <errno.h>
Mark Slee29050782006-09-29 00:12:30 +000015#include <fcntl.h>
16#include <sys/select.h>
Mark Sleee8540632006-05-30 09:24:40 +000017
Mark Slee29050782006-09-29 00:12:30 +000018#include "concurrency/Monitor.h"
Marc Slemkod42a2c22006-08-10 03:30:18 +000019#include "TSocket.h"
20#include "TTransportException.h"
Mark Sleee8540632006-05-30 09:24:40 +000021
Marc Slemko6f038a72006-08-03 18:58:09 +000022namespace facebook { namespace thrift { namespace transport {
23
Mark Sleee8540632006-05-30 09:24:40 +000024using namespace std;
Mark Slee29050782006-09-29 00:12:30 +000025using namespace facebook::thrift::concurrency;
Mark Sleee8540632006-05-30 09:24:40 +000026
Mark Slee29050782006-09-29 00:12:30 +000027// Global var to track total socket sys calls
Mark Slee8d7e1f62006-06-07 06:48:56 +000028uint32_t g_socket_syscalls = 0;
29
30/**
31 * TSocket implementation.
32 *
33 * @author Mark Slee <mcslee@facebook.com>
34 */
35
Mark Sleee8540632006-05-30 09:24:40 +000036// Mutex to protect syscalls to netdb
Mark Slee29050782006-09-29 00:12:30 +000037static Monitor s_netdb_monitor;
Mark Sleee8540632006-05-30 09:24:40 +000038
39// TODO(mcslee): Make this an option to the socket class
40#define MAX_RECV_RETRIES 20
Mark Slee29050782006-09-29 00:12:30 +000041
42TSocket::TSocket(string host, int port) :
43 host_(host),
44 port_(port),
Martin Kraemeree341cb2007-02-05 21:40:38 +000045 socket_(-1),
Mark Slee29050782006-09-29 00:12:30 +000046 connTimeout_(0),
47 sendTimeout_(0),
48 recvTimeout_(0),
49 lingerOn_(1),
50 lingerVal_(0),
51 noDelay_(1) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000052 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
53 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
Mark Sleee8540632006-05-30 09:24:40 +000054}
55
Aditya Agarwalebc99e02007-01-15 23:14:58 +000056TSocket::TSocket() :
57 host_(""),
58 port_(0),
Martin Kraemeree341cb2007-02-05 21:40:38 +000059 socket_(-1),
Aditya Agarwalebc99e02007-01-15 23:14:58 +000060 connTimeout_(0),
61 sendTimeout_(0),
62 recvTimeout_(0),
63 lingerOn_(1),
64 lingerVal_(0),
65 noDelay_(1) {
66 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
67 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
68}
69
Mark Slee29050782006-09-29 00:12:30 +000070TSocket::TSocket(int socket) :
71 host_(""),
72 port_(0),
73 socket_(socket),
74 connTimeout_(0),
75 sendTimeout_(0),
76 recvTimeout_(0),
77 lingerOn_(1),
78 lingerVal_(0),
79 noDelay_(1) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000080 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
81 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
Mark Slee29050782006-09-29 00:12:30 +000082}
83
Mark Sleee8540632006-05-30 09:24:40 +000084TSocket::~TSocket() {
85 close();
86}
87
Mark Slee8d7e1f62006-06-07 06:48:56 +000088bool TSocket::isOpen() {
Martin Kraemeree341cb2007-02-05 21:40:38 +000089 return (socket_ >= 0);
Mark Slee8d7e1f62006-06-07 06:48:56 +000090}
91
Mark Sleeb9ff32a2006-11-16 01:00:24 +000092bool TSocket::peek() {
93 if (!isOpen()) {
94 return false;
95 }
96 uint8_t buf;
97 int r = recv(socket_, &buf, 1, MSG_PEEK);
98 if (r == -1) {
99 perror("TSocket::peek()");
100 close();
Mark Sleef9831082007-02-20 20:59:21 +0000101 throw TTransportException(TTransportException::UNKNOWN, "recv() ERROR:" + errno);
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000102 }
103 return (r > 0);
104}
105
Mark Slee8d7e1f62006-06-07 06:48:56 +0000106void TSocket::open() {
Mark Sleea9848d72007-02-21 04:54:05 +0000107 if (isOpen()) {
108 throw TTransportException(TTransportException::ALREADY_OPEN);
109 }
110
Mark Sleee8540632006-05-30 09:24:40 +0000111 // Create socket
112 socket_ = socket(AF_INET, SOCK_STREAM, 0);
113 if (socket_ == -1) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000114 perror("TSocket::open() socket");
115 close();
Mark Sleef9831082007-02-20 20:59:21 +0000116 throw TTransportException(TTransportException::NOT_OPEN, "socket() ERROR:" + errno);
Mark Sleee8540632006-05-30 09:24:40 +0000117 }
Mark Slee29050782006-09-29 00:12:30 +0000118
119 // Send timeout
120 if (sendTimeout_ > 0) {
121 setSendTimeout(sendTimeout_);
122 }
123
124 // Recv timeout
125 if (recvTimeout_ > 0) {
126 setRecvTimeout(recvTimeout_);
127 }
128
129 // Linger
130 setLinger(lingerOn_, lingerVal_);
131
132 // No delay
133 setNoDelay(noDelay_);
134
Mark Slee8d7e1f62006-06-07 06:48:56 +0000135 // Lookup the hostname
Mark Sleee8540632006-05-30 09:24:40 +0000136 struct sockaddr_in addr;
137 addr.sin_family = AF_INET;
138 addr.sin_port = htons(port_);
139
Mark Sleee8540632006-05-30 09:24:40 +0000140 {
Mark Slee29050782006-09-29 00:12:30 +0000141 // Scope lock on host entry lookup
142 Synchronized s(s_netdb_monitor);
Mark Sleee8540632006-05-30 09:24:40 +0000143 struct hostent *host_entry = gethostbyname(host_.c_str());
144
145 if (host_entry == NULL) {
Mark Slee29050782006-09-29 00:12:30 +0000146 perror("TSocket: dns error: failed call to gethostbyname.");
Mark Sleee8540632006-05-30 09:24:40 +0000147 close();
Mark Sleef9831082007-02-20 20:59:21 +0000148 throw TTransportException(TTransportException::NOT_OPEN, "gethostbyname() failed");
Mark Sleee8540632006-05-30 09:24:40 +0000149 }
150
151 addr.sin_port = htons(port_);
152 memcpy(&addr.sin_addr.s_addr,
153 host_entry->h_addr_list[0],
154 host_entry->h_length);
155 }
Mark Slee29050782006-09-29 00:12:30 +0000156
157 // Set the socket to be non blocking for connect if a timeout exists
158 int flags = fcntl(socket_, F_GETFL, 0);
159 if (connTimeout_ > 0) {
Mark Sleea5a783f2007-03-02 19:41:08 +0000160 if (-1 == fcntl(socket_, F_SETFL, flags | O_NONBLOCK)) {
161 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed");
162 }
Mark Slee29050782006-09-29 00:12:30 +0000163 } else {
Mark Sleea5a783f2007-03-02 19:41:08 +0000164 if (-1 == fcntl(socket_, F_SETFL, flags & ~O_NONBLOCK)) {
165 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed");
166 }
Mark Slee29050782006-09-29 00:12:30 +0000167 }
168
169 // Conn timeout
170 struct timeval c = {(int)(connTimeout_/1000),
171 (int)((connTimeout_%1000)*1000)};
Mark Sleee8540632006-05-30 09:24:40 +0000172
173 // Connect the socket
174 int ret = connect(socket_, (struct sockaddr *)&addr, sizeof(addr));
175
Mark Slee29050782006-09-29 00:12:30 +0000176 if (ret == 0) {
177 goto done;
178 }
179
180 if (errno != EINPROGRESS) {
Mark Sleee8540632006-05-30 09:24:40 +0000181 close();
Mark Slee29050782006-09-29 00:12:30 +0000182 char buff[1024];
183 sprintf(buff, "TSocket::open() connect %s %d", host_.c_str(), port_);
184 perror(buff);
Mark Sleef9831082007-02-20 20:59:21 +0000185 throw TTransportException(TTransportException::NOT_OPEN, "open() ERROR: " + errno);
Mark Sleee8540632006-05-30 09:24:40 +0000186 }
187
Mark Slee29050782006-09-29 00:12:30 +0000188 fd_set fds;
189 FD_ZERO(&fds);
190 FD_SET(socket_, &fds);
191 ret = select(socket_+1, NULL, &fds, NULL, &c);
192
193 if (ret > 0) {
194 // Ensure connected
195 int val;
196 socklen_t lon;
197 lon = sizeof(int);
198 int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon);
199 if (ret2 == -1) {
200 close();
201 perror("TSocket::open() getsockopt SO_ERROR");
Mark Sleef9831082007-02-20 20:59:21 +0000202 throw TTransportException(TTransportException::NOT_OPEN, "open() ERROR: " + errno);
Mark Slee29050782006-09-29 00:12:30 +0000203 }
204 if (val == 0) {
205 goto done;
206 }
207 close();
208 perror("TSocket::open() SO_ERROR was set");
Mark Sleef9831082007-02-20 20:59:21 +0000209 throw TTransportException(TTransportException::NOT_OPEN, "open() ERROR: " + errno);
Mark Slee29050782006-09-29 00:12:30 +0000210 } else if (ret == 0) {
211 close();
212 perror("TSocket::open() timeed out");
Mark Sleef9831082007-02-20 20:59:21 +0000213 throw TTransportException(TTransportException::NOT_OPEN, "open() ERROR: " + errno);
Mark Slee29050782006-09-29 00:12:30 +0000214 } else {
215 close();
216 perror("TSocket::open() select error");
Mark Sleef9831082007-02-20 20:59:21 +0000217 throw TTransportException(TTransportException::NOT_OPEN, "open() ERROR: " + errno);
Mark Slee29050782006-09-29 00:12:30 +0000218 }
219
220 done:
221 // Set socket back to normal mode (blocking)
222 fcntl(socket_, F_SETFL, flags);
Mark Sleee8540632006-05-30 09:24:40 +0000223}
224
225void TSocket::close() {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000226 if (socket_ >= 0) {
Mark Sleee8540632006-05-30 09:24:40 +0000227 shutdown(socket_, SHUT_RDWR);
228 ::close(socket_);
229 }
Martin Kraemeree341cb2007-02-05 21:40:38 +0000230 socket_ = -1;
Mark Sleee8540632006-05-30 09:24:40 +0000231}
232
Mark Slee8d7e1f62006-06-07 06:48:56 +0000233uint32_t TSocket::read(uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000234 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000235 throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000236 }
Mark Sleee8540632006-05-30 09:24:40 +0000237
Mark Sleee8540632006-05-30 09:24:40 +0000238 uint32_t retries = 0;
Mark Slee8d7e1f62006-06-07 06:48:56 +0000239
240 try_again:
241 // Read from the socket
242 int got = recv(socket_, buf, len, 0);
243 ++g_socket_syscalls;
244
245 // Check for error on read
Mark Sleec4257802007-01-24 23:14:30 +0000246 if (got < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000247 // If temporarily out of resources, sleep a bit and try again
248 if (errno == EAGAIN && retries++ < MAX_RECV_RETRIES) {
249 usleep(50);
250 goto try_again;
Mark Sleee8540632006-05-30 09:24:40 +0000251 }
252
Mark Slee8d7e1f62006-06-07 06:48:56 +0000253 // If interrupted, try again
254 if (errno == EINTR && retries++ < MAX_RECV_RETRIES) {
255 goto try_again;
Mark Sleee8540632006-05-30 09:24:40 +0000256 }
257
Mark Sleec4257802007-01-24 23:14:30 +0000258 // Now it's not a try again case, but a real probblez
259 perror("TSocket::read()");
260
Mark Slee8d7e1f62006-06-07 06:48:56 +0000261 // If we disconnect with no linger time
262 if (errno == ECONNRESET) {
Mark Sleef9831082007-02-20 20:59:21 +0000263 throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000264 }
265
266 // This ish isn't open
267 if (errno == ENOTCONN) {
Mark Sleef9831082007-02-20 20:59:21 +0000268 throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000269 }
270
271 // Timed out!
272 if (errno == ETIMEDOUT) {
Mark Sleef9831082007-02-20 20:59:21 +0000273 throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000274 }
275
276 // Some other error, whatevz
Mark Slee5f68c712007-05-11 17:58:54 +0000277 char buff[1024];
278 sprintf(buff, "ERROR errno: %d", errno);
279 throw TTransportException(TTransportException::UNKNOWN, buff);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000280 }
281
282 // The remote host has closed the socket
283 if (got == 0) {
284 close();
285 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000286 }
287
288 // Pack data into string
Mark Slee8d7e1f62006-06-07 06:48:56 +0000289 return got;
Mark Sleee8540632006-05-30 09:24:40 +0000290}
291
Mark Slee8d7e1f62006-06-07 06:48:56 +0000292void TSocket::write(const uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000293 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000294 throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000295 }
296
Mark Sleee8540632006-05-30 09:24:40 +0000297 uint32_t sent = 0;
298
Mark Slee8d7e1f62006-06-07 06:48:56 +0000299 while (sent < len) {
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000300
301 int flags = 0;
Mark Slee29050782006-09-29 00:12:30 +0000302 #ifdef MSG_NOSIGNAL
Mark Slee8d7e1f62006-06-07 06:48:56 +0000303 // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we
304 // check for the EPIPE return condition and close the socket in that case
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000305 flags |= MSG_NOSIGNAL;
Mark Slee29050782006-09-29 00:12:30 +0000306 #endif // ifdef MSG_NOSIGNAL
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000307
308 int b = send(socket_, buf + sent, len - sent, flags);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000309 ++g_socket_syscalls;
310
Mark Sleee8540632006-05-30 09:24:40 +0000311 // Fail on a send error
312 if (b < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000313 if (errno == EPIPE) {
314 close();
Mark Sleef9831082007-02-20 20:59:21 +0000315 throw TTransportException(TTransportException::NOT_OPEN, "EPIPE");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000316 }
317
318 if (errno == ECONNRESET) {
319 close();
Mark Sleef9831082007-02-20 20:59:21 +0000320 throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000321 }
322
323 if (errno == ENOTCONN) {
324 close();
Mark Sleef9831082007-02-20 20:59:21 +0000325 throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000326 }
327
328 perror("TSocket::write() send < 0");
Mark Sleef9831082007-02-20 20:59:21 +0000329 throw TTransportException(TTransportException::UNKNOWN, "ERROR:" + errno);
Mark Sleee8540632006-05-30 09:24:40 +0000330 }
331
332 // Fail on blocked send
333 if (b == 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000334 throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0.");
Mark Sleee8540632006-05-30 09:24:40 +0000335 }
Mark Sleee8540632006-05-30 09:24:40 +0000336 sent += b;
337 }
338}
339
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000340void TSocket::setHost(string host) {
341 host_ = host;
342}
343
344void TSocket::setPort(int port) {
345 port_ = port;
346}
347
Mark Slee8d7e1f62006-06-07 06:48:56 +0000348void TSocket::setLinger(bool on, int linger) {
Mark Slee29050782006-09-29 00:12:30 +0000349 lingerOn_ = on;
350 lingerVal_ = linger;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000351 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000352 return;
353 }
354
Mark Slee29050782006-09-29 00:12:30 +0000355 struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_};
356 int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
357 if (ret == -1) {
Mark Sleee8540632006-05-30 09:24:40 +0000358 perror("TSocket::setLinger()");
Mark Sleee8540632006-05-30 09:24:40 +0000359 }
Mark Sleee8540632006-05-30 09:24:40 +0000360}
361
Mark Slee8d7e1f62006-06-07 06:48:56 +0000362void TSocket::setNoDelay(bool noDelay) {
Mark Slee29050782006-09-29 00:12:30 +0000363 noDelay_ = noDelay;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000364 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000365 return;
366 }
367
Mark Sleee8540632006-05-30 09:24:40 +0000368 // Set socket to NODELAY
Mark Slee29050782006-09-29 00:12:30 +0000369 int v = noDelay_ ? 1 : 0;
370 int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
371 if (ret == -1) {
Mark Sleee8540632006-05-30 09:24:40 +0000372 perror("TSocket::setNoDelay()");
Mark Sleee8540632006-05-30 09:24:40 +0000373 }
Mark Sleee8540632006-05-30 09:24:40 +0000374}
Mark Slee29050782006-09-29 00:12:30 +0000375
376void TSocket::setConnTimeout(int ms) {
377 connTimeout_ = ms;
378}
379
380void TSocket::setRecvTimeout(int ms) {
381 recvTimeout_ = ms;
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000382 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
383 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
Martin Kraemeree341cb2007-02-05 21:40:38 +0000384 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000385 return;
386 }
387
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000388 // Copy because select may modify
389 struct timeval r = recvTimeval_;
Mark Slee29050782006-09-29 00:12:30 +0000390 int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r));
391 if (ret == -1) {
392 perror("TSocket::setRecvTimeout()");
393 }
394}
395
396void TSocket::setSendTimeout(int ms) {
397 sendTimeout_ = ms;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000398 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000399 return;
400 }
401
402 struct timeval s = {(int)(sendTimeout_/1000),
403 (int)((sendTimeout_%1000)*1000)};
404 int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s));
405 if (ret == -1) {
406 perror("TSocket::setSendTimeout()");
407 }
408}
409
Marc Slemko6f038a72006-08-03 18:58:09 +0000410}}} // facebook::thrift::transport