On 7/9/24 2:16 AM, Geliang Tang wrote:
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,
Similar to the comment in the earlier revision on the existing
connect_to_fd_opts. The "int type" is redundant of "int client_fd".
and where is the "int type" arg actually used in this new function?
Beside, is it more useful for patch 8 to add connect_to_addr_str() which calls
socket()/client_socket(), connect(), and then return the client_fd instead?
Something like this?
int connect_to_addr_str(int family, int type, const char *addr_str, __u16 port,
const struct network_helper_opts *opts)
Patch 1-6 is applied with the mentioned minor changes. Thanks.
+ 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)
{