blob: 48d00075b35d148c06b49409d4fb37e69528d4c0 [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
184#ifdef _WIN32
185 TWinsockSingleton::create();
186#endif // _WIN32
187
Mark Sleea9848d72007-02-21 04:54:05 +0000188 if (isOpen()) {
Bryan Duxbury010f1e02010-09-02 00:56:53 +0000189 return;
Mark Sleea9848d72007-02-21 04:54:05 +0000190 }
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000191
Bryan Duxburya18364a2010-09-28 14:36:07 +0000192 if (! path_.empty()) {
193 socket_ = socket(PF_UNIX, SOCK_STREAM, IPPROTO_IP);
194 } else {
195 socket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
196 }
197
Mark Sleee8540632006-05-30 09:24:40 +0000198 if (socket_ == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000199 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000200 GlobalOutput.perror("TSocket::open() socket() " + getSocketInfo(), errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000201 throw TTransportException(TTransportException::NOT_OPEN, "socket()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000202 }
Mark Slee29050782006-09-29 00:12:30 +0000203
204 // Send timeout
205 if (sendTimeout_ > 0) {
206 setSendTimeout(sendTimeout_);
207 }
208
209 // Recv timeout
210 if (recvTimeout_ > 0) {
211 setRecvTimeout(recvTimeout_);
212 }
213
214 // Linger
215 setLinger(lingerOn_, lingerVal_);
216
217 // No delay
218 setNoDelay(noDelay_);
219
David Reiss1c20c872010-03-09 05:20:14 +0000220 // Uses a low min RTO if asked to.
221#ifdef TCP_LOW_MIN_RTO
222 if (getUseLowMinRto()) {
223 int one = 1;
224 setsockopt(socket_, IPPROTO_TCP, TCP_LOW_MIN_RTO, &one, sizeof(one));
225 }
226#endif
227
228
Mark Slee29050782006-09-29 00:12:30 +0000229 // Set the socket to be non blocking for connect if a timeout exists
Mark Slee256bdc42007-11-27 08:42:19 +0000230 int flags = fcntl(socket_, F_GETFL, 0);
Mark Slee29050782006-09-29 00:12:30 +0000231 if (connTimeout_ > 0) {
Mark Sleea5a783f2007-03-02 19:41:08 +0000232 if (-1 == fcntl(socket_, F_SETFL, flags | O_NONBLOCK)) {
David Reiss9b209552008-04-08 06:26:05 +0000233 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000234 GlobalOutput.perror("TSocket::open() fcntl() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000235 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000236 }
Mark Slee29050782006-09-29 00:12:30 +0000237 } else {
Mark Sleea5a783f2007-03-02 19:41:08 +0000238 if (-1 == fcntl(socket_, F_SETFL, flags & ~O_NONBLOCK)) {
David Reiss9b209552008-04-08 06:26:05 +0000239 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000240 GlobalOutput.perror("TSocket::open() fcntl " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000241 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000242 }
Mark Slee29050782006-09-29 00:12:30 +0000243 }
244
Mark Sleee8540632006-05-30 09:24:40 +0000245 // Connect the socket
Bryan Duxburya18364a2010-09-28 14:36:07 +0000246 int ret;
247 if (! path_.empty()) {
Roger Meier84e4a3c2011-09-16 20:58:44 +0000248
249#ifndef _WIN32
250
Bryan Duxburya18364a2010-09-28 14:36:07 +0000251 struct sockaddr_un address;
252 socklen_t len;
253
254 if (path_.length() > sizeof(address.sun_path)) {
255 int errno_copy = errno;
256 GlobalOutput.perror("TSocket::open() Unix Domain socket path too long", errno_copy);
257 throw TTransportException(TTransportException::NOT_OPEN, " Unix Domain socket path too long");
258 }
259
260 address.sun_family = AF_UNIX;
Roger Meierd11ca5a2010-10-18 08:22:57 +0000261 snprintf(address.sun_path, sizeof(address.sun_path), "%s", path_.c_str());
Bryan Duxburya18364a2010-09-28 14:36:07 +0000262 len = sizeof(address);
263 ret = connect(socket_, (struct sockaddr *) &address, len);
Roger Meier84e4a3c2011-09-16 20:58:44 +0000264
265#else
266 GlobalOutput.perror("TSocket::open() Unix Domain socket path not supported on windows", -99);
267 throw TTransportException(TTransportException::NOT_OPEN, " Unix Domain socket path not supported");
268#endif
269
Bryan Duxburya18364a2010-09-28 14:36:07 +0000270 } else {
271 ret = connect(socket_, res->ai_addr, res->ai_addrlen);
272 }
Mark Slee256bdc42007-11-27 08:42:19 +0000273
David Reiss9b209552008-04-08 06:26:05 +0000274 // success case
Mark Slee29050782006-09-29 00:12:30 +0000275 if (ret == 0) {
276 goto done;
277 }
278
279 if (errno != EINPROGRESS) {
David Reissbc3dddb2007-08-22 23:20:24 +0000280 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000281 GlobalOutput.perror("TSocket::open() connect() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000282 throw TTransportException(TTransportException::NOT_OPEN, "connect() failed", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000283 }
284
David Reiss22b18862008-04-08 06:25:45 +0000285
286 struct pollfd fds[1];
David Reissc88eb8c2008-06-11 01:18:54 +0000287 std::memset(fds, 0 , sizeof(fds));
David Reiss22b18862008-04-08 06:25:45 +0000288 fds[0].fd = socket_;
289 fds[0].events = POLLOUT;
290 ret = poll(fds, 1, connTimeout_);
Mark Slee29050782006-09-29 00:12:30 +0000291
292 if (ret > 0) {
David Reiss9b209552008-04-08 06:26:05 +0000293 // Ensure the socket is connected and that there are no errors set
Mark Slee29050782006-09-29 00:12:30 +0000294 int val;
295 socklen_t lon;
296 lon = sizeof(int);
Roger Meier84e4a3c2011-09-16 20:58:44 +0000297 int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, cast_sockopt(&val), &lon);
Mark Slee29050782006-09-29 00:12:30 +0000298 if (ret2 == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000299 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000300 GlobalOutput.perror("TSocket::open() getsockopt() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000301 throw TTransportException(TTransportException::NOT_OPEN, "getsockopt()", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000302 }
David Reiss9b209552008-04-08 06:26:05 +0000303 // no errors on socket, go to town
Mark Slee29050782006-09-29 00:12:30 +0000304 if (val == 0) {
305 goto done;
306 }
David Reiss01e55c12008-07-13 22:18:51 +0000307 GlobalOutput.perror("TSocket::open() error on socket (after poll) " + getSocketInfo(), val);
David Reiss9b209552008-04-08 06:26:05 +0000308 throw TTransportException(TTransportException::NOT_OPEN, "socket open() error", val);
Mark Slee29050782006-09-29 00:12:30 +0000309 } else if (ret == 0) {
David Reiss9b209552008-04-08 06:26:05 +0000310 // socket timed out
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000311 string errStr = "TSocket::open() timed out " + getSocketInfo();
312 GlobalOutput(errStr.c_str());
David Reiss9b209552008-04-08 06:26:05 +0000313 throw TTransportException(TTransportException::NOT_OPEN, "open() timed out");
Mark Slee29050782006-09-29 00:12:30 +0000314 } else {
David Reiss9b209552008-04-08 06:26:05 +0000315 // error on poll()
David Reissbc3dddb2007-08-22 23:20:24 +0000316 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000317 GlobalOutput.perror("TSocket::open() poll() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000318 throw TTransportException(TTransportException::NOT_OPEN, "poll() failed", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000319 }
320
321 done:
322 // Set socket back to normal mode (blocking)
323 fcntl(socket_, F_SETFL, flags);
David Reiss23248712010-10-06 17:10:08 +0000324
Roger Meier18f10502011-06-04 08:57:43 +0000325 if (path_.empty()) {
326 setCachedAddress(res->ai_addr, res->ai_addrlen);
327 }
Mark Sleee8540632006-05-30 09:24:40 +0000328}
329
Mark Slee6d56eb92007-07-06 22:28:15 +0000330void TSocket::open() {
331 if (isOpen()) {
Bryan Duxbury010f1e02010-09-02 00:56:53 +0000332 return;
Mark Slee6d56eb92007-07-06 22:28:15 +0000333 }
Bryan Duxburya18364a2010-09-28 14:36:07 +0000334 if (! path_.empty()) {
335 unix_open();
336 } else {
337 local_open();
338 }
339}
340
341void TSocket::unix_open(){
342 if (! path_.empty()) {
343 // Unix Domain SOcket does not need addrinfo struct, so we pass NULL
344 openConnection(NULL);
345 }
346}
347
348void TSocket::local_open(){
349 if (isOpen()) {
350 return;
351 }
Mark Slee6d56eb92007-07-06 22:28:15 +0000352
353 // Validate port number
David Reiss450e35d2010-03-09 05:19:41 +0000354 if (port_ < 0 || port_ > 0xFFFF) {
Mark Slee6d56eb92007-07-06 22:28:15 +0000355 throw TTransportException(TTransportException::NOT_OPEN, "Specified port is invalid");
356 }
357
358 struct addrinfo hints, *res, *res0;
David Reiss9b209552008-04-08 06:26:05 +0000359 res = NULL;
360 res0 = NULL;
Mark Slee6d56eb92007-07-06 22:28:15 +0000361 int error;
David Reiss450e35d2010-03-09 05:19:41 +0000362 char port[sizeof("65535")];
David Reissc88eb8c2008-06-11 01:18:54 +0000363 std::memset(&hints, 0, sizeof(hints));
Mark Slee6d56eb92007-07-06 22:28:15 +0000364 hints.ai_family = PF_UNSPEC;
365 hints.ai_socktype = SOCK_STREAM;
Mark Slee256bdc42007-11-27 08:42:19 +0000366 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
Mark Slee6d56eb92007-07-06 22:28:15 +0000367 sprintf(port, "%d", port_);
Mark Slee256bdc42007-11-27 08:42:19 +0000368
Mark Sleec37b4c52007-12-05 23:03:37 +0000369 error = getaddrinfo(host_.c_str(), port, &hints, &res0);
370
Mark Slee6d56eb92007-07-06 22:28:15 +0000371 if (error) {
David Reiss9b209552008-04-08 06:26:05 +0000372 string errStr = "TSocket::open() getaddrinfo() " + getSocketInfo() + string(gai_strerror(error));
373 GlobalOutput(errStr.c_str());
Mark Slee6d56eb92007-07-06 22:28:15 +0000374 close();
375 throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for client socket.");
376 }
Mark Slee256bdc42007-11-27 08:42:19 +0000377
Mark Slee6d56eb92007-07-06 22:28:15 +0000378 // Cycle through all the returned addresses until one
379 // connects or push the exception up.
380 for (res = res0; res; res = res->ai_next) {
381 try {
382 openConnection(res);
383 break;
384 } catch (TTransportException& ttx) {
385 if (res->ai_next) {
386 close();
387 } else {
388 close();
Mark Slee85287d32007-07-09 19:50:30 +0000389 freeaddrinfo(res0); // cleanup on failure
Mark Slee6d56eb92007-07-06 22:28:15 +0000390 throw;
391 }
392 }
393 }
Mark Slee85287d32007-07-09 19:50:30 +0000394
395 // Free address structure memory
396 freeaddrinfo(res0);
Mark Slee6d56eb92007-07-06 22:28:15 +0000397}
398
Mark Sleee8540632006-05-30 09:24:40 +0000399void TSocket::close() {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000400 if (socket_ >= 0) {
Roger Meier84e4a3c2011-09-16 20:58:44 +0000401
402#ifdef _WIN32
403 shutdown(socket_, SD_BOTH);
404 ::closesocket(socket_);
405#else
406 shutdown(socket_, SHUT_RDWR);
407 ::close(socket_);
408#endif
409
Mark Sleee8540632006-05-30 09:24:40 +0000410 }
Martin Kraemeree341cb2007-02-05 21:40:38 +0000411 socket_ = -1;
Mark Sleee8540632006-05-30 09:24:40 +0000412}
413
David Reiss105961d2010-10-06 17:10:17 +0000414void TSocket::setSocketFD(int socket) {
415 if (socket_ >= 0) {
416 close();
417 }
418 socket_ = socket;
419}
420
Mark Slee8d7e1f62006-06-07 06:48:56 +0000421uint32_t TSocket::read(uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000422 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000423 throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000424 }
Mark Sleee8540632006-05-30 09:24:40 +0000425
Aditya Agarwale04475b2007-05-23 02:14:58 +0000426 int32_t retries = 0;
427
428 // EAGAIN can be signalled both when a timeout has occurred and when
429 // the system is out of resources (an awesome undocumented feature).
430 // The following is an approximation of the time interval under which
431 // EAGAIN is taken to indicate an out of resources error.
432 uint32_t eagainThresholdMicros = 0;
433 if (recvTimeout_) {
Mark Slee256bdc42007-11-27 08:42:19 +0000434 // if a readTimeout is specified along with a max number of recv retries, then
Aditya Agarwale04475b2007-05-23 02:14:58 +0000435 // the threshold will ensure that the read timeout is not exceeded even in the
436 // case of resource errors
437 eagainThresholdMicros = (recvTimeout_*1000)/ ((maxRecvRetries_>0) ? maxRecvRetries_ : 2);
438 }
439
Mark Slee256bdc42007-11-27 08:42:19 +0000440 try_again:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000441 // Read from the socket
Aditya Agarwale04475b2007-05-23 02:14:58 +0000442 struct timeval begin;
David Reiss105961d2010-10-06 17:10:17 +0000443 if (recvTimeout_ > 0) {
444 gettimeofday(&begin, NULL);
445 } else {
446 // if there is no read timeout we don't need the TOD to determine whether
447 // an EAGAIN is due to a timeout or an out-of-resource condition.
448 begin.tv_sec = begin.tv_usec = 0;
449 }
Roger Meier84e4a3c2011-09-16 20:58:44 +0000450 int got = recv(socket_, cast_sockopt(buf), len, 0);
Kevin Clark022b2242009-03-05 21:05:37 +0000451 int errno_copy = errno; //gettimeofday can change errno
Mark Slee8d7e1f62006-06-07 06:48:56 +0000452 ++g_socket_syscalls;
Aditya Agarwale04475b2007-05-23 02:14:58 +0000453
Mark Slee8d7e1f62006-06-07 06:48:56 +0000454 // Check for error on read
Mark Slee256bdc42007-11-27 08:42:19 +0000455 if (got < 0) {
Kevin Clark022b2242009-03-05 21:05:37 +0000456 if (errno_copy == EAGAIN) {
David Reiss105961d2010-10-06 17:10:17 +0000457 // if no timeout we can assume that resource exhaustion has occurred.
458 if (recvTimeout_ == 0) {
459 throw TTransportException(TTransportException::TIMED_OUT,
460 "EAGAIN (unavailable resources)");
461 }
Aditya Agarwale04475b2007-05-23 02:14:58 +0000462 // check if this is the lack of resources or timeout case
David Reissa1a15112010-03-09 05:19:54 +0000463 struct timeval end;
464 gettimeofday(&end, NULL);
465 uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000)
466 + (((uint64_t)(end.tv_usec - begin.tv_usec))));
467
Aditya Agarwale04475b2007-05-23 02:14:58 +0000468 if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) {
469 if (retries++ < maxRecvRetries_) {
470 usleep(50);
471 goto try_again;
472 } else {
Mark Slee256bdc42007-11-27 08:42:19 +0000473 throw TTransportException(TTransportException::TIMED_OUT,
Aditya Agarwale04475b2007-05-23 02:14:58 +0000474 "EAGAIN (unavailable resources)");
475 }
476 } else {
477 // infer that timeout has been hit
Mark Slee256bdc42007-11-27 08:42:19 +0000478 throw TTransportException(TTransportException::TIMED_OUT,
Aditya Agarwale04475b2007-05-23 02:14:58 +0000479 "EAGAIN (timed out)");
480 }
Mark Sleee8540632006-05-30 09:24:40 +0000481 }
Mark Slee256bdc42007-11-27 08:42:19 +0000482
Mark Slee8d7e1f62006-06-07 06:48:56 +0000483 // If interrupted, try again
Kevin Clark022b2242009-03-05 21:05:37 +0000484 if (errno_copy == EINTR && retries++ < maxRecvRetries_) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000485 goto try_again;
Mark Sleee8540632006-05-30 09:24:40 +0000486 }
Mark Slee256bdc42007-11-27 08:42:19 +0000487
David Reiss840e7522009-06-04 00:10:50 +0000488 #if defined __FreeBSD__ || defined __MACH__
Kevin Clark022b2242009-03-05 21:05:37 +0000489 if (errno_copy == ECONNRESET) {
Kevin Clark022b2242009-03-05 21:05:37 +0000490 /* shigin: freebsd doesn't follow POSIX semantic of recv and fails with
491 * ECONNRESET if peer performed shutdown
David Reiss105961d2010-10-06 17:10:17 +0000492 * edhall: eliminated close() since we do that in the destructor.
Kevin Clark022b2242009-03-05 21:05:37 +0000493 */
Kevin Clark022b2242009-03-05 21:05:37 +0000494 return 0;
David Reiss840e7522009-06-04 00:10:50 +0000495 }
496 #endif
497
498 // Now it's not a try again case, but a real probblez
499 GlobalOutput.perror("TSocket::read() recv() " + getSocketInfo(), errno_copy);
500
501 // If we disconnect with no linger time
502 if (errno_copy == ECONNRESET) {
Mark Sleef9831082007-02-20 20:59:21 +0000503 throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000504 }
Mark Slee256bdc42007-11-27 08:42:19 +0000505
Mark Slee8d7e1f62006-06-07 06:48:56 +0000506 // This ish isn't open
Kevin Clark022b2242009-03-05 21:05:37 +0000507 if (errno_copy == ENOTCONN) {
Mark Sleef9831082007-02-20 20:59:21 +0000508 throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000509 }
Mark Slee256bdc42007-11-27 08:42:19 +0000510
Mark Slee8d7e1f62006-06-07 06:48:56 +0000511 // Timed out!
Kevin Clark022b2242009-03-05 21:05:37 +0000512 if (errno_copy == ETIMEDOUT) {
Mark Sleef9831082007-02-20 20:59:21 +0000513 throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000514 }
Mark Slee256bdc42007-11-27 08:42:19 +0000515
Mark Slee8d7e1f62006-06-07 06:48:56 +0000516 // Some other error, whatevz
David Reiss01e55c12008-07-13 22:18:51 +0000517 throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000518 }
Mark Slee256bdc42007-11-27 08:42:19 +0000519
Mark Slee8d7e1f62006-06-07 06:48:56 +0000520 // The remote host has closed the socket
521 if (got == 0) {
David Reiss105961d2010-10-06 17:10:17 +0000522 // edhall: we used to call close() here, but our caller may want to deal
523 // with the socket fd and we'll close() in our destructor in any case.
Mark Slee8d7e1f62006-06-07 06:48:56 +0000524 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000525 }
Mark Slee256bdc42007-11-27 08:42:19 +0000526
Mark Sleee8540632006-05-30 09:24:40 +0000527 // Pack data into string
Mark Slee8d7e1f62006-06-07 06:48:56 +0000528 return got;
Mark Sleee8540632006-05-30 09:24:40 +0000529}
530
Mark Slee8d7e1f62006-06-07 06:48:56 +0000531void TSocket::write(const uint8_t* buf, uint32_t len) {
David Reiss105961d2010-10-06 17:10:17 +0000532 uint32_t sent = 0;
533
534 while (sent < len) {
535 uint32_t b = write_partial(buf + sent, len - sent);
536 if (b == 0) {
Bryan Duxbury97592662011-08-29 18:05:26 +0000537 // This should only happen if the timeout set with SO_SNDTIMEO expired.
538 // Raise an exception.
539 throw TTransportException(TTransportException::TIMED_OUT,
540 "send timeout expired");
David Reiss105961d2010-10-06 17:10:17 +0000541 }
542 sent += b;
543 }
544}
545
546uint32_t TSocket::write_partial(const uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000547 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000548 throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000549 }
550
Mark Sleee8540632006-05-30 09:24:40 +0000551 uint32_t sent = 0;
Mark Slee256bdc42007-11-27 08:42:19 +0000552
David Reiss105961d2010-10-06 17:10:17 +0000553 int flags = 0;
554#ifdef MSG_NOSIGNAL
555 // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we
556 // check for the EPIPE return condition and close the socket in that case
557 flags |= MSG_NOSIGNAL;
558#endif // ifdef MSG_NOSIGNAL
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000559
Roger Meier84e4a3c2011-09-16 20:58:44 +0000560 int b = send(socket_, const_cast_sockopt(buf + sent), len - sent, flags);
David Reiss105961d2010-10-06 17:10:17 +0000561 ++g_socket_syscalls;
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000562
David Reiss105961d2010-10-06 17:10:17 +0000563 if (b < 0) {
564 if (errno == EWOULDBLOCK || errno == EAGAIN) {
565 return 0;
566 }
Mark Sleee8540632006-05-30 09:24:40 +0000567 // Fail on a send error
David Reiss105961d2010-10-06 17:10:17 +0000568 int errno_copy = errno;
569 GlobalOutput.perror("TSocket::write_partial() send() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000570
David Reiss105961d2010-10-06 17:10:17 +0000571 if (errno_copy == EPIPE || errno_copy == ECONNRESET || errno_copy == ENOTCONN) {
572 close();
573 throw TTransportException(TTransportException::NOT_OPEN, "write() send()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000574 }
Mark Slee256bdc42007-11-27 08:42:19 +0000575
David Reiss105961d2010-10-06 17:10:17 +0000576 throw TTransportException(TTransportException::UNKNOWN, "write() send()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000577 }
David Reiss105961d2010-10-06 17:10:17 +0000578
579 // Fail on blocked send
580 if (b == 0) {
581 throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0.");
582 }
583 return b;
Mark Sleee8540632006-05-30 09:24:40 +0000584}
585
dweatherford14b0ed62007-10-19 01:03:32 +0000586std::string TSocket::getHost() {
587 return host_;
588}
589
590int TSocket::getPort() {
591 return port_;
592}
593
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000594void TSocket::setHost(string host) {
595 host_ = host;
596}
597
598void TSocket::setPort(int port) {
599 port_ = port;
600}
601
Mark Slee8d7e1f62006-06-07 06:48:56 +0000602void TSocket::setLinger(bool on, int linger) {
Mark Slee29050782006-09-29 00:12:30 +0000603 lingerOn_ = on;
604 lingerVal_ = linger;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000605 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000606 return;
607 }
608
Mark Slee29050782006-09-29 00:12:30 +0000609 struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_};
Roger Meier84e4a3c2011-09-16 20:58:44 +0000610 int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, cast_sockopt(&l), sizeof(l));
Mark Slee29050782006-09-29 00:12:30 +0000611 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000612 int errno_copy = errno; // Copy errno because we're allocating memory.
613 GlobalOutput.perror("TSocket::setLinger() setsockopt() " + getSocketInfo(), errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000614 }
Mark Sleee8540632006-05-30 09:24:40 +0000615}
616
Mark Slee8d7e1f62006-06-07 06:48:56 +0000617void TSocket::setNoDelay(bool noDelay) {
Mark Slee29050782006-09-29 00:12:30 +0000618 noDelay_ = noDelay;
Roger Meier18f10502011-06-04 08:57:43 +0000619 if (socket_ < 0 || !path_.empty()) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000620 return;
621 }
622
Mark Sleee8540632006-05-30 09:24:40 +0000623 // Set socket to NODELAY
Mark Slee29050782006-09-29 00:12:30 +0000624 int v = noDelay_ ? 1 : 0;
Roger Meier84e4a3c2011-09-16 20:58:44 +0000625 int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, cast_sockopt(&v), sizeof(v));
Mark Slee29050782006-09-29 00:12:30 +0000626 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000627 int errno_copy = errno; // Copy errno because we're allocating memory.
628 GlobalOutput.perror("TSocket::setNoDelay() setsockopt() " + getSocketInfo(), errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000629 }
Mark Sleee8540632006-05-30 09:24:40 +0000630}
Mark Slee29050782006-09-29 00:12:30 +0000631
632void TSocket::setConnTimeout(int ms) {
633 connTimeout_ = ms;
634}
635
636void TSocket::setRecvTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000637 if (ms < 0) {
638 char errBuf[512];
639 sprintf(errBuf, "TSocket::setRecvTimeout with negative input: %d\n", ms);
640 GlobalOutput(errBuf);
641 return;
642 }
Mark Slee29050782006-09-29 00:12:30 +0000643 recvTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000644
Martin Kraemeree341cb2007-02-05 21:40:38 +0000645 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000646 return;
647 }
648
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000649 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
650 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
651
David Reiss22b18862008-04-08 06:25:45 +0000652 // Copy because poll may modify
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000653 struct timeval r = recvTimeval_;
Roger Meier84e4a3c2011-09-16 20:58:44 +0000654 int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, cast_sockopt(&r), sizeof(r));
Mark Slee29050782006-09-29 00:12:30 +0000655 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000656 int errno_copy = errno; // Copy errno because we're allocating memory.
657 GlobalOutput.perror("TSocket::setRecvTimeout() setsockopt() " + getSocketInfo(), errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000658 }
659}
660
661void TSocket::setSendTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000662 if (ms < 0) {
663 char errBuf[512];
664 sprintf(errBuf, "TSocket::setSendTimeout with negative input: %d\n", ms);
665 GlobalOutput(errBuf);
666 return;
667 }
Mark Slee29050782006-09-29 00:12:30 +0000668 sendTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000669
Martin Kraemeree341cb2007-02-05 21:40:38 +0000670 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000671 return;
672 }
Mark Slee256bdc42007-11-27 08:42:19 +0000673
Mark Slee29050782006-09-29 00:12:30 +0000674 struct timeval s = {(int)(sendTimeout_/1000),
675 (int)((sendTimeout_%1000)*1000)};
Roger Meier84e4a3c2011-09-16 20:58:44 +0000676 int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, cast_sockopt(&s), sizeof(s));
Mark Slee29050782006-09-29 00:12:30 +0000677 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000678 int errno_copy = errno; // Copy errno because we're allocating memory.
679 GlobalOutput.perror("TSocket::setSendTimeout() setsockopt() " + getSocketInfo(), errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000680 }
681}
682
Aditya Agarwale04475b2007-05-23 02:14:58 +0000683void TSocket::setMaxRecvRetries(int maxRecvRetries) {
684 maxRecvRetries_ = maxRecvRetries;
685}
686
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000687string TSocket::getSocketInfo() {
688 std::ostringstream oss;
David Reiss105961d2010-10-06 17:10:17 +0000689 if (host_.empty() || port_ == 0) {
690 oss << "<Host: " << getPeerAddress();
691 oss << " Port: " << getPeerPort() << ">";
692 } else {
693 oss << "<Host: " << host_ << " Port: " << port_ << ">";
694 }
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000695 return oss.str();
696}
697
Mark Sleeb4552922007-11-28 00:12:11 +0000698std::string TSocket::getPeerHost() {
Roger Meier18f10502011-06-04 08:57:43 +0000699 if (peerHost_.empty() && path_.empty()) {
Mark Sleeb4552922007-11-28 00:12:11 +0000700 struct sockaddr_storage addr;
David Reiss23248712010-10-06 17:10:08 +0000701 struct sockaddr* addrPtr;
702 socklen_t addrLen;
Mark Sleeb4552922007-11-28 00:12:11 +0000703
704 if (socket_ < 0) {
705 return host_;
706 }
707
David Reiss23248712010-10-06 17:10:08 +0000708 addrPtr = getCachedAddress(&addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000709
David Reiss23248712010-10-06 17:10:08 +0000710 if (addrPtr == NULL) {
711 addrLen = sizeof(addr);
712 if (getpeername(socket_, (sockaddr*) &addr, &addrLen) != 0) {
713 return peerHost_;
714 }
715 addrPtr = (sockaddr*)&addr;
716
717 setCachedAddress(addrPtr, addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000718 }
719
720 char clienthost[NI_MAXHOST];
721 char clientservice[NI_MAXSERV];
722
David Reiss23248712010-10-06 17:10:08 +0000723 getnameinfo((sockaddr*) addrPtr, addrLen,
Mark Sleeb4552922007-11-28 00:12:11 +0000724 clienthost, sizeof(clienthost),
725 clientservice, sizeof(clientservice), 0);
726
727 peerHost_ = clienthost;
728 }
729 return peerHost_;
730}
731
732std::string TSocket::getPeerAddress() {
Roger Meier18f10502011-06-04 08:57:43 +0000733 if (peerAddress_.empty() && path_.empty()) {
Mark Sleeb4552922007-11-28 00:12:11 +0000734 struct sockaddr_storage addr;
David Reiss23248712010-10-06 17:10:08 +0000735 struct sockaddr* addrPtr;
736 socklen_t addrLen;
Mark Sleeb4552922007-11-28 00:12:11 +0000737
738 if (socket_ < 0) {
739 return peerAddress_;
740 }
741
David Reiss23248712010-10-06 17:10:08 +0000742 addrPtr = getCachedAddress(&addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000743
David Reiss23248712010-10-06 17:10:08 +0000744 if (addrPtr == NULL) {
745 addrLen = sizeof(addr);
746 if (getpeername(socket_, (sockaddr*) &addr, &addrLen) != 0) {
747 return peerAddress_;
748 }
749 addrPtr = (sockaddr*)&addr;
750
751 setCachedAddress(addrPtr, addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000752 }
753
754 char clienthost[NI_MAXHOST];
755 char clientservice[NI_MAXSERV];
756
David Reiss23248712010-10-06 17:10:08 +0000757 getnameinfo(addrPtr, addrLen,
Mark Sleeb4552922007-11-28 00:12:11 +0000758 clienthost, sizeof(clienthost),
759 clientservice, sizeof(clientservice),
760 NI_NUMERICHOST|NI_NUMERICSERV);
761
762 peerAddress_ = clienthost;
763 peerPort_ = std::atoi(clientservice);
764 }
765 return peerAddress_;
766}
767
768int TSocket::getPeerPort() {
769 getPeerAddress();
770 return peerPort_;
771}
772
David Reiss23248712010-10-06 17:10:08 +0000773void TSocket::setCachedAddress(const sockaddr* addr, socklen_t len) {
Roger Meier18f10502011-06-04 08:57:43 +0000774 if (!path_.empty()) {
775 return;
776 }
777
David Reiss23248712010-10-06 17:10:08 +0000778 switch (addr->sa_family) {
779 case AF_INET:
780 if (len == sizeof(sockaddr_in)) {
781 memcpy((void*)&cachedPeerAddr_.ipv4, (void*)addr, len);
782 }
783 break;
784
785 case AF_INET6:
786 if (len == sizeof(sockaddr_in6)) {
787 memcpy((void*)&cachedPeerAddr_.ipv6, (void*)addr, len);
788 }
789 break;
790 }
791}
792
793sockaddr* TSocket::getCachedAddress(socklen_t* len) const {
794 switch (cachedPeerAddr_.ipv4.sin_family) {
795 case AF_INET:
796 *len = sizeof(sockaddr_in);
797 return (sockaddr*) &cachedPeerAddr_.ipv4;
798
799 case AF_INET6:
800 *len = sizeof(sockaddr_in6);
801 return (sockaddr*) &cachedPeerAddr_.ipv6;
802
803 default:
804 return NULL;
805 }
806}
807
David Reiss1c20c872010-03-09 05:20:14 +0000808bool TSocket::useLowMinRto_ = false;
809void TSocket::setUseLowMinRto(bool useLowMinRto) {
810 useLowMinRto_ = useLowMinRto;
811}
812bool TSocket::getUseLowMinRto() {
813 return useLowMinRto_;
814}
815
T Jake Lucianib5e62212009-01-31 22:36:20 +0000816}}} // apache::thrift::transport