blob: 73cb56dd62db7fff51e6d81c4104aca100f12480 [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>
30#include <netdb.h>
31#include <unistd.h>
32#include <errno.h>
Mark Slee29050782006-09-29 00:12:30 +000033#include <fcntl.h>
Mark Sleee8540632006-05-30 09:24:40 +000034
Mark Slee29050782006-09-29 00:12:30 +000035#include "concurrency/Monitor.h"
Marc Slemkod42a2c22006-08-10 03:30:18 +000036#include "TSocket.h"
37#include "TTransportException.h"
Mark Sleee8540632006-05-30 09:24:40 +000038
T Jake Lucianib5e62212009-01-31 22:36:20 +000039namespace apache { namespace thrift { namespace transport {
Marc Slemko6f038a72006-08-03 18:58:09 +000040
Mark Sleee8540632006-05-30 09:24:40 +000041using namespace std;
42
Mark Slee29050782006-09-29 00:12:30 +000043// Global var to track total socket sys calls
Mark Slee8d7e1f62006-06-07 06:48:56 +000044uint32_t g_socket_syscalls = 0;
45
46/**
47 * TSocket implementation.
48 *
Mark Slee8d7e1f62006-06-07 06:48:56 +000049 */
50
Mark Slee256bdc42007-11-27 08:42:19 +000051TSocket::TSocket(string host, int port) :
Mark Slee29050782006-09-29 00:12:30 +000052 host_(host),
53 port_(port),
Bryan Duxburya18364a2010-09-28 14:36:07 +000054 path_(""),
55 socket_(-1),
56 connTimeout_(0),
57 sendTimeout_(0),
58 recvTimeout_(0),
59 lingerOn_(1),
60 lingerVal_(0),
61 noDelay_(1),
62 maxRecvRetries_(5) {
63 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
64 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
65}
66
67TSocket::TSocket(string path) :
68 host_(""),
69 port_(0),
70 path_(path),
Martin Kraemeree341cb2007-02-05 21:40:38 +000071 socket_(-1),
Mark Slee29050782006-09-29 00:12:30 +000072 connTimeout_(0),
73 sendTimeout_(0),
74 recvTimeout_(0),
75 lingerOn_(1),
76 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +000077 noDelay_(1),
78 maxRecvRetries_(5) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000079 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
80 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
David Reiss23248712010-10-06 17:10:08 +000081 cachedPeerAddr_.ipv4.sin_family = AF_UNSPEC;
Mark Sleee8540632006-05-30 09:24:40 +000082}
83
Mark Slee256bdc42007-11-27 08:42:19 +000084TSocket::TSocket() :
Aditya Agarwalebc99e02007-01-15 23:14:58 +000085 host_(""),
86 port_(0),
Bryan Duxburya18364a2010-09-28 14:36:07 +000087 path_(""),
Martin Kraemeree341cb2007-02-05 21:40:38 +000088 socket_(-1),
Aditya Agarwalebc99e02007-01-15 23:14:58 +000089 connTimeout_(0),
90 sendTimeout_(0),
91 recvTimeout_(0),
92 lingerOn_(1),
93 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +000094 noDelay_(1),
95 maxRecvRetries_(5) {
Aditya Agarwalebc99e02007-01-15 23:14:58 +000096 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
97 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
David Reiss23248712010-10-06 17:10:08 +000098 cachedPeerAddr_.ipv4.sin_family = AF_UNSPEC;
Aditya Agarwalebc99e02007-01-15 23:14:58 +000099}
100
Mark Slee29050782006-09-29 00:12:30 +0000101TSocket::TSocket(int socket) :
102 host_(""),
103 port_(0),
Bryan Duxburya18364a2010-09-28 14:36:07 +0000104 path_(""),
Mark Slee29050782006-09-29 00:12:30 +0000105 socket_(socket),
106 connTimeout_(0),
107 sendTimeout_(0),
108 recvTimeout_(0),
109 lingerOn_(1),
110 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +0000111 noDelay_(1),
112 maxRecvRetries_(5) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000113 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
114 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
David Reiss23248712010-10-06 17:10:08 +0000115 cachedPeerAddr_.ipv4.sin_family = AF_UNSPEC;
Mark Slee29050782006-09-29 00:12:30 +0000116}
Mark Slee256bdc42007-11-27 08:42:19 +0000117
Mark Sleee8540632006-05-30 09:24:40 +0000118TSocket::~TSocket() {
119 close();
120}
121
Mark Slee256bdc42007-11-27 08:42:19 +0000122bool TSocket::isOpen() {
123 return (socket_ >= 0);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000124}
125
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000126bool TSocket::peek() {
127 if (!isOpen()) {
128 return false;
129 }
130 uint8_t buf;
131 int r = recv(socket_, &buf, 1, MSG_PEEK);
132 if (r == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000133 int errno_copy = errno;
David Reiss840e7522009-06-04 00:10:50 +0000134 #if defined __FreeBSD__ || defined __MACH__
Kevin Clark022b2242009-03-05 21:05:37 +0000135 /* shigin:
136 * freebsd returns -1 and ECONNRESET if socket was closed by
137 * the other side
138 */
139 if (errno_copy == ECONNRESET)
140 {
141 close();
142 return false;
143 }
144 #endif
David Reiss01e55c12008-07-13 22:18:51 +0000145 GlobalOutput.perror("TSocket::peek() recv() " + getSocketInfo(), errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000146 throw TTransportException(TTransportException::UNKNOWN, "recv()", errno_copy);
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000147 }
148 return (r > 0);
149}
150
Mark Slee6d56eb92007-07-06 22:28:15 +0000151void TSocket::openConnection(struct addrinfo *res) {
Mark Sleea9848d72007-02-21 04:54:05 +0000152 if (isOpen()) {
Bryan Duxbury010f1e02010-09-02 00:56:53 +0000153 return;
Mark Sleea9848d72007-02-21 04:54:05 +0000154 }
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000155
Bryan Duxburya18364a2010-09-28 14:36:07 +0000156 if (! path_.empty()) {
157 socket_ = socket(PF_UNIX, SOCK_STREAM, IPPROTO_IP);
158 } else {
159 socket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
160 }
161
Mark Sleee8540632006-05-30 09:24:40 +0000162 if (socket_ == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000163 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000164 GlobalOutput.perror("TSocket::open() socket() " + getSocketInfo(), errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000165 throw TTransportException(TTransportException::NOT_OPEN, "socket()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000166 }
Mark Slee29050782006-09-29 00:12:30 +0000167
168 // Send timeout
169 if (sendTimeout_ > 0) {
170 setSendTimeout(sendTimeout_);
171 }
172
173 // Recv timeout
174 if (recvTimeout_ > 0) {
175 setRecvTimeout(recvTimeout_);
176 }
177
178 // Linger
179 setLinger(lingerOn_, lingerVal_);
180
181 // No delay
182 setNoDelay(noDelay_);
183
David Reiss1c20c872010-03-09 05:20:14 +0000184 // Uses a low min RTO if asked to.
185#ifdef TCP_LOW_MIN_RTO
186 if (getUseLowMinRto()) {
187 int one = 1;
188 setsockopt(socket_, IPPROTO_TCP, TCP_LOW_MIN_RTO, &one, sizeof(one));
189 }
190#endif
191
192
Mark Slee29050782006-09-29 00:12:30 +0000193 // Set the socket to be non blocking for connect if a timeout exists
Mark Slee256bdc42007-11-27 08:42:19 +0000194 int flags = fcntl(socket_, F_GETFL, 0);
Mark Slee29050782006-09-29 00:12:30 +0000195 if (connTimeout_ > 0) {
Mark Sleea5a783f2007-03-02 19:41:08 +0000196 if (-1 == fcntl(socket_, F_SETFL, flags | O_NONBLOCK)) {
David Reiss9b209552008-04-08 06:26:05 +0000197 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000198 GlobalOutput.perror("TSocket::open() fcntl() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000199 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000200 }
Mark Slee29050782006-09-29 00:12:30 +0000201 } else {
Mark Sleea5a783f2007-03-02 19:41:08 +0000202 if (-1 == fcntl(socket_, F_SETFL, flags & ~O_NONBLOCK)) {
David Reiss9b209552008-04-08 06:26:05 +0000203 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000204 GlobalOutput.perror("TSocket::open() fcntl " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000205 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000206 }
Mark Slee29050782006-09-29 00:12:30 +0000207 }
208
Mark Sleee8540632006-05-30 09:24:40 +0000209 // Connect the socket
Bryan Duxburya18364a2010-09-28 14:36:07 +0000210 int ret;
211 if (! path_.empty()) {
212 struct sockaddr_un address;
213 socklen_t len;
214
215 if (path_.length() > sizeof(address.sun_path)) {
216 int errno_copy = errno;
217 GlobalOutput.perror("TSocket::open() Unix Domain socket path too long", errno_copy);
218 throw TTransportException(TTransportException::NOT_OPEN, " Unix Domain socket path too long");
219 }
220
221 address.sun_family = AF_UNIX;
Roger Meierd11ca5a2010-10-18 08:22:57 +0000222 snprintf(address.sun_path, sizeof(address.sun_path), "%s", path_.c_str());
Bryan Duxburya18364a2010-09-28 14:36:07 +0000223 len = sizeof(address);
224 ret = connect(socket_, (struct sockaddr *) &address, len);
225 } else {
226 ret = connect(socket_, res->ai_addr, res->ai_addrlen);
227 }
Mark Slee256bdc42007-11-27 08:42:19 +0000228
David Reiss9b209552008-04-08 06:26:05 +0000229 // success case
Mark Slee29050782006-09-29 00:12:30 +0000230 if (ret == 0) {
231 goto done;
232 }
233
234 if (errno != EINPROGRESS) {
David Reissbc3dddb2007-08-22 23:20:24 +0000235 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000236 GlobalOutput.perror("TSocket::open() connect() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000237 throw TTransportException(TTransportException::NOT_OPEN, "connect() failed", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000238 }
239
David Reiss22b18862008-04-08 06:25:45 +0000240
241 struct pollfd fds[1];
David Reissc88eb8c2008-06-11 01:18:54 +0000242 std::memset(fds, 0 , sizeof(fds));
David Reiss22b18862008-04-08 06:25:45 +0000243 fds[0].fd = socket_;
244 fds[0].events = POLLOUT;
245 ret = poll(fds, 1, connTimeout_);
Mark Slee29050782006-09-29 00:12:30 +0000246
247 if (ret > 0) {
David Reiss9b209552008-04-08 06:26:05 +0000248 // Ensure the socket is connected and that there are no errors set
Mark Slee29050782006-09-29 00:12:30 +0000249 int val;
250 socklen_t lon;
251 lon = sizeof(int);
252 int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon);
253 if (ret2 == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000254 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000255 GlobalOutput.perror("TSocket::open() getsockopt() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000256 throw TTransportException(TTransportException::NOT_OPEN, "getsockopt()", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000257 }
David Reiss9b209552008-04-08 06:26:05 +0000258 // no errors on socket, go to town
Mark Slee29050782006-09-29 00:12:30 +0000259 if (val == 0) {
260 goto done;
261 }
David Reiss01e55c12008-07-13 22:18:51 +0000262 GlobalOutput.perror("TSocket::open() error on socket (after poll) " + getSocketInfo(), val);
David Reiss9b209552008-04-08 06:26:05 +0000263 throw TTransportException(TTransportException::NOT_OPEN, "socket open() error", val);
Mark Slee29050782006-09-29 00:12:30 +0000264 } else if (ret == 0) {
David Reiss9b209552008-04-08 06:26:05 +0000265 // socket timed out
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000266 string errStr = "TSocket::open() timed out " + getSocketInfo();
267 GlobalOutput(errStr.c_str());
David Reiss9b209552008-04-08 06:26:05 +0000268 throw TTransportException(TTransportException::NOT_OPEN, "open() timed out");
Mark Slee29050782006-09-29 00:12:30 +0000269 } else {
David Reiss9b209552008-04-08 06:26:05 +0000270 // error on poll()
David Reissbc3dddb2007-08-22 23:20:24 +0000271 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000272 GlobalOutput.perror("TSocket::open() poll() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000273 throw TTransportException(TTransportException::NOT_OPEN, "poll() failed", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000274 }
275
276 done:
277 // Set socket back to normal mode (blocking)
278 fcntl(socket_, F_SETFL, flags);
David Reiss23248712010-10-06 17:10:08 +0000279
280 setCachedAddress(res->ai_addr, res->ai_addrlen);
Mark Sleee8540632006-05-30 09:24:40 +0000281}
282
Mark Slee6d56eb92007-07-06 22:28:15 +0000283void TSocket::open() {
284 if (isOpen()) {
Bryan Duxbury010f1e02010-09-02 00:56:53 +0000285 return;
Mark Slee6d56eb92007-07-06 22:28:15 +0000286 }
Bryan Duxburya18364a2010-09-28 14:36:07 +0000287 if (! path_.empty()) {
288 unix_open();
289 } else {
290 local_open();
291 }
292}
293
294void TSocket::unix_open(){
295 if (! path_.empty()) {
296 // Unix Domain SOcket does not need addrinfo struct, so we pass NULL
297 openConnection(NULL);
298 }
299}
300
301void TSocket::local_open(){
302 if (isOpen()) {
303 return;
304 }
Mark Slee6d56eb92007-07-06 22:28:15 +0000305
306 // Validate port number
David Reiss450e35d2010-03-09 05:19:41 +0000307 if (port_ < 0 || port_ > 0xFFFF) {
Mark Slee6d56eb92007-07-06 22:28:15 +0000308 throw TTransportException(TTransportException::NOT_OPEN, "Specified port is invalid");
309 }
310
311 struct addrinfo hints, *res, *res0;
David Reiss9b209552008-04-08 06:26:05 +0000312 res = NULL;
313 res0 = NULL;
Mark Slee6d56eb92007-07-06 22:28:15 +0000314 int error;
David Reiss450e35d2010-03-09 05:19:41 +0000315 char port[sizeof("65535")];
David Reissc88eb8c2008-06-11 01:18:54 +0000316 std::memset(&hints, 0, sizeof(hints));
Mark Slee6d56eb92007-07-06 22:28:15 +0000317 hints.ai_family = PF_UNSPEC;
318 hints.ai_socktype = SOCK_STREAM;
Mark Slee256bdc42007-11-27 08:42:19 +0000319 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
Mark Slee6d56eb92007-07-06 22:28:15 +0000320 sprintf(port, "%d", port_);
Mark Slee256bdc42007-11-27 08:42:19 +0000321
Mark Sleec37b4c52007-12-05 23:03:37 +0000322 error = getaddrinfo(host_.c_str(), port, &hints, &res0);
323
Mark Slee6d56eb92007-07-06 22:28:15 +0000324 if (error) {
David Reiss9b209552008-04-08 06:26:05 +0000325 string errStr = "TSocket::open() getaddrinfo() " + getSocketInfo() + string(gai_strerror(error));
326 GlobalOutput(errStr.c_str());
Mark Slee6d56eb92007-07-06 22:28:15 +0000327 close();
328 throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for client socket.");
329 }
Mark Slee256bdc42007-11-27 08:42:19 +0000330
Mark Slee6d56eb92007-07-06 22:28:15 +0000331 // Cycle through all the returned addresses until one
332 // connects or push the exception up.
333 for (res = res0; res; res = res->ai_next) {
334 try {
335 openConnection(res);
336 break;
337 } catch (TTransportException& ttx) {
338 if (res->ai_next) {
339 close();
340 } else {
341 close();
Mark Slee85287d32007-07-09 19:50:30 +0000342 freeaddrinfo(res0); // cleanup on failure
Mark Slee6d56eb92007-07-06 22:28:15 +0000343 throw;
344 }
345 }
346 }
Mark Slee85287d32007-07-09 19:50:30 +0000347
348 // Free address structure memory
349 freeaddrinfo(res0);
Mark Slee6d56eb92007-07-06 22:28:15 +0000350}
351
Mark Sleee8540632006-05-30 09:24:40 +0000352void TSocket::close() {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000353 if (socket_ >= 0) {
Mark Sleee8540632006-05-30 09:24:40 +0000354 shutdown(socket_, SHUT_RDWR);
355 ::close(socket_);
356 }
Martin Kraemeree341cb2007-02-05 21:40:38 +0000357 socket_ = -1;
Mark Sleee8540632006-05-30 09:24:40 +0000358}
359
David Reiss105961d2010-10-06 17:10:17 +0000360void TSocket::setSocketFD(int socket) {
361 if (socket_ >= 0) {
362 close();
363 }
364 socket_ = socket;
365}
366
Mark Slee8d7e1f62006-06-07 06:48:56 +0000367uint32_t TSocket::read(uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000368 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000369 throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000370 }
Mark Sleee8540632006-05-30 09:24:40 +0000371
Aditya Agarwale04475b2007-05-23 02:14:58 +0000372 int32_t retries = 0;
373
374 // EAGAIN can be signalled both when a timeout has occurred and when
375 // the system is out of resources (an awesome undocumented feature).
376 // The following is an approximation of the time interval under which
377 // EAGAIN is taken to indicate an out of resources error.
378 uint32_t eagainThresholdMicros = 0;
379 if (recvTimeout_) {
Mark Slee256bdc42007-11-27 08:42:19 +0000380 // if a readTimeout is specified along with a max number of recv retries, then
Aditya Agarwale04475b2007-05-23 02:14:58 +0000381 // the threshold will ensure that the read timeout is not exceeded even in the
382 // case of resource errors
383 eagainThresholdMicros = (recvTimeout_*1000)/ ((maxRecvRetries_>0) ? maxRecvRetries_ : 2);
384 }
385
Mark Slee256bdc42007-11-27 08:42:19 +0000386 try_again:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000387 // Read from the socket
Aditya Agarwale04475b2007-05-23 02:14:58 +0000388 struct timeval begin;
David Reiss105961d2010-10-06 17:10:17 +0000389 if (recvTimeout_ > 0) {
390 gettimeofday(&begin, NULL);
391 } else {
392 // if there is no read timeout we don't need the TOD to determine whether
393 // an EAGAIN is due to a timeout or an out-of-resource condition.
394 begin.tv_sec = begin.tv_usec = 0;
395 }
Mark Slee8d7e1f62006-06-07 06:48:56 +0000396 int got = recv(socket_, buf, len, 0);
Kevin Clark022b2242009-03-05 21:05:37 +0000397 int errno_copy = errno; //gettimeofday can change errno
Mark Slee8d7e1f62006-06-07 06:48:56 +0000398 ++g_socket_syscalls;
Aditya Agarwale04475b2007-05-23 02:14:58 +0000399
Mark Slee8d7e1f62006-06-07 06:48:56 +0000400 // Check for error on read
Mark Slee256bdc42007-11-27 08:42:19 +0000401 if (got < 0) {
Kevin Clark022b2242009-03-05 21:05:37 +0000402 if (errno_copy == EAGAIN) {
David Reiss105961d2010-10-06 17:10:17 +0000403 // if no timeout we can assume that resource exhaustion has occurred.
404 if (recvTimeout_ == 0) {
405 throw TTransportException(TTransportException::TIMED_OUT,
406 "EAGAIN (unavailable resources)");
407 }
Aditya Agarwale04475b2007-05-23 02:14:58 +0000408 // check if this is the lack of resources or timeout case
David Reissa1a15112010-03-09 05:19:54 +0000409 struct timeval end;
410 gettimeofday(&end, NULL);
411 uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000)
412 + (((uint64_t)(end.tv_usec - begin.tv_usec))));
413
Aditya Agarwale04475b2007-05-23 02:14:58 +0000414 if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) {
415 if (retries++ < maxRecvRetries_) {
416 usleep(50);
417 goto try_again;
418 } else {
Mark Slee256bdc42007-11-27 08:42:19 +0000419 throw TTransportException(TTransportException::TIMED_OUT,
Aditya Agarwale04475b2007-05-23 02:14:58 +0000420 "EAGAIN (unavailable resources)");
421 }
422 } else {
423 // infer that timeout has been hit
Mark Slee256bdc42007-11-27 08:42:19 +0000424 throw TTransportException(TTransportException::TIMED_OUT,
Aditya Agarwale04475b2007-05-23 02:14:58 +0000425 "EAGAIN (timed out)");
426 }
Mark Sleee8540632006-05-30 09:24:40 +0000427 }
Mark Slee256bdc42007-11-27 08:42:19 +0000428
Mark Slee8d7e1f62006-06-07 06:48:56 +0000429 // If interrupted, try again
Kevin Clark022b2242009-03-05 21:05:37 +0000430 if (errno_copy == EINTR && retries++ < maxRecvRetries_) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000431 goto try_again;
Mark Sleee8540632006-05-30 09:24:40 +0000432 }
Mark Slee256bdc42007-11-27 08:42:19 +0000433
David Reiss840e7522009-06-04 00:10:50 +0000434 #if defined __FreeBSD__ || defined __MACH__
Kevin Clark022b2242009-03-05 21:05:37 +0000435 if (errno_copy == ECONNRESET) {
Kevin Clark022b2242009-03-05 21:05:37 +0000436 /* shigin: freebsd doesn't follow POSIX semantic of recv and fails with
437 * ECONNRESET if peer performed shutdown
David Reiss105961d2010-10-06 17:10:17 +0000438 * edhall: eliminated close() since we do that in the destructor.
Kevin Clark022b2242009-03-05 21:05:37 +0000439 */
Kevin Clark022b2242009-03-05 21:05:37 +0000440 return 0;
David Reiss840e7522009-06-04 00:10:50 +0000441 }
442 #endif
443
444 // Now it's not a try again case, but a real probblez
445 GlobalOutput.perror("TSocket::read() recv() " + getSocketInfo(), errno_copy);
446
447 // If we disconnect with no linger time
448 if (errno_copy == ECONNRESET) {
Mark Sleef9831082007-02-20 20:59:21 +0000449 throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000450 }
Mark Slee256bdc42007-11-27 08:42:19 +0000451
Mark Slee8d7e1f62006-06-07 06:48:56 +0000452 // This ish isn't open
Kevin Clark022b2242009-03-05 21:05:37 +0000453 if (errno_copy == ENOTCONN) {
Mark Sleef9831082007-02-20 20:59:21 +0000454 throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000455 }
Mark Slee256bdc42007-11-27 08:42:19 +0000456
Mark Slee8d7e1f62006-06-07 06:48:56 +0000457 // Timed out!
Kevin Clark022b2242009-03-05 21:05:37 +0000458 if (errno_copy == ETIMEDOUT) {
Mark Sleef9831082007-02-20 20:59:21 +0000459 throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000460 }
Mark Slee256bdc42007-11-27 08:42:19 +0000461
Mark Slee8d7e1f62006-06-07 06:48:56 +0000462 // Some other error, whatevz
David Reiss01e55c12008-07-13 22:18:51 +0000463 throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000464 }
Mark Slee256bdc42007-11-27 08:42:19 +0000465
Mark Slee8d7e1f62006-06-07 06:48:56 +0000466 // The remote host has closed the socket
467 if (got == 0) {
David Reiss105961d2010-10-06 17:10:17 +0000468 // edhall: we used to call close() here, but our caller may want to deal
469 // with the socket fd and we'll close() in our destructor in any case.
Mark Slee8d7e1f62006-06-07 06:48:56 +0000470 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000471 }
Mark Slee256bdc42007-11-27 08:42:19 +0000472
Mark Sleee8540632006-05-30 09:24:40 +0000473 // Pack data into string
Mark Slee8d7e1f62006-06-07 06:48:56 +0000474 return got;
Mark Sleee8540632006-05-30 09:24:40 +0000475}
476
Mark Slee8d7e1f62006-06-07 06:48:56 +0000477void TSocket::write(const uint8_t* buf, uint32_t len) {
David Reiss105961d2010-10-06 17:10:17 +0000478 uint32_t sent = 0;
479
480 while (sent < len) {
481 uint32_t b = write_partial(buf + sent, len - sent);
482 if (b == 0) {
483 // We assume that we got 0 because send() errored with EAGAIN due to
484 // lack of system resources; release the CPU for a bit.
485 usleep(50);
486 }
487 sent += b;
488 }
489}
490
491uint32_t TSocket::write_partial(const uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000492 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000493 throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000494 }
495
Mark Sleee8540632006-05-30 09:24:40 +0000496 uint32_t sent = 0;
Mark Slee256bdc42007-11-27 08:42:19 +0000497
David Reiss105961d2010-10-06 17:10:17 +0000498 int flags = 0;
499#ifdef MSG_NOSIGNAL
500 // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we
501 // check for the EPIPE return condition and close the socket in that case
502 flags |= MSG_NOSIGNAL;
503#endif // ifdef MSG_NOSIGNAL
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000504
David Reiss105961d2010-10-06 17:10:17 +0000505 int b = send(socket_, buf + sent, len - sent, flags);
506 ++g_socket_syscalls;
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000507
David Reiss105961d2010-10-06 17:10:17 +0000508 if (b < 0) {
509 if (errno == EWOULDBLOCK || errno == EAGAIN) {
510 return 0;
511 }
Mark Sleee8540632006-05-30 09:24:40 +0000512 // Fail on a send error
David Reiss105961d2010-10-06 17:10:17 +0000513 int errno_copy = errno;
514 GlobalOutput.perror("TSocket::write_partial() send() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000515
David Reiss105961d2010-10-06 17:10:17 +0000516 if (errno_copy == EPIPE || errno_copy == ECONNRESET || errno_copy == ENOTCONN) {
517 close();
518 throw TTransportException(TTransportException::NOT_OPEN, "write() send()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000519 }
Mark Slee256bdc42007-11-27 08:42:19 +0000520
David Reiss105961d2010-10-06 17:10:17 +0000521 throw TTransportException(TTransportException::UNKNOWN, "write() send()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000522 }
David Reiss105961d2010-10-06 17:10:17 +0000523
524 // Fail on blocked send
525 if (b == 0) {
526 throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0.");
527 }
528 return b;
Mark Sleee8540632006-05-30 09:24:40 +0000529}
530
dweatherford14b0ed62007-10-19 01:03:32 +0000531std::string TSocket::getHost() {
532 return host_;
533}
534
535int TSocket::getPort() {
536 return port_;
537}
538
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000539void TSocket::setHost(string host) {
540 host_ = host;
541}
542
543void TSocket::setPort(int port) {
544 port_ = port;
545}
546
Mark Slee8d7e1f62006-06-07 06:48:56 +0000547void TSocket::setLinger(bool on, int linger) {
Mark Slee29050782006-09-29 00:12:30 +0000548 lingerOn_ = on;
549 lingerVal_ = linger;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000550 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000551 return;
552 }
553
Mark Slee29050782006-09-29 00:12:30 +0000554 struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_};
555 int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
556 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000557 int errno_copy = errno; // Copy errno because we're allocating memory.
558 GlobalOutput.perror("TSocket::setLinger() setsockopt() " + getSocketInfo(), errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000559 }
Mark Sleee8540632006-05-30 09:24:40 +0000560}
561
Mark Slee8d7e1f62006-06-07 06:48:56 +0000562void TSocket::setNoDelay(bool noDelay) {
Mark Slee29050782006-09-29 00:12:30 +0000563 noDelay_ = noDelay;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000564 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000565 return;
566 }
567
Mark Sleee8540632006-05-30 09:24:40 +0000568 // Set socket to NODELAY
Mark Slee29050782006-09-29 00:12:30 +0000569 int v = noDelay_ ? 1 : 0;
570 int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
571 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000572 int errno_copy = errno; // Copy errno because we're allocating memory.
573 GlobalOutput.perror("TSocket::setNoDelay() setsockopt() " + getSocketInfo(), errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000574 }
Mark Sleee8540632006-05-30 09:24:40 +0000575}
Mark Slee29050782006-09-29 00:12:30 +0000576
577void TSocket::setConnTimeout(int ms) {
578 connTimeout_ = ms;
579}
580
581void TSocket::setRecvTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000582 if (ms < 0) {
583 char errBuf[512];
584 sprintf(errBuf, "TSocket::setRecvTimeout with negative input: %d\n", ms);
585 GlobalOutput(errBuf);
586 return;
587 }
Mark Slee29050782006-09-29 00:12:30 +0000588 recvTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000589
Martin Kraemeree341cb2007-02-05 21:40:38 +0000590 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000591 return;
592 }
593
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000594 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
595 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
596
David Reiss22b18862008-04-08 06:25:45 +0000597 // Copy because poll may modify
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000598 struct timeval r = recvTimeval_;
Mark Slee29050782006-09-29 00:12:30 +0000599 int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r));
600 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000601 int errno_copy = errno; // Copy errno because we're allocating memory.
602 GlobalOutput.perror("TSocket::setRecvTimeout() setsockopt() " + getSocketInfo(), errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000603 }
604}
605
606void TSocket::setSendTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000607 if (ms < 0) {
608 char errBuf[512];
609 sprintf(errBuf, "TSocket::setSendTimeout with negative input: %d\n", ms);
610 GlobalOutput(errBuf);
611 return;
612 }
Mark Slee29050782006-09-29 00:12:30 +0000613 sendTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000614
Martin Kraemeree341cb2007-02-05 21:40:38 +0000615 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000616 return;
617 }
Mark Slee256bdc42007-11-27 08:42:19 +0000618
Mark Slee29050782006-09-29 00:12:30 +0000619 struct timeval s = {(int)(sendTimeout_/1000),
620 (int)((sendTimeout_%1000)*1000)};
621 int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s));
622 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000623 int errno_copy = errno; // Copy errno because we're allocating memory.
624 GlobalOutput.perror("TSocket::setSendTimeout() setsockopt() " + getSocketInfo(), errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000625 }
626}
627
Aditya Agarwale04475b2007-05-23 02:14:58 +0000628void TSocket::setMaxRecvRetries(int maxRecvRetries) {
629 maxRecvRetries_ = maxRecvRetries;
630}
631
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000632string TSocket::getSocketInfo() {
633 std::ostringstream oss;
David Reiss105961d2010-10-06 17:10:17 +0000634 if (host_.empty() || port_ == 0) {
635 oss << "<Host: " << getPeerAddress();
636 oss << " Port: " << getPeerPort() << ">";
637 } else {
638 oss << "<Host: " << host_ << " Port: " << port_ << ">";
639 }
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000640 return oss.str();
641}
642
Mark Sleeb4552922007-11-28 00:12:11 +0000643std::string TSocket::getPeerHost() {
644 if (peerHost_.empty()) {
645 struct sockaddr_storage addr;
David Reiss23248712010-10-06 17:10:08 +0000646 struct sockaddr* addrPtr;
647 socklen_t addrLen;
Mark Sleeb4552922007-11-28 00:12:11 +0000648
649 if (socket_ < 0) {
650 return host_;
651 }
652
David Reiss23248712010-10-06 17:10:08 +0000653 addrPtr = getCachedAddress(&addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000654
David Reiss23248712010-10-06 17:10:08 +0000655 if (addrPtr == NULL) {
656 addrLen = sizeof(addr);
657 if (getpeername(socket_, (sockaddr*) &addr, &addrLen) != 0) {
658 return peerHost_;
659 }
660 addrPtr = (sockaddr*)&addr;
661
662 setCachedAddress(addrPtr, addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000663 }
664
665 char clienthost[NI_MAXHOST];
666 char clientservice[NI_MAXSERV];
667
David Reiss23248712010-10-06 17:10:08 +0000668 getnameinfo((sockaddr*) addrPtr, addrLen,
Mark Sleeb4552922007-11-28 00:12:11 +0000669 clienthost, sizeof(clienthost),
670 clientservice, sizeof(clientservice), 0);
671
672 peerHost_ = clienthost;
673 }
674 return peerHost_;
675}
676
677std::string TSocket::getPeerAddress() {
678 if (peerAddress_.empty()) {
679 struct sockaddr_storage addr;
David Reiss23248712010-10-06 17:10:08 +0000680 struct sockaddr* addrPtr;
681 socklen_t addrLen;
Mark Sleeb4552922007-11-28 00:12:11 +0000682
683 if (socket_ < 0) {
684 return peerAddress_;
685 }
686
David Reiss23248712010-10-06 17:10:08 +0000687 addrPtr = getCachedAddress(&addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000688
David Reiss23248712010-10-06 17:10:08 +0000689 if (addrPtr == NULL) {
690 addrLen = sizeof(addr);
691 if (getpeername(socket_, (sockaddr*) &addr, &addrLen) != 0) {
692 return peerAddress_;
693 }
694 addrPtr = (sockaddr*)&addr;
695
696 setCachedAddress(addrPtr, addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000697 }
698
699 char clienthost[NI_MAXHOST];
700 char clientservice[NI_MAXSERV];
701
David Reiss23248712010-10-06 17:10:08 +0000702 getnameinfo(addrPtr, addrLen,
Mark Sleeb4552922007-11-28 00:12:11 +0000703 clienthost, sizeof(clienthost),
704 clientservice, sizeof(clientservice),
705 NI_NUMERICHOST|NI_NUMERICSERV);
706
707 peerAddress_ = clienthost;
708 peerPort_ = std::atoi(clientservice);
709 }
710 return peerAddress_;
711}
712
713int TSocket::getPeerPort() {
714 getPeerAddress();
715 return peerPort_;
716}
717
David Reiss23248712010-10-06 17:10:08 +0000718void TSocket::setCachedAddress(const sockaddr* addr, socklen_t len) {
719 switch (addr->sa_family) {
720 case AF_INET:
721 if (len == sizeof(sockaddr_in)) {
722 memcpy((void*)&cachedPeerAddr_.ipv4, (void*)addr, len);
723 }
724 break;
725
726 case AF_INET6:
727 if (len == sizeof(sockaddr_in6)) {
728 memcpy((void*)&cachedPeerAddr_.ipv6, (void*)addr, len);
729 }
730 break;
731 }
732}
733
734sockaddr* TSocket::getCachedAddress(socklen_t* len) const {
735 switch (cachedPeerAddr_.ipv4.sin_family) {
736 case AF_INET:
737 *len = sizeof(sockaddr_in);
738 return (sockaddr*) &cachedPeerAddr_.ipv4;
739
740 case AF_INET6:
741 *len = sizeof(sockaddr_in6);
742 return (sockaddr*) &cachedPeerAddr_.ipv6;
743
744 default:
745 return NULL;
746 }
747}
748
David Reiss1c20c872010-03-09 05:20:14 +0000749bool TSocket::useLowMinRto_ = false;
750void TSocket::setUseLowMinRto(bool useLowMinRto) {
751 useLowMinRto_ = useLowMinRto;
752}
753bool TSocket::getUseLowMinRto() {
754 return useLowMinRto_;
755}
756
T Jake Lucianib5e62212009-01-31 22:36:20 +0000757}}} // apache::thrift::transport