Skip to content

Commit deeafea

Browse files
authored
[lldb][NFC] Move few static helpers to the class Socket (#106640)
Fixed a typo in Socket::SetOption().
1 parent 69657eb commit deeafea

File tree

3 files changed

+45
-41
lines changed

3 files changed

+45
-41
lines changed

lldb/include/lldb/Host/Socket.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,17 @@ class Socket : public IOObject {
112112
static llvm::Expected<std::unique_ptr<UDPSocket>>
113113
UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit);
114114

115-
int GetOption(int level, int option_name, int &option_value);
116-
int SetOption(int level, int option_name, int option_value);
115+
static int GetOption(NativeSocket sockfd, int level, int option_name,
116+
int &option_value);
117+
int GetOption(int level, int option_name, int &option_value) {
118+
return GetOption(m_socket, level, option_name, option_value);
119+
};
120+
121+
static int SetOption(NativeSocket sockfd, int level, int option_name,
122+
int option_value);
123+
int SetOption(int level, int option_name, int option_value) {
124+
return SetOption(m_socket, level, option_name, option_value);
125+
};
117126

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

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

150+
static int CloseSocket(NativeSocket sockfd);
151+
static Status GetLastError();
141152
static void SetLastError(Status &error);
142153
static NativeSocket CreateSocket(const int domain, const int type,
143154
const int protocol,

lldb/source/Host/common/Socket.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,7 @@ Status Socket::Close() {
386386
LLDB_LOGF(log, "%p Socket::Close (fd = %" PRIu64 ")",
387387
static_cast<void *>(this), static_cast<uint64_t>(m_socket));
388388

389-
#if defined(_WIN32)
390-
bool success = closesocket(m_socket) == 0;
391-
#else
392-
bool success = ::close(m_socket) == 0;
393-
#endif
389+
bool success = CloseSocket(m_socket) == 0;
394390
// A reference to a FD was passed in, set it to an invalid value
395391
m_socket = kInvalidSocketValue;
396392
if (!success) {
@@ -400,18 +396,20 @@ Status Socket::Close() {
400396
return error;
401397
}
402398

403-
int Socket::GetOption(int level, int option_name, int &option_value) {
399+
int Socket::GetOption(NativeSocket sockfd, int level, int option_name,
400+
int &option_value) {
404401
get_socket_option_arg_type option_value_p =
405402
reinterpret_cast<get_socket_option_arg_type>(&option_value);
406403
socklen_t option_value_size = sizeof(int);
407-
return ::getsockopt(m_socket, level, option_name, option_value_p,
404+
return ::getsockopt(sockfd, level, option_name, option_value_p,
408405
&option_value_size);
409406
}
410407

411-
int Socket::SetOption(int level, int option_name, int option_value) {
408+
int Socket::SetOption(NativeSocket sockfd, int level, int option_name,
409+
int option_value) {
412410
set_socket_option_arg_type option_value_p =
413-
reinterpret_cast<get_socket_option_arg_type>(&option_value);
414-
return ::setsockopt(m_socket, level, option_name, option_value_p,
411+
reinterpret_cast<set_socket_option_arg_type>(&option_value);
412+
return ::setsockopt(sockfd, level, option_name, option_value_p,
415413
sizeof(option_value));
416414
}
417415

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

428+
Status Socket::GetLastError() {
429+
std::error_code EC;
430+
#ifdef _WIN32
431+
EC = llvm::mapWindowsError(WSAGetLastError());
432+
#else
433+
EC = std::error_code(errno, std::generic_category());
434+
#endif
435+
return EC;
436+
}
437+
438+
int Socket::CloseSocket(NativeSocket sockfd) {
439+
#ifdef _WIN32
440+
return ::closesocket(sockfd);
441+
#else
442+
return ::close(sockfd);
443+
#endif
444+
}
445+
430446
NativeSocket Socket::CreateSocket(const int domain, const int type,
431447
const int protocol,
432448
bool child_processes_inherit, Status &error) {

lldb/source/Host/common/TCPSocket.cpp

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,9 @@
3333
#include <winsock2.h>
3434
#endif
3535

36-
#ifdef _WIN32
37-
#define CLOSE_SOCKET closesocket
38-
typedef const char *set_socket_option_arg_type;
39-
#else
40-
#include <unistd.h>
41-
#define CLOSE_SOCKET ::close
42-
typedef const void *set_socket_option_arg_type;
43-
#endif
44-
4536
using namespace lldb;
4637
using namespace lldb_private;
4738

48-
static Status GetLastSocketError() {
49-
std::error_code EC;
50-
#ifdef _WIN32
51-
EC = llvm::mapWindowsError(WSAGetLastError());
52-
#else
53-
EC = std::error_code(errno, std::generic_category());
54-
#endif
55-
return EC;
56-
}
57-
5839
static const int kType = SOCK_STREAM;
5940

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

210191
// enable local address reuse
211-
int option_value = 1;
212-
set_socket_option_arg_type option_value_p =
213-
reinterpret_cast<set_socket_option_arg_type>(&option_value);
214-
if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, option_value_p,
215-
sizeof(option_value)) == -1) {
216-
CLOSE_SOCKET(fd);
192+
if (SetOption(fd, SOL_SOCKET, SO_REUSEADDR, 1) == -1) {
193+
CloseSocket(fd);
217194
continue;
218195
}
219196

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

231208
if (err == -1) {
232-
error = GetLastSocketError();
233-
CLOSE_SOCKET(fd);
209+
error = GetLastError();
210+
CloseSocket(fd);
234211
continue;
235212
}
236213

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

252229
void TCPSocket::CloseListenSockets() {
253230
for (auto socket : m_listen_sockets)
254-
CLOSE_SOCKET(socket.first);
231+
CloseSocket(socket.first);
255232
m_listen_sockets.clear();
256233
}
257234

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

281258
const lldb_private::SocketAddress &AddrIn = m_listen_sockets[fd];
282259
if (!AddrIn.IsAnyAddr() && AcceptAddr != AddrIn) {
283-
CLOSE_SOCKET(sock);
260+
CloseSocket(sock);
284261
LLDB_LOG(log, "rejecting incoming connection from {0} (expecting {1})",
285262
AcceptAddr.GetIPAddress(), AddrIn.GetIPAddress());
286263
return;

0 commit comments

Comments
 (0)