Skip to content

Commit 2b42d68

Browse files
Geliang TangMartin KaFai Lau
authored andcommitted
selftests/bpf: Add connect_to_addr_str helper
Similar to connect_to_addr() helper for connecting to a server with the given sockaddr_storage type address, this patch adds a new helper named connect_to_addr_str() for connecting to a server with the given string type address "addr_str", together with its "family" and "port" as other parameters of connect_to_addr_str(). In connect_to_addr_str(), the parameters "family", "addr_str" and "port" are used to create a sockaddr_storage type address "addr" by invoking make_sockaddr(). Then pass this "addr" together with "addrlen", "type" and "opts" to connect_to_addr(). Suggested-by: Martin KaFai Lau <[email protected]> Signed-off-by: Geliang Tang <[email protected]> Link: https://lore.kernel.org/r/647e82170831558dbde132a7a3d86df660dba2c4.1721282219.git.tanggeliang@kylinos.cn Signed-off-by: Martin KaFai Lau <[email protected]>
1 parent 4e085e3 commit 2b42d68

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

tools/testing/selftests/bpf/network_helpers.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,21 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add
300300
return fd;
301301
}
302302

303+
int connect_to_addr_str(int family, int type, const char *addr_str, __u16 port,
304+
const struct network_helper_opts *opts)
305+
{
306+
struct sockaddr_storage addr;
307+
socklen_t addrlen;
308+
309+
if (!opts)
310+
opts = &default_opts;
311+
312+
if (make_sockaddr(family, addr_str, port, &addr, &addrlen))
313+
return -1;
314+
315+
return connect_to_addr(type, &addr, addrlen, opts);
316+
}
317+
303318
int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)
304319
{
305320
struct sockaddr_storage addr;

tools/testing/selftests/bpf/network_helpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ int client_socket(int family, int type,
6969
const struct network_helper_opts *opts);
7070
int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len,
7171
const struct network_helper_opts *opts);
72+
int connect_to_addr_str(int family, int type, const char *addr_str, __u16 port,
73+
const struct network_helper_opts *opts);
7274
int connect_to_fd(int server_fd, int timeout_ms);
7375
int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts);
7476
int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms);

0 commit comments

Comments
 (0)