Skip to content

Commit

Permalink
Add network interface name validation api (#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
waahm7 authored Oct 16, 2024
1 parent 5227c06 commit e363740
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/aws/io/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ AWS_IO_API int aws_socket_validate_port_for_bind(uint32_t port, enum aws_socket_
*/
AWS_IO_API void aws_socket_endpoint_init_local_address_for_test(struct aws_socket_endpoint *endpoint);

/**
* Validates whether the network interface name is valid. On Windows, it will always return false since we don't support
* network_interface_name on Windows */
AWS_IO_API bool aws_is_network_interface_name_valid(const char *interface_name);

AWS_EXTERN_C_END
AWS_POP_SANE_WARNING_LEVEL

Expand Down
8 changes: 8 additions & 0 deletions source/posix/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -2008,3 +2008,11 @@ void aws_socket_endpoint_init_local_address_for_test(struct aws_socket_endpoint
AWS_FATAL_ASSERT(aws_uuid_to_str(&uuid, &uuid_buf) == AWS_OP_SUCCESS);
snprintf(endpoint->address, sizeof(endpoint->address), "testsock" PRInSTR ".sock", AWS_BYTE_BUF_PRI(uuid_buf));
}

bool aws_is_network_interface_name_valid(const char *interface_name) {
if (if_nametoindex(interface_name) == 0) {
AWS_LOGF_ERROR(AWS_LS_IO_SOCKET, "network_interface_name(%s) is invalid with errno: %d", interface_name, errno);
return false;
}
return true;
}
6 changes: 6 additions & 0 deletions source/windows/iocp/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -3272,3 +3272,9 @@ void aws_socket_endpoint_init_local_address_for_test(struct aws_socket_endpoint
AWS_FATAL_ASSERT(aws_uuid_to_str(&uuid, &uuid_buf) == AWS_OP_SUCCESS);
snprintf(endpoint->address, sizeof(endpoint->address), "\\\\.\\pipe\\testsock" PRInSTR, AWS_BYTE_BUF_PRI(uuid_buf));
}

bool aws_is_network_interface_name_valid(const char *interface_name) {
(void)interface_name;
AWS_LOGF_ERROR(AWS_LS_IO_SOCKET, "network_interface_names are not supported on Windows");
return false;
}
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ add_net_test_case(tcp_socket_communication)
add_net_test_case(udp_socket_communication)
add_net_test_case(test_socket_with_bind_to_interface)
add_net_test_case(test_socket_with_bind_to_invalid_interface)
add_net_test_case(test_is_network_interface_name_valid)
add_test_case(udp_bind_connect_communication)
add_net_test_case(connect_timeout)
add_net_test_case(connect_timeout_cancelation)
Expand Down
14 changes: 14 additions & 0 deletions tests/socket_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,20 @@ static int s_test_socket_with_bind_to_invalid_interface(struct aws_allocator *al
}
AWS_TEST_CASE(test_socket_with_bind_to_invalid_interface, s_test_socket_with_bind_to_invalid_interface)

static int s_test_is_network_interface_name_valid(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
(void)allocator;

ASSERT_FALSE(aws_is_network_interface_name_valid("invalid_name"));
#if defined(AWS_OS_LINUX)
ASSERT_TRUE(aws_is_network_interface_name_valid("lo"));
#elif !defined(AWS_OS_WINDOWS)
ASSERT_TRUE(aws_is_network_interface_name_valid("lo0"));
#endif
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_is_network_interface_name_valid, s_test_is_network_interface_name_valid)

#if defined(USE_VSOCK)
static int s_test_vsock_loopback_socket_communication(struct aws_allocator *allocator, void *ctx) {
/* Without vsock loopback it's difficult to test vsock functionality.
Expand Down

0 comments on commit e363740

Please sign in to comment.