blob: a0cc77a3915539db8404cc6fa944cf48d0864a50 [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
Roger Meier3faaedf2011-10-02 10:51:45 +0000499#ifdef _WIN32
500 if(errno_copy == WSAECONNRESET) {
501 return 0; // EOF
502 }
503#endif
504
David Reiss840e7522009-06-04 00:10:50 +0000505 // Now it's not a try again case, but a real probblez
506 GlobalOutput.perror("TSocket::read() recv() " + getSocketInfo(), errno_copy);
507
508 // If we disconnect with no linger time
509 if (errno_copy == ECONNRESET) {
Mark Sleef9831082007-02-20 20:59:21 +0000510 throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000511 }
Mark Slee256bdc42007-11-27 08:42:19 +0000512
Mark Slee8d7e1f62006-06-07 06:48:56 +0000513 // This ish isn't open
Kevin Clark022b2242009-03-05 21:05:37 +0000514 if (errno_copy == ENOTCONN) {
Mark Sleef9831082007-02-20 20:59:21 +0000515 throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000516 }
Mark Slee256bdc42007-11-27 08:42:19 +0000517
Mark Slee8d7e1f62006-06-07 06:48:56 +0000518 // Timed out!
Kevin Clark022b2242009-03-05 21:05:37 +0000519 if (errno_copy == ETIMEDOUT) {
Mark Sleef9831082007-02-20 20:59:21 +0000520 throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000521 }
Mark Slee256bdc42007-11-27 08:42:19 +0000522
Mark Slee8d7e1f62006-06-07 06:48:56 +0000523 // Some other error, whatevz
David Reiss01e55c12008-07-13 22:18:51 +0000524 throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy);
Mark Slee8d7e1f62006-06-07 06:48:56 +0000525 }
Mark Slee256bdc42007-11-27 08:42:19 +0000526
Mark Slee8d7e1f62006-06-07 06:48:56 +0000527 // The remote host has closed the socket
528 if (got == 0) {
David Reiss105961d2010-10-06 17:10:17 +0000529 // edhall: we used to call close() here, but our caller may want to deal
530 // with the socket fd and we'll close() in our destructor in any case.
Mark Slee8d7e1f62006-06-07 06:48:56 +0000531 return 0;
Mark Sleee8540632006-05-30 09:24:40 +0000532 }
Mark Slee256bdc42007-11-27 08:42:19 +0000533
Mark Sleee8540632006-05-30 09:24:40 +0000534 // Pack data into string
Mark Slee8d7e1f62006-06-07 06:48:56 +0000535 return got;
Mark Sleee8540632006-05-30 09:24:40 +0000536}
537
Mark Slee8d7e1f62006-06-07 06:48:56 +0000538void TSocket::write(const uint8_t* buf, uint32_t len) {
David Reiss105961d2010-10-06 17:10:17 +0000539 uint32_t sent = 0;
540
541 while (sent < len) {
542 uint32_t b = write_partial(buf + sent, len - sent);
543 if (b == 0) {
Bryan Duxbury97592662011-08-29 18:05:26 +0000544 // This should only happen if the timeout set with SO_SNDTIMEO expired.
545 // Raise an exception.
546 throw TTransportException(TTransportException::TIMED_OUT,
547 "send timeout expired");
David Reiss105961d2010-10-06 17:10:17 +0000548 }
549 sent += b;
550 }
551}
552
553uint32_t TSocket::write_partial(const uint8_t* buf, uint32_t len) {
Martin Kraemeree341cb2007-02-05 21:40:38 +0000554 if (socket_ < 0) {
Mark Sleef9831082007-02-20 20:59:21 +0000555 throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket");
Mark Slee8d7e1f62006-06-07 06:48:56 +0000556 }
557
Mark Sleee8540632006-05-30 09:24:40 +0000558 uint32_t sent = 0;
Mark Slee256bdc42007-11-27 08:42:19 +0000559
David Reiss105961d2010-10-06 17:10:17 +0000560 int flags = 0;
561#ifdef MSG_NOSIGNAL
562 // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we
563 // check for the EPIPE return condition and close the socket in that case
564 flags |= MSG_NOSIGNAL;
565#endif // ifdef MSG_NOSIGNAL
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000566
Roger Meier84e4a3c2011-09-16 20:58:44 +0000567 int b = send(socket_, const_cast_sockopt(buf + sent), len - sent, flags);
David Reiss105961d2010-10-06 17:10:17 +0000568 ++g_socket_syscalls;
Marc Slemko9d4a3e22006-07-21 19:53:48 +0000569
David Reiss105961d2010-10-06 17:10:17 +0000570 if (b < 0) {
571 if (errno == EWOULDBLOCK || errno == EAGAIN) {
572 return 0;
573 }
Mark Sleee8540632006-05-30 09:24:40 +0000574 // Fail on a send error
David Reiss105961d2010-10-06 17:10:17 +0000575 int errno_copy = errno;
576 GlobalOutput.perror("TSocket::write_partial() send() " + getSocketInfo(), errno_copy);
David Reiss9b209552008-04-08 06:26:05 +0000577
David Reiss105961d2010-10-06 17:10:17 +0000578 if (errno_copy == EPIPE || errno_copy == ECONNRESET || errno_copy == ENOTCONN) {
579 close();
580 throw TTransportException(TTransportException::NOT_OPEN, "write() send()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000581 }
Mark Slee256bdc42007-11-27 08:42:19 +0000582
David Reiss105961d2010-10-06 17:10:17 +0000583 throw TTransportException(TTransportException::UNKNOWN, "write() send()", errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000584 }
David Reiss105961d2010-10-06 17:10:17 +0000585
586 // Fail on blocked send
587 if (b == 0) {
588 throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0.");
589 }
590 return b;
Mark Sleee8540632006-05-30 09:24:40 +0000591}
592
dweatherford14b0ed62007-10-19 01:03:32 +0000593std::string TSocket::getHost() {
594 return host_;
595}
596
597int TSocket::getPort() {
598 return port_;
599}
600
Aditya Agarwalebc99e02007-01-15 23:14:58 +0000601void TSocket::setHost(string host) {
602 host_ = host;
603}
604
605void TSocket::setPort(int port) {
606 port_ = port;
607}
608
Mark Slee8d7e1f62006-06-07 06:48:56 +0000609void TSocket::setLinger(bool on, int linger) {
Mark Slee29050782006-09-29 00:12:30 +0000610 lingerOn_ = on;
611 lingerVal_ = linger;
Martin Kraemeree341cb2007-02-05 21:40:38 +0000612 if (socket_ < 0) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000613 return;
614 }
615
Mark Slee29050782006-09-29 00:12:30 +0000616 struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_};
Roger Meier84e4a3c2011-09-16 20:58:44 +0000617 int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, cast_sockopt(&l), sizeof(l));
Mark Slee29050782006-09-29 00:12:30 +0000618 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000619 int errno_copy = errno; // Copy errno because we're allocating memory.
620 GlobalOutput.perror("TSocket::setLinger() setsockopt() " + getSocketInfo(), errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000621 }
Mark Sleee8540632006-05-30 09:24:40 +0000622}
623
Mark Slee8d7e1f62006-06-07 06:48:56 +0000624void TSocket::setNoDelay(bool noDelay) {
Mark Slee29050782006-09-29 00:12:30 +0000625 noDelay_ = noDelay;
Roger Meier18f10502011-06-04 08:57:43 +0000626 if (socket_ < 0 || !path_.empty()) {
Mark Slee8d7e1f62006-06-07 06:48:56 +0000627 return;
628 }
629
Mark Sleee8540632006-05-30 09:24:40 +0000630 // Set socket to NODELAY
Mark Slee29050782006-09-29 00:12:30 +0000631 int v = noDelay_ ? 1 : 0;
Roger Meier84e4a3c2011-09-16 20:58:44 +0000632 int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, cast_sockopt(&v), sizeof(v));
Mark Slee29050782006-09-29 00:12:30 +0000633 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000634 int errno_copy = errno; // Copy errno because we're allocating memory.
635 GlobalOutput.perror("TSocket::setNoDelay() setsockopt() " + getSocketInfo(), errno_copy);
Mark Sleee8540632006-05-30 09:24:40 +0000636 }
Mark Sleee8540632006-05-30 09:24:40 +0000637}
Mark Slee29050782006-09-29 00:12:30 +0000638
639void TSocket::setConnTimeout(int ms) {
640 connTimeout_ = ms;
641}
642
643void TSocket::setRecvTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000644 if (ms < 0) {
645 char errBuf[512];
646 sprintf(errBuf, "TSocket::setRecvTimeout with negative input: %d\n", ms);
647 GlobalOutput(errBuf);
648 return;
649 }
Mark Slee29050782006-09-29 00:12:30 +0000650 recvTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000651
Martin Kraemeree341cb2007-02-05 21:40:38 +0000652 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000653 return;
654 }
655
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000656 recvTimeval_.tv_sec = (int)(recvTimeout_/1000);
657 recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000);
658
David Reiss22b18862008-04-08 06:25:45 +0000659 // Copy because poll may modify
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000660 struct timeval r = recvTimeval_;
Roger Meier84e4a3c2011-09-16 20:58:44 +0000661 int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, cast_sockopt(&r), sizeof(r));
Mark Slee29050782006-09-29 00:12:30 +0000662 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000663 int errno_copy = errno; // Copy errno because we're allocating memory.
664 GlobalOutput.perror("TSocket::setRecvTimeout() setsockopt() " + getSocketInfo(), errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000665 }
666}
667
668void TSocket::setSendTimeout(int ms) {
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000669 if (ms < 0) {
670 char errBuf[512];
671 sprintf(errBuf, "TSocket::setSendTimeout with negative input: %d\n", ms);
672 GlobalOutput(errBuf);
673 return;
674 }
Mark Slee29050782006-09-29 00:12:30 +0000675 sendTimeout_ = ms;
Aditya Agarwalc31769c2007-12-11 22:23:51 +0000676
Martin Kraemeree341cb2007-02-05 21:40:38 +0000677 if (socket_ < 0) {
Mark Slee29050782006-09-29 00:12:30 +0000678 return;
679 }
Mark Slee256bdc42007-11-27 08:42:19 +0000680
Mark Slee29050782006-09-29 00:12:30 +0000681 struct timeval s = {(int)(sendTimeout_/1000),
682 (int)((sendTimeout_%1000)*1000)};
Roger Meier84e4a3c2011-09-16 20:58:44 +0000683 int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, cast_sockopt(&s), sizeof(s));
Mark Slee29050782006-09-29 00:12:30 +0000684 if (ret == -1) {
David Reiss01e55c12008-07-13 22:18:51 +0000685 int errno_copy = errno; // Copy errno because we're allocating memory.
686 GlobalOutput.perror("TSocket::setSendTimeout() setsockopt() " + getSocketInfo(), errno_copy);
Mark Slee29050782006-09-29 00:12:30 +0000687 }
688}
689
Aditya Agarwale04475b2007-05-23 02:14:58 +0000690void TSocket::setMaxRecvRetries(int maxRecvRetries) {
691 maxRecvRetries_ = maxRecvRetries;
692}
693
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000694string TSocket::getSocketInfo() {
695 std::ostringstream oss;
David Reiss105961d2010-10-06 17:10:17 +0000696 if (host_.empty() || port_ == 0) {
697 oss << "<Host: " << getPeerAddress();
698 oss << " Port: " << getPeerPort() << ">";
699 } else {
700 oss << "<Host: " << host_ << " Port: " << port_ << ">";
701 }
Aditya Agarwal4529c4b2007-09-05 01:01:15 +0000702 return oss.str();
703}
704
Mark Sleeb4552922007-11-28 00:12:11 +0000705std::string TSocket::getPeerHost() {
Roger Meier18f10502011-06-04 08:57:43 +0000706 if (peerHost_.empty() && path_.empty()) {
Mark Sleeb4552922007-11-28 00:12:11 +0000707 struct sockaddr_storage addr;
David Reiss23248712010-10-06 17:10:08 +0000708 struct sockaddr* addrPtr;
709 socklen_t addrLen;
Mark Sleeb4552922007-11-28 00:12:11 +0000710
711 if (socket_ < 0) {
712 return host_;
713 }
714
David Reiss23248712010-10-06 17:10:08 +0000715 addrPtr = getCachedAddress(&addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000716
David Reiss23248712010-10-06 17:10:08 +0000717 if (addrPtr == NULL) {
718 addrLen = sizeof(addr);
719 if (getpeername(socket_, (sockaddr*) &addr, &addrLen) != 0) {
720 return peerHost_;
721 }
722 addrPtr = (sockaddr*)&addr;
723
724 setCachedAddress(addrPtr, addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000725 }
726
727 char clienthost[NI_MAXHOST];
728 char clientservice[NI_MAXSERV];
729
David Reiss23248712010-10-06 17:10:08 +0000730 getnameinfo((sockaddr*) addrPtr, addrLen,
Mark Sleeb4552922007-11-28 00:12:11 +0000731 clienthost, sizeof(clienthost),
732 clientservice, sizeof(clientservice), 0);
733
734 peerHost_ = clienthost;
735 }
736 return peerHost_;
737}
738
739std::string TSocket::getPeerAddress() {
Roger Meier18f10502011-06-04 08:57:43 +0000740 if (peerAddress_.empty() && path_.empty()) {
Mark Sleeb4552922007-11-28 00:12:11 +0000741 struct sockaddr_storage addr;
David Reiss23248712010-10-06 17:10:08 +0000742 struct sockaddr* addrPtr;
743 socklen_t addrLen;
Mark Sleeb4552922007-11-28 00:12:11 +0000744
745 if (socket_ < 0) {
746 return peerAddress_;
747 }
748
David Reiss23248712010-10-06 17:10:08 +0000749 addrPtr = getCachedAddress(&addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000750
David Reiss23248712010-10-06 17:10:08 +0000751 if (addrPtr == NULL) {
752 addrLen = sizeof(addr);
753 if (getpeername(socket_, (sockaddr*) &addr, &addrLen) != 0) {
754 return peerAddress_;
755 }
756 addrPtr = (sockaddr*)&addr;
757
758 setCachedAddress(addrPtr, addrLen);
Mark Sleeb4552922007-11-28 00:12:11 +0000759 }
760
761 char clienthost[NI_MAXHOST];
762 char clientservice[NI_MAXSERV];
763
David Reiss23248712010-10-06 17:10:08 +0000764 getnameinfo(addrPtr, addrLen,
Mark Sleeb4552922007-11-28 00:12:11 +0000765 clienthost, sizeof(clienthost),
766 clientservice, sizeof(clientservice),
767 NI_NUMERICHOST|NI_NUMERICSERV);
768
769 peerAddress_ = clienthost;
770 peerPort_ = std::atoi(clientservice);
771 }
772 return peerAddress_;
773}
774
775int TSocket::getPeerPort() {
776 getPeerAddress();
777 return peerPort_;
778}
779
David Reiss23248712010-10-06 17:10:08 +0000780void TSocket::setCachedAddress(const sockaddr* addr, socklen_t len) {
Roger Meier18f10502011-06-04 08:57:43 +0000781 if (!path_.empty()) {
782 return;
783 }
784
David Reiss23248712010-10-06 17:10:08 +0000785 switch (addr->sa_family) {
786 case AF_INET:
787 if (len == sizeof(sockaddr_in)) {
788 memcpy((void*)&cachedPeerAddr_.ipv4, (void*)addr, len);
789 }
790 break;
791
792 case AF_INET6:
793 if (len == sizeof(sockaddr_in6)) {
794 memcpy((void*)&cachedPeerAddr_.ipv6, (void*)addr, len);
795 }
796 break;
797 }
798}
799
800sockaddr* TSocket::getCachedAddress(socklen_t* len) const {
801 switch (cachedPeerAddr_.ipv4.sin_family) {
802 case AF_INET:
803 *len = sizeof(sockaddr_in);
804 return (sockaddr*) &cachedPeerAddr_.ipv4;
805
806 case AF_INET6:
807 *len = sizeof(sockaddr_in6);
808 return (sockaddr*) &cachedPeerAddr_.ipv6;
809
810 default:
811 return NULL;
812 }
813}
814
David Reiss1c20c872010-03-09 05:20:14 +0000815bool TSocket::useLowMinRto_ = false;
816void TSocket::setUseLowMinRto(bool useLowMinRto) {
817 useLowMinRto_ = useLowMinRto;
818}
819bool TSocket::getUseLowMinRto() {
820 return useLowMinRto_;
821}
822
T Jake Lucianib5e62212009-01-31 22:36:20 +0000823}}} // apache::thrift::transport