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 | |
Marc Slemko | e03da18 | 2006-07-21 21:32:36 +0000 | [diff] [blame] | 20 | #include <config.h> |
David Reiss | c88eb8c | 2008-06-11 01:18:54 +0000 | [diff] [blame] | 21 | #include <cstring> |
| 22 | #include <sstream> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 23 | #include <sys/socket.h> |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 24 | #include <sys/un.h> |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 25 | #include <sys/poll.h> |
Mark Slee | dd56497 | 2007-08-21 02:39:57 +0000 | [diff] [blame] | 26 | #include <sys/types.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 27 | #include <arpa/inet.h> |
| 28 | #include <netinet/in.h> |
| 29 | #include <netinet/tcp.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 30 | #include <unistd.h> |
| 31 | #include <errno.h> |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 32 | #include <fcntl.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 33 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 34 | #include "concurrency/Monitor.h" |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 35 | #include "TSocket.h" |
| 36 | #include "TTransportException.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 37 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 38 | namespace apache { namespace thrift { namespace transport { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 39 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 40 | using namespace std; |
| 41 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 42 | // Global var to track total socket sys calls |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 43 | uint32_t g_socket_syscalls = 0; |
| 44 | |
| 45 | /** |
| 46 | * TSocket implementation. |
| 47 | * |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 48 | */ |
| 49 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 50 | TSocket::TSocket(string host, int port) : |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 51 | host_(host), |
| 52 | port_(port), |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 53 | path_(""), |
| 54 | socket_(-1), |
| 55 | connTimeout_(0), |
| 56 | sendTimeout_(0), |
| 57 | recvTimeout_(0), |
| 58 | lingerOn_(1), |
| 59 | lingerVal_(0), |
| 60 | noDelay_(1), |
| 61 | maxRecvRetries_(5) { |
| 62 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 63 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
| 64 | } |
| 65 | |
| 66 | TSocket::TSocket(string path) : |
| 67 | host_(""), |
| 68 | port_(0), |
| 69 | path_(path), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 70 | socket_(-1), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 71 | connTimeout_(0), |
| 72 | sendTimeout_(0), |
| 73 | recvTimeout_(0), |
| 74 | lingerOn_(1), |
| 75 | lingerVal_(0), |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 76 | noDelay_(1), |
| 77 | maxRecvRetries_(5) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 78 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 79 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 80 | cachedPeerAddr_.ipv4.sin_family = AF_UNSPEC; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 83 | TSocket::TSocket() : |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 84 | host_(""), |
| 85 | port_(0), |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 86 | path_(""), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 87 | socket_(-1), |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 88 | connTimeout_(0), |
| 89 | sendTimeout_(0), |
| 90 | recvTimeout_(0), |
| 91 | lingerOn_(1), |
| 92 | lingerVal_(0), |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 93 | noDelay_(1), |
| 94 | maxRecvRetries_(5) { |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 95 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 96 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 97 | cachedPeerAddr_.ipv4.sin_family = AF_UNSPEC; |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 100 | TSocket::TSocket(int socket) : |
| 101 | host_(""), |
| 102 | port_(0), |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 103 | path_(""), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 104 | socket_(socket), |
| 105 | connTimeout_(0), |
| 106 | sendTimeout_(0), |
| 107 | recvTimeout_(0), |
| 108 | lingerOn_(1), |
| 109 | lingerVal_(0), |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 110 | noDelay_(1), |
| 111 | maxRecvRetries_(5) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 112 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 113 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 114 | cachedPeerAddr_.ipv4.sin_family = AF_UNSPEC; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 115 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 116 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 117 | TSocket::~TSocket() { |
| 118 | close(); |
| 119 | } |
| 120 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 121 | bool TSocket::isOpen() { |
| 122 | return (socket_ >= 0); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 125 | bool TSocket::peek() { |
| 126 | if (!isOpen()) { |
| 127 | return false; |
| 128 | } |
| 129 | uint8_t buf; |
| 130 | int r = recv(socket_, &buf, 1, MSG_PEEK); |
| 131 | if (r == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 132 | int errno_copy = errno; |
David Reiss | 840e752 | 2009-06-04 00:10:50 +0000 | [diff] [blame] | 133 | #if defined __FreeBSD__ || defined __MACH__ |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 134 | /* shigin: |
| 135 | * freebsd returns -1 and ECONNRESET if socket was closed by |
| 136 | * the other side |
| 137 | */ |
| 138 | if (errno_copy == ECONNRESET) |
| 139 | { |
| 140 | close(); |
| 141 | return false; |
| 142 | } |
| 143 | #endif |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 144 | GlobalOutput.perror("TSocket::peek() recv() " + getSocketInfo(), errno_copy); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 145 | throw TTransportException(TTransportException::UNKNOWN, "recv()", errno_copy); |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 146 | } |
| 147 | return (r > 0); |
| 148 | } |
| 149 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 150 | void TSocket::openConnection(struct addrinfo *res) { |
Mark Slee | a9848d7 | 2007-02-21 04:54:05 +0000 | [diff] [blame] | 151 | if (isOpen()) { |
Bryan Duxbury | 010f1e0 | 2010-09-02 00:56:53 +0000 | [diff] [blame] | 152 | return; |
Mark Slee | a9848d7 | 2007-02-21 04:54:05 +0000 | [diff] [blame] | 153 | } |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 154 | |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 155 | if (! path_.empty()) { |
| 156 | socket_ = socket(PF_UNIX, SOCK_STREAM, IPPROTO_IP); |
| 157 | } else { |
| 158 | socket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
| 159 | } |
| 160 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 161 | if (socket_ == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 162 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 163 | GlobalOutput.perror("TSocket::open() socket() " + getSocketInfo(), errno_copy); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 164 | throw TTransportException(TTransportException::NOT_OPEN, "socket()", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 165 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 166 | |
| 167 | // Send timeout |
| 168 | if (sendTimeout_ > 0) { |
| 169 | setSendTimeout(sendTimeout_); |
| 170 | } |
| 171 | |
| 172 | // Recv timeout |
| 173 | if (recvTimeout_ > 0) { |
| 174 | setRecvTimeout(recvTimeout_); |
| 175 | } |
| 176 | |
| 177 | // Linger |
| 178 | setLinger(lingerOn_, lingerVal_); |
| 179 | |
| 180 | // No delay |
| 181 | setNoDelay(noDelay_); |
| 182 | |
David Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 183 | // Uses a low min RTO if asked to. |
| 184 | #ifdef TCP_LOW_MIN_RTO |
| 185 | if (getUseLowMinRto()) { |
| 186 | int one = 1; |
| 187 | setsockopt(socket_, IPPROTO_TCP, TCP_LOW_MIN_RTO, &one, sizeof(one)); |
| 188 | } |
| 189 | #endif |
| 190 | |
| 191 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 192 | // 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] | 193 | int flags = fcntl(socket_, F_GETFL, 0); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 194 | if (connTimeout_ > 0) { |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 195 | if (-1 == fcntl(socket_, F_SETFL, flags | O_NONBLOCK)) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 196 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 197 | GlobalOutput.perror("TSocket::open() fcntl() " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 198 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 199 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 200 | } else { |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 201 | if (-1 == fcntl(socket_, F_SETFL, flags & ~O_NONBLOCK)) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 202 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 203 | GlobalOutput.perror("TSocket::open() fcntl " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 204 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 205 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 208 | // Connect the socket |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 209 | int ret; |
| 210 | if (! path_.empty()) { |
| 211 | struct sockaddr_un address; |
| 212 | socklen_t len; |
| 213 | |
| 214 | if (path_.length() > sizeof(address.sun_path)) { |
| 215 | int errno_copy = errno; |
| 216 | GlobalOutput.perror("TSocket::open() Unix Domain socket path too long", errno_copy); |
| 217 | throw TTransportException(TTransportException::NOT_OPEN, " Unix Domain socket path too long"); |
| 218 | } |
| 219 | |
| 220 | address.sun_family = AF_UNIX; |
Roger Meier | d11ca5a | 2010-10-18 08:22:57 +0000 | [diff] [blame] | 221 | snprintf(address.sun_path, sizeof(address.sun_path), "%s", path_.c_str()); |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 222 | len = sizeof(address); |
| 223 | ret = connect(socket_, (struct sockaddr *) &address, len); |
| 224 | } else { |
| 225 | ret = connect(socket_, res->ai_addr, res->ai_addrlen); |
| 226 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 227 | |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 228 | // success case |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 229 | if (ret == 0) { |
| 230 | goto done; |
| 231 | } |
| 232 | |
| 233 | if (errno != EINPROGRESS) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 234 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 235 | GlobalOutput.perror("TSocket::open() connect() " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 236 | throw TTransportException(TTransportException::NOT_OPEN, "connect() failed", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 237 | } |
| 238 | |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 239 | |
| 240 | struct pollfd fds[1]; |
David Reiss | c88eb8c | 2008-06-11 01:18:54 +0000 | [diff] [blame] | 241 | std::memset(fds, 0 , sizeof(fds)); |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 242 | fds[0].fd = socket_; |
| 243 | fds[0].events = POLLOUT; |
| 244 | ret = poll(fds, 1, connTimeout_); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 245 | |
| 246 | if (ret > 0) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 247 | // Ensure the socket is connected and that there are no errors set |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 248 | int val; |
| 249 | socklen_t lon; |
| 250 | lon = sizeof(int); |
| 251 | int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon); |
| 252 | if (ret2 == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 253 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 254 | GlobalOutput.perror("TSocket::open() getsockopt() " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 255 | throw TTransportException(TTransportException::NOT_OPEN, "getsockopt()", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 256 | } |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 257 | // no errors on socket, go to town |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 258 | if (val == 0) { |
| 259 | goto done; |
| 260 | } |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 261 | GlobalOutput.perror("TSocket::open() error on socket (after poll) " + getSocketInfo(), val); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 262 | throw TTransportException(TTransportException::NOT_OPEN, "socket open() error", val); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 263 | } else if (ret == 0) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 264 | // socket timed out |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 265 | string errStr = "TSocket::open() timed out " + getSocketInfo(); |
| 266 | GlobalOutput(errStr.c_str()); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 267 | throw TTransportException(TTransportException::NOT_OPEN, "open() timed out"); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 268 | } else { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 269 | // error on poll() |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 270 | int errno_copy = errno; |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 271 | GlobalOutput.perror("TSocket::open() poll() " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 272 | throw TTransportException(TTransportException::NOT_OPEN, "poll() failed", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | done: |
| 276 | // Set socket back to normal mode (blocking) |
| 277 | fcntl(socket_, F_SETFL, flags); |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 278 | |
Roger Meier | 18f1050 | 2011-06-04 08:57:43 +0000 | [diff] [blame] | 279 | if (path_.empty()) { |
| 280 | setCachedAddress(res->ai_addr, res->ai_addrlen); |
| 281 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 284 | void TSocket::open() { |
| 285 | if (isOpen()) { |
Bryan Duxbury | 010f1e0 | 2010-09-02 00:56:53 +0000 | [diff] [blame] | 286 | return; |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 287 | } |
Bryan Duxbury | a18364a | 2010-09-28 14:36:07 +0000 | [diff] [blame] | 288 | if (! path_.empty()) { |
| 289 | unix_open(); |
| 290 | } else { |
| 291 | local_open(); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | void TSocket::unix_open(){ |
| 296 | if (! path_.empty()) { |
| 297 | // Unix Domain SOcket does not need addrinfo struct, so we pass NULL |
| 298 | openConnection(NULL); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | void TSocket::local_open(){ |
| 303 | if (isOpen()) { |
| 304 | return; |
| 305 | } |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 306 | |
| 307 | // Validate port number |
David Reiss | 450e35d | 2010-03-09 05:19:41 +0000 | [diff] [blame] | 308 | if (port_ < 0 || port_ > 0xFFFF) { |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 309 | throw TTransportException(TTransportException::NOT_OPEN, "Specified port is invalid"); |
| 310 | } |
| 311 | |
| 312 | struct addrinfo hints, *res, *res0; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 313 | res = NULL; |
| 314 | res0 = NULL; |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 315 | int error; |
David Reiss | 450e35d | 2010-03-09 05:19:41 +0000 | [diff] [blame] | 316 | char port[sizeof("65535")]; |
David Reiss | c88eb8c | 2008-06-11 01:18:54 +0000 | [diff] [blame] | 317 | std::memset(&hints, 0, sizeof(hints)); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 318 | hints.ai_family = PF_UNSPEC; |
| 319 | hints.ai_socktype = SOCK_STREAM; |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 320 | hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 321 | sprintf(port, "%d", port_); |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 322 | |
Mark Slee | c37b4c5 | 2007-12-05 23:03:37 +0000 | [diff] [blame] | 323 | error = getaddrinfo(host_.c_str(), port, &hints, &res0); |
| 324 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 325 | if (error) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 326 | string errStr = "TSocket::open() getaddrinfo() " + getSocketInfo() + string(gai_strerror(error)); |
| 327 | GlobalOutput(errStr.c_str()); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 328 | close(); |
| 329 | throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for client socket."); |
| 330 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 331 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 332 | // Cycle through all the returned addresses until one |
| 333 | // connects or push the exception up. |
| 334 | for (res = res0; res; res = res->ai_next) { |
| 335 | try { |
| 336 | openConnection(res); |
| 337 | break; |
| 338 | } catch (TTransportException& ttx) { |
| 339 | if (res->ai_next) { |
| 340 | close(); |
| 341 | } else { |
| 342 | close(); |
Mark Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame] | 343 | freeaddrinfo(res0); // cleanup on failure |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 344 | throw; |
| 345 | } |
| 346 | } |
| 347 | } |
Mark Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame] | 348 | |
| 349 | // Free address structure memory |
| 350 | freeaddrinfo(res0); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 353 | void TSocket::close() { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 354 | if (socket_ >= 0) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 355 | shutdown(socket_, SHUT_RDWR); |
| 356 | ::close(socket_); |
| 357 | } |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 358 | socket_ = -1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 359 | } |
| 360 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 361 | void TSocket::setSocketFD(int socket) { |
| 362 | if (socket_ >= 0) { |
| 363 | close(); |
| 364 | } |
| 365 | socket_ = socket; |
| 366 | } |
| 367 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 368 | uint32_t TSocket::read(uint8_t* buf, uint32_t len) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 369 | if (socket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 370 | throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 371 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 372 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 373 | int32_t retries = 0; |
| 374 | |
| 375 | // EAGAIN can be signalled both when a timeout has occurred and when |
| 376 | // the system is out of resources (an awesome undocumented feature). |
| 377 | // The following is an approximation of the time interval under which |
| 378 | // EAGAIN is taken to indicate an out of resources error. |
| 379 | uint32_t eagainThresholdMicros = 0; |
| 380 | if (recvTimeout_) { |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 381 | // 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] | 382 | // the threshold will ensure that the read timeout is not exceeded even in the |
| 383 | // case of resource errors |
| 384 | eagainThresholdMicros = (recvTimeout_*1000)/ ((maxRecvRetries_>0) ? maxRecvRetries_ : 2); |
| 385 | } |
| 386 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 387 | try_again: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 388 | // Read from the socket |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 389 | struct timeval begin; |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 390 | if (recvTimeout_ > 0) { |
| 391 | gettimeofday(&begin, NULL); |
| 392 | } else { |
| 393 | // if there is no read timeout we don't need the TOD to determine whether |
| 394 | // an EAGAIN is due to a timeout or an out-of-resource condition. |
| 395 | begin.tv_sec = begin.tv_usec = 0; |
| 396 | } |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 397 | int got = recv(socket_, buf, len, 0); |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 398 | int errno_copy = errno; //gettimeofday can change errno |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 399 | ++g_socket_syscalls; |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 400 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 401 | // Check for error on read |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 402 | if (got < 0) { |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 403 | if (errno_copy == EAGAIN) { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 404 | // if no timeout we can assume that resource exhaustion has occurred. |
| 405 | if (recvTimeout_ == 0) { |
| 406 | throw TTransportException(TTransportException::TIMED_OUT, |
| 407 | "EAGAIN (unavailable resources)"); |
| 408 | } |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 409 | // check if this is the lack of resources or timeout case |
David Reiss | a1a1511 | 2010-03-09 05:19:54 +0000 | [diff] [blame] | 410 | struct timeval end; |
| 411 | gettimeofday(&end, NULL); |
| 412 | uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000) |
| 413 | + (((uint64_t)(end.tv_usec - begin.tv_usec)))); |
| 414 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 415 | if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) { |
| 416 | if (retries++ < maxRecvRetries_) { |
| 417 | usleep(50); |
| 418 | goto try_again; |
| 419 | } else { |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 420 | throw TTransportException(TTransportException::TIMED_OUT, |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 421 | "EAGAIN (unavailable resources)"); |
| 422 | } |
| 423 | } else { |
| 424 | // infer that timeout has been hit |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 425 | throw TTransportException(TTransportException::TIMED_OUT, |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 426 | "EAGAIN (timed out)"); |
| 427 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 428 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 429 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 430 | // If interrupted, try again |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 431 | if (errno_copy == EINTR && retries++ < maxRecvRetries_) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 432 | goto try_again; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 433 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 434 | |
David Reiss | 840e752 | 2009-06-04 00:10:50 +0000 | [diff] [blame] | 435 | #if defined __FreeBSD__ || defined __MACH__ |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 436 | if (errno_copy == ECONNRESET) { |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 437 | /* shigin: freebsd doesn't follow POSIX semantic of recv and fails with |
| 438 | * ECONNRESET if peer performed shutdown |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 439 | * edhall: eliminated close() since we do that in the destructor. |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 440 | */ |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 441 | return 0; |
David Reiss | 840e752 | 2009-06-04 00:10:50 +0000 | [diff] [blame] | 442 | } |
| 443 | #endif |
| 444 | |
| 445 | // Now it's not a try again case, but a real probblez |
| 446 | GlobalOutput.perror("TSocket::read() recv() " + getSocketInfo(), errno_copy); |
| 447 | |
| 448 | // If we disconnect with no linger time |
| 449 | if (errno_copy == ECONNRESET) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 450 | throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 451 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 452 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 453 | // This ish isn't open |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 454 | if (errno_copy == ENOTCONN) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 455 | throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 456 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 457 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 458 | // Timed out! |
Kevin Clark | 022b224 | 2009-03-05 21:05:37 +0000 | [diff] [blame] | 459 | if (errno_copy == ETIMEDOUT) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 460 | throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 461 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 462 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 463 | // Some other error, whatevz |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 464 | throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 465 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 466 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 467 | // The remote host has closed the socket |
| 468 | if (got == 0) { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 469 | // edhall: we used to call close() here, but our caller may want to deal |
| 470 | // 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] | 471 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 472 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 473 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 474 | // Pack data into string |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 475 | return got; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 478 | void TSocket::write(const uint8_t* buf, uint32_t len) { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 479 | uint32_t sent = 0; |
| 480 | |
| 481 | while (sent < len) { |
| 482 | uint32_t b = write_partial(buf + sent, len - sent); |
| 483 | if (b == 0) { |
Bryan Duxbury | 9759266 | 2011-08-29 18:05:26 +0000 | [diff] [blame^] | 484 | // This should only happen if the timeout set with SO_SNDTIMEO expired. |
| 485 | // Raise an exception. |
| 486 | throw TTransportException(TTransportException::TIMED_OUT, |
| 487 | "send timeout expired"); |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 488 | } |
| 489 | sent += b; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | uint32_t TSocket::write_partial(const uint8_t* buf, uint32_t len) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 494 | if (socket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 495 | throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 498 | uint32_t sent = 0; |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 499 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 500 | int flags = 0; |
| 501 | #ifdef MSG_NOSIGNAL |
| 502 | // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we |
| 503 | // check for the EPIPE return condition and close the socket in that case |
| 504 | flags |= MSG_NOSIGNAL; |
| 505 | #endif // ifdef MSG_NOSIGNAL |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 506 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 507 | int b = send(socket_, buf + sent, len - sent, flags); |
| 508 | ++g_socket_syscalls; |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 509 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 510 | if (b < 0) { |
| 511 | if (errno == EWOULDBLOCK || errno == EAGAIN) { |
| 512 | return 0; |
| 513 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 514 | // Fail on a send error |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 515 | int errno_copy = errno; |
| 516 | GlobalOutput.perror("TSocket::write_partial() send() " + getSocketInfo(), errno_copy); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 517 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 518 | if (errno_copy == EPIPE || errno_copy == ECONNRESET || errno_copy == ENOTCONN) { |
| 519 | close(); |
| 520 | throw TTransportException(TTransportException::NOT_OPEN, "write() send()", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 521 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 522 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 523 | throw TTransportException(TTransportException::UNKNOWN, "write() send()", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 524 | } |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 525 | |
| 526 | // Fail on blocked send |
| 527 | if (b == 0) { |
| 528 | throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0."); |
| 529 | } |
| 530 | return b; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 531 | } |
| 532 | |
dweatherford | 14b0ed6 | 2007-10-19 01:03:32 +0000 | [diff] [blame] | 533 | std::string TSocket::getHost() { |
| 534 | return host_; |
| 535 | } |
| 536 | |
| 537 | int TSocket::getPort() { |
| 538 | return port_; |
| 539 | } |
| 540 | |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 541 | void TSocket::setHost(string host) { |
| 542 | host_ = host; |
| 543 | } |
| 544 | |
| 545 | void TSocket::setPort(int port) { |
| 546 | port_ = port; |
| 547 | } |
| 548 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 549 | void TSocket::setLinger(bool on, int linger) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 550 | lingerOn_ = on; |
| 551 | lingerVal_ = linger; |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 552 | if (socket_ < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 553 | return; |
| 554 | } |
| 555 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 556 | struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_}; |
| 557 | int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l)); |
| 558 | if (ret == -1) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 559 | int errno_copy = errno; // Copy errno because we're allocating memory. |
| 560 | GlobalOutput.perror("TSocket::setLinger() setsockopt() " + getSocketInfo(), errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 561 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 564 | void TSocket::setNoDelay(bool noDelay) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 565 | noDelay_ = noDelay; |
Roger Meier | 18f1050 | 2011-06-04 08:57:43 +0000 | [diff] [blame] | 566 | if (socket_ < 0 || !path_.empty()) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 567 | return; |
| 568 | } |
| 569 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 570 | // Set socket to NODELAY |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 571 | int v = noDelay_ ? 1 : 0; |
| 572 | int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v)); |
| 573 | if (ret == -1) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 574 | int errno_copy = errno; // Copy errno because we're allocating memory. |
| 575 | GlobalOutput.perror("TSocket::setNoDelay() setsockopt() " + getSocketInfo(), errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 576 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 577 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 578 | |
| 579 | void TSocket::setConnTimeout(int ms) { |
| 580 | connTimeout_ = ms; |
| 581 | } |
| 582 | |
| 583 | void TSocket::setRecvTimeout(int ms) { |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 584 | if (ms < 0) { |
| 585 | char errBuf[512]; |
| 586 | sprintf(errBuf, "TSocket::setRecvTimeout with negative input: %d\n", ms); |
| 587 | GlobalOutput(errBuf); |
| 588 | return; |
| 589 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 590 | recvTimeout_ = ms; |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 591 | |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 592 | if (socket_ < 0) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 593 | return; |
| 594 | } |
| 595 | |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 596 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 597 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
| 598 | |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 599 | // Copy because poll may modify |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 600 | struct timeval r = recvTimeval_; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 601 | int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r)); |
| 602 | if (ret == -1) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 603 | int errno_copy = errno; // Copy errno because we're allocating memory. |
| 604 | GlobalOutput.perror("TSocket::setRecvTimeout() setsockopt() " + getSocketInfo(), errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
| 608 | void TSocket::setSendTimeout(int ms) { |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 609 | if (ms < 0) { |
| 610 | char errBuf[512]; |
| 611 | sprintf(errBuf, "TSocket::setSendTimeout with negative input: %d\n", ms); |
| 612 | GlobalOutput(errBuf); |
| 613 | return; |
| 614 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 615 | sendTimeout_ = ms; |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 616 | |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 617 | if (socket_ < 0) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 618 | return; |
| 619 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 620 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 621 | struct timeval s = {(int)(sendTimeout_/1000), |
| 622 | (int)((sendTimeout_%1000)*1000)}; |
| 623 | int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s)); |
| 624 | if (ret == -1) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 625 | int errno_copy = errno; // Copy errno because we're allocating memory. |
| 626 | GlobalOutput.perror("TSocket::setSendTimeout() setsockopt() " + getSocketInfo(), errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 630 | void TSocket::setMaxRecvRetries(int maxRecvRetries) { |
| 631 | maxRecvRetries_ = maxRecvRetries; |
| 632 | } |
| 633 | |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 634 | string TSocket::getSocketInfo() { |
| 635 | std::ostringstream oss; |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 636 | if (host_.empty() || port_ == 0) { |
| 637 | oss << "<Host: " << getPeerAddress(); |
| 638 | oss << " Port: " << getPeerPort() << ">"; |
| 639 | } else { |
| 640 | oss << "<Host: " << host_ << " Port: " << port_ << ">"; |
| 641 | } |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 642 | return oss.str(); |
| 643 | } |
| 644 | |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 645 | std::string TSocket::getPeerHost() { |
Roger Meier | 18f1050 | 2011-06-04 08:57:43 +0000 | [diff] [blame] | 646 | if (peerHost_.empty() && path_.empty()) { |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 647 | struct sockaddr_storage addr; |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 648 | struct sockaddr* addrPtr; |
| 649 | socklen_t addrLen; |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 650 | |
| 651 | if (socket_ < 0) { |
| 652 | return host_; |
| 653 | } |
| 654 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 655 | addrPtr = getCachedAddress(&addrLen); |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 656 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 657 | if (addrPtr == NULL) { |
| 658 | addrLen = sizeof(addr); |
| 659 | if (getpeername(socket_, (sockaddr*) &addr, &addrLen) != 0) { |
| 660 | return peerHost_; |
| 661 | } |
| 662 | addrPtr = (sockaddr*)&addr; |
| 663 | |
| 664 | setCachedAddress(addrPtr, addrLen); |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | char clienthost[NI_MAXHOST]; |
| 668 | char clientservice[NI_MAXSERV]; |
| 669 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 670 | getnameinfo((sockaddr*) addrPtr, addrLen, |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 671 | clienthost, sizeof(clienthost), |
| 672 | clientservice, sizeof(clientservice), 0); |
| 673 | |
| 674 | peerHost_ = clienthost; |
| 675 | } |
| 676 | return peerHost_; |
| 677 | } |
| 678 | |
| 679 | std::string TSocket::getPeerAddress() { |
Roger Meier | 18f1050 | 2011-06-04 08:57:43 +0000 | [diff] [blame] | 680 | if (peerAddress_.empty() && path_.empty()) { |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 681 | struct sockaddr_storage addr; |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 682 | struct sockaddr* addrPtr; |
| 683 | socklen_t addrLen; |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 684 | |
| 685 | if (socket_ < 0) { |
| 686 | return peerAddress_; |
| 687 | } |
| 688 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 689 | addrPtr = getCachedAddress(&addrLen); |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 690 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 691 | if (addrPtr == NULL) { |
| 692 | addrLen = sizeof(addr); |
| 693 | if (getpeername(socket_, (sockaddr*) &addr, &addrLen) != 0) { |
| 694 | return peerAddress_; |
| 695 | } |
| 696 | addrPtr = (sockaddr*)&addr; |
| 697 | |
| 698 | setCachedAddress(addrPtr, addrLen); |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | char clienthost[NI_MAXHOST]; |
| 702 | char clientservice[NI_MAXSERV]; |
| 703 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 704 | getnameinfo(addrPtr, addrLen, |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 705 | clienthost, sizeof(clienthost), |
| 706 | clientservice, sizeof(clientservice), |
| 707 | NI_NUMERICHOST|NI_NUMERICSERV); |
| 708 | |
| 709 | peerAddress_ = clienthost; |
| 710 | peerPort_ = std::atoi(clientservice); |
| 711 | } |
| 712 | return peerAddress_; |
| 713 | } |
| 714 | |
| 715 | int TSocket::getPeerPort() { |
| 716 | getPeerAddress(); |
| 717 | return peerPort_; |
| 718 | } |
| 719 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 720 | void TSocket::setCachedAddress(const sockaddr* addr, socklen_t len) { |
Roger Meier | 18f1050 | 2011-06-04 08:57:43 +0000 | [diff] [blame] | 721 | if (!path_.empty()) { |
| 722 | return; |
| 723 | } |
| 724 | |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 725 | switch (addr->sa_family) { |
| 726 | case AF_INET: |
| 727 | if (len == sizeof(sockaddr_in)) { |
| 728 | memcpy((void*)&cachedPeerAddr_.ipv4, (void*)addr, len); |
| 729 | } |
| 730 | break; |
| 731 | |
| 732 | case AF_INET6: |
| 733 | if (len == sizeof(sockaddr_in6)) { |
| 734 | memcpy((void*)&cachedPeerAddr_.ipv6, (void*)addr, len); |
| 735 | } |
| 736 | break; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | sockaddr* TSocket::getCachedAddress(socklen_t* len) const { |
| 741 | switch (cachedPeerAddr_.ipv4.sin_family) { |
| 742 | case AF_INET: |
| 743 | *len = sizeof(sockaddr_in); |
| 744 | return (sockaddr*) &cachedPeerAddr_.ipv4; |
| 745 | |
| 746 | case AF_INET6: |
| 747 | *len = sizeof(sockaddr_in6); |
| 748 | return (sockaddr*) &cachedPeerAddr_.ipv6; |
| 749 | |
| 750 | default: |
| 751 | return NULL; |
| 752 | } |
| 753 | } |
| 754 | |
David Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 755 | bool TSocket::useLowMinRto_ = false; |
| 756 | void TSocket::setUseLowMinRto(bool useLowMinRto) { |
| 757 | useLowMinRto_ = useLowMinRto; |
| 758 | } |
| 759 | bool TSocket::getUseLowMinRto() { |
| 760 | return useLowMinRto_; |
| 761 | } |
| 762 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 763 | }}} // apache::thrift::transport |