From: Geliang Tang <tanggeliang@xxxxxxxxxx> Similar to connect_fd_to_fd() helper to connect from a client fd to a server fd, this patch adds a new helper connect_fd_to_addr_str() to connect from a client fd to a server address. It accepts the server address string "addr_str", together with the server family, type and port, as parameters instead of using a "server_fd" like connect_fd_to_fd(). Signed-off-by: Geliang Tang <tanggeliang@xxxxxxxxxx> --- tools/testing/selftests/bpf/network_helpers.c | 21 +++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 3 +++ 2 files changed, 24 insertions(+) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index e0cba4178e41..9758e707b859 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -388,6 +388,27 @@ int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms) return 0; } +int connect_fd_to_addr_str(int client_fd, int family, int type, + const char *addr_str, __u16 port, + const struct network_helper_opts *opts) +{ + struct sockaddr_storage addr; + socklen_t len; + + if (!opts) + opts = &default_opts; + + if (settimeo(client_fd, opts->timeout_ms)) + return -1; + + if (make_sockaddr(family, addr_str, port, &addr, &len)) { + log_err("Failed to make server addr"); + return -1; + } + + return connect_fd_to_addr(client_fd, &addr, len, false); +} + int make_sockaddr(int family, const char *addr_str, __u16 port, struct sockaddr_storage *addr, socklen_t *len) { diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 4f26bfc2dbf5..43526271366f 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -67,6 +67,9 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len int connect_to_fd(int server_fd, int timeout_ms); int connect_to_fd_opts(int server_fd, int type, const struct network_helper_opts *opts); int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms); +int connect_fd_to_addr_str(int client_fd, int family, int type, + const char *addr_str, __u16 port, + const struct network_helper_opts *opts); int fastopen_connect(int server_fd, const char *data, unsigned int data_len, int timeout_ms); int make_sockaddr(int family, const char *addr_str, __u16 port, -- 2.43.0