Skip to content

Commit

Permalink
initialize socklen
Browse files Browse the repository at this point in the history
  • Loading branch information
madhurajayaraman committed Oct 14, 2024
1 parent f4cdc69 commit 2df4e05
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions starboard/nplb/posix_compliance/posix_socket_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ int PosixSocketCreateAndConnect(int server_domain,
if (*listen_socket_fd < 0) {
return -1;
}
SB_DLOG(INFO) << "Socket was created";
// set socket reuseable
const int on = 1;
result =
Expand All @@ -47,38 +46,33 @@ int PosixSocketCreateAndConnect(int server_domain,
close(*listen_socket_fd);
return -1;
}
SB_DLOG(INFO) << "Socket option reuse address set";

// bind socket with local address
sockaddr_in6 address = {};
EXPECT_TRUE(
PosixGetLocalAddressIPv4(reinterpret_cast<sockaddr*>(&address)) == 0 ||
PosixGetLocalAddressIPv6(reinterpret_cast<sockaddr*>(&address)) == 0);
address.sin6_port = htons(PosixGetPortNumberForTests());
SB_DLOG(INFO) << "About to bind";

result = bind(*listen_socket_fd, reinterpret_cast<struct sockaddr*>(&address),
sizeof(struct sockaddr_in));
if (result != 0) {
close(*listen_socket_fd);
return -1;
}
SB_DLOG(INFO) << "Socket was bound";

result = listen(*listen_socket_fd, kMaxConn);
if (result != 0) {
close(*listen_socket_fd);
return -1;
}
SB_DLOG(INFO) << "Socket is listening";

// create client socket and connect to localhost:<port>
*client_socket_fd = socket(client_domain, SOCK_STREAM, IPPROTO_TCP);
if (*client_socket_fd < 0) {
close(*listen_socket_fd);
return -1;
}
SB_DLOG(INFO) << "Socket was created pt 2";

result =
setsockopt(*client_socket_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
Expand All @@ -87,13 +81,11 @@ int PosixSocketCreateAndConnect(int server_domain,
close(*client_socket_fd);
return -1;
}
SB_DLOG(INFO) << "Client Socket was set reuse address";

result =
connect(*client_socket_fd, reinterpret_cast<struct sockaddr*>(&address),
sizeof(struct sockaddr));
if (result != 0) {
SB_DLOG(INFO) << "result of connection is not 0";
close(*listen_socket_fd);
close(*client_socket_fd);
return -1;
Expand Down Expand Up @@ -200,15 +192,15 @@ void PosixInitializePortNumberForTests() {
// Create a listening socket. Let the system choose a port for us.
int socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (socket_fd < 0) {
ADD_FAILURE() << "Socket create failed";
return errno;
ADD_FAILURE() << "Socket create failed errno: " << errno;
return;
}

int on = 1;
if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) {
ADD_FAILURE() << "Socket Set Reuse Address failed";
ADD_FAILURE() << "Socket Set Reuse Address failed, errno: " << errno;
HANDLE_EINTR(close(socket_fd));
return errno;
return;
}

// bind socket with local address
Expand All @@ -220,28 +212,29 @@ void PosixInitializePortNumberForTests() {
bind(socket_fd, reinterpret_cast<sockaddr*>(&address), sizeof(sockaddr));

if (bind_result != 0) {
ADD_FAILURE() << "Socket Bind to " << 0 << " failed: " << bind_result;
ADD_FAILURE() << "Socket Bind to 0 failed, errno: " << errno;
HANDLE_EINTR(close(socket_fd));
return errno;
return;
}

int listen_result = listen(socket_fd, kMaxConn);
if (listen_result != 0) {
ADD_FAILURE() << "Socket Listen failed: " << listen_result;
ADD_FAILURE() << "Socket Listen failed, errno: " << errno;
HANDLE_EINTR(close(socket_fd));
return errno;
return;
}

// Query which port this socket was bound to and save it to valid_port_number.
socklen_t socklen;
struct sockaddr_in addr_in = {0};
socklen_t socklen = static_cast<socklen_t>(sizeof(addr_in));
int local_add_result =
getsockname(socket_fd, reinterpret_cast<sockaddr*>(&addr_in), &socklen);

if (local_add_result < 0) {
ADD_FAILURE() << "Socket get local address failed: " << local_add_result;
ADD_FAILURE() << "Socket get local address failed: " << local_add_result
<< " Errno: " << errno;
HANDLE_EINTR(close(socket_fd));
return errno;
return;
}
port_number_for_tests = addr_in.sin_port;

Expand All @@ -254,7 +247,6 @@ int PosixGetPortNumberForTests() {
pthread_once(&valid_port_once_control, &PosixInitializePortNumberForTests);
SB_DLOG(INFO) << "PosixGetPortNumberForTests port_number_for_tests : "
<< port_number_for_tests;
assert(port_number_for_tests != 0);
return port_number_for_tests;
}

Expand Down

0 comments on commit 2df4e05

Please sign in to comment.