1 //===-- Socket.cpp ----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Host/Socket.h"
12 #include "lldb/Core/Log.h"
13 #include "lldb/Core/RegularExpression.h"
14 #include "lldb/Host/Config.h"
15 #include "lldb/Host/FileSystem.h"
16 #include "lldb/Host/Host.h"
17 #include "lldb/Host/SocketAddress.h"
18 #include "lldb/Host/TimeValue.h"
19 #include "lldb/Interpreter/Args.h"
21 #ifdef __ANDROID_NDK__
22 #include <linux/tcp.h>
23 #include <bits/error_constants.h>
24 #include <asm-generic/errno-base.h>
26 #include <arpa/inet.h>
29 #ifndef LLDB_DISABLE_POSIX
30 #include <arpa/inet.h>
32 #include <netinet/in.h>
33 #include <netinet/tcp.h>
34 #include <sys/socket.h>
39 using namespace lldb_private;
42 typedef const char * set_socket_option_arg_type;
43 typedef char * get_socket_option_arg_type;
44 const NativeSocket Socket::kInvalidSocketValue = INVALID_SOCKET;
45 #else // #if defined(_WIN32)
46 typedef const void * set_socket_option_arg_type;
47 typedef void * get_socket_option_arg_type;
48 const NativeSocket Socket::kInvalidSocketValue = -1;
49 #endif // #if defined(_WIN32)
52 // Android does not have SUN_LEN
54 #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) + strlen((ptr)->sun_path))
56 #endif // #ifdef __ANDROID__
60 NativeSocket CreateSocket(const int domain, const int type, const int protocol, bool child_processes_inherit)
62 auto socketType = type;
64 if (!child_processes_inherit) {
65 socketType |= SOCK_CLOEXEC;
68 return ::socket (domain, socketType, protocol);
71 NativeSocket Accept(NativeSocket sockfd, struct sockaddr *addr, socklen_t *addrlen, bool child_processes_inherit)
75 if (!child_processes_inherit) {
76 flags |= SOCK_CLOEXEC;
78 return ::accept4 (sockfd, addr, addrlen, flags);
80 return ::accept (sockfd, addr, addrlen);
85 Socket::Socket(NativeSocket socket, SocketProtocol protocol, bool should_close)
86 : IOObject(eFDTypeSocket, should_close)
87 , m_protocol(protocol)
98 Error Socket::TcpConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket)
100 // Store the result in a unique_ptr in case we error out, the memory will get correctly freed.
101 std::unique_ptr<Socket> final_socket;
102 NativeSocket sock = kInvalidSocketValue;
105 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_HOST));
107 log->Printf ("Socket::TcpConnect (host/port = %s)", host_and_port.data());
109 std::string host_str;
110 std::string port_str;
111 int32_t port = INT32_MIN;
112 if (!DecodeHostAndPort (host_and_port, host_str, port_str, port, &error))
116 sock = CreateSocket (AF_INET, SOCK_STREAM, IPPROTO_TCP, child_processes_inherit);
117 if (sock == kInvalidSocketValue)
119 // TODO: On Windows, use WSAGetLastError().
120 error.SetErrorToErrno();
124 // Since they both refer to the same socket descriptor, arbitrarily choose the send socket to
126 final_socket.reset(new Socket(sock, ProtocolTcp, true));
128 // Enable local address reuse
129 final_socket->SetOption(SOL_SOCKET, SO_REUSEADDR, 1);
131 struct sockaddr_in sa;
132 ::memset (&sa, 0, sizeof (sa));
133 sa.sin_family = AF_INET;
134 sa.sin_port = htons (port);
136 int inet_pton_result = ::inet_pton (AF_INET, host_str.c_str(), &sa.sin_addr);
138 if (inet_pton_result <= 0)
140 struct hostent *host_entry = gethostbyname (host_str.c_str());
142 host_str = ::inet_ntoa (*(struct in_addr *)*host_entry->h_addr_list);
143 inet_pton_result = ::inet_pton (AF_INET, host_str.c_str(), &sa.sin_addr);
144 if (inet_pton_result <= 0)
146 // TODO: On Windows, use WSAGetLastError()
147 if (inet_pton_result == -1)
148 error.SetErrorToErrno();
150 error.SetErrorStringWithFormat("invalid host string: '%s'", host_str.c_str());
156 if (-1 == ::connect (sock, (const struct sockaddr *)&sa, sizeof(sa)))
158 // TODO: On Windows, use WSAGetLastError()
159 error.SetErrorToErrno();
163 // Keep our TCP packets coming without any delays.
164 final_socket->SetOption(IPPROTO_TCP, TCP_NODELAY, 1);
166 socket = final_socket.release();
170 Error Socket::TcpListen(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket, Predicate<uint16_t>* predicate)
172 std::unique_ptr<Socket> listen_socket;
173 NativeSocket listen_sock = kInvalidSocketValue;
176 const sa_family_t family = AF_INET;
177 const int socktype = SOCK_STREAM;
178 const int protocol = IPPROTO_TCP;
179 listen_sock = ::CreateSocket (family, socktype, protocol, child_processes_inherit);
180 if (listen_sock == kInvalidSocketValue)
182 error.SetErrorToErrno();
186 listen_socket.reset(new Socket(listen_sock, ProtocolTcp, true));
188 // enable local address reuse
189 listen_socket->SetOption(SOL_SOCKET, SO_REUSEADDR, 1);
191 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION));
193 log->Printf ("ConnectionFileDescriptor::SocketListen (%s)", host_and_port.data());
195 std::string host_str;
196 std::string port_str;
197 int32_t port = INT32_MIN;
198 if (!DecodeHostAndPort (host_and_port, host_str, port_str, port, &error))
201 SocketAddress anyaddr;
202 if (anyaddr.SetToAnyAddress (family, port))
204 int err = ::bind (listen_sock, anyaddr, anyaddr.GetLength());
207 // TODO: On Windows, use WSAGetLastError()
208 error.SetErrorToErrno();
212 err = ::listen (listen_sock, 1);
215 // TODO: On Windows, use WSAGetLastError()
216 error.SetErrorToErrno();
220 // We were asked to listen on port zero which means we
221 // must now read the actual port that was given to us
222 // as port zero is a special code for "find an open port
225 port = listen_socket->GetPortNumber();
227 // Set the port predicate since when doing a listen://<host>:<port>
228 // it often needs to accept the incoming connection which is a blocking
229 // system call. Allowing access to the bound port using a predicate allows
230 // us to wait for the port predicate to be set to a non-zero value from
231 // another thread in an efficient manor.
233 predicate->SetValue(port, eBroadcastAlways);
235 socket = listen_socket.release();
241 Error Socket::BlockingAccept(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket)
244 std::string host_str;
245 std::string port_str;
247 if (!DecodeHostAndPort(host_and_port, host_str, port_str, port, &error))
250 const sa_family_t family = AF_INET;
251 const int socktype = SOCK_STREAM;
252 const int protocol = IPPROTO_TCP;
253 SocketAddress listen_addr;
254 if (host_str.empty())
255 listen_addr.SetToLocalhost(family, port);
256 else if (host_str.compare("*") == 0)
257 listen_addr.SetToAnyAddress(family, port);
260 if (!listen_addr.getaddrinfo(host_str.c_str(), port_str.c_str(), family, socktype, protocol))
262 error.SetErrorStringWithFormat("unable to resolve hostname '%s'", host_str.c_str());
267 bool accept_connection = false;
268 std::unique_ptr<Socket> accepted_socket;
270 // Loop until we are happy with our connection
271 while (!accept_connection)
273 struct sockaddr_in accept_addr;
274 ::memset (&accept_addr, 0, sizeof accept_addr);
275 #if !(defined (__linux__) || defined(_WIN32))
276 accept_addr.sin_len = sizeof accept_addr;
278 socklen_t accept_addr_len = sizeof accept_addr;
280 int sock = Accept (this->GetNativeSocket(),
281 (struct sockaddr *)&accept_addr,
283 child_processes_inherit);
285 if (sock == kInvalidSocketValue)
287 // TODO: On Windows, use WSAGetLastError()
288 error.SetErrorToErrno();
292 bool is_same_addr = true;
293 #if !(defined(__linux__) || (defined(_WIN32)))
294 is_same_addr = (accept_addr_len == listen_addr.sockaddr_in().sin_len);
297 is_same_addr = (accept_addr.sin_addr.s_addr == listen_addr.sockaddr_in().sin_addr.s_addr);
299 if (is_same_addr || (listen_addr.sockaddr_in().sin_addr.s_addr == INADDR_ANY))
301 accept_connection = true;
302 // Since both sockets have the same descriptor, arbitrarily choose the send
303 // socket to be the owner.
304 accepted_socket.reset(new Socket(sock, ProtocolTcp, true));
308 const uint8_t *accept_ip = (const uint8_t *)&accept_addr.sin_addr.s_addr;
309 const uint8_t *listen_ip = (const uint8_t *)&listen_addr.sockaddr_in().sin_addr.s_addr;
310 ::fprintf (stderr, "error: rejecting incoming connection from %u.%u.%u.%u (expecting %u.%u.%u.%u)\n",
311 accept_ip[0], accept_ip[1], accept_ip[2], accept_ip[3],
312 listen_ip[0], listen_ip[1], listen_ip[2], listen_ip[3]);
313 accepted_socket.reset();
317 if (!accepted_socket)
320 // Keep our TCP packets coming without any delays.
321 accepted_socket->SetOption (IPPROTO_TCP, TCP_NODELAY, 1);
323 socket = accepted_socket.release();
328 Error Socket::UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&send_socket, Socket *&recv_socket)
330 std::unique_ptr<Socket> final_send_socket;
331 std::unique_ptr<Socket> final_recv_socket;
332 NativeSocket final_send_fd = kInvalidSocketValue;
333 NativeSocket final_recv_fd = kInvalidSocketValue;
335 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION));
337 log->Printf ("Socket::UdpConnect (host/port = %s)", host_and_port.data());
340 std::string host_str;
341 std::string port_str;
342 int32_t port = INT32_MIN;
343 if (!DecodeHostAndPort (host_and_port, host_str, port_str, port, &error))
346 // Setup the receiving end of the UDP connection on this localhost
347 // on port zero. After we bind to port zero we can read the port.
348 final_recv_fd = ::CreateSocket (AF_INET, SOCK_DGRAM, 0, child_processes_inherit);
349 if (final_recv_fd == kInvalidSocketValue)
351 // Socket creation failed...
352 // TODO: On Windows, use WSAGetLastError().
353 error.SetErrorToErrno();
357 final_recv_socket.reset(new Socket(final_recv_fd, ProtocolUdp, true));
359 // Socket was created, now lets bind to the requested port
361 addr.SetToAnyAddress (AF_INET, 0);
363 if (::bind (final_recv_fd, addr, addr.GetLength()) == -1)
366 // TODO: On Windows use WSAGetLastError()
367 error.SetErrorToErrno();
371 assert(error.Fail() == !(final_recv_socket && final_recv_socket->IsValid()));
375 // At this point we have setup the receive port, now we need to
376 // setup the UDP send socket
378 struct addrinfo hints;
379 struct addrinfo *service_info_list = NULL;
381 ::memset (&hints, 0, sizeof(hints));
382 hints.ai_family = AF_INET;
383 hints.ai_socktype = SOCK_DGRAM;
384 int err = ::getaddrinfo (host_str.c_str(), port_str.c_str(), &hints, &service_info_list);
387 error.SetErrorStringWithFormat("getaddrinfo(%s, %s, &hints, &info) returned error %i (%s)",
395 for (struct addrinfo *service_info_ptr = service_info_list;
396 service_info_ptr != NULL;
397 service_info_ptr = service_info_ptr->ai_next)
399 final_send_fd = ::CreateSocket (service_info_ptr->ai_family,
400 service_info_ptr->ai_socktype,
401 service_info_ptr->ai_protocol,
402 child_processes_inherit);
404 if (final_send_fd != kInvalidSocketValue)
406 final_send_socket.reset(new Socket(final_send_fd, ProtocolUdp, true));
407 final_send_socket->m_udp_send_sockaddr = service_info_ptr;
414 :: freeaddrinfo (service_info_list);
416 if (final_send_fd == kInvalidSocketValue)
418 // TODO: On Windows, use WSAGetLastError().
419 error.SetErrorToErrno();
423 send_socket = final_send_socket.release();
424 recv_socket = final_recv_socket.release();
429 Error Socket::UnixDomainConnect(llvm::StringRef name, bool child_processes_inherit, Socket *&socket)
432 #ifndef LLDB_DISABLE_POSIX
433 std::unique_ptr<Socket> final_socket;
435 // Open the socket that was passed in as an option
436 struct sockaddr_un saddr_un;
437 int fd = ::CreateSocket (AF_UNIX, SOCK_STREAM, 0, child_processes_inherit);
438 if (fd == kInvalidSocketValue)
440 error.SetErrorToErrno();
444 final_socket.reset(new Socket(fd, ProtocolUnixDomain, true));
446 saddr_un.sun_family = AF_UNIX;
447 ::strncpy(saddr_un.sun_path, name.data(), sizeof(saddr_un.sun_path) - 1);
448 saddr_un.sun_path[sizeof(saddr_un.sun_path) - 1] = '\0';
449 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
450 saddr_un.sun_len = SUN_LEN (&saddr_un);
453 if (::connect (fd, (struct sockaddr *)&saddr_un, SUN_LEN (&saddr_un)) < 0)
455 error.SetErrorToErrno();
459 socket = final_socket.release();
461 error.SetErrorString("Unix domain sockets are not supported on this platform.");
466 Error Socket::UnixDomainAccept(llvm::StringRef name, bool child_processes_inherit, Socket *&socket)
469 #ifndef LLDB_DISABLE_POSIX
470 struct sockaddr_un saddr_un;
471 std::unique_ptr<Socket> listen_socket;
472 std::unique_ptr<Socket> final_socket;
473 NativeSocket listen_fd = kInvalidSocketValue;
474 NativeSocket socket_fd = kInvalidSocketValue;
476 listen_fd = ::CreateSocket (AF_UNIX, SOCK_STREAM, 0, child_processes_inherit);
477 if (listen_fd == kInvalidSocketValue)
479 error.SetErrorToErrno();
483 listen_socket.reset(new Socket(listen_fd, ProtocolUnixDomain, true));
485 saddr_un.sun_family = AF_UNIX;
486 ::strncpy(saddr_un.sun_path, name.data(), sizeof(saddr_un.sun_path) - 1);
487 saddr_un.sun_path[sizeof(saddr_un.sun_path) - 1] = '\0';
488 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
489 saddr_un.sun_len = SUN_LEN (&saddr_un);
492 FileSystem::Unlink(name.data());
493 bool success = false;
494 if (::bind (listen_fd, (struct sockaddr *)&saddr_un, SUN_LEN (&saddr_un)) == 0)
496 if (::listen (listen_fd, 5) == 0)
498 socket_fd = Accept (listen_fd, NULL, 0, child_processes_inherit);
501 final_socket.reset(new Socket(socket_fd, ProtocolUnixDomain, true));
509 error.SetErrorToErrno();
512 // We are done with the listen port
513 listen_socket.reset();
515 socket = final_socket.release();
517 error.SetErrorString("Unix domain sockets are not supported on this platform.");
523 Socket::DecodeHostAndPort(llvm::StringRef host_and_port,
524 std::string &host_str,
525 std::string &port_str,
529 static RegularExpression g_regex ("([^:]+):([0-9]+)");
530 RegularExpression::Match regex_match(2);
531 if (g_regex.Execute (host_and_port.data(), ®ex_match))
533 if (regex_match.GetMatchAtIndex (host_and_port.data(), 1, host_str) &&
534 regex_match.GetMatchAtIndex (host_and_port.data(), 2, port_str))
536 port = Args::StringToSInt32 (port_str.c_str(), INT32_MIN);
537 if (port != INT32_MIN)
546 // If this was unsuccessful, then check if it's simply a signed 32-bit integer, representing
547 // a port with an empty host.
550 port = Args::StringToSInt32(host_and_port.data(), INT32_MIN);
551 if (port != INT32_MIN)
553 port_str = host_and_port;
558 error_ptr->SetErrorStringWithFormat("invalid host:port specification: '%s'", host_and_port.data());
562 IOObject::WaitableHandle Socket::GetWaitableHandle()
564 // TODO: On Windows, use WSAEventSelect
568 Error Socket::Read (void *buf, size_t &num_bytes)
571 int bytes_received = 0;
574 bytes_received = ::recv (m_socket, static_cast<char *>(buf), num_bytes, 0);
575 // TODO: Use WSAGetLastError on windows.
576 } while (bytes_received < 0 && errno == EINTR);
578 if (bytes_received < 0)
580 error.SetErrorToErrno();
584 num_bytes = bytes_received;
586 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_HOST | LIBLLDB_LOG_COMMUNICATION));
589 log->Printf ("%p Socket::Read() (socket = %" PRIu64 ", src = %p, src_len = %" PRIu64 ", flags = 0) => %" PRIi64 " (error = %s)",
590 static_cast<void*>(this),
591 static_cast<uint64_t>(m_socket),
593 static_cast<uint64_t>(num_bytes),
594 static_cast<int64_t>(bytes_received),
601 Error Socket::Write (const void *buf, size_t &num_bytes)
607 if (m_protocol == ProtocolUdp)
609 bytes_sent = ::sendto (m_socket,
610 static_cast<const char*>(buf),
614 m_udp_send_sockaddr.GetLength());
617 bytes_sent = ::send (m_socket, static_cast<const char *>(buf), num_bytes, 0);
618 // TODO: Use WSAGetLastError on windows.
619 } while (bytes_sent < 0 && errno == EINTR);
623 // TODO: On Windows, use WSAGEtLastError.
624 error.SetErrorToErrno();
628 num_bytes = bytes_sent;
630 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_HOST));
633 log->Printf ("%p Socket::Write() (socket = %" PRIu64 ", src = %p, src_len = %" PRIu64 ", flags = 0) => %" PRIi64 " (error = %s)",
634 static_cast<void*>(this),
635 static_cast<uint64_t>(m_socket),
637 static_cast<uint64_t>(num_bytes),
638 static_cast<int64_t>(bytes_sent),
645 Error Socket::PreDisconnect()
651 Error Socket::Close()
654 if (!IsValid() || !m_should_close_fd)
657 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION));
659 log->Printf ("%p Socket::Close (fd = %i)", static_cast<void*>(this), m_socket);
662 bool success = !!closesocket(m_socket);
664 bool success = !!::close (m_socket);
666 // A reference to a FD was passed in, set it to an invalid value
667 m_socket = kInvalidSocketValue;
670 // TODO: On Windows, use WSAGetLastError().
671 error.SetErrorToErrno();
678 int Socket::GetOption(int level, int option_name, int &option_value)
680 get_socket_option_arg_type option_value_p = reinterpret_cast<get_socket_option_arg_type>(&option_value);
681 socklen_t option_value_size = sizeof(int);
682 return ::getsockopt(m_socket, level, option_name, option_value_p, &option_value_size);
685 int Socket::SetOption(int level, int option_name, int option_value)
687 set_socket_option_arg_type option_value_p = reinterpret_cast<get_socket_option_arg_type>(&option_value);
688 return ::setsockopt(m_socket, level, option_name, option_value_p, sizeof(option_value));
691 uint16_t Socket::GetPortNumber(const NativeSocket& socket)
693 // We bound to port zero, so we need to figure out which port we actually bound to
696 SocketAddress sock_addr;
697 socklen_t sock_addr_len = sock_addr.GetMaxLength ();
698 if (::getsockname (socket, sock_addr, &sock_addr_len) == 0)
699 return sock_addr.GetPort ();
704 // Return the port number that is being used by the socket.
705 uint16_t Socket::GetPortNumber() const
707 return GetPortNumber(m_socket);