1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*\
3f08c3bdfSopenharmony_ci * Basic sendmmsg() test that sends and receives messages.
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci * This test is based on source contained in the man pages for sendmmsg and
6f08c3bdfSopenharmony_ci * recvmmsg in release 4.15 of the Linux man-pages project.
7f08c3bdfSopenharmony_ci */
8f08c3bdfSopenharmony_ci
9f08c3bdfSopenharmony_ci#define _GNU_SOURCE
10f08c3bdfSopenharmony_ci#include "sendmmsg.h"
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ci#define BUFSIZE 16
13f08c3bdfSopenharmony_ci#define VLEN 2
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_cistatic int send_sockfd;
16f08c3bdfSopenharmony_cistatic int receive_sockfd;
17f08c3bdfSopenharmony_cistatic struct mmsghdr *snd_msg, *rcv_msg;
18f08c3bdfSopenharmony_cistatic struct iovec *snd1, *snd2, *rcv1, *rcv2;
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_cistatic void run(void)
21f08c3bdfSopenharmony_ci{
22f08c3bdfSopenharmony_ci	struct time64_variants *tv = &variants[tst_variant];
23f08c3bdfSopenharmony_ci	struct tst_ts timeout;
24f08c3bdfSopenharmony_ci	int retval;
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci	retval = tv->sendmmsg(send_sockfd, snd_msg, VLEN, 0);
27f08c3bdfSopenharmony_ci	if (retval < 0 || snd_msg[0].msg_len != 6 || snd_msg[1].msg_len != 6) {
28f08c3bdfSopenharmony_ci		tst_res(TFAIL | TERRNO, "sendmmsg() failed");
29f08c3bdfSopenharmony_ci		return;
30f08c3bdfSopenharmony_ci	}
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci	memset(rcv1->iov_base, 0, rcv1->iov_len);
33f08c3bdfSopenharmony_ci	memset(rcv2->iov_base, 0, rcv2->iov_len);
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	timeout.type = tv->ts_type;
36f08c3bdfSopenharmony_ci	tst_ts_set_sec(&timeout, 1);
37f08c3bdfSopenharmony_ci	tst_ts_set_nsec(&timeout, 0);
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	retval = tv->recvmmsg(receive_sockfd, rcv_msg, VLEN, 0, tst_ts_get(&timeout));
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	if (retval == -1) {
42f08c3bdfSopenharmony_ci		tst_res(TFAIL | TERRNO, "recvmmsg() failed");
43f08c3bdfSopenharmony_ci		return;
44f08c3bdfSopenharmony_ci	}
45f08c3bdfSopenharmony_ci	if (retval != 2) {
46f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Received unexpected number of messages (%d)",
47f08c3bdfSopenharmony_ci			retval);
48f08c3bdfSopenharmony_ci		return;
49f08c3bdfSopenharmony_ci	}
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci	if (memcmp(rcv1->iov_base, "onetwo", 6))
52f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Error in first received message");
53f08c3bdfSopenharmony_ci	else
54f08c3bdfSopenharmony_ci		tst_res(TPASS, "First message received successfully");
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci	if (memcmp(rcv2->iov_base, "three", 5))
57f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Error in second received message");
58f08c3bdfSopenharmony_ci	else
59f08c3bdfSopenharmony_ci		tst_res(TPASS, "Second message received successfully");
60f08c3bdfSopenharmony_ci}
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_cistatic void setup(void)
63f08c3bdfSopenharmony_ci{
64f08c3bdfSopenharmony_ci	struct sockaddr_in addr;
65f08c3bdfSopenharmony_ci	unsigned int port = TST_GET_UNUSED_PORT(AF_INET, SOCK_DGRAM);
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	send_sockfd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
68f08c3bdfSopenharmony_ci	receive_sockfd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci	addr.sin_family = AF_INET;
71f08c3bdfSopenharmony_ci	addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
72f08c3bdfSopenharmony_ci	addr.sin_port = port;
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci	SAFE_BIND(receive_sockfd, (struct sockaddr *)&addr, sizeof(addr));
75f08c3bdfSopenharmony_ci	SAFE_CONNECT(send_sockfd, (struct sockaddr *)&addr, sizeof(addr));
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci	memcpy(snd1[0].iov_base, "one", snd1[0].iov_len);
78f08c3bdfSopenharmony_ci	memcpy(snd1[1].iov_base, "two", snd1[1].iov_len);
79f08c3bdfSopenharmony_ci	memcpy(snd2->iov_base, "three3", snd2->iov_len);
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_ci	memset(snd_msg, 0, VLEN * sizeof(*snd_msg));
82f08c3bdfSopenharmony_ci	snd_msg[0].msg_hdr.msg_iov = snd1;
83f08c3bdfSopenharmony_ci	snd_msg[0].msg_hdr.msg_iovlen = 2;
84f08c3bdfSopenharmony_ci	snd_msg[1].msg_hdr.msg_iov = snd2;
85f08c3bdfSopenharmony_ci	snd_msg[1].msg_hdr.msg_iovlen = 1;
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_ci	memset(rcv_msg, 0, VLEN * sizeof(*rcv_msg));
88f08c3bdfSopenharmony_ci	rcv_msg[0].msg_hdr.msg_iov = rcv1;
89f08c3bdfSopenharmony_ci	rcv_msg[0].msg_hdr.msg_iovlen = 1;
90f08c3bdfSopenharmony_ci	rcv_msg[1].msg_hdr.msg_iov = rcv2;
91f08c3bdfSopenharmony_ci	rcv_msg[1].msg_hdr.msg_iovlen = 1;
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ci	tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
94f08c3bdfSopenharmony_ci}
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_cistatic void cleanup(void)
97f08c3bdfSopenharmony_ci{
98f08c3bdfSopenharmony_ci	if (send_sockfd > 0)
99f08c3bdfSopenharmony_ci		SAFE_CLOSE(send_sockfd);
100f08c3bdfSopenharmony_ci	if (receive_sockfd > 0)
101f08c3bdfSopenharmony_ci		SAFE_CLOSE(receive_sockfd);
102f08c3bdfSopenharmony_ci}
103f08c3bdfSopenharmony_ci
104f08c3bdfSopenharmony_cistatic struct tst_test test = {
105f08c3bdfSopenharmony_ci	.test_all = run,
106f08c3bdfSopenharmony_ci	.setup = setup,
107f08c3bdfSopenharmony_ci	.cleanup = cleanup,
108f08c3bdfSopenharmony_ci	.test_variants = ARRAY_SIZE(variants),
109f08c3bdfSopenharmony_ci	.bufs = (struct tst_buffers []) {
110f08c3bdfSopenharmony_ci		{&snd1, .iov_sizes = (int[]){3, 3, -1}},
111f08c3bdfSopenharmony_ci		{&snd2, .iov_sizes = (int[]){6, -1}},
112f08c3bdfSopenharmony_ci		{&rcv1, .iov_sizes = (int[]){6, -1}},
113f08c3bdfSopenharmony_ci		{&rcv2, .iov_sizes = (int[]){5, -1}},
114f08c3bdfSopenharmony_ci		{&snd_msg, .size = VLEN * sizeof(*snd_msg)},
115f08c3bdfSopenharmony_ci		{&rcv_msg, .size = VLEN * sizeof(*rcv_msg)},
116f08c3bdfSopenharmony_ci		{},
117f08c3bdfSopenharmony_ci	}
118f08c3bdfSopenharmony_ci};
119