blob: b5ed8292d1c865a2669ecf85b0e8f067a5bb2c51 [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) {
Bryan Duxbury97592662011-08-29 18:05:26 +0000484 // This should only happen if the timeout set with SO_SNDTIMEO expired.
485 // Raise an exception.
486 throw TTransportException(TTransportException::TIMED_OUT,
487 "send timeout expired");
David Reiss105961d2010-10-06 17:10:17 +0000488 }
489 sent += b;
490 }
491}
492
493uint32_t TSocket::write_partial(const uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000494 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000495 throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000496 }
497
Mark Sleee8540632006-05-30 09:24:40 +0000498 uint32_t sent = 0;
Mark Slee256bdc42007-11-27 08:42:19 +0000499
David Reiss105961d2010-10-06 17:10:17 +0000500 int flags = 0;
501#ifdef MSG_NOSIGNAL
502 // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we
503 // check for the EPIPE return condition and close the socket in that case
504 flags |= MSG_NOSIGNAL;
505#endif // ifdef MSG_NOSIGNAL
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000506
David Reiss105961d2010-10-06 17:10:17 +0000507 int b = send(socket_, buf + sent, len - sent, flags);
508 ++g_socket_syscalls;
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000509
David Reiss105961d2010-10-06 17:10:17 +0000510 if (b < 0) {
511 if (errno == EWOULDBLOCK || errno == EAGAIN) {
512 return 0;
513 }
Mark Sleee8540632006-05-30 09:24:40 +0000514 // Fail on a send error
David Reiss105961d2010-10-06 17:10:17 +0000515 int errno_copy = errno;
516 GlobalOutput.perror("TSocket::write_partial() send() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000517
David Reiss105961d2010-10-06 17:10:17 +0000518 if (errno_copy == EPIPE || errno_copy == ECONNRESET || errno_copy == ENOTCONN) {
519 close();
520 throw TTransportException(TTransportException::NOT_OPEN, "write() send()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000521 }
Mark Slee256bdc42007-11-27 08:42:19 +0000522
David Reiss105961d2010-10-06 17:10:17 +0000523 throw TTransportException(TTransportException::UNKNOWN, "write() send()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000524 }
David Reiss105961d2010-10-06 17:10:17 +0000525
526 // Fail on blocked send
527 if (b == 0) {
528 throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0.");
529 }
530 return b;
Mark Sleee8540632006-05-30 09:24:40 +0000531}
532
dweatherford14b0ed62007-10-19 01:03:32 +0000533std::string TSocket::getHost() {
534 return host_;
535}
536
537int TSocket::getPort() {
538 return port_;
539}
540
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000541void TSocket::setHost(string host) {
542 host_ = host;
543}
544
545void TSocket::setPort(int port) {
546 port_ = port;
547}
548
Mark Slee8d7e1f62006-06-07 06:48:56 +0000549void TSocket::setLinger(bool on, int linger) {
Mark Slee29050782006-09-29 00:12:30 +0000550 lingerOn_ = on;
551 lingerVal_ = linger;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000552 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000553 return;
554 }
555
Mark Slee29050782006-09-29 00:12:30 +0000556 struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_};
557 int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
558 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000559 int errno_copy = errno; // Copy errno because we're allocating memory.
560 GlobalOutput.perror("TSocket::setLinger() setsockopt() " + getSocketInfo(), errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000561 }
Mark Sleee8540632006-05-30 09:24:40 +0000562}
563
Mark Slee8d7e1f62006-06-07 06:48:56 +0000564void TSocket::setNoDelay(bool noDelay) {
Mark Slee29050782006-09-29 00:12:30 +0000565 noDelay_ = noDelay;
Roger Meier18f10502011-06-04 08:57:43 +0000566 if (socket_ < 0 || !path_.empty()) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000567 return;
568 }
569
Mark Sleee8540632006-05-30 09:24:40 +0000570 // Set socket to NODELAY
Mark Slee29050782006-09-29 00:12:30 +0000571 int v = noDelay_ ? 1 : 0;
572 int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
573 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000574 int errno_copy = errno; // Copy errno because we're allocating memory.
575 GlobalOutput.perror("TSocket::setNoDelay() setsockopt() " + getSocketInfo(), errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000576 }
Mark Sleee8540632006-05-30 09:24:40 +0000577}
Mark Slee29050782006-09-29 00:12:30 +0000578
579void TSocket::setConnTimeout(int ms) {
580 connTimeout_ = ms;
581}
582
583void TSocket::setRecvTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000584 if (ms < 0) {
585 char errBuf[512];
586 sprintf(errBuf, "TSocket::setRecvTimeout with negative input: %d\n", ms);
587 GlobalOutput(errBuf);
588 return;
589 }
Mark Slee29050782006-09-29 00:12:30 +0000590 recvTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000591
Martin Kraemeree341cb2007-02-05 21:40:38 +0000592 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000593 return;
594 }
595
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000596 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
597 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
598
David Reiss22b18862008-04-08 06:25:45 +0000599 // Copy because poll may modify
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000600 struct timeval r = recvTimeval_;
Mark Slee29050782006-09-29 00:12:30 +0000601 int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r));
602 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000603 int errno_copy = errno; // Copy errno because we're allocating memory.
604 GlobalOutput.perror("TSocket::setRecvTimeout() setsockopt() " + getSocketInfo(), errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000605 }
606}
607
608void TSocket::setSendTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000609 if (ms < 0) {
610 char errBuf[512];
611 sprintf(errBuf, "TSocket::setSendTimeout with negative input: %d\n", ms);
612 GlobalOutput(errBuf);
613 return;
614 }
Mark Slee29050782006-09-29 00:12:30 +0000615 sendTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000616
Martin Kraemeree341cb2007-02-05 21:40:38 +0000617 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000618 return;
619 }
Mark Slee256bdc42007-11-27 08:42:19 +0000620
Mark Slee29050782006-09-29 00:12:30 +0000621 struct timeval s = {(int)(sendTimeout_/1000),
622 (int)((sendTimeout_%1000)*1000)};
623 int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s));
624 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000625 int errno_copy = errno; // Copy errno because we're allocating memory.
626 GlobalOutput.perror("TSocket::setSendTimeout() setsockopt() " + getSocketInfo(), errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000627 }
628}
629
Aditya Agarwale04475b2007-05-23 02:14:58 +0000630void TSocket::setMaxRecvRetries(int maxRecvRetries) {
631 maxRecvRetries_ = maxRecvRetries;
632}
633
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000634string TSocket::getSocketInfo() {
635 std::ostringstream oss;
David Reiss105961d2010-10-06 17:10:17 +0000636 if (host_.empty() || port_ == 0) {
637 oss << "<Host: " << getPeerAddress();
638 oss << " Port: " << getPeerPort() << ">";
639 } else {
640 oss << "<Host: " << host_ << " Port: " << port_ << ">";
641 }
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000642 return oss.str();
643}
644
Mark Sleeb4552922007-11-28 00:12:11 +0000645std::string TSocket::getPeerHost() {
Roger Meier18f10502011-06-04 08:57:43 +0000646 if (peerHost_.empty() && path_.empty()) {
Mark Sleeb4552922007-11-28 00:12:11 +0000647 struct sockaddr_storage addr;
David Reiss23248712010-10-06 17:10:08 +0000648 struct sockaddr* addrPtr;
649 socklen_t addrLen;
Mark Sleeb4552922007-11-28 00:12:11 +0000650
651 if (socket_ < 0) {
652 return host_;
653 }
654
David Reiss23248712010-10-06 17:10:08 +0000655 addrPtr = getCachedAddress(&addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000656
David Reiss23248712010-10-06 17:10:08 +0000657 if (addrPtr == NULL) {
658 addrLen = sizeof(addr);
659 if (getpeername(socket_, (sockaddr*) &addr, &addrLen) != 0) {
660 return peerHost_;
661 }
662 addrPtr = (sockaddr*)&addr;
663
664 setCachedAddress(addrPtr, addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000665 }
666
667 char clienthost[NI_MAXHOST];
668 char clientservice[NI_MAXSERV];
669
David Reiss23248712010-10-06 17:10:08 +0000670 getnameinfo((sockaddr*) addrPtr, addrLen,
Mark Sleeb4552922007-11-28 00:12:11 +0000671 clienthost, sizeof(clienthost),
672 clientservice, sizeof(clientservice), 0);
673
674 peerHost_ = clienthost;
675 }
676 return peerHost_;
677}
678
679std::string TSocket::getPeerAddress() {
Roger Meier18f10502011-06-04 08:57:43 +0000680 if (peerAddress_.empty() && path_.empty()) {
Mark Sleeb4552922007-11-28 00:12:11 +0000681 struct sockaddr_storage addr;
David Reiss23248712010-10-06 17:10:08 +0000682 struct sockaddr* addrPtr;
683 socklen_t addrLen;
Mark Sleeb4552922007-11-28 00:12:11 +0000684
685 if (socket_ < 0) {
686 return peerAddress_;
687 }
688
David Reiss23248712010-10-06 17:10:08 +0000689 addrPtr = getCachedAddress(&addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000690
David Reiss23248712010-10-06 17:10:08 +0000691 if (addrPtr == NULL) {
692 addrLen = sizeof(addr);
693 if (getpeername(socket_, (sockaddr*) &addr, &addrLen) != 0) {
694 return peerAddress_;
695 }
696 addrPtr = (sockaddr*)&addr;
697
698 setCachedAddress(addrPtr, addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000699 }
700
701 char clienthost[NI_MAXHOST];
702 char clientservice[NI_MAXSERV];
703
David Reiss23248712010-10-06 17:10:08 +0000704 getnameinfo(addrPtr, addrLen,
Mark Sleeb4552922007-11-28 00:12:11 +0000705 clienthost, sizeof(clienthost),
706 clientservice, sizeof(clientservice),
707 NI_NUMERICHOST|NI_NUMERICSERV);
708
709 peerAddress_ = clienthost;
710 peerPort_ = std::atoi(clientservice);
711 }
712 return peerAddress_;
713}
714
715int TSocket::getPeerPort() {
716 getPeerAddress();
717 return peerPort_;
718}
719
David Reiss23248712010-10-06 17:10:08 +0000720void TSocket::setCachedAddress(const sockaddr* addr, socklen_t len) {
Roger Meier18f10502011-06-04 08:57:43 +0000721 if (!path_.empty()) {
722 return;
723 }
724
David Reiss23248712010-10-06 17:10:08 +0000725 switch (addr->sa_family) {
726 case AF_INET:
727 if (len == sizeof(sockaddr_in)) {
728 memcpy((void*)&cachedPeerAddr_.ipv4, (void*)addr, len);
729 }
730 break;
731
732 case AF_INET6:
733 if (len == sizeof(sockaddr_in6)) {
734 memcpy((void*)&cachedPeerAddr_.ipv6, (void*)addr, len);
735 }
736 break;
737 }
738}
739
740sockaddr* TSocket::getCachedAddress(socklen_t* len) const {
741 switch (cachedPeerAddr_.ipv4.sin_family) {
742 case AF_INET:
743 *len = sizeof(sockaddr_in);
744 return (sockaddr*) &cachedPeerAddr_.ipv4;
745
746 case AF_INET6:
747 *len = sizeof(sockaddr_in6);
748 return (sockaddr*) &cachedPeerAddr_.ipv6;
749
750 default:
751 return NULL;
752 }
753}
754
David Reiss1c20c872010-03-09 05:20:14 +0000755bool TSocket::useLowMinRto_ = false;
756void TSocket::setUseLowMinRto(bool useLowMinRto) {
757 useLowMinRto_ = useLowMinRto;
758}
759bool TSocket::getUseLowMinRto() {
760 return useLowMinRto_;
761}
762
T Jake Lucianib5e62212009-01-31 22:36:20 +0000763}}} // apache::thrift::transport