blob: 38ca36372331735a8b0ad51406cdb8601ae9e8f2 [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
Mark Sleee8540632006-05-30 09:24:40 +00007#include <sys/socket.h>
Mark Sleea5a783f2007-03-02 19:41:08 +00008#include <sys/select.h>
Mark Sleedd564972007-08-21 02:39:57 +00009#include <sys/types.h>
Mark Sleee8540632006-05-30 09:24:40 +000010#include <netinet/in.h>
Mark Slee29050782006-09-29 00:12:30 +000011#include <netinet/tcp.h>
Mark Slee6d56eb92007-07-06 22:28:15 +000012#include <netdb.h>
Mark Sleea5a783f2007-03-02 19:41:08 +000013#include <fcntl.h>
Mark Slee8d7e1f62006-06-07 06:48:56 +000014#include <errno.h>
Mark Sleee8540632006-05-30 09:24:40 +000015
Marc Slemkod42a2c22006-08-10 03:30:18 +000016#include "TSocket.h"
17#include "TServerSocket.h"
Marc Slemko16698852006-08-04 03:16:10 +000018#include <boost/shared_ptr.hpp>
Mark Sleee8540632006-05-30 09:24:40 +000019
Mark Slee256bdc42007-11-27 08:42:19 +000020namespace facebook { namespace thrift { namespace transport {
Marc Slemko6f038a72006-08-03 18:58:09 +000021
Martin Kraemere6c4fa62007-07-09 19:08:25 +000022using namespace std;
David Reissd4a269c2007-08-23 02:37:19 +000023using boost::shared_ptr;
Marc Slemko16698852006-08-04 03:16:10 +000024
Mark Sleee8540632006-05-30 09:24:40 +000025TServerSocket::TServerSocket(int port) :
Mark Slee29050782006-09-29 00:12:30 +000026 port_(port),
Martin Kraemeree341cb2007-02-05 21:40:38 +000027 serverSocket_(-1),
Mark Slee29050782006-09-29 00:12:30 +000028 acceptBacklog_(1024),
29 sendTimeout_(0),
Mark Sleea5a783f2007-03-02 19:41:08 +000030 recvTimeout_(0),
boz1ea81ce2007-05-14 23:04:33 +000031 retryLimit_(0),
32 retryDelay_(0),
Christopher Piro9cc63b52008-03-21 00:40:42 +000033 tcpSendBuffer_(0),
34 tcpRecvBuffer_(0),
Mark Slee561b5362007-03-09 19:26:29 +000035 intSock1_(-1),
36 intSock2_(-1) {}
Mark Slee29050782006-09-29 00:12:30 +000037
38TServerSocket::TServerSocket(int port, int sendTimeout, int recvTimeout) :
39 port_(port),
Martin Kraemeree341cb2007-02-05 21:40:38 +000040 serverSocket_(-1),
Mark Slee29050782006-09-29 00:12:30 +000041 acceptBacklog_(1024),
42 sendTimeout_(sendTimeout),
Mark Sleea5a783f2007-03-02 19:41:08 +000043 recvTimeout_(recvTimeout),
boz1ea81ce2007-05-14 23:04:33 +000044 retryLimit_(0),
45 retryDelay_(0),
Christopher Piro9cc63b52008-03-21 00:40:42 +000046 tcpSendBuffer_(0),
47 tcpRecvBuffer_(0),
Mark Slee561b5362007-03-09 19:26:29 +000048 intSock1_(-1),
49 intSock2_(-1) {}
Mark Sleee8540632006-05-30 09:24:40 +000050
51TServerSocket::~TServerSocket() {
52 close();
53}
54
Mark Slee29050782006-09-29 00:12:30 +000055void TServerSocket::setSendTimeout(int sendTimeout) {
56 sendTimeout_ = sendTimeout;
57}
58
59void TServerSocket::setRecvTimeout(int recvTimeout) {
60 recvTimeout_ = recvTimeout;
61}
62
boz1ea81ce2007-05-14 23:04:33 +000063void TServerSocket::setRetryLimit(int retryLimit) {
64 retryLimit_ = retryLimit;
65}
66
67void TServerSocket::setRetryDelay(int retryDelay) {
68 retryDelay_ = retryDelay;
69}
70
Christopher Piro9cc63b52008-03-21 00:40:42 +000071void TServerSocket::setTcpSendBuffer(int tcpSendBuffer) {
72 tcpSendBuffer_ = tcpSendBuffer;
73}
74
75void TServerSocket::setTcpRecvBuffer(int tcpRecvBuffer) {
76 tcpRecvBuffer_ = tcpRecvBuffer;
77}
78
Mark Slee8d7e1f62006-06-07 06:48:56 +000079void TServerSocket::listen() {
Mark Slee561b5362007-03-09 19:26:29 +000080 int sv[2];
81 if (-1 == socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
boz6ded7752007-06-05 22:41:18 +000082 GlobalOutput("TServerSocket::init()");
Mark Slee561b5362007-03-09 19:26:29 +000083 intSock1_ = -1;
84 intSock2_ = -1;
85 } else {
Mark Sleee02385b2007-06-09 01:21:16 +000086 intSock1_ = sv[1];
87 intSock2_ = sv[0];
Mark Slee561b5362007-03-09 19:26:29 +000088 }
89
Mark Slee6d56eb92007-07-06 22:28:15 +000090 struct addrinfo hints, *res, *res0;
91 int error;
92 char port[sizeof("65536") + 1];
93 memset(&hints, 0, sizeof(hints));
94 hints.ai_family = PF_UNSPEC;
95 hints.ai_socktype = SOCK_STREAM;
Mark Slee256bdc42007-11-27 08:42:19 +000096 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
Mark Slee6d56eb92007-07-06 22:28:15 +000097 sprintf(port, "%d", port_);
98
99 // Wildcard address
100 error = getaddrinfo(NULL, port, &hints, &res0);
101 if (error) {
102 fprintf(stderr, "getaddrinfo %d: %s\n", error, gai_strerror(error));
103 close();
104 throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for server socket.");
105 }
106
107 // Pick the ipv6 address first since ipv4 addresses can be mapped
108 // into ipv6 space.
109 for (res = res0; res; res = res->ai_next) {
110 if (res->ai_family == AF_INET6 || res->ai_next == NULL)
111 break;
112 }
Mark Slee256bdc42007-11-27 08:42:19 +0000113
Mark Slee6d56eb92007-07-06 22:28:15 +0000114 serverSocket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
Mark Sleee8540632006-05-30 09:24:40 +0000115 if (serverSocket_ == -1) {
boz6ded7752007-06-05 22:41:18 +0000116 GlobalOutput("TServerSocket::listen() socket");
Mark Sleee8540632006-05-30 09:24:40 +0000117 close();
Mark Sleef9831082007-02-20 20:59:21 +0000118 throw TTransportException(TTransportException::NOT_OPEN, "Could not create server socket.");
Mark Sleee8540632006-05-30 09:24:40 +0000119 }
120
121 // Set reusaddress to prevent 2MSL delay on accept
122 int one = 1;
123 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_REUSEADDR,
124 &one, sizeof(one))) {
boz6ded7752007-06-05 22:41:18 +0000125 GlobalOutput("TServerSocket::listen() SO_REUSEADDR");
Mark Sleee8540632006-05-30 09:24:40 +0000126 close();
Mark Sleef9831082007-02-20 20:59:21 +0000127 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_REUSEADDR");
Mark Sleee8540632006-05-30 09:24:40 +0000128 }
129
Christopher Piro9cc63b52008-03-21 00:40:42 +0000130 // Set TCP buffer sizes
131 if (tcpSendBuffer_ > 0) {
132 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_SNDBUF,
133 &tcpSendBuffer_, sizeof(tcpSendBuffer_))) {
134 GlobalOutput("TServerSocket::listen() SO_SNDBUF");
135 close();
136 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_SNDBUF");
137 }
138 }
139
140 if (tcpRecvBuffer_ > 0) {
141 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_RCVBUF,
142 &tcpRecvBuffer_, sizeof(tcpRecvBuffer_))) {
143 GlobalOutput("TServerSocket::listen() SO_RCVBUF");
144 close();
145 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_RCVBUF");
146 }
147 }
148
Mark Slee29050782006-09-29 00:12:30 +0000149 // Defer accept
150 #ifdef TCP_DEFER_ACCEPT
151 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, TCP_DEFER_ACCEPT,
152 &one, sizeof(one))) {
boz6ded7752007-06-05 22:41:18 +0000153 GlobalOutput("TServerSocket::listen() TCP_DEFER_ACCEPT");
Mark Slee29050782006-09-29 00:12:30 +0000154 close();
Mark Sleef9831082007-02-20 20:59:21 +0000155 throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_DEFER_ACCEPT");
Mark Slee29050782006-09-29 00:12:30 +0000156 }
157 #endif // #ifdef TCP_DEFER_ACCEPT
158
Mark Sleee8540632006-05-30 09:24:40 +0000159 // Turn linger off, don't want to block on calls to close
160 struct linger ling = {0, 0};
161 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_LINGER,
162 &ling, sizeof(ling))) {
Mark Sleee8540632006-05-30 09:24:40 +0000163 close();
boz6ded7752007-06-05 22:41:18 +0000164 GlobalOutput("TServerSocket::listen() SO_LINGER");
Mark Sleef9831082007-02-20 20:59:21 +0000165 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_LINGER");
Mark Sleee8540632006-05-30 09:24:40 +0000166 }
167
Mark Slee29050782006-09-29 00:12:30 +0000168 // TCP Nodelay, speed over bandwidth
169 if (-1 == setsockopt(serverSocket_, IPPROTO_TCP, TCP_NODELAY,
170 &one, sizeof(one))) {
171 close();
boz6ded7752007-06-05 22:41:18 +0000172 GlobalOutput("setsockopt TCP_NODELAY");
Mark Sleef9831082007-02-20 20:59:21 +0000173 throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_NODELAY");
Mark Slee29050782006-09-29 00:12:30 +0000174 }
175
Mark Sleea5a783f2007-03-02 19:41:08 +0000176 // Set NONBLOCK on the accept socket
177 int flags = fcntl(serverSocket_, F_GETFL, 0);
178 if (flags == -1) {
179 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed");
180 }
Mark Slee561b5362007-03-09 19:26:29 +0000181
Mark Sleea5a783f2007-03-02 19:41:08 +0000182 if (-1 == fcntl(serverSocket_, F_SETFL, flags | O_NONBLOCK)) {
183 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed");
184 }
185
boz1ea81ce2007-05-14 23:04:33 +0000186 // prepare the port information
Mark Slee256bdc42007-11-27 08:42:19 +0000187 // we may want to try to bind more than once, since SO_REUSEADDR doesn't
boz1ea81ce2007-05-14 23:04:33 +0000188 // always seem to work. The client can configure the retry variables.
189 int retries = 0;
190 do {
Mark Slee6d56eb92007-07-06 22:28:15 +0000191 if (0 == bind(serverSocket_, res->ai_addr, res->ai_addrlen)) {
boz1ea81ce2007-05-14 23:04:33 +0000192 break;
193 }
194
195 // use short circuit evaluation here to only sleep if we need to
196 } while ((retries++ < retryLimit_) && (sleep(retryDelay_) == 0));
197
Mark Slee6d56eb92007-07-06 22:28:15 +0000198 // free addrinfo
199 freeaddrinfo(res0);
Mark Slee256bdc42007-11-27 08:42:19 +0000200
boz1ea81ce2007-05-14 23:04:33 +0000201 // throw an error if we failed to bind properly
202 if (retries > retryLimit_) {
Mark Sleee8540632006-05-30 09:24:40 +0000203 char errbuf[1024];
204 sprintf(errbuf, "TServerSocket::listen() BIND %d", port_);
boz6ded7752007-06-05 22:41:18 +0000205 GlobalOutput(errbuf);
Mark Sleee8540632006-05-30 09:24:40 +0000206 close();
Mark Sleef9831082007-02-20 20:59:21 +0000207 throw TTransportException(TTransportException::NOT_OPEN, "Could not bind");
Mark Sleee8540632006-05-30 09:24:40 +0000208 }
209
210 // Call listen
211 if (-1 == ::listen(serverSocket_, acceptBacklog_)) {
boz6ded7752007-06-05 22:41:18 +0000212 GlobalOutput("TServerSocket::listen() LISTEN");
Mark Sleee8540632006-05-30 09:24:40 +0000213 close();
Mark Sleef9831082007-02-20 20:59:21 +0000214 throw TTransportException(TTransportException::NOT_OPEN, "Could not listen");
Mark Sleee8540632006-05-30 09:24:40 +0000215 }
216
217 // The socket is now listening!
Mark Sleee8540632006-05-30 09:24:40 +0000218}
219
Marc Slemko16698852006-08-04 03:16:10 +0000220shared_ptr<TTransport> TServerSocket::acceptImpl() {
Martin Kraemer10640d82007-02-03 01:59:12 +0000221 if (serverSocket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000222 throw TTransportException(TTransportException::NOT_OPEN, "TServerSocket not listening");
Mark Sleee8540632006-05-30 09:24:40 +0000223 }
224
Mark Sleea5a783f2007-03-02 19:41:08 +0000225 fd_set fds;
226
Aditya Agarwal7859a572007-05-31 01:33:07 +0000227 int maxEintrs = 5;
228 int numEintrs = 0;
229
Mark Sleea5a783f2007-03-02 19:41:08 +0000230 while (true) {
231 FD_ZERO(&fds);
232 FD_SET(serverSocket_, &fds);
Mark Slee561b5362007-03-09 19:26:29 +0000233 if (intSock2_ >= 0) {
234 FD_SET(intSock2_, &fds);
Mark Sleea5a783f2007-03-02 19:41:08 +0000235 }
Mark Slee561b5362007-03-09 19:26:29 +0000236 int ret = select(serverSocket_+1, &fds, NULL, NULL, NULL);
Mark Sleea5a783f2007-03-02 19:41:08 +0000237
Mark Slee561b5362007-03-09 19:26:29 +0000238 if (ret < 0) {
Aditya Agarwal7859a572007-05-31 01:33:07 +0000239 // error cases
bozf83c9db2007-05-31 23:38:37 +0000240 if (errno == EINTR && (numEintrs++ < maxEintrs)) {
Mark Slee256bdc42007-11-27 08:42:19 +0000241 // EINTR needs to be handled manually and we can tolerate
Aditya Agarwal7859a572007-05-31 01:33:07 +0000242 // a certain number
243 continue;
244 }
boz6ded7752007-06-05 22:41:18 +0000245 GlobalOutput("TServerSocket::acceptImpl() select -1");
Mark Sleea5a783f2007-03-02 19:41:08 +0000246 throw TTransportException(TTransportException::UNKNOWN);
Mark Slee561b5362007-03-09 19:26:29 +0000247 } else if (ret > 0) {
248 // Check for an interrupt signal
Mark Slee256bdc42007-11-27 08:42:19 +0000249 if (intSock2_ >= 0 && FD_ISSET(intSock2_, &fds)) {
Mark Slee561b5362007-03-09 19:26:29 +0000250 int8_t buf;
251 if (-1 == recv(intSock2_, &buf, sizeof(int8_t), 0)) {
boz6ded7752007-06-05 22:41:18 +0000252 GlobalOutput("TServerSocket::acceptImpl() interrupt receive");
Mark Slee561b5362007-03-09 19:26:29 +0000253 }
254 throw TTransportException(TTransportException::INTERRUPTED);
255 }
256 // Check for the actual server socket being ready
257 if (FD_ISSET(serverSocket_, &fds)) {
258 break;
259 }
260 } else {
boz6ded7752007-06-05 22:41:18 +0000261 GlobalOutput("TServerSocket::acceptImpl() select 0");
Mark Slee256bdc42007-11-27 08:42:19 +0000262 throw TTransportException(TTransportException::UNKNOWN);
Mark Sleea5a783f2007-03-02 19:41:08 +0000263 }
264 }
265
Mark Slee6d56eb92007-07-06 22:28:15 +0000266 struct sockaddr_storage clientAddress;
Mark Sleee8540632006-05-30 09:24:40 +0000267 int size = sizeof(clientAddress);
268 int clientSocket = ::accept(serverSocket_,
269 (struct sockaddr *) &clientAddress,
270 (socklen_t *) &size);
Mark Slee256bdc42007-11-27 08:42:19 +0000271
Martin Kraemeree341cb2007-02-05 21:40:38 +0000272 if (clientSocket < 0) {
David Reissbc3dddb2007-08-22 23:20:24 +0000273 int errno_copy = errno;
boz6ded7752007-06-05 22:41:18 +0000274 GlobalOutput("TServerSocket::accept()");
David Reissbc3dddb2007-08-22 23:20:24 +0000275 throw TTransportException(TTransportException::UNKNOWN, "accept()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000276 }
Mark Sleea5a783f2007-03-02 19:41:08 +0000277
278 // Make sure client socket is blocking
279 int flags = fcntl(clientSocket, F_GETFL, 0);
280 if (flags == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000281 int errno_copy = errno;
boz6ded7752007-06-05 22:41:18 +0000282 GlobalOutput("TServerSocket::select() fcntl GETFL");
David Reissbc3dddb2007-08-22 23:20:24 +0000283 throw TTransportException(TTransportException::UNKNOWN, "fcntl(F_GETFL)", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000284 }
285 if (-1 == fcntl(clientSocket, F_SETFL, flags & ~O_NONBLOCK)) {
David Reissbc3dddb2007-08-22 23:20:24 +0000286 int errno_copy = errno;
boz6ded7752007-06-05 22:41:18 +0000287 GlobalOutput("TServerSocket::select() fcntl SETFL");
David Reissbc3dddb2007-08-22 23:20:24 +0000288 throw TTransportException(TTransportException::UNKNOWN, "fcntl(F_SETFL)", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000289 }
Mark Slee256bdc42007-11-27 08:42:19 +0000290
Mark Slee29050782006-09-29 00:12:30 +0000291 shared_ptr<TSocket> client(new TSocket(clientSocket));
292 if (sendTimeout_ > 0) {
293 client->setSendTimeout(sendTimeout_);
294 }
295 if (recvTimeout_ > 0) {
296 client->setRecvTimeout(recvTimeout_);
Mark Sleea5a783f2007-03-02 19:41:08 +0000297 }
Mark Slee256bdc42007-11-27 08:42:19 +0000298
Mark Slee29050782006-09-29 00:12:30 +0000299 return client;
Mark Sleee8540632006-05-30 09:24:40 +0000300}
301
Mark Slee561b5362007-03-09 19:26:29 +0000302void TServerSocket::interrupt() {
303 if (intSock1_ >= 0) {
304 int8_t byte = 0;
305 if (-1 == send(intSock1_, &byte, sizeof(int8_t), 0)) {
boz6ded7752007-06-05 22:41:18 +0000306 GlobalOutput("TServerSocket::interrupt()");
Mark Slee561b5362007-03-09 19:26:29 +0000307 }
308 }
309}
310
Mark Sleee8540632006-05-30 09:24:40 +0000311void TServerSocket::close() {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000312 if (serverSocket_ >= 0) {
Mark Sleee8540632006-05-30 09:24:40 +0000313 shutdown(serverSocket_, SHUT_RDWR);
314 ::close(serverSocket_);
315 }
Mark Slee561b5362007-03-09 19:26:29 +0000316 if (intSock1_ >= 0) {
317 ::close(intSock1_);
318 }
319 if (intSock2_ >= 0) {
320 ::close(intSock2_);
321 }
Martin Kraemeree341cb2007-02-05 21:40:38 +0000322 serverSocket_ = -1;
Mark Slee561b5362007-03-09 19:26:29 +0000323 intSock1_ = -1;
324 intSock2_ = -1;
Mark Sleee8540632006-05-30 09:24:40 +0000325}
Marc Slemko6f038a72006-08-03 18:58:09 +0000326
327}}} // facebook::thrift::transport