Skip to content

Commit

Permalink
Debug socket create error, fix querying for local address
Browse files Browse the repository at this point in the history
  • Loading branch information
madhurajayaraman committed Oct 4, 2024
1 parent e3f03a5 commit b16b00a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
30 changes: 20 additions & 10 deletions starboard/nplb/posix_compliance/posix_socket_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <sched.h>
#include <sys/socket.h>

#include "net/base/net_errors.h"
#include "starboard/shared/posix/handle_eintr.h"
#include "starboard/shared/posix/socket_internal.h"
#include "starboard/thread.h"
Expand Down Expand Up @@ -52,7 +53,8 @@ int PosixSocketCreateAndConnect(int server_domain,
EXPECT_TRUE(
PosixGetLocalAddressIPv4(reinterpret_cast<sockaddr*>(&address)) == 0 ||
PosixGetLocalAddressIPv6(reinterpret_cast<sockaddr*>(&address)) == 0);
address.sin6_port = htons(GetPortNumberForTests());
address.sin6_port = htons(PosixGetPortNumberForTests());
SB_DLOG(INFO) << "Port Number : " << address.sin6_port;

result = bind(*listen_socket_fd, reinterpret_cast<struct sockaddr*>(&address),
sizeof(struct sockaddr_in));
Expand Down Expand Up @@ -189,19 +191,19 @@ int port_number_for_tests = 0;
pthread_once_t valid_port_once_control = PTHREAD_ONCE_INIT;

void PosixInitializePortNumberForTests() {
SB_DLOG(INFO) << "PosixInitializePortNumberForTests";
// Create a listening socket. Let the system choose a port for us.
int socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_UDP);
int socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (socket_fd < 0) {
ADD_FAILURE() << "SbSocketCreate failed";
return -1;
return errno;
}

int on = 1;
if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) {
ADD_FAILURE() << "SbSocketSetReuseAddress failed";
HANDLE_EINTR(close(socket_fd));
// TODO: what return address do we want if this fails
return -1;
return errno;
}

// bind socket with local address
Expand All @@ -215,26 +217,34 @@ void PosixInitializePortNumberForTests() {
if (bind_result != 0) {
ADD_FAILURE() << "SbSocketBind to " << 0 << " failed: " << bind_result;
HANDLE_EINTR(close(socket_fd));
// TODO: what return address do we want if this fails
return -1;
return errno;
}

int listen_result = listen(socket_fd, kMaxConn);
if (listen_result != 0) {
ADD_FAILURE() << "SbSocketListen failed: " << listen_result;
HANDLE_EINTR(close(socket_fd));
// TODO: what return address do we want if this fails
return -1;
return errno;
}

port_number_for_tests = address.sin_port;
SB_DLOG(INFO) << "About to query";
// Query which port this socket was bound to and save it to valid_port_number.
socklen_t socklen;
struct sockaddr_in addr_in = {0};
int local_add_result =
getsockname(socket_fd, reinterpret_cast<sockaddr*>(&addr_in), &socklen);

SB_DCHECK(local_add_result >= 0);
SB_DLOG(INFO) << "Port in Posix fn : " << addr_in.sin_port;
port_number_for_tests = addr_in.sin_port;

// Clean up the socket.
bool result = HANDLE_EINTR(close(socket_fd)) >= 0;
SB_DCHECK(result);
}

int PosixGetPortNumberForTests() {
SB_DLOG(INFO) << "In function";
pthread_once(&valid_port_once_control, &PosixInitializePortNumberForTests);
return port_number_for_tests;
}
Expand Down
2 changes: 1 addition & 1 deletion starboard/nplb/socket_get_local_address_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ TEST_F(SbSocketGetLocalAddressTest, SunnyDayBoundSpecified) {
}

TEST_P(PairSbSocketGetLocalAddressTest, SunnyDayConnected) {
const int kPort = GetPortNumberForTests();
const int kPort = PosixGetPortNumberForTests();
ConnectedTrio trio = CreateAndConnect(
GetServerAddressType(), GetClientAddressType(), kPort, kSocketTimeout);
ASSERT_TRUE(SbSocketIsValid(trio.server_socket));
Expand Down
11 changes: 3 additions & 8 deletions starboard/nplb/socket_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ void InitializePortNumberForTests() {
bool result = SbSocketGetLocalAddress(socket, &socket_address);
SB_DCHECK(result);
port_number_for_tests = socket_address.port;
SB_DLOG(INFO) << "Socket fn port : " << socket_address.port;

// Clean up the socket.
result = SbSocketDestroy(socket);
Expand All @@ -56,16 +57,8 @@ void InitializePortNumberForTests() {
} // namespace

int GetPortNumberForTests() {
#if defined(SB_SOCKET_OVERRIDE_PORT_FOR_TESTS)
static int incremental = 0;
if (incremental + SB_SOCKET_OVERRIDE_PORT_FOR_TESTS == 65535) {
incremental = 0;
}
return SB_SOCKET_OVERRIDE_PORT_FOR_TESTS + ++incremental;
#else
pthread_once(&valid_port_once_control, &InitializePortNumberForTests);
return port_number_for_tests;
#endif
}

SbSocket CreateServerTcpSocket(SbSocketAddressType address_type) {
Expand All @@ -92,6 +85,7 @@ SbSocket CreateBoundTcpSocket(SbSocketAddressType address_type, int port) {

SbSocketAddress address = GetUnspecifiedAddress(address_type, port);
SbSocketError result = SbSocketBind(server_socket, &address);
SB_DLOG(INFO) << "CreateBoundTCPSocket Port: " << port;
if (result != kSbSocketOk) {
ADD_FAILURE() << "SbSocketBind to " << port << " failed: " << result;
SbSocketDestroy(server_socket);
Expand All @@ -102,6 +96,7 @@ SbSocket CreateBoundTcpSocket(SbSocketAddressType address_type, int port) {
}

SbSocket CreateListeningTcpSocket(SbSocketAddressType address_type, int port) {
SB_DLOG(INFO) << "CreateListeningTcpSocket Port: " << port;
SbSocket server_socket = CreateBoundTcpSocket(address_type, port);
if (!SbSocketIsValid(server_socket)) {
return kSbSocketInvalid;
Expand Down
2 changes: 1 addition & 1 deletion starboard/nplb/socket_wrapper_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ TEST_P(PairSbSocketWrapperTest, SunnyDay) {

std::unique_ptr<ConnectedTrioWrapped> trio =
CreateAndConnectWrapped(GetServerAddressType(), GetClientAddressType(),
GetPortNumberForTests(), kSocketTimeout);
PosixGetPortNumberForTests(), kSocketTimeout);
ASSERT_TRUE(trio);
ASSERT_TRUE(trio->server_socket);
ASSERT_TRUE(trio->server_socket->IsValid());
Expand Down

0 comments on commit b16b00a

Please sign in to comment.