diff --git a/starboard/nplb/posix_compliance/posix_socket_helpers.cc b/starboard/nplb/posix_compliance/posix_socket_helpers.cc index fa2a5fbf7ef..bcaf4e72ff9 100644 --- a/starboard/nplb/posix_compliance/posix_socket_helpers.cc +++ b/starboard/nplb/posix_compliance/posix_socket_helpers.cc @@ -17,6 +17,7 @@ #include #include +#include "net/base/net_errors.h" #include "starboard/shared/posix/handle_eintr.h" #include "starboard/shared/posix/socket_internal.h" #include "starboard/thread.h" @@ -52,7 +53,8 @@ int PosixSocketCreateAndConnect(int server_domain, EXPECT_TRUE( PosixGetLocalAddressIPv4(reinterpret_cast(&address)) == 0 || PosixGetLocalAddressIPv6(reinterpret_cast(&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(&address), sizeof(struct sockaddr_in)); @@ -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 @@ -215,19 +217,26 @@ 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(&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; @@ -235,6 +244,7 @@ void PosixInitializePortNumberForTests() { } int PosixGetPortNumberForTests() { + SB_DLOG(INFO) << "In function"; pthread_once(&valid_port_once_control, &PosixInitializePortNumberForTests); return port_number_for_tests; } diff --git a/starboard/nplb/socket_get_local_address_test.cc b/starboard/nplb/socket_get_local_address_test.cc index 88b3dc9975f..9848c667b57 100644 --- a/starboard/nplb/socket_get_local_address_test.cc +++ b/starboard/nplb/socket_get_local_address_test.cc @@ -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)); diff --git a/starboard/nplb/socket_helpers.cc b/starboard/nplb/socket_helpers.cc index 603efeab885..7363f4c9113 100644 --- a/starboard/nplb/socket_helpers.cc +++ b/starboard/nplb/socket_helpers.cc @@ -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); @@ -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) { @@ -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); @@ -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; diff --git a/starboard/nplb/socket_wrapper_test.cc b/starboard/nplb/socket_wrapper_test.cc index 295d05bb66f..4179a457a9f 100644 --- a/starboard/nplb/socket_wrapper_test.cc +++ b/starboard/nplb/socket_wrapper_test.cc @@ -37,7 +37,7 @@ TEST_P(PairSbSocketWrapperTest, SunnyDay) { std::unique_ptr trio = CreateAndConnectWrapped(GetServerAddressType(), GetClientAddressType(), - GetPortNumberForTests(), kSocketTimeout); + PosixGetPortNumberForTests(), kSocketTimeout); ASSERT_TRUE(trio); ASSERT_TRUE(trio->server_socket); ASSERT_TRUE(trio->server_socket->IsValid());