Message ID | 96f04aa5677d628ad5eae1981565b24bf94c9d77.1720515893.git.tanggeliang@kylinos.cn (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | use network helpers, part 8 | expand |
On 7/9/24 2:16 AM, Geliang Tang wrote: > From: Geliang Tang <tanggeliang@kylinos.cn> > > 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@kylinos.cn> > --- > 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) > {
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,