Skip to content

[lldb][NFC] Move few static helpers to the class Socket #106640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lldb/include/lldb/Host/Socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,17 @@ class Socket : public IOObject {
static llvm::Expected<std::unique_ptr<UDPSocket>>
UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit);

int GetOption(int level, int option_name, int &option_value);
int SetOption(int level, int option_name, int option_value);
static int GetOption(NativeSocket sockfd, int level, int option_name,
int &option_value);
int GetOption(int level, int option_name, int &option_value) {
return GetOption(m_socket, level, option_name, option_value);
};

static int SetOption(NativeSocket sockfd, int level, int option_name,
int option_value);
int SetOption(int level, int option_name, int option_value) {
return SetOption(m_socket, level, option_name, option_value);
};

NativeSocket GetNativeSocket() const { return m_socket; }
SocketProtocol GetSocketProtocol() const { return m_protocol; }
Expand All @@ -138,6 +147,8 @@ class Socket : public IOObject {

virtual size_t Send(const void *buf, const size_t num_bytes);

static int CloseSocket(NativeSocket sockfd);
static Status GetLastError();
static void SetLastError(Status &error);
static NativeSocket CreateSocket(const int domain, const int type,
const int protocol,
Expand Down
36 changes: 26 additions & 10 deletions lldb/source/Host/common/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,7 @@ Status Socket::Close() {
LLDB_LOGF(log, "%p Socket::Close (fd = %" PRIu64 ")",
static_cast<void *>(this), static_cast<uint64_t>(m_socket));

#if defined(_WIN32)
bool success = closesocket(m_socket) == 0;
#else
bool success = ::close(m_socket) == 0;
#endif
bool success = CloseSocket(m_socket) == 0;
// A reference to a FD was passed in, set it to an invalid value
m_socket = kInvalidSocketValue;
if (!success) {
Expand All @@ -400,18 +396,20 @@ Status Socket::Close() {
return error;
}

int Socket::GetOption(int level, int option_name, int &option_value) {
int Socket::GetOption(NativeSocket sockfd, int level, int option_name,
int &option_value) {
get_socket_option_arg_type option_value_p =
reinterpret_cast<get_socket_option_arg_type>(&option_value);
socklen_t option_value_size = sizeof(int);
return ::getsockopt(m_socket, level, option_name, option_value_p,
return ::getsockopt(sockfd, level, option_name, option_value_p,
&option_value_size);
}

int Socket::SetOption(int level, int option_name, int option_value) {
int Socket::SetOption(NativeSocket sockfd, int level, int option_name,
int option_value) {
set_socket_option_arg_type option_value_p =
reinterpret_cast<get_socket_option_arg_type>(&option_value);
return ::setsockopt(m_socket, level, option_name, option_value_p,
reinterpret_cast<set_socket_option_arg_type>(&option_value);
return ::setsockopt(sockfd, level, option_name, option_value_p,
sizeof(option_value));
}

Expand All @@ -427,6 +425,24 @@ void Socket::SetLastError(Status &error) {
#endif
}

Status Socket::GetLastError() {
std::error_code EC;
#ifdef _WIN32
EC = llvm::mapWindowsError(WSAGetLastError());
#else
EC = std::error_code(errno, std::generic_category());
#endif
return EC;
}

int Socket::CloseSocket(NativeSocket sockfd) {
#ifdef _WIN32
return ::closesocket(sockfd);
#else
return ::close(sockfd);
#endif
}

NativeSocket Socket::CreateSocket(const int domain, const int type,
const int protocol,
bool child_processes_inherit, Status &error) {
Expand Down
35 changes: 6 additions & 29 deletions lldb/source/Host/common/TCPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,9 @@
#include <winsock2.h>
#endif

#ifdef _WIN32
#define CLOSE_SOCKET closesocket
typedef const char *set_socket_option_arg_type;
#else
#include <unistd.h>
#define CLOSE_SOCKET ::close
typedef const void *set_socket_option_arg_type;
#endif

using namespace lldb;
using namespace lldb_private;

static Status GetLastSocketError() {
std::error_code EC;
#ifdef _WIN32
EC = llvm::mapWindowsError(WSAGetLastError());
#else
EC = std::error_code(errno, std::generic_category());
#endif
return EC;
}

static const int kType = SOCK_STREAM;

TCPSocket::TCPSocket(bool should_close, bool child_processes_inherit)
Expand Down Expand Up @@ -208,12 +189,8 @@ Status TCPSocket::Listen(llvm::StringRef name, int backlog) {
continue;

// enable local address reuse
int option_value = 1;
set_socket_option_arg_type option_value_p =
reinterpret_cast<set_socket_option_arg_type>(&option_value);
if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, option_value_p,
sizeof(option_value)) == -1) {
CLOSE_SOCKET(fd);
if (SetOption(fd, SOL_SOCKET, SO_REUSEADDR, 1) == -1) {
CloseSocket(fd);
continue;
}

Expand All @@ -229,8 +206,8 @@ Status TCPSocket::Listen(llvm::StringRef name, int backlog) {
err = ::listen(fd, backlog);

if (err == -1) {
error = GetLastSocketError();
CLOSE_SOCKET(fd);
error = GetLastError();
CloseSocket(fd);
continue;
}

Expand All @@ -251,7 +228,7 @@ Status TCPSocket::Listen(llvm::StringRef name, int backlog) {

void TCPSocket::CloseListenSockets() {
for (auto socket : m_listen_sockets)
CLOSE_SOCKET(socket.first);
CloseSocket(socket.first);
m_listen_sockets.clear();
}

Expand Down Expand Up @@ -280,7 +257,7 @@ llvm::Expected<std::vector<MainLoopBase::ReadHandleUP>> TCPSocket::Accept(

const lldb_private::SocketAddress &AddrIn = m_listen_sockets[fd];
if (!AddrIn.IsAnyAddr() && AcceptAddr != AddrIn) {
CLOSE_SOCKET(sock);
CloseSocket(sock);
LLDB_LOG(log, "rejecting incoming connection from {0} (expecting {1})",
AcceptAddr.GetIPAddress(), AddrIn.GetIPAddress());
return;
Expand Down
Loading