diff mbox series

[bpf-next,05/15] selftests/bpf: Add send_to_addr_str helper

Message ID 7a909bf0164987f1b92f505a9dfb85509c57bf9d.1721771340.git.tanggeliang@kylinos.cn
State New
Headers show
Series [bpf-next,01/15] selftests/bpf: Use start_server_str in skc_to_unix_sock | expand

Commit Message

Geliang Tang July 23, 2024, 10:22 p.m. UTC
From: Geliang Tang <tanggeliang@kylinos.cn>

Similar to network helper connect_to_addr_str(), this patch adds a new
helper send_to_addr_str(). It uses make_sockaddr() to make a
sockaddr_storage type address from the "family", "addr_str" and "port"
parameters and passes it to send_to_addr().

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
 tools/testing/selftests/bpf/network_helpers.c | 16 ++++++++++++++++
 tools/testing/selftests/bpf/network_helpers.h |  3 +++
 2 files changed, 19 insertions(+)
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c
index 311c3a559414..a6c158718267 100644
--- a/tools/testing/selftests/bpf/network_helpers.c
+++ b/tools/testing/selftests/bpf/network_helpers.c
@@ -229,6 +229,22 @@  static int send_to_addr(int type, const void *data, size_t datalen, int flags,
 	return fd;
 }
 
+int send_to_addr_str(int family, int type, const char *addr_str, __u16 port,
+		     const void *data, size_t datalen, int flags,
+		     const struct network_helper_opts *opts)
+{
+	struct sockaddr_storage addr;
+	socklen_t addrlen;
+
+	if (!opts)
+		opts = &default_opts;
+
+	if (make_sockaddr(family, addr_str, port, &addr, &addrlen))
+		return -1;
+
+	return send_to_addr(type, data, datalen, flags, &addr, addrlen, opts);
+}
+
 int send_to_fd_opts(int server_fd, const void *data, size_t datalen, int flags,
 		    const struct network_helper_opts *opts)
 {
diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h
index 19210a234bc1..ebed1fb1bd49 100644
--- a/tools/testing/selftests/bpf/network_helpers.h
+++ b/tools/testing/selftests/bpf/network_helpers.h
@@ -76,6 +76,9 @@  int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts);
 int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms);
 int send_to_fd_opts(int server_fd, const void *data, size_t datalen, int flags,
 		    const struct network_helper_opts *opts);
+int send_to_addr_str(int family, int type, const char *addr_str, __u16 port,
+		     const void *data, size_t datalen, int flags,
+		     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,