blob: 2f5a0b0a1394d0f68f91c82206fbabac59f95079 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Mark Slee9f0c6512007-02-28 23:58:26 +000019
Marc Slemkoe03da182006-07-21 21:32:36 +000020#include <config.h>
David Reissc88eb8c2008-06-11 01:18:54 +000021#include <cstring>
22#include <sstream>
Mark Sleee8540632006-05-30 09:24:40 +000023#include <sys/socket.h>
Bryan Duxburya18364a2010-09-28 14:36:07 +000024#include <sys/un.h>
David Reiss22b18862008-04-08 06:25:45 +000025#include <sys/poll.h>
Mark Sleedd564972007-08-21 02:39:57 +000026#include <sys/types.h>
Mark Sleee8540632006-05-30 09:24:40 +000027#include <arpa/inet.h>
28#include <netinet/in.h>
29#include <netinet/tcp.h>
Mark Sleee8540632006-05-30 09:24:40 +000030#include <unistd.h>
31#include <errno.h>
Mark Slee29050782006-09-29 00:12:30 +000032#include <fcntl.h>
Mark Sleee8540632006-05-30 09:24:40 +000033
Mark Slee29050782006-09-29 00:12:30 +000034#include "concurrency/Monitor.h"
Marc Slemkod42a2c22006-08-10 03:30:18 +000035#include "TSocket.h"
36#include "TTransportException.h"
Mark Sleee8540632006-05-30 09:24:40 +000037
T Jake Lucianib5e62212009-01-31 22:36:20 +000038namespace apache { namespace thrift { namespace transport {
Marc Slemko6f038a72006-08-03 18:58:09 +000039
Mark Sleee8540632006-05-30 09:24:40 +000040using namespace std;
41
Mark Slee29050782006-09-29 00:12:30 +000042// Global var to track total socket sys calls
Mark Slee8d7e1f62006-06-07 06:48:56 +000043uint32_t g_socket_syscalls = 0;
44
45/**
46 * TSocket implementation.
47 *
Mark Slee8d7e1f62006-06-07 06:48:56 +000048 */
49
Mark Slee256bdc42007-11-27 08:42:19 +000050TSocket::TSocket(string host, int port) :
Mark Slee29050782006-09-29 00:12:30 +000051 host_(host),
52 port_(port),
Bryan Duxburya18364a2010-09-28 14:36:07 +000053 path_(""),
54 socket_(-1),
55 connTimeout_(0),
56 sendTimeout_(0),
57 recvTimeout_(0),
58 lingerOn_(1),
59 lingerVal_(0),
60 noDelay_(1),
61 maxRecvRetries_(5) {
62 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
63 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
64}
65
66TSocket::TSocket(string path) :
67 host_(""),
68 port_(0),
69 path_(path),
Martin Kraemeree341cb2007-02-05 21:40:38 +000070 socket_(-1),
Mark Slee29050782006-09-29 00:12:30 +000071 connTimeout_(0),
72 sendTimeout_(0),
73 recvTimeout_(0),
74 lingerOn_(1),
75 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +000076 noDelay_(1),
77 maxRecvRetries_(5) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000078 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
79 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
David Reiss23248712010-10-06 17:10:08 +000080 cachedPeerAddr_.ipv4.sin_family = AF_UNSPEC;
Mark Sleee8540632006-05-30 09:24:40 +000081}
82
Mark Slee256bdc42007-11-27 08:42:19 +000083TSocket::TSocket() :
Aditya Agarwalebc99e02007-01-15 23:14:58 +000084 host_(""),
85 port_(0),
Bryan Duxburya18364a2010-09-28 14:36:07 +000086 path_(""),
Martin Kraemeree341cb2007-02-05 21:40:38 +000087 socket_(-1),
Aditya Agarwalebc99e02007-01-15 23:14:58 +000088 connTimeout_(0),
89 sendTimeout_(0),
90 recvTimeout_(0),
91 lingerOn_(1),
92 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +000093 noDelay_(1),
94 maxRecvRetries_(5) {
Aditya Agarwalebc99e02007-01-15 23:14:58 +000095 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
96 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
David Reiss23248712010-10-06 17:10:08 +000097 cachedPeerAddr_.ipv4.sin_family = AF_UNSPEC;
Aditya Agarwalebc99e02007-01-15 23:14:58 +000098}
99
Mark Slee29050782006-09-29 00:12:30 +0000100TSocket::TSocket(int socket) :
101 host_(""),
102 port_(0),
Bryan Duxburya18364a2010-09-28 14:36:07 +0000103 path_(""),
Mark Slee29050782006-09-29 00:12:30 +0000104 socket_(socket),
105 connTimeout_(0),
106 sendTimeout_(0),
107 recvTimeout_(0),
108 lingerOn_(1),
109 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +0000110 noDelay_(1),
111 maxRecvRetries_(5) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000112 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
113 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
David Reiss23248712010-10-06 17:10:08 +0000114 cachedPeerAddr_.ipv4.sin_family = AF_UNSPEC;
Mark Slee29050782006-09-29 00:12:30 +0000115}
Mark Slee256bdc42007-11-27 08:42:19 +0000116
Mark Sleee8540632006-05-30 09:24:40 +0000117TSocket::~TSocket() {
118 close();
119}
120
Mark Slee256bdc42007-11-27 08:42:19 +0000121bool TSocket::isOpen() {
122 return (socket_ >= 0);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000123}
124
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000125bool TSocket::peek() {
126 if (!isOpen()) {
127 return false;
128 }
129 uint8_t buf;
130 int r = recv(socket_, &buf, 1, MSG_PEEK);
131 if (r == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000132 int errno_copy = errno;
David Reiss840e7522009-06-04 00:10:50 +0000133 #if defined __FreeBSD__ || defined __MACH__
Kevin Clark022b2242009-03-05 21:05:37 +0000134 /* shigin:
135 * freebsd returns -1 and ECONNRESET if socket was closed by
136 * the other side
137 */
138 if (errno_copy == ECONNRESET)
139 {
140 close();
141 return false;
142 }
143 #endif
David Reiss01e55c12008-07-13 22:18:51 +0000144 GlobalOutput.perror("TSocket::peek() recv() " + getSocketInfo(), errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000145 throw TTransportException(TTransportException::UNKNOWN, "recv()", errno_copy);
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000146 }
147 return (r > 0);
148}
149
Mark Slee6d56eb92007-07-06 22:28:15 +0000150void TSocket::openConnection(struct addrinfo *res) {
Mark Sleea9848d72007-02-21 04:54:05 +0000151 if (isOpen()) {
Bryan Duxbury010f1e02010-09-02 00:56:53 +0000152 return;
Mark Sleea9848d72007-02-21 04:54:05 +0000153 }
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000154
Bryan Duxburya18364a2010-09-28 14:36:07 +0000155 if (! path_.empty()) {
156 socket_ = socket(PF_UNIX, SOCK_STREAM, IPPROTO_IP);
157 } else {
158 socket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
159 }
160
Mark Sleee8540632006-05-30 09:24:40 +0000161 if (socket_ == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000162 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000163 GlobalOutput.perror("TSocket::open() socket() " + getSocketInfo(), errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000164 throw TTransportException(TTransportException::NOT_OPEN, "socket()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000165 }
Mark Slee29050782006-09-29 00:12:30 +0000166
167 // Send timeout
168 if (sendTimeout_ > 0) {
169 setSendTimeout(sendTimeout_);
170 }
171
172 // Recv timeout
173 if (recvTimeout_ > 0) {
174 setRecvTimeout(recvTimeout_);
175 }
176
177 // Linger
178 setLinger(lingerOn_, lingerVal_);
179
180 // No delay
181 setNoDelay(noDelay_);
182
David Reiss1c20c872010-03-09 05:20:14 +0000183 // Uses a low min RTO if asked to.
184#ifdef TCP_LOW_MIN_RTO
185 if (getUseLowMinRto()) {
186 int one = 1;
187 setsockopt(socket_, IPPROTO_TCP, TCP_LOW_MIN_RTO, &one, sizeof(one));
188 }
189#endif
190
191
Mark Slee29050782006-09-29 00:12:30 +0000192 // Set the socket to be non blocking for connect if a timeout exists
Mark Slee256bdc42007-11-27 08:42:19 +0000193 int flags = fcntl(socket_, F_GETFL, 0);
Mark Slee29050782006-09-29 00:12:30 +0000194 if (connTimeout_ > 0) {
Mark Sleea5a783f2007-03-02 19:41:08 +0000195 if (-1 == fcntl(socket_, F_SETFL, flags | O_NONBLOCK)) {
David Reiss9b209552008-04-08 06:26:05 +0000196 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000197 GlobalOutput.perror("TSocket::open() fcntl() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000198 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000199 }
Mark Slee29050782006-09-29 00:12:30 +0000200 } else {
Mark Sleea5a783f2007-03-02 19:41:08 +0000201 if (-1 == fcntl(socket_, F_SETFL, flags & ~O_NONBLOCK)) {
David Reiss9b209552008-04-08 06:26:05 +0000202 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000203 GlobalOutput.perror("TSocket::open() fcntl " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000204 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000205 }
Mark Slee29050782006-09-29 00:12:30 +0000206 }
207
Mark Sleee8540632006-05-30 09:24:40 +0000208 // Connect the socket
Bryan Duxburya18364a2010-09-28 14:36:07 +0000209 int ret;
210 if (! path_.empty()) {
211 struct sockaddr_un address;
212 socklen_t len;
213
214 if (path_.length() > sizeof(address.sun_path)) {
215 int errno_copy = errno;
216 GlobalOutput.perror("TSocket::open() Unix Domain socket path too long", errno_copy);
217 throw TTransportException(TTransportException::NOT_OPEN, " Unix Domain socket path too long");
218 }
219
220 address.sun_family = AF_UNIX;
Roger Meierd11ca5a2010-10-18 08:22:57 +0000221 snprintf(address.sun_path, sizeof(address.sun_path), "%s", path_.c_str());
Bryan Duxburya18364a2010-09-28 14:36:07 +0000222 len = sizeof(address);
223 ret = connect(socket_, (struct sockaddr *) &address, len);
224 } else {
225 ret = connect(socket_, res->ai_addr, res->ai_addrlen);
226 }
Mark Slee256bdc42007-11-27 08:42:19 +0000227
David Reiss9b209552008-04-08 06:26:05 +0000228 // success case
Mark Slee29050782006-09-29 00:12:30 +0000229 if (ret == 0) {
230 goto done;
231 }
232
233 if (errno != EINPROGRESS) {
David Reissbc3dddb2007-08-22 23:20:24 +0000234 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000235 GlobalOutput.perror("TSocket::open() connect() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000236 throw TTransportException(TTransportException::NOT_OPEN, "connect() failed", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000237 }
238
David Reiss22b18862008-04-08 06:25:45 +0000239
240 struct pollfd fds[1];
David Reissc88eb8c2008-06-11 01:18:54 +0000241 std::memset(fds, 0 , sizeof(fds));
David Reiss22b18862008-04-08 06:25:45 +0000242 fds[0].fd = socket_;
243 fds[0].events = POLLOUT;
244 ret = poll(fds, 1, connTimeout_);
Mark Slee29050782006-09-29 00:12:30 +0000245
246 if (ret > 0) {
David Reiss9b209552008-04-08 06:26:05 +0000247 // Ensure the socket is connected and that there are no errors set
Mark Slee29050782006-09-29 00:12:30 +0000248 int val;
249 socklen_t lon;
250 lon = sizeof(int);
251 int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon);
252 if (ret2 == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000253 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000254 GlobalOutput.perror("TSocket::open() getsockopt() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000255 throw TTransportException(TTransportException::NOT_OPEN, "getsockopt()", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000256 }
David Reiss9b209552008-04-08 06:26:05 +0000257 // no errors on socket, go to town
Mark Slee29050782006-09-29 00:12:30 +0000258 if (val == 0) {
259 goto done;
260 }
David Reiss01e55c12008-07-13 22:18:51 +0000261 GlobalOutput.perror("TSocket::open() error on socket (after poll) " + getSocketInfo(), val);
David Reiss9b209552008-04-08 06:26:05 +0000262 throw TTransportException(TTransportException::NOT_OPEN, "socket open() error", val);
Mark Slee29050782006-09-29 00:12:30 +0000263 } else if (ret == 0) {
David Reiss9b209552008-04-08 06:26:05 +0000264 // socket timed out
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000265 string errStr = "TSocket::open() timed out " + getSocketInfo();
266 GlobalOutput(errStr.c_str());
David Reiss9b209552008-04-08 06:26:05 +0000267 throw TTransportException(TTransportException::NOT_OPEN, "open() timed out");
Mark Slee29050782006-09-29 00:12:30 +0000268 } else {
David Reiss9b209552008-04-08 06:26:05 +0000269 // error on poll()
David Reissbc3dddb2007-08-22 23:20:24 +0000270 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000271 GlobalOutput.perror("TSocket::open() poll() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000272 throw TTransportException(TTransportException::NOT_OPEN, "poll() failed", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000273 }
274
275 done:
276 // Set socket back to normal mode (blocking)
277 fcntl(socket_, F_SETFL, flags);
David Reiss23248712010-10-06 17:10:08 +0000278
Roger Meier18f10502011-06-04 08:57:43 +0000279 if (path_.empty()) {
280 setCachedAddress(res->ai_addr, res->ai_addrlen);
281 }
Mark Sleee8540632006-05-30 09:24:40 +0000282}
283
Mark Slee6d56eb92007-07-06 22:28:15 +0000284void TSocket::open() {
285 if (isOpen()) {
Bryan Duxbury010f1e02010-09-02 00:56:53 +0000286 return;
Mark Slee6d56eb92007-07-06 22:28:15 +0000287 }
Bryan Duxburya18364a2010-09-28 14:36:07 +0000288 if (! path_.empty()) {
289 unix_open();
290 } else {
291 local_open();
292 }
293}
294
295void TSocket::unix_open(){
296 if (! path_.empty()) {
297 // Unix Domain SOcket does not need addrinfo struct, so we pass NULL
298 openConnection(NULL);
299 }
300}
301
302void TSocket::local_open(){
303 if (isOpen()) {
304 return;
305 }
Mark Slee6d56eb92007-07-06 22:28:15 +0000306
307 // Validate port number
David Reiss450e35d2010-03-09 05:19:41 +0000308 if (port_ < 0 || port_ > 0xFFFF) {
Mark Slee6d56eb92007-07-06 22:28:15 +0000309 throw TTransportException(TTransportException::NOT_OPEN, "Specified port is invalid");
310 }
311
312 struct addrinfo hints, *res, *res0;
David Reiss9b209552008-04-08 06:26:05 +0000313 res = NULL;
314 res0 = NULL;
Mark Slee6d56eb92007-07-06 22:28:15 +0000315 int error;
David Reiss450e35d2010-03-09 05:19:41 +0000316 char port[sizeof("65535")];
David Reissc88eb8c2008-06-11 01:18:54 +0000317 std::memset(&hints, 0, sizeof(hints));
Mark Slee6d56eb92007-07-06 22:28:15 +0000318 hints.ai_family = PF_UNSPEC;
319 hints.ai_socktype = SOCK_STREAM;
Mark Slee256bdc42007-11-27 08:42:19 +0000320 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
Mark Slee6d56eb92007-07-06 22:28:15 +0000321 sprintf(port, "%d", port_);
Mark Slee256bdc42007-11-27 08:42:19 +0000322
Mark Sleec37b4c52007-12-05 23:03:37 +0000323 error = getaddrinfo(host_.c_str(), port, &hints, &res0);
324
Mark Slee6d56eb92007-07-06 22:28:15 +0000325 if (error) {
David Reiss9b209552008-04-08 06:26:05 +0000326 string errStr = "TSocket::open() getaddrinfo() " + getSocketInfo() + string(gai_strerror(error));
327 GlobalOutput(errStr.c_str());
Mark Slee6d56eb92007-07-06 22:28:15 +0000328 close();
329 throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for client socket.");
330 }
Mark Slee256bdc42007-11-27 08:42:19 +0000331
Mark Slee6d56eb92007-07-06 22:28:15 +0000332 // Cycle through all the returned addresses until one
333 // connects or push the exception up.
334 for (res = res0; res; res = res->ai_next) {
335 try {
336 openConnection(res);
337 break;
338 } catch (TTransportException& ttx) {
339 if (res->ai_next) {
340 close();
341 } else {
342 close();
Mark Slee85287d32007-07-09 19:50:30 +0000343 freeaddrinfo(res0); // cleanup on failure
Mark Slee6d56eb92007-07-06 22:28:15 +0000344 throw;
345 }
346 }
347 }
Mark Slee85287d32007-07-09 19:50:30 +0000348
349 // Free address structure memory
350 freeaddrinfo(res0);
Mark Slee6d56eb92007-07-06 22:28:15 +0000351}
352
Mark Sleee8540632006-05-30 09:24:40 +0000353void TSocket::close() {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000354 if (socket_ >= 0) {
Mark Sleee8540632006-05-30 09:24:40 +0000355 shutdown(socket_, SHUT_RDWR);
356 ::close(socket_);
357 }
Martin Kraemeree341cb2007-02-05 21:40:38 +0000358 socket_ = -1;
Mark Sleee8540632006-05-30 09:24:40 +0000359}
360
David Reiss105961d2010-10-06 17:10:17 +0000361void TSocket::setSocketFD(int socket) {
362 if (socket_ >= 0) {
363 close();
364 }
365 socket_ = socket;
366}
367
Mark Slee8d7e1f62006-06-07 06:48:56 +0000368uint32_t TSocket::read(uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000369 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000370 throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000371 }
Mark Sleee8540632006-05-30 09:24:40 +0000372
Aditya Agarwale04475b2007-05-23 02:14:58 +0000373 int32_t retries = 0;
374
375 // EAGAIN can be signalled both when a timeout has occurred and when
376 // the system is out of resources (an awesome undocumented feature).
377 // The following is an approximation of the time interval under which
378 // EAGAIN is taken to indicate an out of resources error.
379 uint32_t eagainThresholdMicros = 0;
380 if (recvTimeout_) {
Mark Slee256bdc42007-11-27 08:42:19 +0000381 // if a readTimeout is specified along with a max number of recv retries, then
Aditya Agarwale04475b2007-05-23 02:14:58 +0000382 // the threshold will ensure that the read timeout is not exceeded even in the
383 // case of resource errors
384 eagainThresholdMicros = (recvTimeout_*1000)/ ((maxRecvRetries_>0) ? maxRecvRetries_ : 2);
385 }
386
Mark Slee256bdc42007-11-27 08:42:19 +0000387 try_again:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000388 // Read from the socket
Aditya Agarwale04475b2007-05-23 02:14:58 +0000389 struct timeval begin;
David Reiss105961d2010-10-06 17:10:17 +0000390 if (recvTimeout_ > 0) {
391 gettimeofday(&begin, NULL);
392 } else {
393 // if there is no read timeout we don't need the TOD to determine whether
394 // an EAGAIN is due to a timeout or an out-of-resource condition.
395 begin.tv_sec = begin.tv_usec = 0;
396 }
Mark Slee8d7e1f62006-06-07 06:48:56 +0000397 int got = recv(socket_, buf, len, 0);
Kevin Clark022b2242009-03-05 21:05:37 +0000398 int errno_copy = errno; //gettimeofday can change errno
Mark Slee8d7e1f62006-06-07 06:48:56 +0000399 ++g_socket_syscalls;
Aditya Agarwale04475b2007-05-23 02:14:58 +0000400
Mark Slee8d7e1f62006-06-07 06:48:56 +0000401 // Check for error on read
Mark Slee256bdc42007-11-27 08:42:19 +0000402 if (got < 0) {
Kevin Clark022b2242009-03-05 21:05:37 +0000403 if (errno_copy == EAGAIN) {
David Reiss105961d2010-10-06 17:10:17 +0000404 // if no timeout we can assume that resource exhaustion has occurred.
405 if (recvTimeout_ == 0) {
406 throw TTransportException(TTransportException::TIMED_OUT,
407 "EAGAIN (unavailable resources)");
408 }
Aditya Agarwale04475b2007-05-23 02:14:58 +0000409 // check if this is the lack of resources or timeout case
David Reissa1a15112010-03-09 05:19:54 +0000410 struct timeval end;
411 gettimeofday(&end, NULL);
412 uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000)
413 + (((uint64_t)(end.tv_usec - begin.tv_usec))));
414
Aditya Agarwale04475b2007-05-23 02:14:58 +0000415 if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) {
416 if (retries++ < maxRecvRetries_) {
417 usleep(50);
418 goto try_again;
419 } else {
Mark Slee256bdc42007-11-27 08:42:19 +0000420 throw TTransportException(TTransportException::TIMED_OUT,
Aditya Agarwale04475b2007-05-23 02:14:58 +0000421 "EAGAIN (unavailable resources)");
422 }
423 } else {
424 // infer that timeout has been hit
Mark Slee256bdc42007-11-27 08:42:19 +0000425 throw TTransportException(TTransportException::TIMED_OUT,
Aditya Agarwale04475b2007-05-23 02:14:58 +0000426 "EAGAIN (timed out)");
427 }
Mark Sleee8540632006-05-30 09:24:40 +0000428 }
Mark Slee256bdc42007-11-27 08:42:19 +0000429
Mark Slee8d7e1f62006-06-07 06:48:56 +0000430 // If interrupted, try again
Kevin Clark022b2242009-03-05 21:05:37 +0000431 if (errno_copy == EINTR && retries++ < maxRecvRetries_) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000432 goto try_again;
Mark Sleee8540632006-05-30 09:24:40 +0000433 }
Mark Slee256bdc42007-11-27 08:42:19 +0000434
David Reiss840e7522009-06-04 00:10:50 +0000435 #if defined __FreeBSD__ || defined __MACH__
Kevin Clark022b2242009-03-05 21:05:37 +0000436 if (errno_copy == ECONNRESET) {
Kevin Clark022b2242009-03-05 21:05:37 +0000437 /* shigin: freebsd doesn't follow POSIX semantic of recv and fails with
438 * ECONNRESET if peer performed shutdown
David Reiss105961d2010-10-06 17:10:17 +0000439 * edhall: eliminated close() since we do that in the destructor.
Kevin Clark022b2242009-03-05 21:05:37 +0000440 */
Kevin Clark022b2242009-03-05 21:05:37 +0000441 return 0;
David Reiss840e7522009-06-04 00:10:50 +0000442 }
443 #endif
444
445 // Now it's not a try again case, but a real probblez
446 GlobalOutput.perror("TSocket::read() recv() " + getSocketInfo(), errno_copy);
447
448 // If we disconnect with no linger time
449 if (errno_copy == ECONNRESET) {
Mark Sleef9831082007-02-20 20:59:21 +0000450 throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000451 }
Mark Slee256bdc42007-11-27 08:42:19 +0000452
Mark Slee8d7e1f62006-06-07 06:48:56 +0000453 // This ish isn't open
Kevin Clark022b2242009-03-05 21:05:37 +0000454 if (errno_copy == ENOTCONN) {
Mark Sleef9831082007-02-20 20:59:21 +0000455 throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000456 }
Mark Slee256bdc42007-11-27 08:42:19 +0000457
Mark Slee8d7e1f62006-06-07 06:48:56 +0000458 // Timed out!
Kevin Clark022b2242009-03-05 21:05:37 +0000459 if (errno_copy == ETIMEDOUT) {
Mark Sleef9831082007-02-20 20:59:21 +0000460 throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000461 }
Mark Slee256bdc42007-11-27 08:42:19 +0000462
Mark Slee8d7e1f62006-06-07 06:48:56 +0000463 // Some other error, whatevz
David Reiss01e55c12008-07-13 22:18:51 +0000464 throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000465 }
Mark Slee256bdc42007-11-27 08:42:19 +0000466
Mark Slee8d7e1f62006-06-07 06:48:56 +0000467 // The remote host has closed the socket
468 if (got == 0) {
David Reiss105961d2010-10-06 17:10:17 +0000469 // edhall: we used to call close() here, but our caller may want to deal
470 // with the socket fd and we'll close() in our destructor in any case.
Mark Slee8d7e1f62006-06-07 06:48:56 +0000471 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000472 }
Mark Slee256bdc42007-11-27 08:42:19 +0000473
Mark Sleee8540632006-05-30 09:24:40 +0000474 // Pack data into string
Mark Slee8d7e1f62006-06-07 06:48:56 +0000475 return got;
Mark Sleee8540632006-05-30 09:24:40 +0000476}
477
Mark Slee8d7e1f62006-06-07 06:48:56 +0000478void TSocket::write(const uint8_t* buf, uint32_t len) {
David Reiss105961d2010-10-06 17:10:17 +0000479 uint32_t sent = 0;
480
481 while (sent < len) {
482 uint32_t b = write_partial(buf + sent, len - sent);
483 if (b == 0) {
484 // We assume that we got 0 because send() errored with EAGAIN due to
485 // lack of system resources; release the CPU for a bit.
486 usleep(50);
487 }
488 sent += b;
489 }
490}
491
492uint32_t TSocket::write_partial(const uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000493 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000494 throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000495 }
496
Mark Sleee8540632006-05-30 09:24:40 +0000497 uint32_t sent = 0;
Mark Slee256bdc42007-11-27 08:42:19 +0000498
David Reiss105961d2010-10-06 17:10:17 +0000499 int flags = 0;
500#ifdef MSG_NOSIGNAL
501 // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we
502 // check for the EPIPE return condition and close the socket in that case
503 flags |= MSG_NOSIGNAL;
504#endif // ifdef MSG_NOSIGNAL
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000505
David Reiss105961d2010-10-06 17:10:17 +0000506 int b = send(socket_, buf + sent, len - sent, flags);
507 ++g_socket_syscalls;
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000508
David Reiss105961d2010-10-06 17:10:17 +0000509 if (b < 0) {
510 if (errno == EWOULDBLOCK || errno == EAGAIN) {
511 return 0;
512 }
Mark Sleee8540632006-05-30 09:24:40 +0000513 // Fail on a send error
David Reiss105961d2010-10-06 17:10:17 +0000514 int errno_copy = errno;
515 GlobalOutput.perror("TSocket::write_partial() send() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000516
David Reiss105961d2010-10-06 17:10:17 +0000517 if (errno_copy == EPIPE || errno_copy == ECONNRESET || errno_copy == ENOTCONN) {
518 close();
519 throw TTransportException(TTransportException::NOT_OPEN, "write() send()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000520 }
Mark Slee256bdc42007-11-27 08:42:19 +0000521
David Reiss105961d2010-10-06 17:10:17 +0000522 throw TTransportException(TTransportException::UNKNOWN, "write() send()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000523 }
David Reiss105961d2010-10-06 17:10:17 +0000524
525 // Fail on blocked send
526 if (b == 0) {
527 throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0.");
528 }
529 return b;
Mark Sleee8540632006-05-30 09:24:40 +0000530}
531
dweatherford14b0ed62007-10-19 01:03:32 +0000532std::string TSocket::getHost() {
533 return host_;
534}
535
536int TSocket::getPort() {
537 return port_;
538}
539
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000540void TSocket::setHost(string host) {
541 host_ = host;
542}
543
544void TSocket::setPort(int port) {
545 port_ = port;
546}
547
Mark Slee8d7e1f62006-06-07 06:48:56 +0000548void TSocket::setLinger(bool on, int linger) {
Mark Slee29050782006-09-29 00:12:30 +0000549 lingerOn_ = on;
550 lingerVal_ = linger;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000551 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000552 return;
553 }
554
Mark Slee29050782006-09-29 00:12:30 +0000555 struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_};
556 int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
557 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000558 int errno_copy = errno; // Copy errno because we're allocating memory.
559 GlobalOutput.perror("TSocket::setLinger() setsockopt() " + getSocketInfo(), errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000560 }
Mark Sleee8540632006-05-30 09:24:40 +0000561}
562
Mark Slee8d7e1f62006-06-07 06:48:56 +0000563void TSocket::setNoDelay(bool noDelay) {
Mark Slee29050782006-09-29 00:12:30 +0000564 noDelay_ = noDelay;
Roger Meier18f10502011-06-04 08:57:43 +0000565 if (socket_ < 0 || !path_.empty()) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000566 return;
567 }
568
Mark Sleee8540632006-05-30 09:24:40 +0000569 // Set socket to NODELAY
Mark Slee29050782006-09-29 00:12:30 +0000570 int v = noDelay_ ? 1 : 0;
571 int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
572 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000573 int errno_copy = errno; // Copy errno because we're allocating memory.
574 GlobalOutput.perror("TSocket::setNoDelay() setsockopt() " + getSocketInfo(), errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000575 }
Mark Sleee8540632006-05-30 09:24:40 +0000576}
Mark Slee29050782006-09-29 00:12:30 +0000577
578void TSocket::setConnTimeout(int ms) {
579 connTimeout_ = ms;
580}
581
582void TSocket::setRecvTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000583 if (ms < 0) {
584 char errBuf[512];
585 sprintf(errBuf, "TSocket::setRecvTimeout with negative input: %d\n", ms);
586 GlobalOutput(errBuf);
587 return;
588 }
Mark Slee29050782006-09-29 00:12:30 +0000589 recvTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000590
Martin Kraemeree341cb2007-02-05 21:40:38 +0000591 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000592 return;
593 }
594
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000595 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
596 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
597
David Reiss22b18862008-04-08 06:25:45 +0000598 // Copy because poll may modify
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000599 struct timeval r = recvTimeval_;
Mark Slee29050782006-09-29 00:12:30 +0000600 int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r));
601 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000602 int errno_copy = errno; // Copy errno because we're allocating memory.
603 GlobalOutput.perror("TSocket::setRecvTimeout() setsockopt() " + getSocketInfo(), errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000604 }
605}
606
607void TSocket::setSendTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000608 if (ms < 0) {
609 char errBuf[512];
610 sprintf(errBuf, "TSocket::setSendTimeout with negative input: %d\n", ms);
611 GlobalOutput(errBuf);
612 return;
613 }
Mark Slee29050782006-09-29 00:12:30 +0000614 sendTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000615
Martin Kraemeree341cb2007-02-05 21:40:38 +0000616 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000617 return;
618 }
Mark Slee256bdc42007-11-27 08:42:19 +0000619
Mark Slee29050782006-09-29 00:12:30 +0000620 struct timeval s = {(int)(sendTimeout_/1000),
621 (int)((sendTimeout_%1000)*1000)};
622 int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s));
623 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000624 int errno_copy = errno; // Copy errno because we're allocating memory.
625 GlobalOutput.perror("TSocket::setSendTimeout() setsockopt() " + getSocketInfo(), errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000626 }
627}
628
Aditya Agarwale04475b2007-05-23 02:14:58 +0000629void TSocket::setMaxRecvRetries(int maxRecvRetries) {
630 maxRecvRetries_ = maxRecvRetries;
631}
632
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000633string TSocket::getSocketInfo() {
634 std::ostringstream oss;
David Reiss105961d2010-10-06 17:10:17 +0000635 if (host_.empty() || port_ == 0) {
636 oss << "<Host: " << getPeerAddress();
637 oss << " Port: " << getPeerPort() << ">";
638 } else {
639 oss << "<Host: " << host_ << " Port: " << port_ << ">";
640 }
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000641 return oss.str();
642}
643
Mark Sleeb4552922007-11-28 00:12:11 +0000644std::string TSocket::getPeerHost() {
Roger Meier18f10502011-06-04 08:57:43 +0000645 if (peerHost_.empty() && path_.empty()) {
Mark Sleeb4552922007-11-28 00:12:11 +0000646 struct sockaddr_storage addr;
David Reiss23248712010-10-06 17:10:08 +0000647 struct sockaddr* addrPtr;
648 socklen_t addrLen;
Mark Sleeb4552922007-11-28 00:12:11 +0000649
650 if (socket_ < 0) {
651 return host_;
652 }
653
David Reiss23248712010-10-06 17:10:08 +0000654 addrPtr = getCachedAddress(&addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000655
David Reiss23248712010-10-06 17:10:08 +0000656 if (addrPtr == NULL) {
657 addrLen = sizeof(addr);
658 if (getpeername(socket_, (sockaddr*) &addr, &addrLen) != 0) {
659 return peerHost_;
660 }
661 addrPtr = (sockaddr*)&addr;
662
663 setCachedAddress(addrPtr, addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000664 }
665
666 char clienthost[NI_MAXHOST];
667 char clientservice[NI_MAXSERV];
668
David Reiss23248712010-10-06 17:10:08 +0000669 getnameinfo((sockaddr*) addrPtr, addrLen,
Mark Sleeb4552922007-11-28 00:12:11 +0000670 clienthost, sizeof(clienthost),
671 clientservice, sizeof(clientservice), 0);
672
673 peerHost_ = clienthost;
674 }
675 return peerHost_;
676}
677
678std::string TSocket::getPeerAddress() {
Roger Meier18f10502011-06-04 08:57:43 +0000679 if (peerAddress_.empty() && path_.empty()) {
Mark Sleeb4552922007-11-28 00:12:11 +0000680 struct sockaddr_storage addr;
David Reiss23248712010-10-06 17:10:08 +0000681 struct sockaddr* addrPtr;
682 socklen_t addrLen;
Mark Sleeb4552922007-11-28 00:12:11 +0000683
684 if (socket_ < 0) {
685 return peerAddress_;
686 }
687
David Reiss23248712010-10-06 17:10:08 +0000688 addrPtr = getCachedAddress(&addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000689
David Reiss23248712010-10-06 17:10:08 +0000690 if (addrPtr == NULL) {
691 addrLen = sizeof(addr);
692 if (getpeername(socket_, (sockaddr*) &addr, &addrLen) != 0) {
693 return peerAddress_;
694 }
695 addrPtr = (sockaddr*)&addr;
696
697 setCachedAddress(addrPtr, addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000698 }
699
700 char clienthost[NI_MAXHOST];
701 char clientservice[NI_MAXSERV];
702
David Reiss23248712010-10-06 17:10:08 +0000703 getnameinfo(addrPtr, addrLen,
Mark Sleeb4552922007-11-28 00:12:11 +0000704 clienthost, sizeof(clienthost),
705 clientservice, sizeof(clientservice),
706 NI_NUMERICHOST|NI_NUMERICSERV);
707
708 peerAddress_ = clienthost;
709 peerPort_ = std::atoi(clientservice);
710 }
711 return peerAddress_;
712}
713
714int TSocket::getPeerPort() {
715 getPeerAddress();
716 return peerPort_;
717}
718
David Reiss23248712010-10-06 17:10:08 +0000719void TSocket::setCachedAddress(const sockaddr* addr, socklen_t len) {
Roger Meier18f10502011-06-04 08:57:43 +0000720 if (!path_.empty()) {
721 return;
722 }
723
David Reiss23248712010-10-06 17:10:08 +0000724 switch (addr->sa_family) {
725 case AF_INET:
726 if (len == sizeof(sockaddr_in)) {
727 memcpy((void*)&cachedPeerAddr_.ipv4, (void*)addr, len);
728 }
729 break;
730
731 case AF_INET6:
732 if (len == sizeof(sockaddr_in6)) {
733 memcpy((void*)&cachedPeerAddr_.ipv6, (void*)addr, len);
734 }
735 break;
736 }
737}
738
739sockaddr* TSocket::getCachedAddress(socklen_t* len) const {
740 switch (cachedPeerAddr_.ipv4.sin_family) {
741 case AF_INET:
742 *len = sizeof(sockaddr_in);
743 return (sockaddr*) &cachedPeerAddr_.ipv4;
744
745 case AF_INET6:
746 *len = sizeof(sockaddr_in6);
747 return (sockaddr*) &cachedPeerAddr_.ipv6;
748
749 default:
750 return NULL;
751 }
752}
753
David Reiss1c20c872010-03-09 05:20:14 +0000754bool TSocket::useLowMinRto_ = false;
755void TSocket::setUseLowMinRto(bool useLowMinRto) {
756 useLowMinRto_ = useLowMinRto;
757}
758bool TSocket::getUseLowMinRto() {
759 return useLowMinRto_;
760}
761
T Jake Lucianib5e62212009-01-31 22:36:20 +0000762}}} // apache::thrift::transport