blob: 951ddcf11f750ec5d41ffab1d5104c0cebefc667 [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);
Mark Sleee8540632006-05-30 09:24:40 +000081}
82
Mark Slee256bdc42007-11-27 08:42:19 +000083TSocket::TSocket() :
Aditya Agarwalebc99e02007-01-15 23:14:58 +000084 host_(""),
85 port_(0),
Bryan Duxburya18364a2010-09-28 14:36:07 +000086 path_(""),
Martin Kraemeree341cb2007-02-05 21:40:38 +000087 socket_(-1),
Aditya Agarwalebc99e02007-01-15 23:14:58 +000088 connTimeout_(0),
89 sendTimeout_(0),
90 recvTimeout_(0),
91 lingerOn_(1),
92 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +000093 noDelay_(1),
94 maxRecvRetries_(5) {
Aditya Agarwalebc99e02007-01-15 23:14:58 +000095 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
96 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
97}
98
Mark Slee29050782006-09-29 00:12:30 +000099TSocket::TSocket(int socket) :
100 host_(""),
101 port_(0),
Bryan Duxburya18364a2010-09-28 14:36:07 +0000102 path_(""),
Mark Slee29050782006-09-29 00:12:30 +0000103 socket_(socket),
104 connTimeout_(0),
105 sendTimeout_(0),
106 recvTimeout_(0),
107 lingerOn_(1),
108 lingerVal_(0),
Aditya Agarwale04475b2007-05-23 02:14:58 +0000109 noDelay_(1),
110 maxRecvRetries_(5) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000111 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
112 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
Mark Slee29050782006-09-29 00:12:30 +0000113}
Mark Slee256bdc42007-11-27 08:42:19 +0000114
Mark Sleee8540632006-05-30 09:24:40 +0000115TSocket::~TSocket() {
116 close();
117}
118
Mark Slee256bdc42007-11-27 08:42:19 +0000119bool TSocket::isOpen() {
120 return (socket_ >= 0);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000121}
122
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000123bool TSocket::peek() {
124 if (!isOpen()) {
125 return false;
126 }
127 uint8_t buf;
128 int r = recv(socket_, &buf, 1, MSG_PEEK);
129 if (r == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000130 int errno_copy = errno;
David Reiss840e7522009-06-04 00:10:50 +0000131 #if defined __FreeBSD__ || defined __MACH__
Kevin Clark022b2242009-03-05 21:05:37 +0000132 /* shigin:
133 * freebsd returns -1 and ECONNRESET if socket was closed by
134 * the other side
135 */
136 if (errno_copy == ECONNRESET)
137 {
138 close();
139 return false;
140 }
141 #endif
David Reiss01e55c12008-07-13 22:18:51 +0000142 GlobalOutput.perror("TSocket::peek() recv() " + getSocketInfo(), errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000143 throw TTransportException(TTransportException::UNKNOWN, "recv()", errno_copy);
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000144 }
145 return (r > 0);
146}
147
Mark Slee6d56eb92007-07-06 22:28:15 +0000148void TSocket::openConnection(struct addrinfo *res) {
Mark Sleea9848d72007-02-21 04:54:05 +0000149 if (isOpen()) {
Bryan Duxbury010f1e02010-09-02 00:56:53 +0000150 return;
Mark Sleea9848d72007-02-21 04:54:05 +0000151 }
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000152
Bryan Duxburya18364a2010-09-28 14:36:07 +0000153 if (! path_.empty()) {
154 socket_ = socket(PF_UNIX, SOCK_STREAM, IPPROTO_IP);
155 } else {
156 socket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
157 }
158
Mark Sleee8540632006-05-30 09:24:40 +0000159 if (socket_ == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000160 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000161 GlobalOutput.perror("TSocket::open() socket() " + getSocketInfo(), errno_copy);
David Reissbc3dddb2007-08-22 23:20:24 +0000162 throw TTransportException(TTransportException::NOT_OPEN, "socket()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000163 }
Mark Slee29050782006-09-29 00:12:30 +0000164
165 // Send timeout
166 if (sendTimeout_ > 0) {
167 setSendTimeout(sendTimeout_);
168 }
169
170 // Recv timeout
171 if (recvTimeout_ > 0) {
172 setRecvTimeout(recvTimeout_);
173 }
174
175 // Linger
176 setLinger(lingerOn_, lingerVal_);
177
178 // No delay
179 setNoDelay(noDelay_);
180
David Reiss1c20c872010-03-09 05:20:14 +0000181 // Uses a low min RTO if asked to.
182#ifdef TCP_LOW_MIN_RTO
183 if (getUseLowMinRto()) {
184 int one = 1;
185 setsockopt(socket_, IPPROTO_TCP, TCP_LOW_MIN_RTO, &one, sizeof(one));
186 }
187#endif
188
189
Mark Slee29050782006-09-29 00:12:30 +0000190 // Set the socket to be non blocking for connect if a timeout exists
Mark Slee256bdc42007-11-27 08:42:19 +0000191 int flags = fcntl(socket_, F_GETFL, 0);
Mark Slee29050782006-09-29 00:12:30 +0000192 if (connTimeout_ > 0) {
Mark Sleea5a783f2007-03-02 19:41:08 +0000193 if (-1 == fcntl(socket_, F_SETFL, flags | O_NONBLOCK)) {
David Reiss9b209552008-04-08 06:26:05 +0000194 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000195 GlobalOutput.perror("TSocket::open() fcntl() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000196 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000197 }
Mark Slee29050782006-09-29 00:12:30 +0000198 } else {
Mark Sleea5a783f2007-03-02 19:41:08 +0000199 if (-1 == fcntl(socket_, F_SETFL, flags & ~O_NONBLOCK)) {
David Reiss9b209552008-04-08 06:26:05 +0000200 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000201 GlobalOutput.perror("TSocket::open() fcntl " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000202 throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy);
Mark Sleea5a783f2007-03-02 19:41:08 +0000203 }
Mark Slee29050782006-09-29 00:12:30 +0000204 }
205
Mark Sleee8540632006-05-30 09:24:40 +0000206 // Connect the socket
Bryan Duxburya18364a2010-09-28 14:36:07 +0000207 int ret;
208 if (! path_.empty()) {
209 struct sockaddr_un address;
210 socklen_t len;
211
212 if (path_.length() > sizeof(address.sun_path)) {
213 int errno_copy = errno;
214 GlobalOutput.perror("TSocket::open() Unix Domain socket path too long", errno_copy);
215 throw TTransportException(TTransportException::NOT_OPEN, " Unix Domain socket path too long");
216 }
217
218 address.sun_family = AF_UNIX;
219 sprintf(address.sun_path, path_.c_str());
220 len = sizeof(address);
221 ret = connect(socket_, (struct sockaddr *) &address, len);
222 } else {
223 ret = connect(socket_, res->ai_addr, res->ai_addrlen);
224 }
Mark Slee256bdc42007-11-27 08:42:19 +0000225
David Reiss9b209552008-04-08 06:26:05 +0000226 // success case
Mark Slee29050782006-09-29 00:12:30 +0000227 if (ret == 0) {
228 goto done;
229 }
230
231 if (errno != EINPROGRESS) {
David Reissbc3dddb2007-08-22 23:20:24 +0000232 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000233 GlobalOutput.perror("TSocket::open() connect() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000234 throw TTransportException(TTransportException::NOT_OPEN, "connect() failed", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000235 }
236
David Reiss22b18862008-04-08 06:25:45 +0000237
238 struct pollfd fds[1];
David Reissc88eb8c2008-06-11 01:18:54 +0000239 std::memset(fds, 0 , sizeof(fds));
David Reiss22b18862008-04-08 06:25:45 +0000240 fds[0].fd = socket_;
241 fds[0].events = POLLOUT;
242 ret = poll(fds, 1, connTimeout_);
Mark Slee29050782006-09-29 00:12:30 +0000243
244 if (ret > 0) {
David Reiss9b209552008-04-08 06:26:05 +0000245 // Ensure the socket is connected and that there are no errors set
Mark Slee29050782006-09-29 00:12:30 +0000246 int val;
247 socklen_t lon;
248 lon = sizeof(int);
249 int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon);
250 if (ret2 == -1) {
David Reissbc3dddb2007-08-22 23:20:24 +0000251 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000252 GlobalOutput.perror("TSocket::open() getsockopt() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000253 throw TTransportException(TTransportException::NOT_OPEN, "getsockopt()", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000254 }
David Reiss9b209552008-04-08 06:26:05 +0000255 // no errors on socket, go to town
Mark Slee29050782006-09-29 00:12:30 +0000256 if (val == 0) {
257 goto done;
258 }
David Reiss01e55c12008-07-13 22:18:51 +0000259 GlobalOutput.perror("TSocket::open() error on socket (after poll) " + getSocketInfo(), val);
David Reiss9b209552008-04-08 06:26:05 +0000260 throw TTransportException(TTransportException::NOT_OPEN, "socket open() error", val);
Mark Slee29050782006-09-29 00:12:30 +0000261 } else if (ret == 0) {
David Reiss9b209552008-04-08 06:26:05 +0000262 // socket timed out
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000263 string errStr = "TSocket::open() timed out " + getSocketInfo();
264 GlobalOutput(errStr.c_str());
David Reiss9b209552008-04-08 06:26:05 +0000265 throw TTransportException(TTransportException::NOT_OPEN, "open() timed out");
Mark Slee29050782006-09-29 00:12:30 +0000266 } else {
David Reiss9b209552008-04-08 06:26:05 +0000267 // error on poll()
David Reissbc3dddb2007-08-22 23:20:24 +0000268 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000269 GlobalOutput.perror("TSocket::open() poll() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000270 throw TTransportException(TTransportException::NOT_OPEN, "poll() failed", errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000271 }
272
273 done:
274 // Set socket back to normal mode (blocking)
275 fcntl(socket_, F_SETFL, flags);
Mark Sleee8540632006-05-30 09:24:40 +0000276}
277
Mark Slee6d56eb92007-07-06 22:28:15 +0000278void TSocket::open() {
279 if (isOpen()) {
Bryan Duxbury010f1e02010-09-02 00:56:53 +0000280 return;
Mark Slee6d56eb92007-07-06 22:28:15 +0000281 }
Bryan Duxburya18364a2010-09-28 14:36:07 +0000282 if (! path_.empty()) {
283 unix_open();
284 } else {
285 local_open();
286 }
287}
288
289void TSocket::unix_open(){
290 if (! path_.empty()) {
291 // Unix Domain SOcket does not need addrinfo struct, so we pass NULL
292 openConnection(NULL);
293 }
294}
295
296void TSocket::local_open(){
297 if (isOpen()) {
298 return;
299 }
Mark Slee6d56eb92007-07-06 22:28:15 +0000300
301 // Validate port number
David Reiss450e35d2010-03-09 05:19:41 +0000302 if (port_ < 0 || port_ > 0xFFFF) {
Mark Slee6d56eb92007-07-06 22:28:15 +0000303 throw TTransportException(TTransportException::NOT_OPEN, "Specified port is invalid");
304 }
305
306 struct addrinfo hints, *res, *res0;
David Reiss9b209552008-04-08 06:26:05 +0000307 res = NULL;
308 res0 = NULL;
Mark Slee6d56eb92007-07-06 22:28:15 +0000309 int error;
David Reiss450e35d2010-03-09 05:19:41 +0000310 char port[sizeof("65535")];
David Reissc88eb8c2008-06-11 01:18:54 +0000311 std::memset(&hints, 0, sizeof(hints));
Mark Slee6d56eb92007-07-06 22:28:15 +0000312 hints.ai_family = PF_UNSPEC;
313 hints.ai_socktype = SOCK_STREAM;
Mark Slee256bdc42007-11-27 08:42:19 +0000314 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
Mark Slee6d56eb92007-07-06 22:28:15 +0000315 sprintf(port, "%d", port_);
Mark Slee256bdc42007-11-27 08:42:19 +0000316
Mark Sleec37b4c52007-12-05 23:03:37 +0000317 error = getaddrinfo(host_.c_str(), port, &hints, &res0);
318
Mark Slee6d56eb92007-07-06 22:28:15 +0000319 if (error) {
David Reiss9b209552008-04-08 06:26:05 +0000320 string errStr = "TSocket::open() getaddrinfo() " + getSocketInfo() + string(gai_strerror(error));
321 GlobalOutput(errStr.c_str());
Mark Slee6d56eb92007-07-06 22:28:15 +0000322 close();
323 throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for client socket.");
324 }
Mark Slee256bdc42007-11-27 08:42:19 +0000325
Mark Slee6d56eb92007-07-06 22:28:15 +0000326 // Cycle through all the returned addresses until one
327 // connects or push the exception up.
328 for (res = res0; res; res = res->ai_next) {
329 try {
330 openConnection(res);
331 break;
332 } catch (TTransportException& ttx) {
333 if (res->ai_next) {
334 close();
335 } else {
336 close();
Mark Slee85287d32007-07-09 19:50:30 +0000337 freeaddrinfo(res0); // cleanup on failure
Mark Slee6d56eb92007-07-06 22:28:15 +0000338 throw;
339 }
340 }
341 }
Mark Slee85287d32007-07-09 19:50:30 +0000342
343 // Free address structure memory
344 freeaddrinfo(res0);
Mark Slee6d56eb92007-07-06 22:28:15 +0000345}
346
Mark Sleee8540632006-05-30 09:24:40 +0000347void TSocket::close() {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000348 if (socket_ >= 0) {
Mark Sleee8540632006-05-30 09:24:40 +0000349 shutdown(socket_, SHUT_RDWR);
350 ::close(socket_);
351 }
Martin Kraemeree341cb2007-02-05 21:40:38 +0000352 socket_ = -1;
Mark Sleee8540632006-05-30 09:24:40 +0000353}
354
Mark Slee8d7e1f62006-06-07 06:48:56 +0000355uint32_t TSocket::read(uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000356 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000357 throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000358 }
Mark Sleee8540632006-05-30 09:24:40 +0000359
Aditya Agarwale04475b2007-05-23 02:14:58 +0000360 int32_t retries = 0;
361
362 // EAGAIN can be signalled both when a timeout has occurred and when
363 // the system is out of resources (an awesome undocumented feature).
364 // The following is an approximation of the time interval under which
365 // EAGAIN is taken to indicate an out of resources error.
366 uint32_t eagainThresholdMicros = 0;
367 if (recvTimeout_) {
Mark Slee256bdc42007-11-27 08:42:19 +0000368 // if a readTimeout is specified along with a max number of recv retries, then
Aditya Agarwale04475b2007-05-23 02:14:58 +0000369 // the threshold will ensure that the read timeout is not exceeded even in the
370 // case of resource errors
371 eagainThresholdMicros = (recvTimeout_*1000)/ ((maxRecvRetries_>0) ? maxRecvRetries_ : 2);
372 }
373
Mark Slee256bdc42007-11-27 08:42:19 +0000374 try_again:
Mark Slee8d7e1f62006-06-07 06:48:56 +0000375 // Read from the socket
Aditya Agarwale04475b2007-05-23 02:14:58 +0000376 struct timeval begin;
377 gettimeofday(&begin, NULL);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000378 int got = recv(socket_, buf, len, 0);
Kevin Clark022b2242009-03-05 21:05:37 +0000379 int errno_copy = errno; //gettimeofday can change errno
Mark Slee8d7e1f62006-06-07 06:48:56 +0000380 ++g_socket_syscalls;
Aditya Agarwale04475b2007-05-23 02:14:58 +0000381
Mark Slee8d7e1f62006-06-07 06:48:56 +0000382 // Check for error on read
Mark Slee256bdc42007-11-27 08:42:19 +0000383 if (got < 0) {
Kevin Clark022b2242009-03-05 21:05:37 +0000384 if (errno_copy == EAGAIN) {
Aditya Agarwale04475b2007-05-23 02:14:58 +0000385 // check if this is the lack of resources or timeout case
David Reissa1a15112010-03-09 05:19:54 +0000386 struct timeval end;
387 gettimeofday(&end, NULL);
388 uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000)
389 + (((uint64_t)(end.tv_usec - begin.tv_usec))));
390
Aditya Agarwale04475b2007-05-23 02:14:58 +0000391 if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) {
392 if (retries++ < maxRecvRetries_) {
393 usleep(50);
394 goto try_again;
395 } else {
Mark Slee256bdc42007-11-27 08:42:19 +0000396 throw TTransportException(TTransportException::TIMED_OUT,
Aditya Agarwale04475b2007-05-23 02:14:58 +0000397 "EAGAIN (unavailable resources)");
398 }
399 } else {
400 // infer that timeout has been hit
Mark Slee256bdc42007-11-27 08:42:19 +0000401 throw TTransportException(TTransportException::TIMED_OUT,
Aditya Agarwale04475b2007-05-23 02:14:58 +0000402 "EAGAIN (timed out)");
403 }
Mark Sleee8540632006-05-30 09:24:40 +0000404 }
Mark Slee256bdc42007-11-27 08:42:19 +0000405
Mark Slee8d7e1f62006-06-07 06:48:56 +0000406 // If interrupted, try again
Kevin Clark022b2242009-03-05 21:05:37 +0000407 if (errno_copy == EINTR && retries++ < maxRecvRetries_) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000408 goto try_again;
Mark Sleee8540632006-05-30 09:24:40 +0000409 }
Mark Slee256bdc42007-11-27 08:42:19 +0000410
David Reiss840e7522009-06-04 00:10:50 +0000411 #if defined __FreeBSD__ || defined __MACH__
Kevin Clark022b2242009-03-05 21:05:37 +0000412 if (errno_copy == ECONNRESET) {
Kevin Clark022b2242009-03-05 21:05:37 +0000413 /* shigin: freebsd doesn't follow POSIX semantic of recv and fails with
414 * ECONNRESET if peer performed shutdown
415 */
416 close();
417 return 0;
David Reiss840e7522009-06-04 00:10:50 +0000418 }
419 #endif
420
421 // Now it's not a try again case, but a real probblez
422 GlobalOutput.perror("TSocket::read() recv() " + getSocketInfo(), errno_copy);
423
424 // If we disconnect with no linger time
425 if (errno_copy == ECONNRESET) {
Mark Sleef9831082007-02-20 20:59:21 +0000426 throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000427 }
Mark Slee256bdc42007-11-27 08:42:19 +0000428
Mark Slee8d7e1f62006-06-07 06:48:56 +0000429 // This ish isn't open
Kevin Clark022b2242009-03-05 21:05:37 +0000430 if (errno_copy == ENOTCONN) {
Mark Sleef9831082007-02-20 20:59:21 +0000431 throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000432 }
Mark Slee256bdc42007-11-27 08:42:19 +0000433
Mark Slee8d7e1f62006-06-07 06:48:56 +0000434 // Timed out!
Kevin Clark022b2242009-03-05 21:05:37 +0000435 if (errno_copy == ETIMEDOUT) {
Mark Sleef9831082007-02-20 20:59:21 +0000436 throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000437 }
Mark Slee256bdc42007-11-27 08:42:19 +0000438
Mark Slee8d7e1f62006-06-07 06:48:56 +0000439 // Some other error, whatevz
David Reiss01e55c12008-07-13 22:18:51 +0000440 throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000441 }
Mark Slee256bdc42007-11-27 08:42:19 +0000442
Mark Slee8d7e1f62006-06-07 06:48:56 +0000443 // The remote host has closed the socket
444 if (got == 0) {
445 close();
446 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000447 }
Mark Slee256bdc42007-11-27 08:42:19 +0000448
Mark Sleee8540632006-05-30 09:24:40 +0000449 // Pack data into string
Mark Slee8d7e1f62006-06-07 06:48:56 +0000450 return got;
Mark Sleee8540632006-05-30 09:24:40 +0000451}
452
Mark Slee8d7e1f62006-06-07 06:48:56 +0000453void TSocket::write(const uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000454 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000455 throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000456 }
457
Mark Sleee8540632006-05-30 09:24:40 +0000458 uint32_t sent = 0;
Mark Slee256bdc42007-11-27 08:42:19 +0000459
Mark Slee8d7e1f62006-06-07 06:48:56 +0000460 while (sent < len) {
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000461
462 int flags = 0;
Mark Slee29050782006-09-29 00:12:30 +0000463 #ifdef MSG_NOSIGNAL
Mark Slee8d7e1f62006-06-07 06:48:56 +0000464 // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we
465 // check for the EPIPE return condition and close the socket in that case
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000466 flags |= MSG_NOSIGNAL;
Mark Slee29050782006-09-29 00:12:30 +0000467 #endif // ifdef MSG_NOSIGNAL
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000468
469 int b = send(socket_, buf + sent, len - sent, flags);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000470 ++g_socket_syscalls;
471
Mark Sleee8540632006-05-30 09:24:40 +0000472 // Fail on a send error
473 if (b < 0) {
David Reiss9b209552008-04-08 06:26:05 +0000474 int errno_copy = errno;
David Reiss01e55c12008-07-13 22:18:51 +0000475 GlobalOutput.perror("TSocket::write() send() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000476
David Reissbc3dddb2007-08-22 23:20:24 +0000477 if (errno == EPIPE || errno == ECONNRESET || errno == ENOTCONN) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000478 close();
David Reiss9b209552008-04-08 06:26:05 +0000479 throw TTransportException(TTransportException::NOT_OPEN, "write() send()", errno_copy);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000480 }
481
David Reiss9b209552008-04-08 06:26:05 +0000482 throw TTransportException(TTransportException::UNKNOWN, "write() send()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000483 }
Mark Slee256bdc42007-11-27 08:42:19 +0000484
Mark Sleee8540632006-05-30 09:24:40 +0000485 // Fail on blocked send
486 if (b == 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000487 throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0.");
Mark Sleee8540632006-05-30 09:24:40 +0000488 }
Mark Sleee8540632006-05-30 09:24:40 +0000489 sent += b;
490 }
491}
492
dweatherford14b0ed62007-10-19 01:03:32 +0000493std::string TSocket::getHost() {
494 return host_;
495}
496
497int TSocket::getPort() {
498 return port_;
499}
500
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000501void TSocket::setHost(string host) {
502 host_ = host;
503}
504
505void TSocket::setPort(int port) {
506 port_ = port;
507}
508
Mark Slee8d7e1f62006-06-07 06:48:56 +0000509void TSocket::setLinger(bool on, int linger) {
Mark Slee29050782006-09-29 00:12:30 +0000510 lingerOn_ = on;
511 lingerVal_ = linger;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000512 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000513 return;
514 }
515
Mark Slee29050782006-09-29 00:12:30 +0000516 struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_};
517 int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
518 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000519 int errno_copy = errno; // Copy errno because we're allocating memory.
520 GlobalOutput.perror("TSocket::setLinger() setsockopt() " + getSocketInfo(), errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000521 }
Mark Sleee8540632006-05-30 09:24:40 +0000522}
523
Mark Slee8d7e1f62006-06-07 06:48:56 +0000524void TSocket::setNoDelay(bool noDelay) {
Mark Slee29050782006-09-29 00:12:30 +0000525 noDelay_ = noDelay;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000526 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000527 return;
528 }
529
Mark Sleee8540632006-05-30 09:24:40 +0000530 // Set socket to NODELAY
Mark Slee29050782006-09-29 00:12:30 +0000531 int v = noDelay_ ? 1 : 0;
532 int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
533 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000534 int errno_copy = errno; // Copy errno because we're allocating memory.
535 GlobalOutput.perror("TSocket::setNoDelay() setsockopt() " + getSocketInfo(), errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000536 }
Mark Sleee8540632006-05-30 09:24:40 +0000537}
Mark Slee29050782006-09-29 00:12:30 +0000538
539void TSocket::setConnTimeout(int ms) {
540 connTimeout_ = ms;
541}
542
543void TSocket::setRecvTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000544 if (ms < 0) {
545 char errBuf[512];
546 sprintf(errBuf, "TSocket::setRecvTimeout with negative input: %d\n", ms);
547 GlobalOutput(errBuf);
548 return;
549 }
Mark Slee29050782006-09-29 00:12:30 +0000550 recvTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000551
Martin Kraemeree341cb2007-02-05 21:40:38 +0000552 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000553 return;
554 }
555
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000556 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
557 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
558
David Reiss22b18862008-04-08 06:25:45 +0000559 // Copy because poll may modify
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000560 struct timeval r = recvTimeval_;
Mark Slee29050782006-09-29 00:12:30 +0000561 int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r));
562 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000563 int errno_copy = errno; // Copy errno because we're allocating memory.
564 GlobalOutput.perror("TSocket::setRecvTimeout() setsockopt() " + getSocketInfo(), errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000565 }
566}
567
568void TSocket::setSendTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000569 if (ms < 0) {
570 char errBuf[512];
571 sprintf(errBuf, "TSocket::setSendTimeout with negative input: %d\n", ms);
572 GlobalOutput(errBuf);
573 return;
574 }
Mark Slee29050782006-09-29 00:12:30 +0000575 sendTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000576
Martin Kraemeree341cb2007-02-05 21:40:38 +0000577 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000578 return;
579 }
Mark Slee256bdc42007-11-27 08:42:19 +0000580
Mark Slee29050782006-09-29 00:12:30 +0000581 struct timeval s = {(int)(sendTimeout_/1000),
582 (int)((sendTimeout_%1000)*1000)};
583 int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s));
584 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000585 int errno_copy = errno; // Copy errno because we're allocating memory.
586 GlobalOutput.perror("TSocket::setSendTimeout() setsockopt() " + getSocketInfo(), errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000587 }
588}
589
Aditya Agarwale04475b2007-05-23 02:14:58 +0000590void TSocket::setMaxRecvRetries(int maxRecvRetries) {
591 maxRecvRetries_ = maxRecvRetries;
592}
593
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000594string TSocket::getSocketInfo() {
595 std::ostringstream oss;
596 oss << "<Host: " << host_ << " Port: " << port_ << ">";
597 return oss.str();
598}
599
Mark Sleeb4552922007-11-28 00:12:11 +0000600std::string TSocket::getPeerHost() {
601 if (peerHost_.empty()) {
602 struct sockaddr_storage addr;
603 socklen_t addrLen = sizeof(addr);
604
605 if (socket_ < 0) {
606 return host_;
607 }
608
609 int rv = getpeername(socket_, (sockaddr*) &addr, &addrLen);
610
611 if (rv != 0) {
612 return peerHost_;
613 }
614
615 char clienthost[NI_MAXHOST];
616 char clientservice[NI_MAXSERV];
617
618 getnameinfo((sockaddr*) &addr, addrLen,
619 clienthost, sizeof(clienthost),
620 clientservice, sizeof(clientservice), 0);
621
622 peerHost_ = clienthost;
623 }
624 return peerHost_;
625}
626
627std::string TSocket::getPeerAddress() {
628 if (peerAddress_.empty()) {
629 struct sockaddr_storage addr;
630 socklen_t addrLen = sizeof(addr);
631
632 if (socket_ < 0) {
633 return peerAddress_;
634 }
635
636 int rv = getpeername(socket_, (sockaddr*) &addr, &addrLen);
637
638 if (rv != 0) {
639 return peerAddress_;
640 }
641
642 char clienthost[NI_MAXHOST];
643 char clientservice[NI_MAXSERV];
644
645 getnameinfo((sockaddr*) &addr, addrLen,
646 clienthost, sizeof(clienthost),
647 clientservice, sizeof(clientservice),
648 NI_NUMERICHOST|NI_NUMERICSERV);
649
650 peerAddress_ = clienthost;
651 peerPort_ = std::atoi(clientservice);
652 }
653 return peerAddress_;
654}
655
656int TSocket::getPeerPort() {
657 getPeerAddress();
658 return peerPort_;
659}
660
David Reiss1c20c872010-03-09 05:20:14 +0000661bool TSocket::useLowMinRto_ = false;
662void TSocket::setUseLowMinRto(bool useLowMinRto) {
663 useLowMinRto_ = useLowMinRto;
664}
665bool TSocket::getUseLowMinRto() {
666 return useLowMinRto_;
667}
668
T Jake Lucianib5e62212009-01-31 22:36:20 +0000669}}} // apache::thrift::transport