11 #include "lldb/Host/Config.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/Errno.h"
21 #include "llvm/Support/Error.h"
22 #include "llvm/Support/Regex.h"
23 #include "llvm/Support/WindowsError.h"
28 #include <arpa/inet.h>
30 #include <netinet/in.h>
31 #include <netinet/tcp.h>
32 #include <sys/socket.h>
42 #include <arpa/inet.h>
43 #include <asm-generic/errno-base.h>
46 #include <linux/tcp.h>
47 #include <sys/syscall.h>
57 const NativeSocket Socket::kInvalidSocketValue = INVALID_SOCKET;
58 #else // #if defined(_WIN32)
62 #endif // #if defined(_WIN32)
66 return ::WSAGetLastError() == WSAEINTR;
68 return errno == EINTR;
73 bool child_processes_inherit)
74 :
IOObject(eFDTypeSocket), m_protocol(protocol),
75 m_socket(kInvalidSocketValue),
76 m_child_processes_inherit(child_processes_inherit),
77 m_should_close_fd(should_close) {}
83 auto wVersion = WINSOCK_VERSION;
85 int err = ::WSAStartup(wVersion, &wsaData);
87 if (wsaData.wVersion < wVersion) {
89 return llvm::make_error<llvm::StringError>(
90 "WSASock version is not expected.", llvm::inconvertibleErrorCode());
93 return llvm::errorCodeToError(llvm::mapWindowsError(::WSAGetLastError()));
97 return llvm::Error::success();
107 bool child_processes_inherit,
111 std::unique_ptr<Socket> socket_up;
115 std::make_unique<TCPSocket>(
true, child_processes_inherit);
119 std::make_unique<UDPSocket>(
true, child_processes_inherit);
122 #if LLDB_ENABLE_POSIX
124 std::make_unique<DomainSocket>(
true, child_processes_inherit);
126 error.SetErrorString(
127 "Unix domain sockets are not supported on this platform.");
133 std::make_unique<AbstractSocket>(child_processes_inherit);
135 error.SetErrorString(
136 "Abstract domain sockets are not supported on this platform.");
147 llvm::Expected<std::unique_ptr<Socket>>
149 bool child_processes_inherit) {
151 LLDB_LOG(log,
"host_and_port = {0}", host_and_port);
154 std::unique_ptr<Socket> connect_socket(
157 return error.ToError();
159 error = connect_socket->Connect(host_and_port);
161 return std::move(connect_socket);
163 return error.ToError();
166 llvm::Expected<std::unique_ptr<TCPSocket>>
170 LLDB_LOG(log,
"host_and_port = {0}", host_and_port);
172 std::unique_ptr<TCPSocket> listen_socket(
173 new TCPSocket(
true, child_processes_inherit));
175 Status error = listen_socket->Listen(host_and_port, backlog);
177 return error.ToError();
179 return std::move(listen_socket);
182 llvm::Expected<std::unique_ptr<UDPSocket>>
184 bool child_processes_inherit) {
189 static llvm::Regex g_regex(
"([^:]+|\\[[0-9a-fA-F:]+.*\\]):([0-9]+)");
191 llvm::SmallVector<llvm::StringRef, 3> matches;
192 if (g_regex.match(host_and_port, &matches)) {
197 if (to_integer(matches[2], ret.
port, 10))
202 if (to_integer(host_and_port, ret.
port, 10))
206 return llvm::createStringError(llvm::inconvertibleErrorCode(),
207 "invalid host:port specification: '%s'",
208 host_and_port.str().c_str());
218 int bytes_received = 0;
220 bytes_received = ::recv(
m_socket,
static_cast<char *
>(buf), num_bytes, 0);
223 if (bytes_received < 0) {
227 num_bytes = bytes_received;
232 "%p Socket::Read() (socket = %" PRIu64
233 ", src = %p, src_len = %" PRIu64
", flags = 0) => %" PRIi64
235 static_cast<void *
>(
this),
static_cast<uint64_t
>(
m_socket), buf,
236 static_cast<uint64_t
>(num_bytes),
237 static_cast<int64_t
>(bytes_received),
error.AsCString());
244 const size_t src_len = num_bytes;
248 bytes_sent =
Send(buf, num_bytes);
251 if (bytes_sent < 0) {
255 num_bytes = bytes_sent;
260 "%p Socket::Write() (socket = %" PRIu64
261 ", src = %p, src_len = %" PRIu64
", flags = 0) => %" PRIi64
263 static_cast<void *
>(
this),
static_cast<uint64_t
>(
m_socket), buf,
264 static_cast<uint64_t
>(src_len),
265 static_cast<int64_t
>(bytes_sent),
error.AsCString());
282 LLDB_LOGF(log,
"%p Socket::Close (fd = %" PRIu64
")",
283 static_cast<void *
>(
this),
static_cast<uint64_t
>(
m_socket));
286 bool success = closesocket(
m_socket) == 0;
288 bool success = ::close(
m_socket) == 0;
302 socklen_t option_value_size =
sizeof(int);
303 return ::getsockopt(
m_socket, level, option_name, option_value_p,
310 return ::setsockopt(
m_socket, level, option_name, option_value_p,
311 sizeof(option_value));
314 size_t Socket::Send(
const void *buf,
const size_t num_bytes) {
315 return ::send(
m_socket,
static_cast<const char *
>(buf), num_bytes, 0);
322 error.SetErrorToErrno();
330 auto socket_type = type;
332 if (!child_processes_inherit)
333 socket_type |= SOCK_CLOEXEC;
335 auto sock = ::socket(domain, socket_type, protocol);
346 #if defined(ANDROID_USE_ACCEPT_WORKAROUND)
353 int fd = syscall(__NR_accept, sockfd, addr, addrlen);
354 if (fd >= 0 && !child_processes_inherit) {
355 int flags = ::fcntl(fd, F_GETFD);
356 if (flags != -1 && ::fcntl(fd, F_SETFD, flags | FD_CLOEXEC) != -1)
362 #elif defined(SOCK_CLOEXEC) && defined(HAVE_ACCEPT4)
364 if (!child_processes_inherit) {
365 flags |= SOCK_CLOEXEC;
368 static_cast<NativeSocket>(-1), ::accept4, sockfd, addr, addrlen, flags);
371 static_cast<NativeSocket>(-1), ::accept, sockfd, addr, addrlen);