[PATCH v1 bpf-next 4/8] selftests/bpf: Add recv_msg_from_client to network helpers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Copy recv_msg_from_client helper from test_sock_addr self test in order
to use it in the sock_addr_kern test program.

Signed-off-by: Jordan Rife <jrife@xxxxxxxxxx>
---
 tools/testing/selftests/bpf/network_helpers.c | 31 +++++++++++++++++++
 tools/testing/selftests/bpf/network_helpers.h |  1 +
 2 files changed, 32 insertions(+)

diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c
index 8eae4afe3ec3d..ef0a94d9e9d5c 100644
--- a/tools/testing/selftests/bpf/network_helpers.c
+++ b/tools/testing/selftests/bpf/network_helpers.c
@@ -498,6 +498,37 @@ int get_socket_local_port(int sock_fd)
 	return -1;
 }
 
+int recvmsg_from_client(int sockfd, struct sockaddr_storage *src_addr)
+{
+	struct timeval tv;
+	struct msghdr hdr;
+	struct iovec iov;
+	char data[64];
+	fd_set rfds;
+
+	FD_ZERO(&rfds);
+	FD_SET(sockfd, &rfds);
+
+	tv.tv_sec = 2;
+	tv.tv_usec = 0;
+
+	if (select(sockfd + 1, &rfds, NULL, NULL, &tv) <= 0 ||
+	    !FD_ISSET(sockfd, &rfds))
+		return -1;
+
+	memset(&iov, 0, sizeof(iov));
+	iov.iov_base = data;
+	iov.iov_len = sizeof(data);
+
+	memset(&hdr, 0, sizeof(hdr));
+	hdr.msg_name = src_addr;
+	hdr.msg_namelen = sizeof(struct sockaddr_storage);
+	hdr.msg_iov = &iov;
+	hdr.msg_iovlen = 1;
+
+	return recvmsg(sockfd, &hdr, 0);
+}
+
 int cmp_addr(const struct sockaddr_storage *addr1, socklen_t addr1_len,
 	     const struct sockaddr_storage *addr2, socklen_t addr2_len,
 	     bool cmp_port)
diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h
index a4a458c858d59..f88f2470e3fcb 100644
--- a/tools/testing/selftests/bpf/network_helpers.h
+++ b/tools/testing/selftests/bpf/network_helpers.h
@@ -61,6 +61,7 @@ int make_sockaddr(int family, const char *addr_str, __u16 port,
 		  struct sockaddr_storage *addr, socklen_t *len);
 char *ping_command(int family);
 int get_socket_local_port(int sock_fd);
+int recvmsg_from_client(int sockfd, struct sockaddr_storage *src_addr);
 int cmp_addr(const struct sockaddr_storage *addr1, socklen_t addr1_len,
 	     const struct sockaddr_storage *addr2, socklen_t addr2_len,
 	     bool cmp_port);
-- 
2.44.0.478.gd926399ef9-goog





[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux