Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 1 | //===-- Socket.cpp ----------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/Host/Socket.h" |
| 11 | |
| 12 | #include "lldb/Core/Log.h" |
| 13 | #include "lldb/Core/RegularExpression.h" |
| 14 | #include "lldb/Host/Config.h" |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 15 | #include "lldb/Host/FileSystem.h" |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 16 | #include "lldb/Host/Host.h" |
| 17 | #include "lldb/Host/SocketAddress.h" |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 18 | #include "lldb/Host/StringConvert.h" |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 19 | #include "lldb/Host/TimeValue.h" |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 20 | |
Shawn Best | 8da0bf3 | 2014-11-08 01:41:49 +0000 | [diff] [blame] | 21 | #ifdef __ANDROID_NDK__ |
| 22 | #include <linux/tcp.h> |
| 23 | #include <bits/error_constants.h> |
| 24 | #include <asm-generic/errno-base.h> |
| 25 | #include <errno.h> |
| 26 | #include <arpa/inet.h> |
Chaoren Lin | e271658 | 2015-07-15 19:22:12 +0000 | [diff] [blame] | 27 | #if defined(ANDROID_ARM_BUILD_STATIC) |
| 28 | #include <unistd.h> |
| 29 | #include <sys/syscall.h> |
| 30 | #endif // ANDROID_ARM_BUILD_STATIC |
| 31 | #endif // __ANDROID_NDK__ |
Shawn Best | 8da0bf3 | 2014-11-08 01:41:49 +0000 | [diff] [blame] | 32 | |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 33 | #ifndef LLDB_DISABLE_POSIX |
| 34 | #include <arpa/inet.h> |
| 35 | #include <netdb.h> |
| 36 | #include <netinet/in.h> |
| 37 | #include <netinet/tcp.h> |
| 38 | #include <sys/socket.h> |
| 39 | #include <sys/un.h> |
| 40 | #endif |
| 41 | |
| 42 | using namespace lldb; |
| 43 | using namespace lldb_private; |
| 44 | |
| 45 | #if defined(_WIN32) |
| 46 | typedef const char * set_socket_option_arg_type; |
| 47 | typedef char * get_socket_option_arg_type; |
| 48 | const NativeSocket Socket::kInvalidSocketValue = INVALID_SOCKET; |
| 49 | #else // #if defined(_WIN32) |
| 50 | typedef const void * set_socket_option_arg_type; |
| 51 | typedef void * get_socket_option_arg_type; |
| 52 | const NativeSocket Socket::kInvalidSocketValue = -1; |
| 53 | #endif // #if defined(_WIN32) |
| 54 | |
Todd Fiala | cacde7d | 2014-09-27 16:54:22 +0000 | [diff] [blame] | 55 | #ifdef __ANDROID__ |
| 56 | // Android does not have SUN_LEN |
| 57 | #ifndef SUN_LEN |
| 58 | #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) + strlen((ptr)->sun_path)) |
| 59 | #endif |
| 60 | #endif // #ifdef __ANDROID__ |
| 61 | |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 62 | namespace { |
| 63 | |
| 64 | NativeSocket CreateSocket(const int domain, const int type, const int protocol, bool child_processes_inherit) |
| 65 | { |
| 66 | auto socketType = type; |
| 67 | #ifdef SOCK_CLOEXEC |
| 68 | if (!child_processes_inherit) { |
| 69 | socketType |= SOCK_CLOEXEC; |
| 70 | } |
| 71 | #endif |
| 72 | return ::socket (domain, socketType, protocol); |
| 73 | } |
| 74 | |
| 75 | NativeSocket Accept(NativeSocket sockfd, struct sockaddr *addr, socklen_t *addrlen, bool child_processes_inherit) |
| 76 | { |
Chaoren Lin | e271658 | 2015-07-15 19:22:12 +0000 | [diff] [blame] | 77 | #if defined(ANDROID_ARM_BUILD_STATIC) |
| 78 | // Temporary workaround for statically linking Android lldb-server with the |
| 79 | // latest API. |
| 80 | int fd = syscall(__NR_accept, sockfd, addr, addrlen); |
| 81 | if (fd >= 0 && !child_processes_inherit) |
| 82 | { |
| 83 | int flags = ::fcntl(fd, F_GETFD); |
| 84 | if (flags == -1) |
| 85 | return -1; |
| 86 | if (::fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) |
| 87 | return -1; |
| 88 | } |
| 89 | return fd; |
| 90 | #elif defined(SOCK_CLOEXEC) |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 91 | int flags = 0; |
| 92 | if (!child_processes_inherit) { |
| 93 | flags |= SOCK_CLOEXEC; |
| 94 | } |
| 95 | return ::accept4 (sockfd, addr, addrlen, flags); |
| 96 | #else |
| 97 | return ::accept (sockfd, addr, addrlen); |
| 98 | #endif |
| 99 | } |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 100 | |
| 101 | void SetLastError(Error &error) |
| 102 | { |
| 103 | #if defined(_WIN32) |
| 104 | error.SetError(::WSAGetLastError(), lldb::eErrorTypeWin32); |
| 105 | #else |
| 106 | error.SetErrorToErrno(); |
| 107 | #endif |
| 108 | } |
| 109 | |
| 110 | bool IsInterrupted() |
| 111 | { |
| 112 | #if defined(_WIN32) |
| 113 | return ::WSAGetLastError() == WSAEINTR; |
| 114 | #else |
| 115 | return errno == EINTR; |
| 116 | #endif |
| 117 | } |
| 118 | |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 121 | Socket::Socket(NativeSocket socket, SocketProtocol protocol, bool should_close) |
| 122 | : IOObject(eFDTypeSocket, should_close) |
| 123 | , m_protocol(protocol) |
| 124 | , m_socket(socket) |
| 125 | { |
| 126 | |
| 127 | } |
| 128 | |
| 129 | Socket::~Socket() |
| 130 | { |
| 131 | Close(); |
| 132 | } |
| 133 | |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 134 | Error Socket::TcpConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 135 | { |
| 136 | // Store the result in a unique_ptr in case we error out, the memory will get correctly freed. |
| 137 | std::unique_ptr<Socket> final_socket; |
| 138 | NativeSocket sock = kInvalidSocketValue; |
| 139 | Error error; |
| 140 | |
| 141 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_HOST)); |
| 142 | if (log) |
| 143 | log->Printf ("Socket::TcpConnect (host/port = %s)", host_and_port.data()); |
| 144 | |
| 145 | std::string host_str; |
| 146 | std::string port_str; |
| 147 | int32_t port = INT32_MIN; |
| 148 | if (!DecodeHostAndPort (host_and_port, host_str, port_str, port, &error)) |
| 149 | return error; |
| 150 | |
| 151 | // Create the socket |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 152 | sock = CreateSocket (AF_INET, SOCK_STREAM, IPPROTO_TCP, child_processes_inherit); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 153 | if (sock == kInvalidSocketValue) |
| 154 | { |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 155 | SetLastError (error); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 156 | return error; |
| 157 | } |
| 158 | |
| 159 | // Since they both refer to the same socket descriptor, arbitrarily choose the send socket to |
| 160 | // be the owner. |
| 161 | final_socket.reset(new Socket(sock, ProtocolTcp, true)); |
| 162 | |
| 163 | // Enable local address reuse |
| 164 | final_socket->SetOption(SOL_SOCKET, SO_REUSEADDR, 1); |
| 165 | |
| 166 | struct sockaddr_in sa; |
| 167 | ::memset (&sa, 0, sizeof (sa)); |
| 168 | sa.sin_family = AF_INET; |
| 169 | sa.sin_port = htons (port); |
| 170 | |
| 171 | int inet_pton_result = ::inet_pton (AF_INET, host_str.c_str(), &sa.sin_addr); |
| 172 | |
| 173 | if (inet_pton_result <= 0) |
| 174 | { |
| 175 | struct hostent *host_entry = gethostbyname (host_str.c_str()); |
| 176 | if (host_entry) |
| 177 | host_str = ::inet_ntoa (*(struct in_addr *)*host_entry->h_addr_list); |
| 178 | inet_pton_result = ::inet_pton (AF_INET, host_str.c_str(), &sa.sin_addr); |
| 179 | if (inet_pton_result <= 0) |
| 180 | { |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 181 | if (inet_pton_result == -1) |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 182 | SetLastError(error); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 183 | else |
| 184 | error.SetErrorStringWithFormat("invalid host string: '%s'", host_str.c_str()); |
| 185 | |
| 186 | return error; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if (-1 == ::connect (sock, (const struct sockaddr *)&sa, sizeof(sa))) |
| 191 | { |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 192 | SetLastError (error); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 193 | return error; |
| 194 | } |
| 195 | |
| 196 | // Keep our TCP packets coming without any delays. |
| 197 | final_socket->SetOption(IPPROTO_TCP, TCP_NODELAY, 1); |
| 198 | error.Clear(); |
| 199 | socket = final_socket.release(); |
| 200 | return error; |
| 201 | } |
| 202 | |
Vince Harron | 33aea90 | 2015-03-31 00:27:10 +0000 | [diff] [blame] | 203 | Error Socket::TcpListen( |
| 204 | llvm::StringRef host_and_port, |
| 205 | bool child_processes_inherit, |
| 206 | Socket *&socket, |
| 207 | Predicate<uint16_t>* predicate, |
| 208 | int backlog) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 209 | { |
| 210 | std::unique_ptr<Socket> listen_socket; |
| 211 | NativeSocket listen_sock = kInvalidSocketValue; |
| 212 | Error error; |
| 213 | |
| 214 | const sa_family_t family = AF_INET; |
| 215 | const int socktype = SOCK_STREAM; |
| 216 | const int protocol = IPPROTO_TCP; |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 217 | listen_sock = ::CreateSocket (family, socktype, protocol, child_processes_inherit); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 218 | if (listen_sock == kInvalidSocketValue) |
| 219 | { |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 220 | SetLastError (error); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 221 | return error; |
| 222 | } |
| 223 | |
| 224 | listen_socket.reset(new Socket(listen_sock, ProtocolTcp, true)); |
| 225 | |
| 226 | // enable local address reuse |
| 227 | listen_socket->SetOption(SOL_SOCKET, SO_REUSEADDR, 1); |
| 228 | |
| 229 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); |
| 230 | if (log) |
Vince Harron | eb303ee | 2015-01-17 02:20:29 +0000 | [diff] [blame] | 231 | log->Printf ("Socket::TcpListen (%s)", host_and_port.data()); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 232 | |
| 233 | std::string host_str; |
| 234 | std::string port_str; |
| 235 | int32_t port = INT32_MIN; |
| 236 | if (!DecodeHostAndPort (host_and_port, host_str, port_str, port, &error)) |
| 237 | return error; |
| 238 | |
| 239 | SocketAddress anyaddr; |
| 240 | if (anyaddr.SetToAnyAddress (family, port)) |
| 241 | { |
| 242 | int err = ::bind (listen_sock, anyaddr, anyaddr.GetLength()); |
| 243 | if (err == -1) |
| 244 | { |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 245 | SetLastError (error); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 246 | return error; |
| 247 | } |
| 248 | |
Vince Harron | 33aea90 | 2015-03-31 00:27:10 +0000 | [diff] [blame] | 249 | err = ::listen (listen_sock, backlog); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 250 | if (err == -1) |
| 251 | { |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 252 | SetLastError (error); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 253 | return error; |
| 254 | } |
| 255 | |
| 256 | // We were asked to listen on port zero which means we |
| 257 | // must now read the actual port that was given to us |
| 258 | // as port zero is a special code for "find an open port |
| 259 | // for me". |
| 260 | if (port == 0) |
Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 261 | port = listen_socket->GetLocalPortNumber(); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 262 | |
| 263 | // Set the port predicate since when doing a listen://<host>:<port> |
| 264 | // it often needs to accept the incoming connection which is a blocking |
| 265 | // system call. Allowing access to the bound port using a predicate allows |
| 266 | // us to wait for the port predicate to be set to a non-zero value from |
| 267 | // another thread in an efficient manor. |
| 268 | if (predicate) |
Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 269 | predicate->SetValue (port, eBroadcastAlways); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 270 | |
| 271 | socket = listen_socket.release(); |
| 272 | } |
| 273 | |
| 274 | return error; |
| 275 | } |
| 276 | |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 277 | Error Socket::BlockingAccept(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 278 | { |
| 279 | Error error; |
| 280 | std::string host_str; |
| 281 | std::string port_str; |
| 282 | int32_t port; |
| 283 | if (!DecodeHostAndPort(host_and_port, host_str, port_str, port, &error)) |
| 284 | return error; |
| 285 | |
| 286 | const sa_family_t family = AF_INET; |
| 287 | const int socktype = SOCK_STREAM; |
| 288 | const int protocol = IPPROTO_TCP; |
| 289 | SocketAddress listen_addr; |
| 290 | if (host_str.empty()) |
| 291 | listen_addr.SetToLocalhost(family, port); |
| 292 | else if (host_str.compare("*") == 0) |
| 293 | listen_addr.SetToAnyAddress(family, port); |
| 294 | else |
| 295 | { |
| 296 | if (!listen_addr.getaddrinfo(host_str.c_str(), port_str.c_str(), family, socktype, protocol)) |
| 297 | { |
| 298 | error.SetErrorStringWithFormat("unable to resolve hostname '%s'", host_str.c_str()); |
| 299 | return error; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | bool accept_connection = false; |
| 304 | std::unique_ptr<Socket> accepted_socket; |
| 305 | |
| 306 | // Loop until we are happy with our connection |
| 307 | while (!accept_connection) |
| 308 | { |
| 309 | struct sockaddr_in accept_addr; |
| 310 | ::memset (&accept_addr, 0, sizeof accept_addr); |
| 311 | #if !(defined (__linux__) || defined(_WIN32)) |
| 312 | accept_addr.sin_len = sizeof accept_addr; |
| 313 | #endif |
| 314 | socklen_t accept_addr_len = sizeof accept_addr; |
| 315 | |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 316 | int sock = Accept (this->GetNativeSocket(), |
| 317 | (struct sockaddr *)&accept_addr, |
| 318 | &accept_addr_len, |
| 319 | child_processes_inherit); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 320 | |
| 321 | if (sock == kInvalidSocketValue) |
| 322 | { |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 323 | SetLastError (error); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 324 | break; |
| 325 | } |
| 326 | |
| 327 | bool is_same_addr = true; |
| 328 | #if !(defined(__linux__) || (defined(_WIN32))) |
| 329 | is_same_addr = (accept_addr_len == listen_addr.sockaddr_in().sin_len); |
| 330 | #endif |
| 331 | if (is_same_addr) |
| 332 | is_same_addr = (accept_addr.sin_addr.s_addr == listen_addr.sockaddr_in().sin_addr.s_addr); |
| 333 | |
| 334 | if (is_same_addr || (listen_addr.sockaddr_in().sin_addr.s_addr == INADDR_ANY)) |
| 335 | { |
| 336 | accept_connection = true; |
| 337 | // Since both sockets have the same descriptor, arbitrarily choose the send |
| 338 | // socket to be the owner. |
| 339 | accepted_socket.reset(new Socket(sock, ProtocolTcp, true)); |
| 340 | } |
| 341 | else |
| 342 | { |
| 343 | const uint8_t *accept_ip = (const uint8_t *)&accept_addr.sin_addr.s_addr; |
| 344 | const uint8_t *listen_ip = (const uint8_t *)&listen_addr.sockaddr_in().sin_addr.s_addr; |
| 345 | ::fprintf (stderr, "error: rejecting incoming connection from %u.%u.%u.%u (expecting %u.%u.%u.%u)\n", |
| 346 | accept_ip[0], accept_ip[1], accept_ip[2], accept_ip[3], |
| 347 | listen_ip[0], listen_ip[1], listen_ip[2], listen_ip[3]); |
| 348 | accepted_socket.reset(); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | if (!accepted_socket) |
| 353 | return error; |
| 354 | |
| 355 | // Keep our TCP packets coming without any delays. |
| 356 | accepted_socket->SetOption (IPPROTO_TCP, TCP_NODELAY, 1); |
| 357 | error.Clear(); |
| 358 | socket = accepted_socket.release(); |
| 359 | return error; |
| 360 | |
| 361 | } |
| 362 | |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 363 | Error Socket::UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&send_socket, Socket *&recv_socket) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 364 | { |
| 365 | std::unique_ptr<Socket> final_send_socket; |
| 366 | std::unique_ptr<Socket> final_recv_socket; |
| 367 | NativeSocket final_send_fd = kInvalidSocketValue; |
| 368 | NativeSocket final_recv_fd = kInvalidSocketValue; |
| 369 | |
| 370 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); |
| 371 | if (log) |
| 372 | log->Printf ("Socket::UdpConnect (host/port = %s)", host_and_port.data()); |
| 373 | |
| 374 | Error error; |
| 375 | std::string host_str; |
| 376 | std::string port_str; |
| 377 | int32_t port = INT32_MIN; |
| 378 | if (!DecodeHostAndPort (host_and_port, host_str, port_str, port, &error)) |
| 379 | return error; |
| 380 | |
| 381 | // Setup the receiving end of the UDP connection on this localhost |
| 382 | // on port zero. After we bind to port zero we can read the port. |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 383 | final_recv_fd = ::CreateSocket (AF_INET, SOCK_DGRAM, 0, child_processes_inherit); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 384 | if (final_recv_fd == kInvalidSocketValue) |
| 385 | { |
| 386 | // Socket creation failed... |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 387 | SetLastError (error); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 388 | } |
| 389 | else |
| 390 | { |
| 391 | final_recv_socket.reset(new Socket(final_recv_fd, ProtocolUdp, true)); |
| 392 | |
| 393 | // Socket was created, now lets bind to the requested port |
| 394 | SocketAddress addr; |
| 395 | addr.SetToAnyAddress (AF_INET, 0); |
| 396 | |
| 397 | if (::bind (final_recv_fd, addr, addr.GetLength()) == -1) |
| 398 | { |
| 399 | // Bind failed... |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 400 | SetLastError (error); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
| 404 | assert(error.Fail() == !(final_recv_socket && final_recv_socket->IsValid())); |
| 405 | if (error.Fail()) |
| 406 | return error; |
| 407 | |
| 408 | // At this point we have setup the receive port, now we need to |
| 409 | // setup the UDP send socket |
| 410 | |
| 411 | struct addrinfo hints; |
| 412 | struct addrinfo *service_info_list = NULL; |
| 413 | |
| 414 | ::memset (&hints, 0, sizeof(hints)); |
| 415 | hints.ai_family = AF_INET; |
| 416 | hints.ai_socktype = SOCK_DGRAM; |
| 417 | int err = ::getaddrinfo (host_str.c_str(), port_str.c_str(), &hints, &service_info_list); |
| 418 | if (err != 0) |
| 419 | { |
| 420 | error.SetErrorStringWithFormat("getaddrinfo(%s, %s, &hints, &info) returned error %i (%s)", |
| 421 | host_str.c_str(), |
| 422 | port_str.c_str(), |
| 423 | err, |
| 424 | gai_strerror(err)); |
| 425 | return error; |
| 426 | } |
| 427 | |
| 428 | for (struct addrinfo *service_info_ptr = service_info_list; |
| 429 | service_info_ptr != NULL; |
| 430 | service_info_ptr = service_info_ptr->ai_next) |
| 431 | { |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 432 | final_send_fd = ::CreateSocket (service_info_ptr->ai_family, |
| 433 | service_info_ptr->ai_socktype, |
| 434 | service_info_ptr->ai_protocol, |
| 435 | child_processes_inherit); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 436 | |
| 437 | if (final_send_fd != kInvalidSocketValue) |
| 438 | { |
| 439 | final_send_socket.reset(new Socket(final_send_fd, ProtocolUdp, true)); |
| 440 | final_send_socket->m_udp_send_sockaddr = service_info_ptr; |
| 441 | break; |
| 442 | } |
| 443 | else |
| 444 | continue; |
| 445 | } |
| 446 | |
| 447 | :: freeaddrinfo (service_info_list); |
| 448 | |
| 449 | if (final_send_fd == kInvalidSocketValue) |
| 450 | { |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 451 | SetLastError (error); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 452 | return error; |
| 453 | } |
| 454 | |
| 455 | send_socket = final_send_socket.release(); |
| 456 | recv_socket = final_recv_socket.release(); |
| 457 | error.Clear(); |
| 458 | return error; |
| 459 | } |
| 460 | |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 461 | Error Socket::UnixDomainConnect(llvm::StringRef name, bool child_processes_inherit, Socket *&socket) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 462 | { |
| 463 | Error error; |
| 464 | #ifndef LLDB_DISABLE_POSIX |
| 465 | std::unique_ptr<Socket> final_socket; |
| 466 | |
| 467 | // Open the socket that was passed in as an option |
| 468 | struct sockaddr_un saddr_un; |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 469 | int fd = ::CreateSocket (AF_UNIX, SOCK_STREAM, 0, child_processes_inherit); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 470 | if (fd == kInvalidSocketValue) |
| 471 | { |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 472 | SetLastError (error); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 473 | return error; |
| 474 | } |
| 475 | |
| 476 | final_socket.reset(new Socket(fd, ProtocolUnixDomain, true)); |
| 477 | |
| 478 | saddr_un.sun_family = AF_UNIX; |
| 479 | ::strncpy(saddr_un.sun_path, name.data(), sizeof(saddr_un.sun_path) - 1); |
| 480 | saddr_un.sun_path[sizeof(saddr_un.sun_path) - 1] = '\0'; |
| 481 | #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) |
| 482 | saddr_un.sun_len = SUN_LEN (&saddr_un); |
| 483 | #endif |
| 484 | |
| 485 | if (::connect (fd, (struct sockaddr *)&saddr_un, SUN_LEN (&saddr_un)) < 0) |
| 486 | { |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 487 | SetLastError (error); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 488 | return error; |
| 489 | } |
| 490 | |
| 491 | socket = final_socket.release(); |
| 492 | #else |
| 493 | error.SetErrorString("Unix domain sockets are not supported on this platform."); |
| 494 | #endif |
| 495 | return error; |
| 496 | } |
| 497 | |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 498 | Error Socket::UnixDomainAccept(llvm::StringRef name, bool child_processes_inherit, Socket *&socket) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 499 | { |
| 500 | Error error; |
| 501 | #ifndef LLDB_DISABLE_POSIX |
| 502 | struct sockaddr_un saddr_un; |
| 503 | std::unique_ptr<Socket> listen_socket; |
| 504 | std::unique_ptr<Socket> final_socket; |
| 505 | NativeSocket listen_fd = kInvalidSocketValue; |
| 506 | NativeSocket socket_fd = kInvalidSocketValue; |
| 507 | |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 508 | listen_fd = ::CreateSocket (AF_UNIX, SOCK_STREAM, 0, child_processes_inherit); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 509 | if (listen_fd == kInvalidSocketValue) |
| 510 | { |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 511 | SetLastError (error); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 512 | return error; |
| 513 | } |
| 514 | |
| 515 | listen_socket.reset(new Socket(listen_fd, ProtocolUnixDomain, true)); |
| 516 | |
| 517 | saddr_un.sun_family = AF_UNIX; |
| 518 | ::strncpy(saddr_un.sun_path, name.data(), sizeof(saddr_un.sun_path) - 1); |
| 519 | saddr_un.sun_path[sizeof(saddr_un.sun_path) - 1] = '\0'; |
| 520 | #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) |
| 521 | saddr_un.sun_len = SUN_LEN (&saddr_un); |
| 522 | #endif |
| 523 | |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 524 | FileSystem::Unlink(FileSpec{name, true}); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 525 | bool success = false; |
| 526 | if (::bind (listen_fd, (struct sockaddr *)&saddr_un, SUN_LEN (&saddr_un)) == 0) |
| 527 | { |
| 528 | if (::listen (listen_fd, 5) == 0) |
| 529 | { |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 530 | socket_fd = Accept (listen_fd, NULL, 0, child_processes_inherit); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 531 | if (socket_fd > 0) |
| 532 | { |
| 533 | final_socket.reset(new Socket(socket_fd, ProtocolUnixDomain, true)); |
| 534 | success = true; |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | if (!success) |
| 540 | { |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 541 | SetLastError (error); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 542 | return error; |
| 543 | } |
| 544 | // We are done with the listen port |
| 545 | listen_socket.reset(); |
| 546 | |
| 547 | socket = final_socket.release(); |
| 548 | #else |
| 549 | error.SetErrorString("Unix domain sockets are not supported on this platform."); |
| 550 | #endif |
| 551 | return error; |
| 552 | } |
| 553 | |
| 554 | bool |
| 555 | Socket::DecodeHostAndPort(llvm::StringRef host_and_port, |
| 556 | std::string &host_str, |
| 557 | std::string &port_str, |
| 558 | int32_t& port, |
| 559 | Error *error_ptr) |
| 560 | { |
| 561 | static RegularExpression g_regex ("([^:]+):([0-9]+)"); |
| 562 | RegularExpression::Match regex_match(2); |
| 563 | if (g_regex.Execute (host_and_port.data(), ®ex_match)) |
| 564 | { |
| 565 | if (regex_match.GetMatchAtIndex (host_and_port.data(), 1, host_str) && |
| 566 | regex_match.GetMatchAtIndex (host_and_port.data(), 2, port_str)) |
| 567 | { |
Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 568 | bool ok = false; |
| 569 | port = StringConvert::ToUInt32 (port_str.c_str(), UINT32_MAX, 10, &ok); |
| 570 | if (ok && port < UINT16_MAX) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 571 | { |
| 572 | if (error_ptr) |
| 573 | error_ptr->Clear(); |
| 574 | return true; |
| 575 | } |
Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 576 | // port is too large |
| 577 | if (error_ptr) |
| 578 | error_ptr->SetErrorStringWithFormat("invalid host:port specification: '%s'", host_and_port.data()); |
| 579 | return false; |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
| 583 | // If this was unsuccessful, then check if it's simply a signed 32-bit integer, representing |
| 584 | // a port with an empty host. |
| 585 | host_str.clear(); |
| 586 | port_str.clear(); |
Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 587 | bool ok = false; |
| 588 | port = StringConvert::ToUInt32 (host_and_port.data(), UINT32_MAX, 10, &ok); |
| 589 | if (ok && port < UINT16_MAX) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 590 | { |
| 591 | port_str = host_and_port; |
Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 592 | if (error_ptr) |
| 593 | error_ptr->Clear(); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 594 | return true; |
| 595 | } |
| 596 | |
| 597 | if (error_ptr) |
| 598 | error_ptr->SetErrorStringWithFormat("invalid host:port specification: '%s'", host_and_port.data()); |
| 599 | return false; |
| 600 | } |
| 601 | |
| 602 | IOObject::WaitableHandle Socket::GetWaitableHandle() |
| 603 | { |
| 604 | // TODO: On Windows, use WSAEventSelect |
| 605 | return m_socket; |
| 606 | } |
| 607 | |
| 608 | Error Socket::Read (void *buf, size_t &num_bytes) |
| 609 | { |
| 610 | Error error; |
| 611 | int bytes_received = 0; |
| 612 | do |
| 613 | { |
| 614 | bytes_received = ::recv (m_socket, static_cast<char *>(buf), num_bytes, 0); |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 615 | } while (bytes_received < 0 && IsInterrupted ()); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 616 | |
| 617 | if (bytes_received < 0) |
| 618 | { |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 619 | SetLastError (error); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 620 | num_bytes = 0; |
| 621 | } |
| 622 | else |
| 623 | num_bytes = bytes_received; |
| 624 | |
| 625 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_HOST | LIBLLDB_LOG_COMMUNICATION)); |
| 626 | if (log) |
| 627 | { |
| 628 | log->Printf ("%p Socket::Read() (socket = %" PRIu64 ", src = %p, src_len = %" PRIu64 ", flags = 0) => %" PRIi64 " (error = %s)", |
| 629 | static_cast<void*>(this), |
| 630 | static_cast<uint64_t>(m_socket), |
| 631 | buf, |
| 632 | static_cast<uint64_t>(num_bytes), |
| 633 | static_cast<int64_t>(bytes_received), |
| 634 | error.AsCString()); |
| 635 | } |
| 636 | |
| 637 | return error; |
| 638 | } |
| 639 | |
| 640 | Error Socket::Write (const void *buf, size_t &num_bytes) |
| 641 | { |
| 642 | Error error; |
| 643 | int bytes_sent = 0; |
| 644 | do |
| 645 | { |
| 646 | if (m_protocol == ProtocolUdp) |
| 647 | { |
| 648 | bytes_sent = ::sendto (m_socket, |
| 649 | static_cast<const char*>(buf), |
| 650 | num_bytes, |
| 651 | 0, |
| 652 | m_udp_send_sockaddr, |
| 653 | m_udp_send_sockaddr.GetLength()); |
| 654 | } |
| 655 | else |
| 656 | bytes_sent = ::send (m_socket, static_cast<const char *>(buf), num_bytes, 0); |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 657 | } while (bytes_sent < 0 && IsInterrupted ()); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 658 | |
| 659 | if (bytes_sent < 0) |
| 660 | { |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 661 | SetLastError (error); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 662 | num_bytes = 0; |
| 663 | } |
| 664 | else |
| 665 | num_bytes = bytes_sent; |
| 666 | |
| 667 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_HOST)); |
| 668 | if (log) |
| 669 | { |
| 670 | log->Printf ("%p Socket::Write() (socket = %" PRIu64 ", src = %p, src_len = %" PRIu64 ", flags = 0) => %" PRIi64 " (error = %s)", |
| 671 | static_cast<void*>(this), |
| 672 | static_cast<uint64_t>(m_socket), |
| 673 | buf, |
| 674 | static_cast<uint64_t>(num_bytes), |
| 675 | static_cast<int64_t>(bytes_sent), |
| 676 | error.AsCString()); |
| 677 | } |
| 678 | |
| 679 | return error; |
| 680 | } |
| 681 | |
| 682 | Error Socket::PreDisconnect() |
| 683 | { |
| 684 | Error error; |
| 685 | return error; |
| 686 | } |
| 687 | |
| 688 | Error Socket::Close() |
| 689 | { |
| 690 | Error error; |
| 691 | if (!IsValid() || !m_should_close_fd) |
| 692 | return error; |
| 693 | |
| 694 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); |
| 695 | if (log) |
| 696 | log->Printf ("%p Socket::Close (fd = %i)", static_cast<void*>(this), m_socket); |
| 697 | |
| 698 | #if defined(_WIN32) |
| 699 | bool success = !!closesocket(m_socket); |
| 700 | #else |
| 701 | bool success = !!::close (m_socket); |
| 702 | #endif |
| 703 | // A reference to a FD was passed in, set it to an invalid value |
| 704 | m_socket = kInvalidSocketValue; |
| 705 | if (!success) |
| 706 | { |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 707 | SetLastError (error); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | return error; |
| 711 | } |
| 712 | |
| 713 | |
| 714 | int Socket::GetOption(int level, int option_name, int &option_value) |
| 715 | { |
| 716 | get_socket_option_arg_type option_value_p = reinterpret_cast<get_socket_option_arg_type>(&option_value); |
| 717 | socklen_t option_value_size = sizeof(int); |
| 718 | return ::getsockopt(m_socket, level, option_name, option_value_p, &option_value_size); |
| 719 | } |
| 720 | |
| 721 | int Socket::SetOption(int level, int option_name, int option_value) |
| 722 | { |
| 723 | set_socket_option_arg_type option_value_p = reinterpret_cast<get_socket_option_arg_type>(&option_value); |
| 724 | return ::setsockopt(m_socket, level, option_name, option_value_p, sizeof(option_value)); |
| 725 | } |
| 726 | |
Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 727 | uint16_t Socket::GetLocalPortNumber(const NativeSocket& socket) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 728 | { |
| 729 | // We bound to port zero, so we need to figure out which port we actually bound to |
Zachary Turner | 48b475c | 2015-04-02 20:57:38 +0000 | [diff] [blame] | 730 | if (socket != kInvalidSocketValue) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 731 | { |
| 732 | SocketAddress sock_addr; |
| 733 | socklen_t sock_addr_len = sock_addr.GetMaxLength (); |
| 734 | if (::getsockname (socket, sock_addr, &sock_addr_len) == 0) |
| 735 | return sock_addr.GetPort (); |
| 736 | } |
| 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | // Return the port number that is being used by the socket. |
Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 741 | uint16_t Socket::GetLocalPortNumber() const |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 742 | { |
Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 743 | return GetLocalPortNumber (m_socket); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 744 | } |
Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 745 | |
| 746 | std::string Socket::GetLocalIPAddress () const |
| 747 | { |
| 748 | // We bound to port zero, so we need to figure out which port we actually bound to |
Zachary Turner | 48b475c | 2015-04-02 20:57:38 +0000 | [diff] [blame] | 749 | if (m_socket != kInvalidSocketValue) |
Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 750 | { |
| 751 | SocketAddress sock_addr; |
| 752 | socklen_t sock_addr_len = sock_addr.GetMaxLength (); |
| 753 | if (::getsockname (m_socket, sock_addr, &sock_addr_len) == 0) |
| 754 | return sock_addr.GetIPAddress (); |
| 755 | } |
| 756 | return ""; |
| 757 | } |
| 758 | |
| 759 | uint16_t Socket::GetRemotePortNumber () const |
| 760 | { |
Zachary Turner | 48b475c | 2015-04-02 20:57:38 +0000 | [diff] [blame] | 761 | if (m_socket != kInvalidSocketValue) |
Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 762 | { |
| 763 | SocketAddress sock_addr; |
| 764 | socklen_t sock_addr_len = sock_addr.GetMaxLength (); |
| 765 | if (::getpeername (m_socket, sock_addr, &sock_addr_len) == 0) |
| 766 | return sock_addr.GetPort (); |
| 767 | } |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | std::string Socket::GetRemoteIPAddress () const |
| 772 | { |
| 773 | // We bound to port zero, so we need to figure out which port we actually bound to |
Zachary Turner | 48b475c | 2015-04-02 20:57:38 +0000 | [diff] [blame] | 774 | if (m_socket != kInvalidSocketValue) |
Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 775 | { |
| 776 | SocketAddress sock_addr; |
| 777 | socklen_t sock_addr_len = sock_addr.GetMaxLength (); |
| 778 | if (::getpeername (m_socket, sock_addr, &sock_addr_len) == 0) |
| 779 | return sock_addr.GetIPAddress (); |
| 780 | } |
| 781 | return ""; |
| 782 | } |
| 783 | |
| 784 | |