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 | |
Mark Slee | a9848d7 | 2007-02-21 04:54:05 +0000 | [diff] [blame] | 184 | if (isOpen()) { |
Bryan Duxbury | 010f1e0 | 2010-09-02 00:56:53 +0000 | [diff] [blame] | 185 | return; |
Mark Slee | a9848d7 | 2007-02-21 04:54:05 +0000 | [diff] [blame] | 186 | } |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 187 | |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 188 | 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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 194 | if (socket_ == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 195 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 196 | GlobalOutput.perror("TSocket::open() socket() " + getSocketInfo(), errno_copy); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 197 | throw TTransportException(TTransportException::NOT_OPEN, "socket()", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 198 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 199 | |
| 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 Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 216 | // 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 Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 225 | // 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] | 226 | int flags = fcntl(socket_, F_GETFL, 0); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 227 | if (connTimeout_ > 0) { |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 228 | if (-1 == fcntl(socket_, F_SETFL, flags | O_NONBLOCK)) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 229 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 230 | GlobalOutput.perror("TSocket::open() fcntl() " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 231 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 232 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 233 | } else { |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 234 | if (-1 == fcntl(socket_, F_SETFL, flags & ~O_NONBLOCK)) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 235 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 236 | GlobalOutput.perror("TSocket::open() fcntl " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 237 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 238 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 241 | // Connect the socket |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 242 | int ret; |
| 243 | if (! path_.empty()) { |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 244 | |
| 245 | #ifndef _WIN32 |
| 246 | |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 247 | 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 Meier | d11ca5a | 2010-10-18 08:22:57 +0000 | [diff] [blame] | 257 | snprintf(address.sun_path, sizeof(address.sun_path), "%s", path_.c_str()); |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 258 | len = sizeof(address); |
| 259 | ret = connect(socket_, (struct sockaddr *) &address, len); |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 260 | |
| 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 Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 266 | } else { |
| 267 | ret = connect(socket_, res->ai_addr, res->ai_addrlen); |
| 268 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 269 | |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 270 | // success case |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 271 | if (ret == 0) { |
| 272 | goto done; |
| 273 | } |
| 274 | |
Roger Meier | 8cd3b1a | 2011-09-23 14:25:22 +0000 | [diff] [blame^] | 275 | if ((errno != EINPROGRESS) && (errno != EWOULDBLOCK)) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 276 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 277 | GlobalOutput.perror("TSocket::open() connect() " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 278 | throw TTransportException(TTransportException::NOT_OPEN, "connect() failed", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 279 | } |
| 280 | |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 281 | |
| 282 | struct pollfd fds[1]; |
David Reiss | c88eb8c | 2008-06-11 01:18:54 +0000 | [diff] [blame] | 283 | std::memset(fds, 0 , sizeof(fds)); |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 284 | fds[0].fd = socket_; |
| 285 | fds[0].events = POLLOUT; |
| 286 | ret = poll(fds, 1, connTimeout_); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 287 | |
| 288 | if (ret > 0) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 289 | // Ensure the socket is connected and that there are no errors set |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 290 | int val; |
| 291 | socklen_t lon; |
| 292 | lon = sizeof(int); |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 293 | int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, cast_sockopt(&val), &lon); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 294 | if (ret2 == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 295 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 296 | GlobalOutput.perror("TSocket::open() getsockopt() " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 297 | throw TTransportException(TTransportException::NOT_OPEN, "getsockopt()", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 298 | } |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 299 | // no errors on socket, go to town |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 300 | if (val == 0) { |
| 301 | goto done; |
| 302 | } |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 303 | GlobalOutput.perror("TSocket::open() error on socket (after poll) " + getSocketInfo(), val); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 304 | throw TTransportException(TTransportException::NOT_OPEN, "socket open() error", val); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 305 | } else if (ret == 0) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 306 | // socket timed out |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 307 | string errStr = "TSocket::open() timed out " + getSocketInfo(); |
| 308 | GlobalOutput(errStr.c_str()); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 309 | throw TTransportException(TTransportException::NOT_OPEN, "open() timed out"); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 310 | } else { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 311 | // error on poll() |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 312 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 313 | GlobalOutput.perror("TSocket::open() poll() " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 314 | throw TTransportException(TTransportException::NOT_OPEN, "poll() failed", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | done: |
| 318 | // Set socket back to normal mode (blocking) |
| 319 | fcntl(socket_, F_SETFL, flags); |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 320 | |
Roger Meier | 18f1050 | 2011-06-04 08:57:43 +0000 | [diff] [blame] | 321 | if (path_.empty()) { |
| 322 | setCachedAddress(res->ai_addr, res->ai_addrlen); |
| 323 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 326 | void TSocket::open() { |
| 327 | if (isOpen()) { |
Bryan Duxbury | 010f1e0 | 2010-09-02 00:56:53 +0000 | [diff] [blame] | 328 | return; |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 329 | } |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 330 | if (! path_.empty()) { |
| 331 | unix_open(); |
| 332 | } else { |
| 333 | local_open(); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | void 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 | |
| 344 | void TSocket::local_open(){ |
Roger Meier | 8cd3b1a | 2011-09-23 14:25:22 +0000 | [diff] [blame^] | 345 | |
| 346 | #ifdef _WIN32 |
| 347 | TWinsockSingleton::create(); |
| 348 | #endif // _WIN32 |
| 349 | |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 350 | if (isOpen()) { |
| 351 | return; |
| 352 | } |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 353 | |
| 354 | // Validate port number |
David Reiss | 450e35d | 2010-03-09 05:19:41 +0000 | [diff] [blame] | 355 | if (port_ < 0 || port_ > 0xFFFF) { |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 356 | throw TTransportException(TTransportException::NOT_OPEN, "Specified port is invalid"); |
| 357 | } |
| 358 | |
| 359 | struct addrinfo hints, *res, *res0; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 360 | res = NULL; |
| 361 | res0 = NULL; |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 362 | int error; |
David Reiss | 450e35d | 2010-03-09 05:19:41 +0000 | [diff] [blame] | 363 | char port[sizeof("65535")]; |
David Reiss | c88eb8c | 2008-06-11 01:18:54 +0000 | [diff] [blame] | 364 | std::memset(&hints, 0, sizeof(hints)); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 365 | hints.ai_family = PF_UNSPEC; |
| 366 | hints.ai_socktype = SOCK_STREAM; |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 367 | hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 368 | sprintf(port, "%d", port_); |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 369 | |
Mark Slee | c37b4c5 | 2007-12-05 23:03:37 +0000 | [diff] [blame] | 370 | error = getaddrinfo(host_.c_str(), port, &hints, &res0); |
| 371 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 372 | if (error) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 373 | string errStr = "TSocket::open() getaddrinfo() " + getSocketInfo() + string(gai_strerror(error)); |
| 374 | GlobalOutput(errStr.c_str()); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 375 | close(); |
| 376 | throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for client socket."); |
| 377 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 378 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 379 | // 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 Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame] | 390 | freeaddrinfo(res0); // cleanup on failure |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 391 | throw; |
| 392 | } |
| 393 | } |
| 394 | } |
Mark Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame] | 395 | |
| 396 | // Free address structure memory |
| 397 | freeaddrinfo(res0); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 400 | void TSocket::close() { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 401 | if (socket_ >= 0) { |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 402 | |
| 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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 411 | } |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 412 | socket_ = -1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 413 | } |
| 414 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 415 | void TSocket::setSocketFD(int socket) { |
| 416 | if (socket_ >= 0) { |
| 417 | close(); |
| 418 | } |
| 419 | socket_ = socket; |
| 420 | } |
| 421 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 422 | uint32_t TSocket::read(uint8_t* buf, uint32_t len) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 423 | if (socket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 424 | throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 425 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 426 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 427 | 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 Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 435 | // 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] | 436 | // 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 Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 441 | try_again: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 442 | // Read from the socket |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 443 | struct timeval begin; |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 444 | 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 Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 451 | int got = recv(socket_, cast_sockopt(buf), len, 0); |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 452 | int errno_copy = errno; //gettimeofday can change errno |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 453 | ++g_socket_syscalls; |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 454 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 455 | // Check for error on read |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 456 | if (got < 0) { |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 457 | if (errno_copy == EAGAIN) { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 458 | // 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 Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 463 | // check if this is the lack of resources or timeout case |
David Reiss | a1a1511 | 2010-03-09 05:19:54 +0000 | [diff] [blame] | 464 | 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 Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 469 | if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) { |
| 470 | if (retries++ < maxRecvRetries_) { |
| 471 | usleep(50); |
| 472 | goto try_again; |
| 473 | } else { |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 474 | throw TTransportException(TTransportException::TIMED_OUT, |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 475 | "EAGAIN (unavailable resources)"); |
| 476 | } |
| 477 | } else { |
| 478 | // infer that timeout has been hit |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 479 | throw TTransportException(TTransportException::TIMED_OUT, |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 480 | "EAGAIN (timed out)"); |
| 481 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 482 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 483 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 484 | // If interrupted, try again |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 485 | if (errno_copy == EINTR && retries++ < maxRecvRetries_) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 486 | goto try_again; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 487 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 488 | |
David Reiss | 840e752 | 2009-06-04 00:10:50 +0000 | [diff] [blame] | 489 | #if defined __FreeBSD__ || defined __MACH__ |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 490 | if (errno_copy == ECONNRESET) { |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 491 | /* shigin: freebsd doesn't follow POSIX semantic of recv and fails with |
| 492 | * ECONNRESET if peer performed shutdown |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 493 | * edhall: eliminated close() since we do that in the destructor. |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 494 | */ |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 495 | return 0; |
David Reiss | 840e752 | 2009-06-04 00:10:50 +0000 | [diff] [blame] | 496 | } |
| 497 | #endif |
| 498 | |
| 499 | // Now it's not a try again case, but a real probblez |
| 500 | GlobalOutput.perror("TSocket::read() recv() " + getSocketInfo(), errno_copy); |
| 501 | |
| 502 | // If we disconnect with no linger time |
| 503 | if (errno_copy == ECONNRESET) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 504 | throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 505 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 506 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 507 | // This ish isn't open |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 508 | if (errno_copy == ENOTCONN) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 509 | throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 510 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 511 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 512 | // Timed out! |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 513 | if (errno_copy == ETIMEDOUT) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 514 | throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 515 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 516 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 517 | // Some other error, whatevz |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 518 | throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 519 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 520 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 521 | // The remote host has closed the socket |
| 522 | if (got == 0) { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 523 | // edhall: we used to call close() here, but our caller may want to deal |
| 524 | // with the socket fd and we'll close() in our destructor in any case. |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 525 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 526 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 527 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 528 | // Pack data into string |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 529 | return got; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 532 | void TSocket::write(const uint8_t* buf, uint32_t len) { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 533 | uint32_t sent = 0; |
| 534 | |
| 535 | while (sent < len) { |
| 536 | uint32_t b = write_partial(buf + sent, len - sent); |
| 537 | if (b == 0) { |
Bryan Duxbury | 9759266 | 2011-08-29 18:05:26 +0000 | [diff] [blame] | 538 | // This should only happen if the timeout set with SO_SNDTIMEO expired. |
| 539 | // Raise an exception. |
| 540 | throw TTransportException(TTransportException::TIMED_OUT, |
| 541 | "send timeout expired"); |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 542 | } |
| 543 | sent += b; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | uint32_t TSocket::write_partial(const uint8_t* buf, uint32_t len) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 548 | if (socket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 549 | throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 552 | uint32_t sent = 0; |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 553 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 554 | int flags = 0; |
| 555 | #ifdef MSG_NOSIGNAL |
| 556 | // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we |
| 557 | // check for the EPIPE return condition and close the socket in that case |
| 558 | flags |= MSG_NOSIGNAL; |
| 559 | #endif // ifdef MSG_NOSIGNAL |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 560 | |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 561 | int b = send(socket_, const_cast_sockopt(buf + sent), len - sent, flags); |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 562 | ++g_socket_syscalls; |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 563 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 564 | if (b < 0) { |
| 565 | if (errno == EWOULDBLOCK || errno == EAGAIN) { |
| 566 | return 0; |
| 567 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 568 | // Fail on a send error |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 569 | int errno_copy = errno; |
| 570 | GlobalOutput.perror("TSocket::write_partial() send() " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 571 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 572 | if (errno_copy == EPIPE || errno_copy == ECONNRESET || errno_copy == ENOTCONN) { |
| 573 | close(); |
| 574 | throw TTransportException(TTransportException::NOT_OPEN, "write() send()", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 575 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 576 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 577 | throw TTransportException(TTransportException::UNKNOWN, "write() send()", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 578 | } |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 579 | |
| 580 | // Fail on blocked send |
| 581 | if (b == 0) { |
| 582 | throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0."); |
| 583 | } |
| 584 | return b; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 585 | } |
| 586 | |
dweatherford | 14b0ed6 | 2007-10-19 01:03:32 +0000 | [diff] [blame] | 587 | std::string TSocket::getHost() { |
| 588 | return host_; |
| 589 | } |
| 590 | |
| 591 | int TSocket::getPort() { |
| 592 | return port_; |
| 593 | } |
| 594 | |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 595 | void TSocket::setHost(string host) { |
| 596 | host_ = host; |
| 597 | } |
| 598 | |
| 599 | void TSocket::setPort(int port) { |
| 600 | port_ = port; |
| 601 | } |
| 602 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 603 | void TSocket::setLinger(bool on, int linger) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 604 | lingerOn_ = on; |
| 605 | lingerVal_ = linger; |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 606 | if (socket_ < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 607 | return; |
| 608 | } |
| 609 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 610 | struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_}; |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 611 | 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] | 612 | if (ret == -1) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 613 | int errno_copy = errno; // Copy errno because we're allocating memory. |
| 614 | GlobalOutput.perror("TSocket::setLinger() setsockopt() " + getSocketInfo(), errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 615 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 618 | void TSocket::setNoDelay(bool noDelay) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 619 | noDelay_ = noDelay; |
Roger Meier | 18f1050 | 2011-06-04 08:57:43 +0000 | [diff] [blame] | 620 | if (socket_ < 0 || !path_.empty()) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 621 | return; |
| 622 | } |
| 623 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 624 | // Set socket to NODELAY |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 625 | int v = noDelay_ ? 1 : 0; |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 626 | 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] | 627 | if (ret == -1) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 628 | int errno_copy = errno; // Copy errno because we're allocating memory. |
| 629 | GlobalOutput.perror("TSocket::setNoDelay() setsockopt() " + getSocketInfo(), errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 630 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 631 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 632 | |
| 633 | void TSocket::setConnTimeout(int ms) { |
| 634 | connTimeout_ = ms; |
| 635 | } |
| 636 | |
| 637 | void TSocket::setRecvTimeout(int ms) { |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 638 | if (ms < 0) { |
| 639 | char errBuf[512]; |
| 640 | sprintf(errBuf, "TSocket::setRecvTimeout with negative input: %d\n", ms); |
| 641 | GlobalOutput(errBuf); |
| 642 | return; |
| 643 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 644 | recvTimeout_ = ms; |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 645 | |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 646 | if (socket_ < 0) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 647 | return; |
| 648 | } |
| 649 | |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 650 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 651 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
| 652 | |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 653 | // Copy because poll may modify |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 654 | struct timeval r = recvTimeval_; |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 655 | 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] | 656 | if (ret == -1) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 657 | int errno_copy = errno; // Copy errno because we're allocating memory. |
| 658 | GlobalOutput.perror("TSocket::setRecvTimeout() setsockopt() " + getSocketInfo(), errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
| 662 | void TSocket::setSendTimeout(int ms) { |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 663 | if (ms < 0) { |
| 664 | char errBuf[512]; |
| 665 | sprintf(errBuf, "TSocket::setSendTimeout with negative input: %d\n", ms); |
| 666 | GlobalOutput(errBuf); |
| 667 | return; |
| 668 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 669 | sendTimeout_ = ms; |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 670 | |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 671 | if (socket_ < 0) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 672 | return; |
| 673 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 674 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 675 | struct timeval s = {(int)(sendTimeout_/1000), |
| 676 | (int)((sendTimeout_%1000)*1000)}; |
Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame] | 677 | 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] | 678 | if (ret == -1) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 679 | int errno_copy = errno; // Copy errno because we're allocating memory. |
| 680 | GlobalOutput.perror("TSocket::setSendTimeout() setsockopt() " + getSocketInfo(), errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 684 | void TSocket::setMaxRecvRetries(int maxRecvRetries) { |
| 685 | maxRecvRetries_ = maxRecvRetries; |
| 686 | } |
| 687 | |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 688 | string TSocket::getSocketInfo() { |
| 689 | std::ostringstream oss; |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 690 | if (host_.empty() || port_ == 0) { |
| 691 | oss << "<Host: " << getPeerAddress(); |
| 692 | oss << " Port: " << getPeerPort() << ">"; |
| 693 | } else { |
| 694 | oss << "<Host: " << host_ << " Port: " << port_ << ">"; |
| 695 | } |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 696 | return oss.str(); |
| 697 | } |
| 698 | |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 699 | std::string TSocket::getPeerHost() { |
Roger Meier | 18f1050 | 2011-06-04 08:57:43 +0000 | [diff] [blame] | 700 | if (peerHost_.empty() && path_.empty()) { |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 701 | struct sockaddr_storage addr; |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 702 | struct sockaddr* addrPtr; |
| 703 | socklen_t addrLen; |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 704 | |
| 705 | if (socket_ < 0) { |
| 706 | return host_; |
| 707 | } |
| 708 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 709 | addrPtr = getCachedAddress(&addrLen); |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 710 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 711 | if (addrPtr == NULL) { |
| 712 | addrLen = sizeof(addr); |
| 713 | if (getpeername(socket_, (sockaddr*) &addr, &addrLen) != 0) { |
| 714 | return peerHost_; |
| 715 | } |
| 716 | addrPtr = (sockaddr*)&addr; |
| 717 | |
| 718 | setCachedAddress(addrPtr, addrLen); |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | char clienthost[NI_MAXHOST]; |
| 722 | char clientservice[NI_MAXSERV]; |
| 723 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 724 | getnameinfo((sockaddr*) addrPtr, addrLen, |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 725 | clienthost, sizeof(clienthost), |
| 726 | clientservice, sizeof(clientservice), 0); |
| 727 | |
| 728 | peerHost_ = clienthost; |
| 729 | } |
| 730 | return peerHost_; |
| 731 | } |
| 732 | |
| 733 | std::string TSocket::getPeerAddress() { |
Roger Meier | 18f1050 | 2011-06-04 08:57:43 +0000 | [diff] [blame] | 734 | if (peerAddress_.empty() && path_.empty()) { |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 735 | struct sockaddr_storage addr; |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 736 | struct sockaddr* addrPtr; |
| 737 | socklen_t addrLen; |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 738 | |
| 739 | if (socket_ < 0) { |
| 740 | return peerAddress_; |
| 741 | } |
| 742 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 743 | addrPtr = getCachedAddress(&addrLen); |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 744 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 745 | if (addrPtr == NULL) { |
| 746 | addrLen = sizeof(addr); |
| 747 | if (getpeername(socket_, (sockaddr*) &addr, &addrLen) != 0) { |
| 748 | return peerAddress_; |
| 749 | } |
| 750 | addrPtr = (sockaddr*)&addr; |
| 751 | |
| 752 | setCachedAddress(addrPtr, addrLen); |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | char clienthost[NI_MAXHOST]; |
| 756 | char clientservice[NI_MAXSERV]; |
| 757 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 758 | getnameinfo(addrPtr, addrLen, |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 759 | clienthost, sizeof(clienthost), |
| 760 | clientservice, sizeof(clientservice), |
| 761 | NI_NUMERICHOST|NI_NUMERICSERV); |
| 762 | |
| 763 | peerAddress_ = clienthost; |
| 764 | peerPort_ = std::atoi(clientservice); |
| 765 | } |
| 766 | return peerAddress_; |
| 767 | } |
| 768 | |
| 769 | int TSocket::getPeerPort() { |
| 770 | getPeerAddress(); |
| 771 | return peerPort_; |
| 772 | } |
| 773 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 774 | void TSocket::setCachedAddress(const sockaddr* addr, socklen_t len) { |
Roger Meier | 18f1050 | 2011-06-04 08:57:43 +0000 | [diff] [blame] | 775 | if (!path_.empty()) { |
| 776 | return; |
| 777 | } |
| 778 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 779 | switch (addr->sa_family) { |
| 780 | case AF_INET: |
| 781 | if (len == sizeof(sockaddr_in)) { |
| 782 | memcpy((void*)&cachedPeerAddr_.ipv4, (void*)addr, len); |
| 783 | } |
| 784 | break; |
| 785 | |
| 786 | case AF_INET6: |
| 787 | if (len == sizeof(sockaddr_in6)) { |
| 788 | memcpy((void*)&cachedPeerAddr_.ipv6, (void*)addr, len); |
| 789 | } |
| 790 | break; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | sockaddr* TSocket::getCachedAddress(socklen_t* len) const { |
| 795 | switch (cachedPeerAddr_.ipv4.sin_family) { |
| 796 | case AF_INET: |
| 797 | *len = sizeof(sockaddr_in); |
| 798 | return (sockaddr*) &cachedPeerAddr_.ipv4; |
| 799 | |
| 800 | case AF_INET6: |
| 801 | *len = sizeof(sockaddr_in6); |
| 802 | return (sockaddr*) &cachedPeerAddr_.ipv6; |
| 803 | |
| 804 | default: |
| 805 | return NULL; |
| 806 | } |
| 807 | } |
| 808 | |
David Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 809 | bool TSocket::useLowMinRto_ = false; |
| 810 | void TSocket::setUseLowMinRto(bool useLowMinRto) { |
| 811 | useLowMinRto_ = useLowMinRto; |
| 812 | } |
| 813 | bool TSocket::getUseLowMinRto() { |
| 814 | return useLowMinRto_; |
| 815 | } |
| 816 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 817 | }}} // apache::thrift::transport |