Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Marc Slemko | e03da18 | 2006-07-21 21:32:36 +0000 | [diff] [blame] | 7 | #include <config.h> |
David Reiss | c88eb8c | 2008-06-11 01:18:54 +0000 | [diff] [blame] | 8 | #include <cstring> |
| 9 | #include <sstream> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 10 | #include <sys/socket.h> |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 11 | #include <sys/poll.h> |
Mark Slee | dd56497 | 2007-08-21 02:39:57 +0000 | [diff] [blame] | 12 | #include <sys/types.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 13 | #include <arpa/inet.h> |
| 14 | #include <netinet/in.h> |
| 15 | #include <netinet/tcp.h> |
| 16 | #include <netdb.h> |
| 17 | #include <unistd.h> |
| 18 | #include <errno.h> |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 19 | #include <fcntl.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 20 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 21 | #include "concurrency/Monitor.h" |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 22 | #include "TSocket.h" |
| 23 | #include "TTransportException.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 24 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 25 | namespace facebook { namespace thrift { namespace transport { |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 26 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 27 | using namespace std; |
| 28 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 29 | // Global var to track total socket sys calls |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 30 | uint32_t g_socket_syscalls = 0; |
| 31 | |
| 32 | /** |
| 33 | * TSocket implementation. |
| 34 | * |
| 35 | * @author Mark Slee <mcslee@facebook.com> |
| 36 | */ |
| 37 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 38 | TSocket::TSocket(string host, int port) : |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 39 | host_(host), |
| 40 | port_(port), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 41 | socket_(-1), |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 42 | connTimeout_(0), |
| 43 | sendTimeout_(0), |
| 44 | recvTimeout_(0), |
| 45 | lingerOn_(1), |
| 46 | lingerVal_(0), |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 47 | noDelay_(1), |
| 48 | maxRecvRetries_(5) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 49 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 50 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 53 | TSocket::TSocket() : |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 54 | host_(""), |
| 55 | port_(0), |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 56 | socket_(-1), |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 57 | connTimeout_(0), |
| 58 | sendTimeout_(0), |
| 59 | recvTimeout_(0), |
| 60 | lingerOn_(1), |
| 61 | lingerVal_(0), |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 62 | noDelay_(1), |
| 63 | maxRecvRetries_(5) { |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 64 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 65 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
| 66 | } |
| 67 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 68 | TSocket::TSocket(int socket) : |
| 69 | host_(""), |
| 70 | port_(0), |
| 71 | socket_(socket), |
| 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); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 81 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 82 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 83 | TSocket::~TSocket() { |
| 84 | close(); |
| 85 | } |
| 86 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 87 | bool TSocket::isOpen() { |
| 88 | return (socket_ >= 0); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 91 | bool TSocket::peek() { |
| 92 | if (!isOpen()) { |
| 93 | return false; |
| 94 | } |
| 95 | uint8_t buf; |
| 96 | int r = recv(socket_, &buf, 1, MSG_PEEK); |
| 97 | if (r == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 98 | int errno_copy = errno; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 99 | string errStr = "TSocket::peek() recv() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 100 | GlobalOutput(errStr.c_str()); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 101 | throw TTransportException(TTransportException::UNKNOWN, "recv()", errno_copy); |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 102 | } |
| 103 | return (r > 0); |
| 104 | } |
| 105 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 106 | void TSocket::openConnection(struct addrinfo *res) { |
Mark Slee | a9848d7 | 2007-02-21 04:54:05 +0000 | [diff] [blame] | 107 | if (isOpen()) { |
| 108 | throw TTransportException(TTransportException::ALREADY_OPEN); |
| 109 | } |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 110 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 111 | socket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 112 | if (socket_ == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 113 | int errno_copy = errno; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 114 | string errStr = "TSocket::open() socket() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 115 | GlobalOutput(errStr.c_str()); |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 116 | throw TTransportException(TTransportException::NOT_OPEN, "socket()", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 117 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 118 | |
| 119 | // Send timeout |
| 120 | if (sendTimeout_ > 0) { |
| 121 | setSendTimeout(sendTimeout_); |
| 122 | } |
| 123 | |
| 124 | // Recv timeout |
| 125 | if (recvTimeout_ > 0) { |
| 126 | setRecvTimeout(recvTimeout_); |
| 127 | } |
| 128 | |
| 129 | // Linger |
| 130 | setLinger(lingerOn_, lingerVal_); |
| 131 | |
| 132 | // No delay |
| 133 | setNoDelay(noDelay_); |
| 134 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 135 | // 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] | 136 | int flags = fcntl(socket_, F_GETFL, 0); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 137 | if (connTimeout_ > 0) { |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 138 | if (-1 == fcntl(socket_, F_SETFL, flags | O_NONBLOCK)) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 139 | int errno_copy = errno; |
| 140 | string errStr = "TSocket::open() fcntl() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
| 141 | GlobalOutput(errStr.c_str()); |
| 142 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 143 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 144 | } else { |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 145 | if (-1 == fcntl(socket_, F_SETFL, flags & ~O_NONBLOCK)) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 146 | int errno_copy = errno; |
| 147 | string errStr = "TSocket::open() fcntl " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
| 148 | GlobalOutput(errStr.c_str()); |
| 149 | throw TTransportException(TTransportException::NOT_OPEN, "fcntl() failed", errno_copy); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 150 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 153 | // Connect the socket |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 154 | int ret = connect(socket_, res->ai_addr, res->ai_addrlen); |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 155 | |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 156 | // success case |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 157 | if (ret == 0) { |
| 158 | goto done; |
| 159 | } |
| 160 | |
| 161 | if (errno != EINPROGRESS) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 162 | int errno_copy = errno; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 163 | string errStr = "TSocket::open() connect() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 164 | GlobalOutput(errStr.c_str()); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 165 | throw TTransportException(TTransportException::NOT_OPEN, "connect() failed", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 166 | } |
| 167 | |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 168 | |
| 169 | struct pollfd fds[1]; |
David Reiss | c88eb8c | 2008-06-11 01:18:54 +0000 | [diff] [blame] | 170 | std::memset(fds, 0 , sizeof(fds)); |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 171 | fds[0].fd = socket_; |
| 172 | fds[0].events = POLLOUT; |
| 173 | ret = poll(fds, 1, connTimeout_); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 174 | |
| 175 | if (ret > 0) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 176 | // Ensure the socket is connected and that there are no errors set |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 177 | int val; |
| 178 | socklen_t lon; |
| 179 | lon = sizeof(int); |
| 180 | int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon); |
| 181 | if (ret2 == -1) { |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 182 | int errno_copy = errno; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 183 | string errStr = "TSocket::open() getsockopt() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 184 | GlobalOutput(errStr.c_str()); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 185 | throw TTransportException(TTransportException::NOT_OPEN, "getsockopt()", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 186 | } |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 187 | // no errors on socket, go to town |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 188 | if (val == 0) { |
| 189 | goto done; |
| 190 | } |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 191 | string errStr = "TSocket::open() error on socket (after poll) " + getSocketInfo() + TOutput::strerror_s(val); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 192 | GlobalOutput(errStr.c_str()); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 193 | throw TTransportException(TTransportException::NOT_OPEN, "socket open() error", val); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 194 | } else if (ret == 0) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 195 | // socket timed out |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 196 | string errStr = "TSocket::open() timed out " + getSocketInfo(); |
| 197 | GlobalOutput(errStr.c_str()); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 198 | throw TTransportException(TTransportException::NOT_OPEN, "open() timed out"); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 199 | } else { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 200 | // error on poll() |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 201 | int errno_copy = errno; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 202 | string errStr = "TSocket::open() poll() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 203 | GlobalOutput(errStr.c_str()); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 204 | throw TTransportException(TTransportException::NOT_OPEN, "poll() failed", errno_copy); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | done: |
| 208 | // Set socket back to normal mode (blocking) |
| 209 | fcntl(socket_, F_SETFL, flags); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 212 | void TSocket::open() { |
| 213 | if (isOpen()) { |
| 214 | throw TTransportException(TTransportException::ALREADY_OPEN); |
| 215 | } |
| 216 | |
| 217 | // Validate port number |
| 218 | if (port_ < 0 || port_ > 65536) { |
| 219 | throw TTransportException(TTransportException::NOT_OPEN, "Specified port is invalid"); |
| 220 | } |
| 221 | |
| 222 | struct addrinfo hints, *res, *res0; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 223 | res = NULL; |
| 224 | res0 = NULL; |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 225 | int error; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 226 | char port[sizeof("65536")]; |
David Reiss | c88eb8c | 2008-06-11 01:18:54 +0000 | [diff] [blame] | 227 | std::memset(&hints, 0, sizeof(hints)); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 228 | hints.ai_family = PF_UNSPEC; |
| 229 | hints.ai_socktype = SOCK_STREAM; |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 230 | hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 231 | sprintf(port, "%d", port_); |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 232 | |
Mark Slee | c37b4c5 | 2007-12-05 23:03:37 +0000 | [diff] [blame] | 233 | error = getaddrinfo(host_.c_str(), port, &hints, &res0); |
| 234 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 235 | if (error) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 236 | string errStr = "TSocket::open() getaddrinfo() " + getSocketInfo() + string(gai_strerror(error)); |
| 237 | GlobalOutput(errStr.c_str()); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 238 | close(); |
| 239 | throw TTransportException(TTransportException::NOT_OPEN, "Could not resolve host for client socket."); |
| 240 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 241 | |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 242 | // Cycle through all the returned addresses until one |
| 243 | // connects or push the exception up. |
| 244 | for (res = res0; res; res = res->ai_next) { |
| 245 | try { |
| 246 | openConnection(res); |
| 247 | break; |
| 248 | } catch (TTransportException& ttx) { |
| 249 | if (res->ai_next) { |
| 250 | close(); |
| 251 | } else { |
| 252 | close(); |
Mark Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame] | 253 | freeaddrinfo(res0); // cleanup on failure |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 254 | throw; |
| 255 | } |
| 256 | } |
| 257 | } |
Mark Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame] | 258 | |
| 259 | // Free address structure memory |
| 260 | freeaddrinfo(res0); |
Mark Slee | 6d56eb9 | 2007-07-06 22:28:15 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 263 | void TSocket::close() { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 264 | if (socket_ >= 0) { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 265 | shutdown(socket_, SHUT_RDWR); |
| 266 | ::close(socket_); |
| 267 | } |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 268 | socket_ = -1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 271 | uint32_t TSocket::read(uint8_t* buf, uint32_t len) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 272 | if (socket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 273 | throw TTransportException(TTransportException::NOT_OPEN, "Called read on non-open socket"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 274 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 275 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 276 | int32_t retries = 0; |
| 277 | |
| 278 | // EAGAIN can be signalled both when a timeout has occurred and when |
| 279 | // the system is out of resources (an awesome undocumented feature). |
| 280 | // The following is an approximation of the time interval under which |
| 281 | // EAGAIN is taken to indicate an out of resources error. |
| 282 | uint32_t eagainThresholdMicros = 0; |
| 283 | if (recvTimeout_) { |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 284 | // 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] | 285 | // the threshold will ensure that the read timeout is not exceeded even in the |
| 286 | // case of resource errors |
| 287 | eagainThresholdMicros = (recvTimeout_*1000)/ ((maxRecvRetries_>0) ? maxRecvRetries_ : 2); |
| 288 | } |
| 289 | |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 290 | try_again: |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 291 | // Read from the socket |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 292 | struct timeval begin; |
| 293 | gettimeofday(&begin, NULL); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 294 | int got = recv(socket_, buf, len, 0); |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 295 | struct timeval end; |
| 296 | gettimeofday(&end, NULL); |
| 297 | uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000) |
| 298 | + (((uint64_t)(end.tv_usec - begin.tv_usec)))); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 299 | ++g_socket_syscalls; |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 300 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 301 | // Check for error on read |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 302 | if (got < 0) { |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 303 | if (errno == EAGAIN) { |
| 304 | // check if this is the lack of resources or timeout case |
| 305 | if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) { |
| 306 | if (retries++ < maxRecvRetries_) { |
| 307 | usleep(50); |
| 308 | goto try_again; |
| 309 | } else { |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 310 | throw TTransportException(TTransportException::TIMED_OUT, |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 311 | "EAGAIN (unavailable resources)"); |
| 312 | } |
| 313 | } else { |
| 314 | // infer that timeout has been hit |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 315 | throw TTransportException(TTransportException::TIMED_OUT, |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 316 | "EAGAIN (timed out)"); |
| 317 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 318 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 319 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 320 | // If interrupted, try again |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 321 | if (errno == EINTR && retries++ < maxRecvRetries_) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 322 | goto try_again; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 323 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 324 | |
Mark Slee | c425780 | 2007-01-24 23:14:30 +0000 | [diff] [blame] | 325 | // Now it's not a try again case, but a real probblez |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 326 | string errStr = "TSocket::read() recv() " + getSocketInfo() + TOutput::strerror_s(errno); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 327 | GlobalOutput(errStr.c_str()); |
Mark Slee | c425780 | 2007-01-24 23:14:30 +0000 | [diff] [blame] | 328 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 329 | // If we disconnect with no linger time |
| 330 | if (errno == ECONNRESET) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 331 | throw TTransportException(TTransportException::NOT_OPEN, "ECONNRESET"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 332 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 333 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 334 | // This ish isn't open |
| 335 | if (errno == ENOTCONN) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 336 | throw TTransportException(TTransportException::NOT_OPEN, "ENOTCONN"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 337 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 338 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 339 | // Timed out! |
| 340 | if (errno == ETIMEDOUT) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 341 | throw TTransportException(TTransportException::TIMED_OUT, "ETIMEDOUT"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 342 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 343 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 344 | // Some other error, whatevz |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 345 | throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 346 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 347 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 348 | // The remote host has closed the socket |
| 349 | if (got == 0) { |
| 350 | close(); |
| 351 | return 0; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 352 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 353 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 354 | // Pack data into string |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 355 | return got; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 358 | void TSocket::write(const uint8_t* buf, uint32_t len) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 359 | if (socket_ < 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 360 | throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket"); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 363 | uint32_t sent = 0; |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 364 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 365 | while (sent < len) { |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 366 | |
| 367 | int flags = 0; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 368 | #ifdef MSG_NOSIGNAL |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 369 | // Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we |
| 370 | // 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] | 371 | flags |= MSG_NOSIGNAL; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 372 | #endif // ifdef MSG_NOSIGNAL |
Marc Slemko | 9d4a3e2 | 2006-07-21 19:53:48 +0000 | [diff] [blame] | 373 | |
| 374 | int b = send(socket_, buf + sent, len - sent, flags); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 375 | ++g_socket_syscalls; |
| 376 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 377 | // Fail on a send error |
| 378 | if (b < 0) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 379 | int errno_copy = errno; |
| 380 | string errStr = "TSocket::write() send() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
| 381 | GlobalOutput(errStr.c_str()); |
| 382 | |
David Reiss | bc3dddb | 2007-08-22 23:20:24 +0000 | [diff] [blame] | 383 | if (errno == EPIPE || errno == ECONNRESET || errno == ENOTCONN) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 384 | close(); |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 385 | throw TTransportException(TTransportException::NOT_OPEN, "write() send()", errno_copy); |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 386 | } |
| 387 | |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 388 | throw TTransportException(TTransportException::UNKNOWN, "write() send()", errno_copy); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 389 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 390 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 391 | // Fail on blocked send |
| 392 | if (b == 0) { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 393 | throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0."); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 394 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 395 | sent += b; |
| 396 | } |
| 397 | } |
| 398 | |
dweatherford | 14b0ed6 | 2007-10-19 01:03:32 +0000 | [diff] [blame] | 399 | std::string TSocket::getHost() { |
| 400 | return host_; |
| 401 | } |
| 402 | |
| 403 | int TSocket::getPort() { |
| 404 | return port_; |
| 405 | } |
| 406 | |
Aditya Agarwal | ebc99e0 | 2007-01-15 23:14:58 +0000 | [diff] [blame] | 407 | void TSocket::setHost(string host) { |
| 408 | host_ = host; |
| 409 | } |
| 410 | |
| 411 | void TSocket::setPort(int port) { |
| 412 | port_ = port; |
| 413 | } |
| 414 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 415 | void TSocket::setLinger(bool on, int linger) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 416 | lingerOn_ = on; |
| 417 | lingerVal_ = linger; |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 418 | if (socket_ < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 419 | return; |
| 420 | } |
| 421 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 422 | struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_}; |
| 423 | int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l)); |
| 424 | if (ret == -1) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 425 | int errno_copy = errno; |
| 426 | string errStr = "TSocket::setLinger() setsockopt() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 427 | GlobalOutput(errStr.c_str()); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 428 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 431 | void TSocket::setNoDelay(bool noDelay) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 432 | noDelay_ = noDelay; |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 433 | if (socket_ < 0) { |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 434 | return; |
| 435 | } |
| 436 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 437 | // Set socket to NODELAY |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 438 | int v = noDelay_ ? 1 : 0; |
| 439 | int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v)); |
| 440 | if (ret == -1) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 441 | int errno_copy = errno; |
| 442 | string errStr = "TSocket::setNoDelay() setsockopt() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 443 | GlobalOutput(errStr.c_str()); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 444 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 445 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 446 | |
| 447 | void TSocket::setConnTimeout(int ms) { |
| 448 | connTimeout_ = ms; |
| 449 | } |
| 450 | |
| 451 | void TSocket::setRecvTimeout(int ms) { |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 452 | if (ms < 0) { |
| 453 | char errBuf[512]; |
| 454 | sprintf(errBuf, "TSocket::setRecvTimeout with negative input: %d\n", ms); |
| 455 | GlobalOutput(errBuf); |
| 456 | return; |
| 457 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 458 | recvTimeout_ = ms; |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 459 | |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 460 | if (socket_ < 0) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 461 | return; |
| 462 | } |
| 463 | |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 464 | recvTimeval_.tv_sec = (int)(recvTimeout_/1000); |
| 465 | recvTimeval_.tv_usec = (int)((recvTimeout_%1000)*1000); |
| 466 | |
David Reiss | 22b1886 | 2008-04-08 06:25:45 +0000 | [diff] [blame] | 467 | // Copy because poll may modify |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 468 | struct timeval r = recvTimeval_; |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 469 | int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r)); |
| 470 | if (ret == -1) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 471 | int errno_copy = errno; |
| 472 | string errStr = "TSocket::setRecvTimeout() setsockopt() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 473 | GlobalOutput(errStr.c_str()); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
| 477 | void TSocket::setSendTimeout(int ms) { |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 478 | if (ms < 0) { |
| 479 | char errBuf[512]; |
| 480 | sprintf(errBuf, "TSocket::setSendTimeout with negative input: %d\n", ms); |
| 481 | GlobalOutput(errBuf); |
| 482 | return; |
| 483 | } |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 484 | sendTimeout_ = ms; |
Aditya Agarwal | c31769c | 2007-12-11 22:23:51 +0000 | [diff] [blame] | 485 | |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 486 | if (socket_ < 0) { |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 487 | return; |
| 488 | } |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 489 | |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 490 | struct timeval s = {(int)(sendTimeout_/1000), |
| 491 | (int)((sendTimeout_%1000)*1000)}; |
| 492 | int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s)); |
| 493 | if (ret == -1) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 494 | int errno_copy = errno; |
| 495 | string errStr = "TSocket::setSendTimeout() setsockopt() " + getSocketInfo() + TOutput::strerror_s(errno_copy); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 496 | GlobalOutput(errStr.c_str()); |
Mark Slee | 2905078 | 2006-09-29 00:12:30 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
Aditya Agarwal | e04475b | 2007-05-23 02:14:58 +0000 | [diff] [blame] | 500 | void TSocket::setMaxRecvRetries(int maxRecvRetries) { |
| 501 | maxRecvRetries_ = maxRecvRetries; |
| 502 | } |
| 503 | |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 504 | string TSocket::getSocketInfo() { |
| 505 | std::ostringstream oss; |
| 506 | oss << "<Host: " << host_ << " Port: " << port_ << ">"; |
| 507 | return oss.str(); |
| 508 | } |
| 509 | |
Mark Slee | b455292 | 2007-11-28 00:12:11 +0000 | [diff] [blame] | 510 | std::string TSocket::getPeerHost() { |
| 511 | if (peerHost_.empty()) { |
| 512 | struct sockaddr_storage addr; |
| 513 | socklen_t addrLen = sizeof(addr); |
| 514 | |
| 515 | if (socket_ < 0) { |
| 516 | return host_; |
| 517 | } |
| 518 | |
| 519 | int rv = getpeername(socket_, (sockaddr*) &addr, &addrLen); |
| 520 | |
| 521 | if (rv != 0) { |
| 522 | return peerHost_; |
| 523 | } |
| 524 | |
| 525 | char clienthost[NI_MAXHOST]; |
| 526 | char clientservice[NI_MAXSERV]; |
| 527 | |
| 528 | getnameinfo((sockaddr*) &addr, addrLen, |
| 529 | clienthost, sizeof(clienthost), |
| 530 | clientservice, sizeof(clientservice), 0); |
| 531 | |
| 532 | peerHost_ = clienthost; |
| 533 | } |
| 534 | return peerHost_; |
| 535 | } |
| 536 | |
| 537 | std::string TSocket::getPeerAddress() { |
| 538 | if (peerAddress_.empty()) { |
| 539 | struct sockaddr_storage addr; |
| 540 | socklen_t addrLen = sizeof(addr); |
| 541 | |
| 542 | if (socket_ < 0) { |
| 543 | return peerAddress_; |
| 544 | } |
| 545 | |
| 546 | int rv = getpeername(socket_, (sockaddr*) &addr, &addrLen); |
| 547 | |
| 548 | if (rv != 0) { |
| 549 | return peerAddress_; |
| 550 | } |
| 551 | |
| 552 | char clienthost[NI_MAXHOST]; |
| 553 | char clientservice[NI_MAXSERV]; |
| 554 | |
| 555 | getnameinfo((sockaddr*) &addr, addrLen, |
| 556 | clienthost, sizeof(clienthost), |
| 557 | clientservice, sizeof(clientservice), |
| 558 | NI_NUMERICHOST|NI_NUMERICSERV); |
| 559 | |
| 560 | peerAddress_ = clienthost; |
| 561 | peerPort_ = std::atoi(clientservice); |
| 562 | } |
| 563 | return peerAddress_; |
| 564 | } |
| 565 | |
| 566 | int TSocket::getPeerPort() { |
| 567 | getPeerAddress(); |
| 568 | return peerPort_; |
| 569 | } |
| 570 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 571 | }}} // facebook::thrift::transport |