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