diff mbox series

[bpf-next,v4,13/14] selftests/bpf: Use make_sockaddr in test_sock

Message ID 5fd898f4d55d407c2e0be2b7a3e60c93778135d2.1713262052.git.tanggeliang@kylinos.cn (mailing list archive)
State New
Headers show
Series use network helpers, part 1 | expand

Commit Message

Geliang Tang April 16, 2024, 10:13 a.m. UTC
From: Geliang Tang <tanggeliang@kylinos.cn>

This patch uses public helper make_sockaddr() exported in network_helpers.h
instead of open-coding in bind_sock() in test_sock.c. This can avoid
duplicate code.

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
 tools/testing/selftests/bpf/Makefile    |  1 +
 tools/testing/selftests/bpf/test_sock.c | 28 ++++---------------------
 2 files changed, 5 insertions(+), 24 deletions(-)
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index 53713f4495dd..017fe30f49a8 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -308,6 +308,7 @@  $(OUTPUT)/flow_dissector_load: $(TESTING_HELPERS)
 $(OUTPUT)/test_maps: $(TESTING_HELPERS)
 $(OUTPUT)/test_verifier: $(TESTING_HELPERS) $(CAP_HELPERS) $(UNPRIV_HELPERS)
 $(OUTPUT)/xsk.o: $(BPFOBJ)
+$(OUTPUT)/test_sock: $(NETWORK_HELPERS)
 
 BPFTOOL ?= $(DEFAULT_BPFTOOL)
 $(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile)    \
diff --git a/tools/testing/selftests/bpf/test_sock.c b/tools/testing/selftests/bpf/test_sock.c
index 810c3740b2cc..ac00f598ff8e 100644
--- a/tools/testing/selftests/bpf/test_sock.c
+++ b/tools/testing/selftests/bpf/test_sock.c
@@ -13,6 +13,7 @@ 
 #include <bpf/bpf.h>
 
 #include "cgroup_helpers.h"
+#include "network_helpers.h"
 #include <bpf/bpf_endian.h>
 #include "bpf_util.h"
 
@@ -414,8 +415,6 @@  static int bind_sock(int domain, int type, const char *ip,
 		     unsigned short port, unsigned short port_retry)
 {
 	struct sockaddr_storage addr;
-	struct sockaddr_in6 *addr6;
-	struct sockaddr_in *addr4;
 	int sockfd = -1;
 	socklen_t len;
 	int res = SUCCESS;
@@ -424,25 +423,8 @@  static int bind_sock(int domain, int type, const char *ip,
 	if (sockfd < 0)
 		goto err;
 
-	memset(&addr, 0, sizeof(addr));
-
-	if (domain == AF_INET) {
-		len = sizeof(struct sockaddr_in);
-		addr4 = (struct sockaddr_in *)&addr;
-		addr4->sin_family = domain;
-		addr4->sin_port = htons(port);
-		if (inet_pton(domain, ip, (void *)&addr4->sin_addr) != 1)
-			goto err;
-	} else if (domain == AF_INET6) {
-		len = sizeof(struct sockaddr_in6);
-		addr6 = (struct sockaddr_in6 *)&addr;
-		addr6->sin6_family = domain;
-		addr6->sin6_port = htons(port);
-		if (inet_pton(domain, ip, (void *)&addr6->sin6_addr) != 1)
-			goto err;
-	} else {
+	if (make_sockaddr(domain, ip, port, &addr, &len))
 		goto err;
-	}
 
 	if (bind(sockfd, (const struct sockaddr *)&addr, len) == -1) {
 		/* sys_bind() may fail for different reasons, errno has to be
@@ -458,10 +440,8 @@  static int bind_sock(int domain, int type, const char *ip,
 
 	goto out;
 retry:
-	if (domain == AF_INET)
-		addr4->sin_port = htons(port_retry);
-	else
-		addr6->sin6_port = htons(port_retry);
+	if (make_sockaddr(domain, ip, port_retry, &addr, &len))
+		goto err;
 	if (bind(sockfd, (const struct sockaddr *)&addr, len) == -1) {
 		if (errno != EPERM)
 			goto err;