blob: 0a4802930aed4c25ed38680323c12efcaa06d68a [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 Sleee8540632006-05-30 09:24:40 +00009#include <netinet/in.h>
Mark Slee29050782006-09-29 00:12:30 +000010#include <netinet/tcp.h>
Mark Slee6d56eb92007-07-06 22:28:15 +000011#include <netdb.h>
Mark Sleea5a783f2007-03-02 19:41:08 +000012#include <fcntl.h>
Mark Slee8d7e1f62006-06-07 06:48:56 +000013#include <errno.h>
Mark Sleee8540632006-05-30 09:24:40 +000014
Marc Slemkod42a2c22006-08-10 03:30:18 +000015#include "TSocket.h"
16#include "TServerSocket.h"
Marc Slemko16698852006-08-04 03:16:10 +000017#include <boost/shared_ptr.hpp>
Mark Sleee8540632006-05-30 09:24:40 +000018
Marc Slemko6f038a72006-08-03 18:58:09 +000019namespace facebook { namespace thrift { namespace transport {
20
Marc Slemko16698852006-08-04 03:16:10 +000021using namespace boost;
22
Mark Sleee8540632006-05-30 09:24:40 +000023TServerSocket::TServerSocket(int port) :
Mark Slee29050782006-09-29 00:12:30 +000024 port_(port),
Martin Kraemeree341cb2007-02-05 21:40:38 +000025 serverSocket_(-1),
Mark Slee29050782006-09-29 00:12:30 +000026 acceptBacklog_(1024),
27 sendTimeout_(0),
Mark Sleea5a783f2007-03-02 19:41:08 +000028 recvTimeout_(0),
boz1ea81ce2007-05-14 23:04:33 +000029 retryLimit_(0),
30 retryDelay_(0),
Mark Slee561b5362007-03-09 19:26:29 +000031 intSock1_(-1),
32 intSock2_(-1) {}
Mark Slee29050782006-09-29 00:12:30 +000033
34TServerSocket::TServerSocket(int port, int sendTimeout, int recvTimeout) :
35 port_(port),
Martin Kraemeree341cb2007-02-05 21:40:38 +000036 serverSocket_(-1),
Mark Slee29050782006-09-29 00:12:30 +000037 acceptBacklog_(1024),
38 sendTimeout_(sendTimeout),
Mark Sleea5a783f2007-03-02 19:41:08 +000039 recvTimeout_(recvTimeout),
boz1ea81ce2007-05-14 23:04:33 +000040 retryLimit_(0),
41 retryDelay_(0),
Mark Slee561b5362007-03-09 19:26:29 +000042 intSock1_(-1),
43 intSock2_(-1) {}
Mark Sleee8540632006-05-30 09:24:40 +000044
45TServerSocket::~TServerSocket() {
46 close();
47}
48
Mark Slee29050782006-09-29 00:12:30 +000049void TServerSocket::setSendTimeout(int sendTimeout) {
50 sendTimeout_ = sendTimeout;
51}
52
53void TServerSocket::setRecvTimeout(int recvTimeout) {
54 recvTimeout_ = recvTimeout;
55}
56
boz1ea81ce2007-05-14 23:04:33 +000057void TServerSocket::setRetryLimit(int retryLimit) {
58 retryLimit_ = retryLimit;
59}
60
61void TServerSocket::setRetryDelay(int retryDelay) {
62 retryDelay_ = retryDelay;
63}
64
Mark Slee8d7e1f62006-06-07 06:48:56 +000065void TServerSocket::listen() {
Mark Slee561b5362007-03-09 19:26:29 +000066 int sv[2];
67 if (-1 == socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
boz6ded7752007-06-05 22:41:18 +000068 GlobalOutput("TServerSocket::init()");
Mark Slee561b5362007-03-09 19:26:29 +000069 intSock1_ = -1;
70 intSock2_ = -1;
71 } else {
Mark Sleee02385b2007-06-09 01:21:16 +000072 intSock1_ = sv[1];
73 intSock2_ = sv[0];
Mark Slee561b5362007-03-09 19:26:29 +000074 }
75
Mark Slee6d56eb92007-07-06 22:28:15 +000076 struct addrinfo hints, *res, *res0;
77 int error;
78 char port[sizeof("65536") + 1];
79 memset(&hints, 0, sizeof(hints));
80 hints.ai_family = PF_UNSPEC;
81 hints.ai_socktype = SOCK_STREAM;
82 hints.ai_flags = AI_PASSIVE;
83 sprintf(port, "%d", port_);
84
85 // Wildcard address
86 error = getaddrinfo(NULL, port, &hints, &res0);
87 if (error) {
88 fprintf(stderr, "getaddrinfo %d: %s\n", error, gai_strerror(error));
89 close();
90 throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for server socket.");
91 }
92
93 // Pick the ipv6 address first since ipv4 addresses can be mapped
94 // into ipv6 space.
95 for (res = res0; res; res = res->ai_next) {
96 if (res->ai_family == AF_INET6 || res->ai_next == NULL)
97 break;
98 }
99
100 serverSocket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
Mark Sleee8540632006-05-30 09:24:40 +0000101 if (serverSocket_ == -1) {
boz6ded7752007-06-05 22:41:18 +0000102 GlobalOutput("TServerSocket::listen() socket");
Mark Sleee8540632006-05-30 09:24:40 +0000103 close();
Mark Sleef9831082007-02-20 20:59:21 +0000104 throw TTransportException(TTransportException::NOT_OPEN, "Could not create server socket.");
Mark Sleee8540632006-05-30 09:24:40 +0000105 }
106
107 // Set reusaddress to prevent 2MSL delay on accept
108 int one = 1;
109 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_REUSEADDR,
110 &one, sizeof(one))) {
boz6ded7752007-06-05 22:41:18 +0000111 GlobalOutput("TServerSocket::listen() SO_REUSEADDR");
Mark Sleee8540632006-05-30 09:24:40 +0000112 close();
Mark Sleef9831082007-02-20 20:59:21 +0000113 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_REUSEADDR");
Mark Sleee8540632006-05-30 09:24:40 +0000114 }
115
Mark Slee29050782006-09-29 00:12:30 +0000116 // Defer accept
117 #ifdef TCP_DEFER_ACCEPT
118 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, TCP_DEFER_ACCEPT,
119 &one, sizeof(one))) {
boz6ded7752007-06-05 22:41:18 +0000120 GlobalOutput("TServerSocket::listen() TCP_DEFER_ACCEPT");
Mark Slee29050782006-09-29 00:12:30 +0000121 close();
Mark Sleef9831082007-02-20 20:59:21 +0000122 throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_DEFER_ACCEPT");
Mark Slee29050782006-09-29 00:12:30 +0000123 }
124 #endif // #ifdef TCP_DEFER_ACCEPT
125
Mark Sleee8540632006-05-30 09:24:40 +0000126 // Turn linger off, don't want to block on calls to close
127 struct linger ling = {0, 0};
128 if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_LINGER,
129 &ling, sizeof(ling))) {
Mark Sleee8540632006-05-30 09:24:40 +0000130 close();
boz6ded7752007-06-05 22:41:18 +0000131 GlobalOutput("TServerSocket::listen() SO_LINGER");
Mark Sleef9831082007-02-20 20:59:21 +0000132 throw TTransportException(TTransportException::NOT_OPEN, "Could not set SO_LINGER");
Mark Sleee8540632006-05-30 09:24:40 +0000133 }
134
Mark Slee29050782006-09-29 00:12:30 +0000135 // TCP Nodelay, speed over bandwidth
136 if (-1 == setsockopt(serverSocket_, IPPROTO_TCP, TCP_NODELAY,
137 &one, sizeof(one))) {
138 close();
boz6ded7752007-06-05 22:41:18 +0000139 GlobalOutput("setsockopt TCP_NODELAY");
Mark Sleef9831082007-02-20 20:59:21 +0000140 throw TTransportException(TTransportException::NOT_OPEN, "Could not set TCP_NODELAY");
Mark Slee29050782006-09-29 00:12:30 +0000141 }
142
Mark Sleea5a783f2007-03-02 19:41:08 +0000143 // Set NONBLOCK on the accept socket
144 int flags = fcntl(serverSocket_, F_GETFL, 0);
145 if (flags == -1) {
146 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed");
147 }
Mark Slee561b5362007-03-09 19:26:29 +0000148
Mark Sleea5a783f2007-03-02 19:41:08 +0000149 if (-1 == fcntl(serverSocket_, F_SETFL, flags | O_NONBLOCK)) {
150 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed");
151 }
152
boz1ea81ce2007-05-14 23:04:33 +0000153 // prepare the port information
boz1ea81ce2007-05-14 23:04:33 +0000154 // we may want to try to bind more than once, since SO_REUSEADDR doesn't
155 // always seem to work. The client can configure the retry variables.
156 int retries = 0;
157 do {
Mark Slee6d56eb92007-07-06 22:28:15 +0000158 if (0 == bind(serverSocket_, res->ai_addr, res->ai_addrlen)) {
boz1ea81ce2007-05-14 23:04:33 +0000159 break;
160 }
161
162 // use short circuit evaluation here to only sleep if we need to
163 } while ((retries++ < retryLimit_) && (sleep(retryDelay_) == 0));
164
Mark Slee6d56eb92007-07-06 22:28:15 +0000165 // free addrinfo
166 freeaddrinfo(res0);
167
boz1ea81ce2007-05-14 23:04:33 +0000168 // throw an error if we failed to bind properly
169 if (retries > retryLimit_) {
Mark Sleee8540632006-05-30 09:24:40 +0000170 char errbuf[1024];
171 sprintf(errbuf, "TServerSocket::listen() BIND %d", port_);
boz6ded7752007-06-05 22:41:18 +0000172 GlobalOutput(errbuf);
Mark Sleee8540632006-05-30 09:24:40 +0000173 close();
Mark Sleef9831082007-02-20 20:59:21 +0000174 throw TTransportException(TTransportException::NOT_OPEN, "Could not bind");
Mark Sleee8540632006-05-30 09:24:40 +0000175 }
176
177 // Call listen
178 if (-1 == ::listen(serverSocket_, acceptBacklog_)) {
boz6ded7752007-06-05 22:41:18 +0000179 GlobalOutput("TServerSocket::listen() LISTEN");
Mark Sleee8540632006-05-30 09:24:40 +0000180 close();
Mark Sleef9831082007-02-20 20:59:21 +0000181 throw TTransportException(TTransportException::NOT_OPEN, "Could not listen");
Mark Sleee8540632006-05-30 09:24:40 +0000182 }
183
184 // The socket is now listening!
Mark Sleee8540632006-05-30 09:24:40 +0000185}
186
Marc Slemko16698852006-08-04 03:16:10 +0000187shared_ptr<TTransport> TServerSocket::acceptImpl() {
Martin Kraemer10640d82007-02-03 01:59:12 +0000188 if (serverSocket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000189 throw TTransportException(TTransportException::NOT_OPEN, "TServerSocket not listening");
Mark Sleee8540632006-05-30 09:24:40 +0000190 }
191
Mark Sleea5a783f2007-03-02 19:41:08 +0000192 fd_set fds;
193
Aditya Agarwal7859a572007-05-31 01:33:07 +0000194 int maxEintrs = 5;
195 int numEintrs = 0;
196
Mark Sleea5a783f2007-03-02 19:41:08 +0000197 while (true) {
198 FD_ZERO(&fds);
199 FD_SET(serverSocket_, &fds);
Mark Slee561b5362007-03-09 19:26:29 +0000200 if (intSock2_ >= 0) {
201 FD_SET(intSock2_, &fds);
Mark Sleea5a783f2007-03-02 19:41:08 +0000202 }
Mark Slee561b5362007-03-09 19:26:29 +0000203 int ret = select(serverSocket_+1, &fds, NULL, NULL, NULL);
Mark Sleea5a783f2007-03-02 19:41:08 +0000204
Mark Slee561b5362007-03-09 19:26:29 +0000205 if (ret < 0) {
Aditya Agarwal7859a572007-05-31 01:33:07 +0000206 // error cases
bozf83c9db2007-05-31 23:38:37 +0000207 if (errno == EINTR && (numEintrs++ < maxEintrs)) {
Aditya Agarwal7859a572007-05-31 01:33:07 +0000208 // EINTR needs to be handled manually and we can tolerate
209 // a certain number
210 continue;
211 }
boz6ded7752007-06-05 22:41:18 +0000212 GlobalOutput("TServerSocket::acceptImpl() select -1");
Mark Sleea5a783f2007-03-02 19:41:08 +0000213 throw TTransportException(TTransportException::UNKNOWN);
Mark Slee561b5362007-03-09 19:26:29 +0000214 } else if (ret > 0) {
215 // Check for an interrupt signal
216 if (intSock2_ >= 0 && FD_ISSET(intSock2_, &fds)) {
217 int8_t buf;
218 if (-1 == recv(intSock2_, &buf, sizeof(int8_t), 0)) {
boz6ded7752007-06-05 22:41:18 +0000219 GlobalOutput("TServerSocket::acceptImpl() interrupt receive");
Mark Slee561b5362007-03-09 19:26:29 +0000220 }
221 throw TTransportException(TTransportException::INTERRUPTED);
222 }
223 // Check for the actual server socket being ready
224 if (FD_ISSET(serverSocket_, &fds)) {
225 break;
226 }
227 } else {
boz6ded7752007-06-05 22:41:18 +0000228 GlobalOutput("TServerSocket::acceptImpl() select 0");
Mark Slee561b5362007-03-09 19:26:29 +0000229 throw TTransportException(TTransportException::UNKNOWN);
Mark Sleea5a783f2007-03-02 19:41:08 +0000230 }
231 }
232
Mark Slee6d56eb92007-07-06 22:28:15 +0000233 struct sockaddr_storage clientAddress;
Mark Sleee8540632006-05-30 09:24:40 +0000234 int size = sizeof(clientAddress);
235 int clientSocket = ::accept(serverSocket_,
236 (struct sockaddr *) &clientAddress,
237 (socklen_t *) &size);
238
Martin Kraemeree341cb2007-02-05 21:40:38 +0000239 if (clientSocket < 0) {
boz6ded7752007-06-05 22:41:18 +0000240 GlobalOutput("TServerSocket::accept()");
Mark Slee6d56eb92007-07-06 22:28:15 +0000241 throw TTransportException(TTransportException::UNKNOWN, std::string("ERROR:") + sys_errlist[errno]);
Mark Sleee8540632006-05-30 09:24:40 +0000242 }
Mark Sleea5a783f2007-03-02 19:41:08 +0000243
244 // Make sure client socket is blocking
245 int flags = fcntl(clientSocket, F_GETFL, 0);
246 if (flags == -1) {
boz6ded7752007-06-05 22:41:18 +0000247 GlobalOutput("TServerSocket::select() fcntl GETFL");
Mark Slee6d56eb92007-07-06 22:28:15 +0000248 throw TTransportException(TTransportException::UNKNOWN, std::string("ERROR:") + sys_errlist[errno]);
Mark Sleea5a783f2007-03-02 19:41:08 +0000249 }
250 if (-1 == fcntl(clientSocket, F_SETFL, flags & ~O_NONBLOCK)) {
boz6ded7752007-06-05 22:41:18 +0000251 GlobalOutput("TServerSocket::select() fcntl SETFL");
Mark Slee6d56eb92007-07-06 22:28:15 +0000252 throw TTransportException(TTransportException::UNKNOWN, std::string("ERROR:") + sys_errlist[errno]);
Mark Sleea5a783f2007-03-02 19:41:08 +0000253 }
Martin Kraemer10640d82007-02-03 01:59:12 +0000254
Mark Slee29050782006-09-29 00:12:30 +0000255 shared_ptr<TSocket> client(new TSocket(clientSocket));
256 if (sendTimeout_ > 0) {
257 client->setSendTimeout(sendTimeout_);
258 }
259 if (recvTimeout_ > 0) {
260 client->setRecvTimeout(recvTimeout_);
Mark Sleea5a783f2007-03-02 19:41:08 +0000261 }
Aditya Agarwal7859a572007-05-31 01:33:07 +0000262
Mark Slee29050782006-09-29 00:12:30 +0000263 return client;
Mark Sleee8540632006-05-30 09:24:40 +0000264}
265
Mark Slee561b5362007-03-09 19:26:29 +0000266void TServerSocket::interrupt() {
267 if (intSock1_ >= 0) {
268 int8_t byte = 0;
269 if (-1 == send(intSock1_, &byte, sizeof(int8_t), 0)) {
boz6ded7752007-06-05 22:41:18 +0000270 GlobalOutput("TServerSocket::interrupt()");
Mark Slee561b5362007-03-09 19:26:29 +0000271 }
272 }
273}
274
Mark Sleee8540632006-05-30 09:24:40 +0000275void TServerSocket::close() {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000276 if (serverSocket_ >= 0) {
Mark Sleee8540632006-05-30 09:24:40 +0000277 shutdown(serverSocket_, SHUT_RDWR);
278 ::close(serverSocket_);
279 }
Mark Slee561b5362007-03-09 19:26:29 +0000280 if (intSock1_ >= 0) {
281 ::close(intSock1_);
282 }
283 if (intSock2_ >= 0) {
284 ::close(intSock2_);
285 }
Martin Kraemeree341cb2007-02-05 21:40:38 +0000286 serverSocket_ = -1;
Mark Slee561b5362007-03-09 19:26:29 +0000287 intSock1_ = -1;
288 intSock2_ = -1;
Mark Sleee8540632006-05-30 09:24:40 +0000289}
Marc Slemko6f038a72006-08-03 18:58:09 +0000290
291}}} // facebook::thrift::transport