blob: 2db8f8b7079857c90d284cbc88b013633d419fb9 [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
Roger Meier2fa9c312011-09-05 19:15:53 +000020#ifdef HAVE_CONFIG_H
Marc Slemkoe03da182006-07-21 21:32:36 +000021#include <config.h>
Roger Meier2fa9c312011-09-05 19:15:53 +000022#endif
David Reissc88eb8c2008-06-11 01:18:54 +000023#include <cstring>
24#include <sstream>
Roger Meier2fa9c312011-09-05 19:15:53 +000025#ifdef HAVE_SYS_SOCKET_H
Mark Sleee8540632006-05-30 09:24:40 +000026#include <sys/socket.h>
Roger Meier2fa9c312011-09-05 19:15:53 +000027#endif
28#ifdef HAVE_SYS_UN_H
Bryan Duxburya18364a2010-09-28 14:36:07 +000029#include <sys/un.h>
Roger Meier2fa9c312011-09-05 19:15:53 +000030#endif
31#ifdef HAVE_SYS_POLL_H
David Reiss22b18862008-04-08 06:25:45 +000032#include <sys/poll.h>
Roger Meier2fa9c312011-09-05 19:15:53 +000033#endif
Mark Sleedd564972007-08-21 02:39:57 +000034#include <sys/types.h>
Roger Meier2fa9c312011-09-05 19:15:53 +000035#ifdef HAVE_ARPA_INET_H
Mark Sleee8540632006-05-30 09:24:40 +000036#include <arpa/inet.h>
Roger Meier2fa9c312011-09-05 19:15:53 +000037#endif
38#ifdef HAVE_NETINET_IN_H
Mark Sleee8540632006-05-30 09:24:40 +000039#include <netinet/in.h>
40#include <netinet/tcp.h>
Roger Meier2fa9c312011-09-05 19:15:53 +000041#endif
42#ifdef HAVE_UNISTD_H
Mark Sleee8540632006-05-30 09:24:40 +000043#include <unistd.h>
Roger Meier2fa9c312011-09-05 19:15:53 +000044#endif
Mark Sleee8540632006-05-30 09:24:40 +000045#include <errno.h>
Mark Slee29050782006-09-29 00:12:30 +000046#include <fcntl.h>
Mark Sleee8540632006-05-30 09:24:40 +000047
Mark Slee29050782006-09-29 00:12:30 +000048#include "concurrency/Monitor.h"
Marc Slemkod42a2c22006-08-10 03:30:18 +000049#include "TSocket.h"
50#include "TTransportException.h"
Mark Sleee8540632006-05-30 09:24:40 +000051
Roger Meier84e4a3c2011-09-16 20:58:44 +000052#ifndef SOCKOPT_CAST_T
53# ifndef _WIN32
54# define SOCKOPT_CAST_T void
55# else
56# define SOCKOPT_CAST_T char
57# endif // _WIN32
58#endif
59
60template<class T>
61inline const SOCKOPT_CAST_T* const_cast_sockopt(const T* v) {
62 return reinterpret_cast<const SOCKOPT_CAST_T*>(v);
63}
64
65template<class T>
66inline SOCKOPT_CAST_T* cast_sockopt(T* v) {
67 return reinterpret_cast<SOCKOPT_CAST_T*>(v);
68}
69
T Jake Lucianib5e62212009-01-31 22:36:20 +000070namespace apache { namespace thrift { namespace transport {
Marc Slemko6f038a72006-08-03 18:58:09 +000071
Mark Sleee8540632006-05-30 09:24:40 +000072using namespace std;
73
Mark Slee29050782006-09-29 00:12:30 +000074// Global var to track total socket sys calls
Mark Slee8d7e1f62006-06-07 06:48:56 +000075uint32_t g_socket_syscalls = 0;
76
77/**
78 * TSocket implementation.
79 *
Mark Slee8d7e1f62006-06-07 06:48:56 +000080 */
81
Mark Slee256bdc42007-11-27 08:42:19 +000082TSocket::TSocket(string host, int port) :
Mark Slee29050782006-09-29 00:12:30 +000083 host_(host),
84 port_(port),
Bryan Duxburya18364a2010-09-28 14:36:07 +000085 path_(""),
86 socket_(-1),
87 connTimeout_(0),
88 sendTimeout_(0),
89 recvTimeout_(0),
90 lingerOn_(1),
91 lingerVal_(0),
92 noDelay_(1),
93 maxRecvRetries_(5) {
94 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
95 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
96}
97
98TSocket::TSocket(string path) :
99 host_(""),
100 port_(0),
101 path_(path),
Martin Kraemeree341cb2007-02-05 21:40:38 +0000102 socket_(-1),
Mark Slee29050782006-09-29 00:12:30 +0000103 connTimeout_(0),
104 sendTimeout_(0),
105 recvTimeout_(0),
106 lingerOn_(1),
107 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +0000108 noDelay_(1),
109 maxRecvRetries_(5) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000110 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
111 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
David Reiss23248712010-10-06 17:10:08 +0000112 cachedPeerAddr_.ipv4.sin_family = AF_UNSPEC;
Mark Sleee8540632006-05-30 09:24:40 +0000113}
114
Mark Slee256bdc42007-11-27 08:42:19 +0000115TSocket::TSocket() :
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000116 host_(""),
117 port_(0),
Bryan Duxburya18364a2010-09-28 14:36:07 +0000118 path_(""),
Martin Kraemeree341cb2007-02-05 21:40:38 +0000119 socket_(-1),
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000120 connTimeout_(0),
121 sendTimeout_(0),
122 recvTimeout_(0),
123 lingerOn_(1),
124 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +0000125 noDelay_(1),
126 maxRecvRetries_(5) {
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000127 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
128 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
David Reiss23248712010-10-06 17:10:08 +0000129 cachedPeerAddr_.ipv4.sin_family = AF_UNSPEC;
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000130}
131
Mark Slee29050782006-09-29 00:12:30 +0000132TSocket::TSocket(int socket) :
133 host_(""),
134 port_(0),
Bryan Duxburya18364a2010-09-28 14:36:07 +0000135 path_(""),
Mark Slee29050782006-09-29 00:12:30 +0000136 socket_(socket),
137 connTimeout_(0),
138 sendTimeout_(0),
139 recvTimeout_(0),
140 lingerOn_(1),
141 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +0000142 noDelay_(1),
143 maxRecvRetries_(5) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000144 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
145 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
David Reiss23248712010-10-06 17:10:08 +0000146 cachedPeerAddr_.ipv4.sin_family = AF_UNSPEC;
Mark Slee29050782006-09-29 00:12:30 +0000147}
Mark Slee256bdc42007-11-27 08:42:19 +0000148
Mark Sleee8540632006-05-30 09:24:40 +0000149TSocket::~TSocket() {
150 close();
151}
152
Mark Slee256bdc42007-11-27 08:42:19 +0000153bool TSocket::isOpen() {
154 return (socket_ >= 0);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000155}
156
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000157bool TSocket::peek() {
158 if (!isOpen()) {
159 return false;
160 }
161 uint8_t buf;
Roger Meier84e4a3c2011-09-16 20:58:44 +0000162 int r = recv(socket_, cast_sockopt(&buf), 1, MSG_PEEK);
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000163 if (r == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000164 int errno_copy = errno;
David Reiss840e7522009-06-04 00:10:50 +0000165 #if defined __FreeBSD__ || defined __MACH__
Kevin Clark022b2242009-03-05 21:05:37 +0000166 /* shigin:
167 * freebsd returns -1 and ECONNRESET if socket was closed by
168 * the other side
169 */
170 if (errno_copy == ECONNRESET)
171 {
172 close();
173 return false;
174 }
175 #endif
David Reiss01e55c12008-07-13 22:18:51 +0000176 GlobalOutput.perror("TSocket::peek() recv() " + getSocketInfo(), errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000177 throw TTransportException(TTransportException::UNKNOWN, "recv()", errno_copy);
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000178 }
179 return (r > 0);
180}
181
Mark Slee6d56eb92007-07-06 22:28:15 +0000182void TSocket::openConnection(struct addrinfo *res) {
Roger Meier84e4a3c2011-09-16 20:58:44 +0000183
Mark Sleea9848d72007-02-21 04:54:05 +0000184 if (isOpen()) {
Bryan Duxbury010f1e02010-09-02 00:56:53 +0000185 return;
Mark Sleea9848d72007-02-21 04:54:05 +0000186 }
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000187
Bryan Duxburya18364a2010-09-28 14:36:07 +0000188 if (! path_.empty()) {
189 socket_ = socket(PF_UNIX, SOCK_STREAM, IPPROTO_IP);
190 } else {
191 socket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
192 }
193
Mark Sleee8540632006-05-30 09:24:40 +0000194 if (socket_ == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000195 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000196 GlobalOutput.perror("TSocket::open() socket() " + getSocketInfo(), errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000197 throw TTransportException(TTransportException::NOT_OPEN, "socket()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000198 }
Mark Slee29050782006-09-29 00:12:30 +0000199
200 // Send timeout
201 if (sendTimeout_ > 0) {
202 setSendTimeout(sendTimeout_);
203 }
204
205 // Recv timeout
206 if (recvTimeout_ > 0) {
207 setRecvTimeout(recvTimeout_);
208 }
209
210 // Linger
211 setLinger(lingerOn_, lingerVal_);
212
213 // No delay
214 setNoDelay(noDelay_);
215
David Reiss1c20c872010-03-09 05:20:14 +0000216 // Uses a low min RTO if asked to.
217#ifdef TCP_LOW_MIN_RTO
218 if (getUseLowMinRto()) {
219 int one = 1;
220 setsockopt(socket_, IPPROTO_TCP, TCP_LOW_MIN_RTO, &one, sizeof(one));
221 }
222#endif
223
224
Mark Slee29050782006-09-29 00:12:30 +0000225 // Set the socket to be non blocking for connect if a timeout exists
Mark Slee256bdc42007-11-27 08:42:19 +0000226 int flags = fcntl(socket_, F_GETFL, 0);
Mark Slee29050782006-09-29 00:12:30 +0000227 if (connTimeout_ > 0) {
Mark Sleea5a783f2007-03-02 19:41:08 +0000228 if (-1 == fcntl(socket_, F_SETFL, flags | O_NONBLOCK)) {
David Reiss9b209552008-04-08 06:26:05 +0000229 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000230 GlobalOutput.perror("TSocket::open() fcntl() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000231 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000232 }
Mark Slee29050782006-09-29 00:12:30 +0000233 } else {
Mark Sleea5a783f2007-03-02 19:41:08 +0000234 if (-1 == fcntl(socket_, F_SETFL, flags & ~O_NONBLOCK)) {
David Reiss9b209552008-04-08 06:26:05 +0000235 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000236 GlobalOutput.perror("TSocket::open() fcntl " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000237 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000238 }
Mark Slee29050782006-09-29 00:12:30 +0000239 }
240
Mark Sleee8540632006-05-30 09:24:40 +0000241 // Connect the socket
Bryan Duxburya18364a2010-09-28 14:36:07 +0000242 int ret;
243 if (! path_.empty()) {
Roger Meier84e4a3c2011-09-16 20:58:44 +0000244
245#ifndef _WIN32
246
Bryan Duxburya18364a2010-09-28 14:36:07 +0000247 struct sockaddr_un address;
248 socklen_t len;
249
250 if (path_.length() > sizeof(address.sun_path)) {
251 int errno_copy = errno;
252 GlobalOutput.perror("TSocket::open() Unix Domain socket path too long", errno_copy);
253 throw TTransportException(TTransportException::NOT_OPEN, " Unix Domain socket path too long");
254 }
255
256 address.sun_family = AF_UNIX;
Roger Meierd11ca5a2010-10-18 08:22:57 +0000257 snprintf(address.sun_path, sizeof(address.sun_path), "%s", path_.c_str());
Bryan Duxburya18364a2010-09-28 14:36:07 +0000258 len = sizeof(address);
259 ret = connect(socket_, (struct sockaddr *) &address, len);
Roger Meier84e4a3c2011-09-16 20:58:44 +0000260
261#else
262 GlobalOutput.perror("TSocket::open() Unix Domain socket path not supported on windows", -99);
263 throw TTransportException(TTransportException::NOT_OPEN, " Unix Domain socket path not supported");
264#endif
265
Bryan Duxburya18364a2010-09-28 14:36:07 +0000266 } else {
267 ret = connect(socket_, res->ai_addr, res->ai_addrlen);
268 }
Mark Slee256bdc42007-11-27 08:42:19 +0000269
David Reiss9b209552008-04-08 06:26:05 +0000270 // success case
Mark Slee29050782006-09-29 00:12:30 +0000271 if (ret == 0) {
272 goto done;
273 }
274
Roger Meier8cd3b1a2011-09-23 14:25:22 +0000275 if ((errno != EINPROGRESS) && (errno != EWOULDBLOCK)) {
David Reissbc3dddb2007-08-22 23:20:24 +0000276 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000277 GlobalOutput.perror("TSocket::open() connect() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000278 throw TTransportException(TTransportException::NOT_OPEN, "connect() failed", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000279 }
280
David Reiss22b18862008-04-08 06:25:45 +0000281
282 struct pollfd fds[1];
David Reissc88eb8c2008-06-11 01:18:54 +0000283 std::memset(fds, 0 , sizeof(fds));
David Reiss22b18862008-04-08 06:25:45 +0000284 fds[0].fd = socket_;
285 fds[0].events = POLLOUT;
286 ret = poll(fds, 1, connTimeout_);
Mark Slee29050782006-09-29 00:12:30 +0000287
288 if (ret > 0) {
David Reiss9b209552008-04-08 06:26:05 +0000289 // Ensure the socket is connected and that there are no errors set
Mark Slee29050782006-09-29 00:12:30 +0000290 int val;
291 socklen_t lon;
292 lon = sizeof(int);
Roger Meier84e4a3c2011-09-16 20:58:44 +0000293 int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, cast_sockopt(&val), &lon);
Mark Slee29050782006-09-29 00:12:30 +0000294 if (ret2 == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000295 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000296 GlobalOutput.perror("TSocket::open() getsockopt() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000297 throw TTransportException(TTransportException::NOT_OPEN, "getsockopt()", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000298 }
David Reiss9b209552008-04-08 06:26:05 +0000299 // no errors on socket, go to town
Mark Slee29050782006-09-29 00:12:30 +0000300 if (val == 0) {
301 goto done;
302 }
David Reiss01e55c12008-07-13 22:18:51 +0000303 GlobalOutput.perror("TSocket::open() error on socket (after poll) " + getSocketInfo(), val);
David Reiss9b209552008-04-08 06:26:05 +0000304 throw TTransportException(TTransportException::NOT_OPEN, "socket open() error", val);
Mark Slee29050782006-09-29 00:12:30 +0000305 } else if (ret == 0) {
David Reiss9b209552008-04-08 06:26:05 +0000306 // socket timed out
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000307 string errStr = "TSocket::open() timed out " + getSocketInfo();
308 GlobalOutput(errStr.c_str());
David Reiss9b209552008-04-08 06:26:05 +0000309 throw TTransportException(TTransportException::NOT_OPEN, "open() timed out");
Mark Slee29050782006-09-29 00:12:30 +0000310 } else {
David Reiss9b209552008-04-08 06:26:05 +0000311 // error on poll()
David Reissbc3dddb2007-08-22 23:20:24 +0000312 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000313 GlobalOutput.perror("TSocket::open() poll() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000314 throw TTransportException(TTransportException::NOT_OPEN, "poll() failed", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000315 }
316
317 done:
318 // Set socket back to normal mode (blocking)
319 fcntl(socket_, F_SETFL, flags);
David Reiss23248712010-10-06 17:10:08 +0000320
Roger Meier18f10502011-06-04 08:57:43 +0000321 if (path_.empty()) {
322 setCachedAddress(res->ai_addr, res->ai_addrlen);
323 }
Mark Sleee8540632006-05-30 09:24:40 +0000324}
325
Mark Slee6d56eb92007-07-06 22:28:15 +0000326void TSocket::open() {
327 if (isOpen()) {
Bryan Duxbury010f1e02010-09-02 00:56:53 +0000328 return;
Mark Slee6d56eb92007-07-06 22:28:15 +0000329 }
Bryan Duxburya18364a2010-09-28 14:36:07 +0000330 if (! path_.empty()) {
331 unix_open();
332 } else {
333 local_open();
334 }
335}
336
337void TSocket::unix_open(){
338 if (! path_.empty()) {
339 // Unix Domain SOcket does not need addrinfo struct, so we pass NULL
340 openConnection(NULL);
341 }
342}
343
344void TSocket::local_open(){
Roger Meier8cd3b1a2011-09-23 14:25:22 +0000345
346#ifdef _WIN32
347 TWinsockSingleton::create();
348#endif // _WIN32
349
Bryan Duxburya18364a2010-09-28 14:36:07 +0000350 if (isOpen()) {
351 return;
352 }
Mark Slee6d56eb92007-07-06 22:28:15 +0000353
354 // Validate port number
David Reiss450e35d2010-03-09 05:19:41 +0000355 if (port_ < 0 || port_ > 0xFFFF) {
Mark Slee6d56eb92007-07-06 22:28:15 +0000356 throw TTransportException(TTransportException::NOT_OPEN, "Specified port is invalid");
357 }
358
359 struct addrinfo hints, *res, *res0;
David Reiss9b209552008-04-08 06:26:05 +0000360 res = NULL;
361 res0 = NULL;
Mark Slee6d56eb92007-07-06 22:28:15 +0000362 int error;
David Reiss450e35d2010-03-09 05:19:41 +0000363 char port[sizeof("65535")];
David Reissc88eb8c2008-06-11 01:18:54 +0000364 std::memset(&hints, 0, sizeof(hints));
Mark Slee6d56eb92007-07-06 22:28:15 +0000365 hints.ai_family = PF_UNSPEC;
366 hints.ai_socktype = SOCK_STREAM;
Mark Slee256bdc42007-11-27 08:42:19 +0000367 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
Mark Slee6d56eb92007-07-06 22:28:15 +0000368 sprintf(port, "%d", port_);
Mark Slee256bdc42007-11-27 08:42:19 +0000369
Mark Sleec37b4c52007-12-05 23:03:37 +0000370 error = getaddrinfo(host_.c_str(), port, &hints, &res0);
371
Mark Slee6d56eb92007-07-06 22:28:15 +0000372 if (error) {
David Reiss9b209552008-04-08 06:26:05 +0000373 string errStr = "TSocket::open() getaddrinfo() " + getSocketInfo() + string(gai_strerror(error));
374 GlobalOutput(errStr.c_str());
Mark Slee6d56eb92007-07-06 22:28:15 +0000375 close();
376 throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for client socket.");
377 }
Mark Slee256bdc42007-11-27 08:42:19 +0000378
Mark Slee6d56eb92007-07-06 22:28:15 +0000379 // Cycle through all the returned addresses until one
380 // connects or push the exception up.
381 for (res = res0; res; res = res->ai_next) {
382 try {
383 openConnection(res);
384 break;
385 } catch (TTransportException& ttx) {
386 if (res->ai_next) {
387 close();
388 } else {
389 close();
Mark Slee85287d32007-07-09 19:50:30 +0000390 freeaddrinfo(res0); // cleanup on failure
Mark Slee6d56eb92007-07-06 22:28:15 +0000391 throw;
392 }
393 }
394 }
Mark Slee85287d32007-07-09 19:50:30 +0000395
396 // Free address structure memory
397 freeaddrinfo(res0);
Mark Slee6d56eb92007-07-06 22:28:15 +0000398}
399
Mark Sleee8540632006-05-30 09:24:40 +0000400void TSocket::close() {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000401 if (socket_ >= 0) {
Roger Meier84e4a3c2011-09-16 20:58:44 +0000402
403#ifdef _WIN32
404 shutdown(socket_, SD_BOTH);
405 ::closesocket(socket_);
406#else
407 shutdown(socket_, SHUT_RDWR);
408 ::close(socket_);
409#endif
410
Mark Sleee8540632006-05-30 09:24:40 +0000411 }
Martin Kraemeree341cb2007-02-05 21:40:38 +0000412 socket_ = -1;
Mark Sleee8540632006-05-30 09:24:40 +0000413}
414
David Reiss105961d2010-10-06 17:10:17 +0000415void TSocket::setSocketFD(int socket) {
416 if (socket_ >= 0) {
417 close();
418 }
419 socket_ = socket;
420}
421
Mark Slee8d7e1f62006-06-07 06:48:56 +0000422uint32_t TSocket::read(uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000423 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000424 throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000425 }
Mark Sleee8540632006-05-30 09:24:40 +0000426
Aditya Agarwale04475b2007-05-23 02:14:58 +0000427 int32_t retries = 0;
428
429 // EAGAIN can be signalled both when a timeout has occurred and when
430 // the system is out of resources (an awesome undocumented feature).
431 // The following is an approximation of the time interval under which
432 // EAGAIN is taken to indicate an out of resources error.
433 uint32_t eagainThresholdMicros = 0;
434 if (recvTimeout_) {
Mark Slee256bdc42007-11-27 08:42:19 +0000435 // if a readTimeout is specified along with a max number of recv retries, then
Aditya Agarwale04475b2007-05-23 02:14:58 +0000436 // the threshold will ensure that the read timeout is not exceeded even in the
437 // case of resource errors
438 eagainThresholdMicros = (recvTimeout_*1000)/ ((maxRecvRetries_>0) ? maxRecvRetries_ : 2);
439 }
440
Mark Slee256bdc42007-11-27 08:42:19 +0000441 try_again:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000442 // Read from the socket
Aditya Agarwale04475b2007-05-23 02:14:58 +0000443 struct timeval begin;
David Reiss105961d2010-10-06 17:10:17 +0000444 if (recvTimeout_ > 0) {
445 gettimeofday(&begin, NULL);
446 } else {
447 // if there is no read timeout we don't need the TOD to determine whether
448 // an EAGAIN is due to a timeout or an out-of-resource condition.
449 begin.tv_sec = begin.tv_usec = 0;
450 }
Roger Meier84e4a3c2011-09-16 20:58:44 +0000451 int got = recv(socket_, cast_sockopt(buf), len, 0);
Kevin Clark022b2242009-03-05 21:05:37 +0000452 int errno_copy = errno; //gettimeofday can change errno
Mark Slee8d7e1f62006-06-07 06:48:56 +0000453 ++g_socket_syscalls;
Aditya Agarwale04475b2007-05-23 02:14:58 +0000454
Mark Slee8d7e1f62006-06-07 06:48:56 +0000455 // Check for error on read
Mark Slee256bdc42007-11-27 08:42:19 +0000456 if (got < 0) {
Kevin Clark022b2242009-03-05 21:05:37 +0000457 if (errno_copy == EAGAIN) {
David Reiss105961d2010-10-06 17:10:17 +0000458 // if no timeout we can assume that resource exhaustion has occurred.
459 if (recvTimeout_ == 0) {
460 throw TTransportException(TTransportException::TIMED_OUT,
461 "EAGAIN (unavailable resources)");
462 }
Aditya Agarwale04475b2007-05-23 02:14:58 +0000463 // check if this is the lack of resources or timeout case
David Reissa1a15112010-03-09 05:19:54 +0000464 struct timeval end;
465 gettimeofday(&end, NULL);
466 uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000)
467 + (((uint64_t)(end.tv_usec - begin.tv_usec))));
468
Aditya Agarwale04475b2007-05-23 02:14:58 +0000469 if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) {
470 if (retries++ < maxRecvRetries_) {
471 usleep(50);
472 goto try_again;
473 } else {
Mark Slee256bdc42007-11-27 08:42:19 +0000474 throw TTransportException(TTransportException::TIMED_OUT,
Aditya Agarwale04475b2007-05-23 02:14:58 +0000475 "EAGAIN (unavailable resources)");
476 }
477 } else {
478 // infer that timeout has been hit
Mark Slee256bdc42007-11-27 08:42:19 +0000479 throw TTransportException(TTransportException::TIMED_OUT,
Aditya Agarwale04475b2007-05-23 02:14:58 +0000480 "EAGAIN (timed out)");
481 }
Mark Sleee8540632006-05-30 09:24:40 +0000482 }
Mark Slee256bdc42007-11-27 08:42:19 +0000483
Mark Slee8d7e1f62006-06-07 06:48:56 +0000484 // If interrupted, try again
Kevin Clark022b2242009-03-05 21:05:37 +0000485 if (errno_copy == EINTR && retries++ < maxRecvRetries_) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000486 goto try_again;
Mark Sleee8540632006-05-30 09:24:40 +0000487 }
Mark Slee256bdc42007-11-27 08:42:19 +0000488
David Reiss840e7522009-06-04 00:10:50 +0000489 #if defined __FreeBSD__ || defined __MACH__
Kevin Clark022b2242009-03-05 21:05:37 +0000490 if (errno_copy == ECONNRESET) {
Kevin Clark022b2242009-03-05 21:05:37 +0000491 /* shigin: freebsd doesn't follow POSIX semantic of recv and fails with
492 * ECONNRESET if peer performed shutdown
David Reiss105961d2010-10-06 17:10:17 +0000493 * edhall: eliminated close() since we do that in the destructor.
Kevin Clark022b2242009-03-05 21:05:37 +0000494 */
Kevin Clark022b2242009-03-05 21:05:37 +0000495 return 0;
David Reiss840e7522009-06-04 00:10:50 +0000496 }
497 #endif
498
499 // Now it's not a try again case, but a real probblez
500 GlobalOutput.perror("TSocket::read() recv() " + getSocketInfo(), errno_copy);
501
502 // If we disconnect with no linger time
503 if (errno_copy == ECONNRESET) {
Mark Sleef9831082007-02-20 20:59:21 +0000504 throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000505 }
Mark Slee256bdc42007-11-27 08:42:19 +0000506
Mark Slee8d7e1f62006-06-07 06:48:56 +0000507 // This ish isn't open
Kevin Clark022b2242009-03-05 21:05:37 +0000508 if (errno_copy == ENOTCONN) {
Mark Sleef9831082007-02-20 20:59:21 +0000509 throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000510 }
Mark Slee256bdc42007-11-27 08:42:19 +0000511
Mark Slee8d7e1f62006-06-07 06:48:56 +0000512 // Timed out!
Kevin Clark022b2242009-03-05 21:05:37 +0000513 if (errno_copy == ETIMEDOUT) {
Mark Sleef9831082007-02-20 20:59:21 +0000514 throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000515 }
Mark Slee256bdc42007-11-27 08:42:19 +0000516
Mark Slee8d7e1f62006-06-07 06:48:56 +0000517 // Some other error, whatevz
David Reiss01e55c12008-07-13 22:18:51 +0000518 throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000519 }
Mark Slee256bdc42007-11-27 08:42:19 +0000520
Mark Slee8d7e1f62006-06-07 06:48:56 +0000521 // The remote host has closed the socket
522 if (got == 0) {
David Reiss105961d2010-10-06 17:10:17 +0000523 // edhall: we used to call close() here, but our caller may want to deal
524 // with the socket fd and we'll close() in our destructor in any case.
Mark Slee8d7e1f62006-06-07 06:48:56 +0000525 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000526 }
Mark Slee256bdc42007-11-27 08:42:19 +0000527
Mark Sleee8540632006-05-30 09:24:40 +0000528 // Pack data into string
Mark Slee8d7e1f62006-06-07 06:48:56 +0000529 return got;
Mark Sleee8540632006-05-30 09:24:40 +0000530}
531
Mark Slee8d7e1f62006-06-07 06:48:56 +0000532void TSocket::write(const uint8_t* buf, uint32_t len) {
David Reiss105961d2010-10-06 17:10:17 +0000533 uint32_t sent = 0;
534
535 while (sent < len) {
536 uint32_t b = write_partial(buf + sent, len - sent);
537 if (b == 0) {
Bryan Duxbury97592662011-08-29 18:05:26 +0000538 // This should only happen if the timeout set with SO_SNDTIMEO expired.
539 // Raise an exception.
540 throw TTransportException(TTransportException::TIMED_OUT,
541 "send timeout expired");
David Reiss105961d2010-10-06 17:10:17 +0000542 }
543 sent += b;
544 }
545}
546
547uint32_t TSocket::write_partial(const uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000548 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000549 throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000550 }
551
Mark Sleee8540632006-05-30 09:24:40 +0000552 uint32_t sent = 0;
Mark Slee256bdc42007-11-27 08:42:19 +0000553
David Reiss105961d2010-10-06 17:10:17 +0000554 int flags = 0;
555#ifdef MSG_NOSIGNAL
556 // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we
557 // check for the EPIPE return condition and close the socket in that case
558 flags |= MSG_NOSIGNAL;
559#endif // ifdef MSG_NOSIGNAL
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000560
Roger Meier84e4a3c2011-09-16 20:58:44 +0000561 int b = send(socket_, const_cast_sockopt(buf + sent), len - sent, flags);
David Reiss105961d2010-10-06 17:10:17 +0000562 ++g_socket_syscalls;
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000563
David Reiss105961d2010-10-06 17:10:17 +0000564 if (b < 0) {
565 if (errno == EWOULDBLOCK || errno == EAGAIN) {
566 return 0;
567 }
Mark Sleee8540632006-05-30 09:24:40 +0000568 // Fail on a send error
David Reiss105961d2010-10-06 17:10:17 +0000569 int errno_copy = errno;
570 GlobalOutput.perror("TSocket::write_partial() send() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000571
David Reiss105961d2010-10-06 17:10:17 +0000572 if (errno_copy == EPIPE || errno_copy == ECONNRESET || errno_copy == ENOTCONN) {
573 close();
574 throw TTransportException(TTransportException::NOT_OPEN, "write() send()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000575 }
Mark Slee256bdc42007-11-27 08:42:19 +0000576
David Reiss105961d2010-10-06 17:10:17 +0000577 throw TTransportException(TTransportException::UNKNOWN, "write() send()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000578 }
David Reiss105961d2010-10-06 17:10:17 +0000579
580 // Fail on blocked send
581 if (b == 0) {
582 throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0.");
583 }
584 return b;
Mark Sleee8540632006-05-30 09:24:40 +0000585}
586
dweatherford14b0ed62007-10-19 01:03:32 +0000587std::string TSocket::getHost() {
588 return host_;
589}
590
591int TSocket::getPort() {
592 return port_;
593}
594
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000595void TSocket::setHost(string host) {
596 host_ = host;
597}
598
599void TSocket::setPort(int port) {
600 port_ = port;
601}
602
Mark Slee8d7e1f62006-06-07 06:48:56 +0000603void TSocket::setLinger(bool on, int linger) {
Mark Slee29050782006-09-29 00:12:30 +0000604 lingerOn_ = on;
605 lingerVal_ = linger;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000606 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000607 return;
608 }
609
Mark Slee29050782006-09-29 00:12:30 +0000610 struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_};
Roger Meier84e4a3c2011-09-16 20:58:44 +0000611 int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, cast_sockopt(&l), sizeof(l));
Mark Slee29050782006-09-29 00:12:30 +0000612 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000613 int errno_copy = errno; // Copy errno because we're allocating memory.
614 GlobalOutput.perror("TSocket::setLinger() setsockopt() " + getSocketInfo(), errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000615 }
Mark Sleee8540632006-05-30 09:24:40 +0000616}
617
Mark Slee8d7e1f62006-06-07 06:48:56 +0000618void TSocket::setNoDelay(bool noDelay) {
Mark Slee29050782006-09-29 00:12:30 +0000619 noDelay_ = noDelay;
Roger Meier18f10502011-06-04 08:57:43 +0000620 if (socket_ < 0 || !path_.empty()) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000621 return;
622 }
623
Mark Sleee8540632006-05-30 09:24:40 +0000624 // Set socket to NODELAY
Mark Slee29050782006-09-29 00:12:30 +0000625 int v = noDelay_ ? 1 : 0;
Roger Meier84e4a3c2011-09-16 20:58:44 +0000626 int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, cast_sockopt(&v), sizeof(v));
Mark Slee29050782006-09-29 00:12:30 +0000627 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000628 int errno_copy = errno; // Copy errno because we're allocating memory.
629 GlobalOutput.perror("TSocket::setNoDelay() setsockopt() " + getSocketInfo(), errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000630 }
Mark Sleee8540632006-05-30 09:24:40 +0000631}
Mark Slee29050782006-09-29 00:12:30 +0000632
633void TSocket::setConnTimeout(int ms) {
634 connTimeout_ = ms;
635}
636
637void TSocket::setRecvTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000638 if (ms < 0) {
639 char errBuf[512];
640 sprintf(errBuf, "TSocket::setRecvTimeout with negative input: %d\n", ms);
641 GlobalOutput(errBuf);
642 return;
643 }
Mark Slee29050782006-09-29 00:12:30 +0000644 recvTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000645
Martin Kraemeree341cb2007-02-05 21:40:38 +0000646 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000647 return;
648 }
649
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000650 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
651 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
652
David Reiss22b18862008-04-08 06:25:45 +0000653 // Copy because poll may modify
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000654 struct timeval r = recvTimeval_;
Roger Meier84e4a3c2011-09-16 20:58:44 +0000655 int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, cast_sockopt(&r), sizeof(r));
Mark Slee29050782006-09-29 00:12:30 +0000656 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000657 int errno_copy = errno; // Copy errno because we're allocating memory.
658 GlobalOutput.perror("TSocket::setRecvTimeout() setsockopt() " + getSocketInfo(), errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000659 }
660}
661
662void TSocket::setSendTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000663 if (ms < 0) {
664 char errBuf[512];
665 sprintf(errBuf, "TSocket::setSendTimeout with negative input: %d\n", ms);
666 GlobalOutput(errBuf);
667 return;
668 }
Mark Slee29050782006-09-29 00:12:30 +0000669 sendTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000670
Martin Kraemeree341cb2007-02-05 21:40:38 +0000671 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000672 return;
673 }
Mark Slee256bdc42007-11-27 08:42:19 +0000674
Mark Slee29050782006-09-29 00:12:30 +0000675 struct timeval s = {(int)(sendTimeout_/1000),
676 (int)((sendTimeout_%1000)*1000)};
Roger Meier84e4a3c2011-09-16 20:58:44 +0000677 int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, cast_sockopt(&s), sizeof(s));
Mark Slee29050782006-09-29 00:12:30 +0000678 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000679 int errno_copy = errno; // Copy errno because we're allocating memory.
680 GlobalOutput.perror("TSocket::setSendTimeout() setsockopt() " + getSocketInfo(), errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000681 }
682}
683
Aditya Agarwale04475b2007-05-23 02:14:58 +0000684void TSocket::setMaxRecvRetries(int maxRecvRetries) {
685 maxRecvRetries_ = maxRecvRetries;
686}
687
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000688string TSocket::getSocketInfo() {
689 std::ostringstream oss;
David Reiss105961d2010-10-06 17:10:17 +0000690 if (host_.empty() || port_ == 0) {
691 oss << "<Host: " << getPeerAddress();
692 oss << " Port: " << getPeerPort() << ">";
693 } else {
694 oss << "<Host: " << host_ << " Port: " << port_ << ">";
695 }
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000696 return oss.str();
697}
698
Mark Sleeb4552922007-11-28 00:12:11 +0000699std::string TSocket::getPeerHost() {
Roger Meier18f10502011-06-04 08:57:43 +0000700 if (peerHost_.empty() && path_.empty()) {
Mark Sleeb4552922007-11-28 00:12:11 +0000701 struct sockaddr_storage addr;
David Reiss23248712010-10-06 17:10:08 +0000702 struct sockaddr* addrPtr;
703 socklen_t addrLen;
Mark Sleeb4552922007-11-28 00:12:11 +0000704
705 if (socket_ < 0) {
706 return host_;
707 }
708
David Reiss23248712010-10-06 17:10:08 +0000709 addrPtr = getCachedAddress(&addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000710
David Reiss23248712010-10-06 17:10:08 +0000711 if (addrPtr == NULL) {
712 addrLen = sizeof(addr);
713 if (getpeername(socket_, (sockaddr*) &addr, &addrLen) != 0) {
714 return peerHost_;
715 }
716 addrPtr = (sockaddr*)&addr;
717
718 setCachedAddress(addrPtr, addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000719 }
720
721 char clienthost[NI_MAXHOST];
722 char clientservice[NI_MAXSERV];
723
David Reiss23248712010-10-06 17:10:08 +0000724 getnameinfo((sockaddr*) addrPtr, addrLen,
Mark Sleeb4552922007-11-28 00:12:11 +0000725 clienthost, sizeof(clienthost),
726 clientservice, sizeof(clientservice), 0);
727
728 peerHost_ = clienthost;
729 }
730 return peerHost_;
731}
732
733std::string TSocket::getPeerAddress() {
Roger Meier18f10502011-06-04 08:57:43 +0000734 if (peerAddress_.empty() && path_.empty()) {
Mark Sleeb4552922007-11-28 00:12:11 +0000735 struct sockaddr_storage addr;
David Reiss23248712010-10-06 17:10:08 +0000736 struct sockaddr* addrPtr;
737 socklen_t addrLen;
Mark Sleeb4552922007-11-28 00:12:11 +0000738
739 if (socket_ < 0) {
740 return peerAddress_;
741 }
742
David Reiss23248712010-10-06 17:10:08 +0000743 addrPtr = getCachedAddress(&addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000744
David Reiss23248712010-10-06 17:10:08 +0000745 if (addrPtr == NULL) {
746 addrLen = sizeof(addr);
747 if (getpeername(socket_, (sockaddr*) &addr, &addrLen) != 0) {
748 return peerAddress_;
749 }
750 addrPtr = (sockaddr*)&addr;
751
752 setCachedAddress(addrPtr, addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000753 }
754
755 char clienthost[NI_MAXHOST];
756 char clientservice[NI_MAXSERV];
757
David Reiss23248712010-10-06 17:10:08 +0000758 getnameinfo(addrPtr, addrLen,
Mark Sleeb4552922007-11-28 00:12:11 +0000759 clienthost, sizeof(clienthost),
760 clientservice, sizeof(clientservice),
761 NI_NUMERICHOST|NI_NUMERICSERV);
762
763 peerAddress_ = clienthost;
764 peerPort_ = std::atoi(clientservice);
765 }
766 return peerAddress_;
767}
768
769int TSocket::getPeerPort() {
770 getPeerAddress();
771 return peerPort_;
772}
773
David Reiss23248712010-10-06 17:10:08 +0000774void TSocket::setCachedAddress(const sockaddr* addr, socklen_t len) {
Roger Meier18f10502011-06-04 08:57:43 +0000775 if (!path_.empty()) {
776 return;
777 }
778
David Reiss23248712010-10-06 17:10:08 +0000779 switch (addr->sa_family) {
780 case AF_INET:
781 if (len == sizeof(sockaddr_in)) {
782 memcpy((void*)&cachedPeerAddr_.ipv4, (void*)addr, len);
783 }
784 break;
785
786 case AF_INET6:
787 if (len == sizeof(sockaddr_in6)) {
788 memcpy((void*)&cachedPeerAddr_.ipv6, (void*)addr, len);
789 }
790 break;
791 }
792}
793
794sockaddr* TSocket::getCachedAddress(socklen_t* len) const {
795 switch (cachedPeerAddr_.ipv4.sin_family) {
796 case AF_INET:
797 *len = sizeof(sockaddr_in);
798 return (sockaddr*) &cachedPeerAddr_.ipv4;
799
800 case AF_INET6:
801 *len = sizeof(sockaddr_in6);
802 return (sockaddr*) &cachedPeerAddr_.ipv6;
803
804 default:
805 return NULL;
806 }
807}
808
David Reiss1c20c872010-03-09 05:20:14 +0000809bool TSocket::useLowMinRto_ = false;
810void TSocket::setUseLowMinRto(bool useLowMinRto) {
811 useLowMinRto_ = useLowMinRto;
812}
813bool TSocket::getUseLowMinRto() {
814 return useLowMinRto_;
815}
816
T Jake Lucianib5e62212009-01-31 22:36:20 +0000817}}} // apache::thrift::transport