David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 20 | #ifdef HAVE_CONFIG_H |
Marc Slemko | e03da18 | 2006-07-21 21:32:36 +0000 | [diff] [blame] | 21 | #include <config.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 22 | #endif |
David Reiss | c88eb8c | 2008-06-11 01:18:54 +0000 | [diff] [blame] | 23 | #include <cstring> |
| 24 | #include <sstream> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 25 | #ifdef HAVE_SYS_SOCKET_H |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 26 | #include <sys/socket.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 27 | #endif |
| 28 | #ifdef HAVE_SYS_UN_H |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 29 | #include <sys/un.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 30 | #endif |
| 31 | #ifdef HAVE_SYS_POLL_H |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 32 | #include <sys/poll.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 33 | #endif |
Mark Slee | dd56497 | 2007-08-21 02:39:57 +0000 | [diff] [blame] | 34 | #include <sys/types.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 35 | #ifdef HAVE_ARPA_INET_H |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 36 | #include <arpa/inet.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 37 | #endif |
| 38 | #ifdef HAVE_NETINET_IN_H |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 39 | #include <netinet/in.h> |
| 40 | #include <netinet/tcp.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 41 | #endif |
| 42 | #ifdef HAVE_UNISTD_H |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 43 | #include <unistd.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 44 | #endif |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 45 | #include <errno.h> |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 46 | #include <fcntl.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 47 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 48 | #include "concurrency/Monitor.h" |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 49 | #include "TSocket.h" |
| 50 | #include "TTransportException.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 51 | |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 52 | #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 | |
| 60 | template<class T> |
| 61 | inline const SOCKOPT_CAST_T* const_cast_sockopt(const T* v) { |
| 62 | return reinterpret_cast<const SOCKOPT_CAST_T*>(v); |
| 63 | } |
| 64 | |
| 65 | template<class T> |
| 66 | inline SOCKOPT_CAST_T* cast_sockopt(T* v) { |
| 67 | return reinterpret_cast<SOCKOPT_CAST_T*>(v); |
| 68 | } |
| 69 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 70 | namespace apache { namespace thrift { namespace transport { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 71 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 72 | using namespace std; |
| 73 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 74 | // Global var to track total socket sys calls |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 75 | uint32_t g_socket_syscalls = 0; |
| 76 | |
| 77 | /** |
| 78 | * TSocket implementation. |
| 79 | * |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 80 | */ |
| 81 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 82 | TSocket::TSocket(string host, int port) : |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 83 | host_(host), |
| 84 | port_(port), |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 85 | 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 | |
| 98 | TSocket::TSocket(string path) : |
| 99 | host_(""), |
| 100 | port_(0), |
| 101 | path_(path), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 102 | socket_(-1), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 103 | connTimeout_(0), |
| 104 | sendTimeout_(0), |
| 105 | recvTimeout_(0), |
| 106 | lingerOn_(1), |
| 107 | lingerVal_(0), |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 108 | noDelay_(1), |
| 109 | maxRecvRetries_(5) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 110 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 111 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 112 | cachedPeerAddr_.ipv4.sin_family = AF_UNSPEC; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 115 | TSocket::TSocket() : |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 116 | host_(""), |
| 117 | port_(0), |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 118 | path_(""), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 119 | socket_(-1), |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 120 | connTimeout_(0), |
| 121 | sendTimeout_(0), |
| 122 | recvTimeout_(0), |
| 123 | lingerOn_(1), |
| 124 | lingerVal_(0), |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 125 | noDelay_(1), |
| 126 | maxRecvRetries_(5) { |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 127 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 128 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 129 | cachedPeerAddr_.ipv4.sin_family = AF_UNSPEC; |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 132 | TSocket::TSocket(int socket) : |
| 133 | host_(""), |
| 134 | port_(0), |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 135 | path_(""), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 136 | socket_(socket), |
| 137 | connTimeout_(0), |
| 138 | sendTimeout_(0), |
| 139 | recvTimeout_(0), |
| 140 | lingerOn_(1), |
| 141 | lingerVal_(0), |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 142 | noDelay_(1), |
| 143 | maxRecvRetries_(5) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 144 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 145 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 146 | cachedPeerAddr_.ipv4.sin_family = AF_UNSPEC; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 147 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 148 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 149 | TSocket::~TSocket() { |
| 150 | close(); |
| 151 | } |
| 152 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 153 | bool TSocket::isOpen() { |
| 154 | return (socket_ >= 0); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 157 | bool TSocket::peek() { |
| 158 | if (!isOpen()) { |
| 159 | return false; |
| 160 | } |
| 161 | uint8_t buf; |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 162 | int r = recv(socket_, cast_sockopt(&buf), 1, MSG_PEEK); |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 163 | if (r == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 164 | int errno_copy = errno; |
David Reiss | 840e752 | 2009-06-04 00:10:50 +0000 | [diff] [blame] | 165 | #if defined __FreeBSD__ || defined __MACH__ |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 166 | /* 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 Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 176 | GlobalOutput.perror("TSocket::peek() recv() " + getSocketInfo(), errno_copy); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 177 | throw TTransportException(TTransportException::UNKNOWN, "recv()", errno_copy); |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 178 | } |
| 179 | return (r > 0); |
| 180 | } |
| 181 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 182 | void TSocket::openConnection(struct addrinfo *res) { |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 183 | |
| 184 | #ifdef _WIN32 |
| 185 | TWinsockSingleton::create(); |
| 186 | #endif // _WIN32 |
| 187 | |
Mark Slee | a9848d7 | 2007-02-21 04:54:05 +0000 | [diff] [blame] | 188 | if (isOpen()) { |
Bryan Duxbury | 010f1e0 | 2010-09-02 00:56:53 +0000 | [diff] [blame] | 189 | return; |
Mark Slee | a9848d7 | 2007-02-21 04:54:05 +0000 | [diff] [blame] | 190 | } |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 191 | |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 192 | 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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 198 | if (socket_ == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 199 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 200 | GlobalOutput.perror("TSocket::open() socket() " + getSocketInfo(), errno_copy); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 201 | throw TTransportException(TTransportException::NOT_OPEN, "socket()", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 202 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 203 | |
| 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 Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 220 | // 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 Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 229 | // Set the socket to be non blocking for connect if a timeout exists |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 230 | int flags = fcntl(socket_, F_GETFL, 0); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 231 | if (connTimeout_ > 0) { |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 232 | if (-1 == fcntl(socket_, F_SETFL, flags | O_NONBLOCK)) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 233 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 234 | GlobalOutput.perror("TSocket::open() fcntl() " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 235 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 236 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 237 | } else { |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 238 | if (-1 == fcntl(socket_, F_SETFL, flags & ~O_NONBLOCK)) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 239 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 240 | GlobalOutput.perror("TSocket::open() fcntl " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 241 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 242 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 245 | // Connect the socket |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 246 | int ret; |
| 247 | if (! path_.empty()) { |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 248 | |
| 249 | #ifndef _WIN32 |
| 250 | |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 251 | 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 Meier | d11ca5a | 2010-10-18 08:22:57 +0000 | [diff] [blame] | 261 | snprintf(address.sun_path, sizeof(address.sun_path), "%s", path_.c_str()); |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 262 | len = sizeof(address); |
| 263 | ret = connect(socket_, (struct sockaddr *) &address, len); |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 264 | |
| 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 Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 270 | } else { |
| 271 | ret = connect(socket_, res->ai_addr, res->ai_addrlen); |
| 272 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 273 | |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 274 | // success case |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 275 | if (ret == 0) { |
| 276 | goto done; |
| 277 | } |
| 278 | |
| 279 | if (errno != EINPROGRESS) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 280 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 281 | GlobalOutput.perror("TSocket::open() connect() " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 282 | throw TTransportException(TTransportException::NOT_OPEN, "connect() failed", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 283 | } |
| 284 | |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 285 | |
| 286 | struct pollfd fds[1]; |
David Reiss | c88eb8c | 2008-06-11 01:18:54 +0000 | [diff] [blame] | 287 | std::memset(fds, 0 , sizeof(fds)); |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 288 | fds[0].fd = socket_; |
| 289 | fds[0].events = POLLOUT; |
| 290 | ret = poll(fds, 1, connTimeout_); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 291 | |
| 292 | if (ret > 0) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 293 | // Ensure the socket is connected and that there are no errors set |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 294 | int val; |
| 295 | socklen_t lon; |
| 296 | lon = sizeof(int); |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 297 | int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, cast_sockopt(&val), &lon); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 298 | if (ret2 == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 299 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 300 | GlobalOutput.perror("TSocket::open() getsockopt() " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 301 | throw TTransportException(TTransportException::NOT_OPEN, "getsockopt()", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 302 | } |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 303 | // no errors on socket, go to town |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 304 | if (val == 0) { |
| 305 | goto done; |
| 306 | } |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 307 | GlobalOutput.perror("TSocket::open() error on socket (after poll) " + getSocketInfo(), val); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 308 | throw TTransportException(TTransportException::NOT_OPEN, "socket open() error", val); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 309 | } else if (ret == 0) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 310 | // socket timed out |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 311 | string errStr = "TSocket::open() timed out " + getSocketInfo(); |
| 312 | GlobalOutput(errStr.c_str()); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 313 | throw TTransportException(TTransportException::NOT_OPEN, "open() timed out"); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 314 | } else { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 315 | // error on poll() |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 316 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 317 | GlobalOutput.perror("TSocket::open() poll() " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 318 | throw TTransportException(TTransportException::NOT_OPEN, "poll() failed", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | done: |
| 322 | // Set socket back to normal mode (blocking) |
| 323 | fcntl(socket_, F_SETFL, flags); |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 324 | |
Roger Meier | 18f1050 | 2011-06-04 08:57:43 +0000 | [diff] [blame] | 325 | if (path_.empty()) { |
| 326 | setCachedAddress(res->ai_addr, res->ai_addrlen); |
| 327 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 330 | void TSocket::open() { |
| 331 | if (isOpen()) { |
Bryan Duxbury | 010f1e0 | 2010-09-02 00:56:53 +0000 | [diff] [blame] | 332 | return; |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 333 | } |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 334 | if (! path_.empty()) { |
| 335 | unix_open(); |
| 336 | } else { |
| 337 | local_open(); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | void 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 | |
| 348 | void TSocket::local_open(){ |
| 349 | if (isOpen()) { |
| 350 | return; |
| 351 | } |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 352 | |
| 353 | // Validate port number |
David Reiss | 450e35d | 2010-03-09 05:19:41 +0000 | [diff] [blame] | 354 | if (port_ < 0 || port_ > 0xFFFF) { |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 355 | throw TTransportException(TTransportException::NOT_OPEN, "Specified port is invalid"); |
| 356 | } |
| 357 | |
| 358 | struct addrinfo hints, *res, *res0; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 359 | res = NULL; |
| 360 | res0 = NULL; |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 361 | int error; |
David Reiss | 450e35d | 2010-03-09 05:19:41 +0000 | [diff] [blame] | 362 | char port[sizeof("65535")]; |
David Reiss | c88eb8c | 2008-06-11 01:18:54 +0000 | [diff] [blame] | 363 | std::memset(&hints, 0, sizeof(hints)); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 364 | hints.ai_family = PF_UNSPEC; |
| 365 | hints.ai_socktype = SOCK_STREAM; |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 366 | hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 367 | sprintf(port, "%d", port_); |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 368 | |
Mark Slee | c37b4c5 | 2007-12-05 23:03:37 +0000 | [diff] [blame] | 369 | error = getaddrinfo(host_.c_str(), port, &hints, &res0); |
| 370 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 371 | if (error) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 372 | string errStr = "TSocket::open() getaddrinfo() " + getSocketInfo() + string(gai_strerror(error)); |
| 373 | GlobalOutput(errStr.c_str()); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 374 | close(); |
| 375 | throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for client socket."); |
| 376 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 377 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 378 | // 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 Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame] | 389 | freeaddrinfo(res0); // cleanup on failure |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 390 | throw; |
| 391 | } |
| 392 | } |
| 393 | } |
Mark Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame] | 394 | |
| 395 | // Free address structure memory |
| 396 | freeaddrinfo(res0); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 399 | void TSocket::close() { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 400 | if (socket_ >= 0) { |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 401 | |
| 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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 410 | } |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 411 | socket_ = -1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 412 | } |
| 413 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 414 | void TSocket::setSocketFD(int socket) { |
| 415 | if (socket_ >= 0) { |
| 416 | close(); |
| 417 | } |
| 418 | socket_ = socket; |
| 419 | } |
| 420 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 421 | uint32_t TSocket::read(uint8_t* buf, uint32_t len) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 422 | if (socket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 423 | throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 424 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 425 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 426 | 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 Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 434 | // if a readTimeout is specified along with a max number of recv retries, then |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 435 | // 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 Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 440 | try_again: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 441 | // Read from the socket |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 442 | struct timeval begin; |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 443 | 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 Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 450 | int got = recv(socket_, cast_sockopt(buf), len, 0); |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 451 | int errno_copy = errno; //gettimeofday can change errno |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 452 | ++g_socket_syscalls; |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 453 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 454 | // Check for error on read |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 455 | if (got < 0) { |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 456 | if (errno_copy == EAGAIN) { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 457 | // 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 Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 462 | // check if this is the lack of resources or timeout case |
David Reiss | a1a1511 | 2010-03-09 05:19:54 +0000 | [diff] [blame] | 463 | 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 Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 468 | if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) { |
| 469 | if (retries++ < maxRecvRetries_) { |
| 470 | usleep(50); |
| 471 | goto try_again; |
| 472 | } else { |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 473 | throw TTransportException(TTransportException::TIMED_OUT, |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 474 | "EAGAIN (unavailable resources)"); |
| 475 | } |
| 476 | } else { |
| 477 | // infer that timeout has been hit |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 478 | throw TTransportException(TTransportException::TIMED_OUT, |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 479 | "EAGAIN (timed out)"); |
| 480 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 481 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 482 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 483 | // If interrupted, try again |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 484 | if (errno_copy == EINTR && retries++ < maxRecvRetries_) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 485 | goto try_again; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 486 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 487 | |
David Reiss | 840e752 | 2009-06-04 00:10:50 +0000 | [diff] [blame] | 488 | #if defined __FreeBSD__ || defined __MACH__ |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 489 | if (errno_copy == ECONNRESET) { |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 490 | /* shigin: freebsd doesn't follow POSIX semantic of recv and fails with |
| 491 | * ECONNRESET if peer performed shutdown |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 492 | * edhall: eliminated close() since we do that in the destructor. |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 493 | */ |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 494 | return 0; |
David Reiss | 840e752 | 2009-06-04 00:10:50 +0000 | [diff] [blame] | 495 | } |
| 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 Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 503 | throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 504 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 505 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 506 | // This ish isn't open |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 507 | if (errno_copy == ENOTCONN) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 508 | throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 509 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 510 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 511 | // Timed out! |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 512 | if (errno_copy == ETIMEDOUT) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 513 | throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 514 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 515 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 516 | // Some other error, whatevz |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 517 | throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 518 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 519 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 520 | // The remote host has closed the socket |
| 521 | if (got == 0) { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 522 | // 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 Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 524 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 525 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 526 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 527 | // Pack data into string |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 528 | return got; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 531 | void TSocket::write(const uint8_t* buf, uint32_t len) { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 532 | uint32_t sent = 0; |
| 533 | |
| 534 | while (sent < len) { |
| 535 | uint32_t b = write_partial(buf + sent, len - sent); |
| 536 | if (b == 0) { |
Bryan Duxbury | 9759266 | 2011-08-29 18:05:26 +0000 | [diff] [blame] | 537 | // 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 Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 541 | } |
| 542 | sent += b; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | uint32_t TSocket::write_partial(const uint8_t* buf, uint32_t len) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 547 | if (socket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 548 | throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 551 | uint32_t sent = 0; |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 552 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 553 | 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 Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 559 | |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 560 | int b = send(socket_, const_cast_sockopt(buf + sent), len - sent, flags); |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 561 | ++g_socket_syscalls; |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 562 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 563 | if (b < 0) { |
| 564 | if (errno == EWOULDBLOCK || errno == EAGAIN) { |
| 565 | return 0; |
| 566 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 567 | // Fail on a send error |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 568 | int errno_copy = errno; |
| 569 | GlobalOutput.perror("TSocket::write_partial() send() " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 570 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 571 | if (errno_copy == EPIPE || errno_copy == ECONNRESET || errno_copy == ENOTCONN) { |
| 572 | close(); |
| 573 | throw TTransportException(TTransportException::NOT_OPEN, "write() send()", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 574 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 575 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 576 | throw TTransportException(TTransportException::UNKNOWN, "write() send()", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 577 | } |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 578 | |
| 579 | // Fail on blocked send |
| 580 | if (b == 0) { |
| 581 | throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0."); |
| 582 | } |
| 583 | return b; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 584 | } |
| 585 | |
dweatherford | 14b0ed6 | 2007-10-19 01:03:32 +0000 | [diff] [blame] | 586 | std::string TSocket::getHost() { |
| 587 | return host_; |
| 588 | } |
| 589 | |
| 590 | int TSocket::getPort() { |
| 591 | return port_; |
| 592 | } |
| 593 | |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 594 | void TSocket::setHost(string host) { |
| 595 | host_ = host; |
| 596 | } |
| 597 | |
| 598 | void TSocket::setPort(int port) { |
| 599 | port_ = port; |
| 600 | } |
| 601 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 602 | void TSocket::setLinger(bool on, int linger) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 603 | lingerOn_ = on; |
| 604 | lingerVal_ = linger; |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 605 | if (socket_ < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 606 | return; |
| 607 | } |
| 608 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 609 | struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_}; |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 610 | int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, cast_sockopt(&l), sizeof(l)); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 611 | if (ret == -1) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 612 | int errno_copy = errno; // Copy errno because we're allocating memory. |
| 613 | GlobalOutput.perror("TSocket::setLinger() setsockopt() " + getSocketInfo(), errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 614 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 617 | void TSocket::setNoDelay(bool noDelay) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 618 | noDelay_ = noDelay; |
Roger Meier | 18f1050 | 2011-06-04 08:57:43 +0000 | [diff] [blame] | 619 | if (socket_ < 0 || !path_.empty()) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 620 | return; |
| 621 | } |
| 622 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 623 | // Set socket to NODELAY |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 624 | int v = noDelay_ ? 1 : 0; |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 625 | int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, cast_sockopt(&v), sizeof(v)); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 626 | if (ret == -1) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 627 | int errno_copy = errno; // Copy errno because we're allocating memory. |
| 628 | GlobalOutput.perror("TSocket::setNoDelay() setsockopt() " + getSocketInfo(), errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 629 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 630 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 631 | |
| 632 | void TSocket::setConnTimeout(int ms) { |
| 633 | connTimeout_ = ms; |
| 634 | } |
| 635 | |
| 636 | void TSocket::setRecvTimeout(int ms) { |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 637 | 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 Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 643 | recvTimeout_ = ms; |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 644 | |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 645 | if (socket_ < 0) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 646 | return; |
| 647 | } |
| 648 | |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 649 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 650 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
| 651 | |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 652 | // Copy because poll may modify |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 653 | struct timeval r = recvTimeval_; |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 654 | int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, cast_sockopt(&r), sizeof(r)); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 655 | if (ret == -1) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 656 | int errno_copy = errno; // Copy errno because we're allocating memory. |
| 657 | GlobalOutput.perror("TSocket::setRecvTimeout() setsockopt() " + getSocketInfo(), errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
| 661 | void TSocket::setSendTimeout(int ms) { |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 662 | 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 Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 668 | sendTimeout_ = ms; |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 669 | |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 670 | if (socket_ < 0) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 671 | return; |
| 672 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 673 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 674 | struct timeval s = {(int)(sendTimeout_/1000), |
| 675 | (int)((sendTimeout_%1000)*1000)}; |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 676 | int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, cast_sockopt(&s), sizeof(s)); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 677 | if (ret == -1) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 678 | int errno_copy = errno; // Copy errno because we're allocating memory. |
| 679 | GlobalOutput.perror("TSocket::setSendTimeout() setsockopt() " + getSocketInfo(), errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 683 | void TSocket::setMaxRecvRetries(int maxRecvRetries) { |
| 684 | maxRecvRetries_ = maxRecvRetries; |
| 685 | } |
| 686 | |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 687 | string TSocket::getSocketInfo() { |
| 688 | std::ostringstream oss; |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 689 | if (host_.empty() || port_ == 0) { |
| 690 | oss << "<Host: " << getPeerAddress(); |
| 691 | oss << " Port: " << getPeerPort() << ">"; |
| 692 | } else { |
| 693 | oss << "<Host: " << host_ << " Port: " << port_ << ">"; |
| 694 | } |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 695 | return oss.str(); |
| 696 | } |
| 697 | |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 698 | std::string TSocket::getPeerHost() { |
Roger Meier | 18f1050 | 2011-06-04 08:57:43 +0000 | [diff] [blame] | 699 | if (peerHost_.empty() && path_.empty()) { |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 700 | struct sockaddr_storage addr; |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 701 | struct sockaddr* addrPtr; |
| 702 | socklen_t addrLen; |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 703 | |
| 704 | if (socket_ < 0) { |
| 705 | return host_; |
| 706 | } |
| 707 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 708 | addrPtr = getCachedAddress(&addrLen); |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 709 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 710 | 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 Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | char clienthost[NI_MAXHOST]; |
| 721 | char clientservice[NI_MAXSERV]; |
| 722 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 723 | getnameinfo((sockaddr*) addrPtr, addrLen, |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 724 | clienthost, sizeof(clienthost), |
| 725 | clientservice, sizeof(clientservice), 0); |
| 726 | |
| 727 | peerHost_ = clienthost; |
| 728 | } |
| 729 | return peerHost_; |
| 730 | } |
| 731 | |
| 732 | std::string TSocket::getPeerAddress() { |
Roger Meier | 18f1050 | 2011-06-04 08:57:43 +0000 | [diff] [blame] | 733 | if (peerAddress_.empty() && path_.empty()) { |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 734 | struct sockaddr_storage addr; |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 735 | struct sockaddr* addrPtr; |
| 736 | socklen_t addrLen; |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 737 | |
| 738 | if (socket_ < 0) { |
| 739 | return peerAddress_; |
| 740 | } |
| 741 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 742 | addrPtr = getCachedAddress(&addrLen); |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 743 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 744 | 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 Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | char clienthost[NI_MAXHOST]; |
| 755 | char clientservice[NI_MAXSERV]; |
| 756 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 757 | getnameinfo(addrPtr, addrLen, |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 758 | 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 | |
| 768 | int TSocket::getPeerPort() { |
| 769 | getPeerAddress(); |
| 770 | return peerPort_; |
| 771 | } |
| 772 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 773 | void TSocket::setCachedAddress(const sockaddr* addr, socklen_t len) { |
Roger Meier | 18f1050 | 2011-06-04 08:57:43 +0000 | [diff] [blame] | 774 | if (!path_.empty()) { |
| 775 | return; |
| 776 | } |
| 777 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 778 | 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 | |
| 793 | sockaddr* 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 Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 808 | bool TSocket::useLowMinRto_ = false; |
| 809 | void TSocket::setUseLowMinRto(bool useLowMinRto) { |
| 810 | useLowMinRto_ = useLowMinRto; |
| 811 | } |
| 812 | bool TSocket::getUseLowMinRto() { |
| 813 | return useLowMinRto_; |
| 814 | } |
| 815 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 816 | }}} // apache::thrift::transport |