The helper will be used to programmatically retrieve, and pass ports in userspace and kernel selftest programs. Suggested-by: Stanislav Fomichev <sdf@xxxxxxxxxx> Signed-off-by: Aditi Ghag <aditi.ghag@xxxxxxxxxxxxx> --- tools/testing/selftests/bpf/network_helpers.c | 14 ++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 1 + 2 files changed, 15 insertions(+) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 596caa176582..4c1dc7cf7390 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -427,3 +427,17 @@ void close_netns(struct nstoken *token) close(token->orig_netns_fd); free(token); } + +int get_sock_port6(int sock_fd, __u16 *out_port) +{ + struct sockaddr_in6 addr = {}; + socklen_t addr_len = sizeof(addr); + int err; + + err = getsockname(sock_fd, (struct sockaddr *)&addr, &addr_len); + if (err < 0) + return err; + *out_port = addr.sin6_port; + + return err; +} diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index f882c691b790..2ab3b50de0b7 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -56,6 +56,7 @@ int fastopen_connect(int server_fd, const char *data, unsigned int data_len, int make_sockaddr(int family, const char *addr_str, __u16 port, struct sockaddr_storage *addr, socklen_t *len); char *ping_command(int family); +int get_sock_port6(int sock_fd, __u16 *out_port); struct nstoken; /** -- 2.34.1