xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/recvmsg/recvmsg02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2016 Cyril Hrubis <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2016 Michal Kubecek <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker  * This is a regression test for kernel commit:
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * 197c949e7798  udp: properly support MSG_PEEK with truncated buffers
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * NOTE: The testcase will hang on upatched stable kernel.
13*49cdfc7eSAndroid Build Coastguard Worker  */
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
24*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/socket.h"
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker static const char msg[] = "Michael Gilfix was here\341\210\264\r\n";
27*49cdfc7eSAndroid Build Coastguard Worker static const unsigned msglen = ARRAY_SIZE(msg) - 1;
28*49cdfc7eSAndroid Build Coastguard Worker static unsigned char buff[25];
29*49cdfc7eSAndroid Build Coastguard Worker static const int bufflen = ARRAY_SIZE(buff);
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker static int sdr, sdw;
32*49cdfc7eSAndroid Build Coastguard Worker 
verify_recvmsg(void)33*49cdfc7eSAndroid Build Coastguard Worker static void verify_recvmsg(void)
34*49cdfc7eSAndroid Build Coastguard Worker {
35*49cdfc7eSAndroid Build Coastguard Worker 	struct sockaddr_in6 addr_init = {
36*49cdfc7eSAndroid Build Coastguard Worker 		.sin6_family	= AF_INET6,
37*49cdfc7eSAndroid Build Coastguard Worker 		.sin6_port	= htons(0),
38*49cdfc7eSAndroid Build Coastguard Worker 		.sin6_addr	= IN6ADDR_LOOPBACK_INIT,
39*49cdfc7eSAndroid Build Coastguard Worker 	};
40*49cdfc7eSAndroid Build Coastguard Worker 	struct sockaddr_in6 addr_r, addr_w, addr_f;
41*49cdfc7eSAndroid Build Coastguard Worker 	socklen_t addrlen_r, addrlen_w;
42*49cdfc7eSAndroid Build Coastguard Worker 	struct iovec iov = {
43*49cdfc7eSAndroid Build Coastguard Worker 		.iov_base	= buff,
44*49cdfc7eSAndroid Build Coastguard Worker 		.iov_len	= sizeof(buff),
45*49cdfc7eSAndroid Build Coastguard Worker 	};
46*49cdfc7eSAndroid Build Coastguard Worker 	struct msghdr msghdr = {
47*49cdfc7eSAndroid Build Coastguard Worker 		.msg_name	= &addr_f,
48*49cdfc7eSAndroid Build Coastguard Worker 		.msg_namelen	= sizeof(addr_f),
49*49cdfc7eSAndroid Build Coastguard Worker 		.msg_iov	= &iov,
50*49cdfc7eSAndroid Build Coastguard Worker 		.msg_iovlen	= 1,
51*49cdfc7eSAndroid Build Coastguard Worker 		.msg_control	= NULL,
52*49cdfc7eSAndroid Build Coastguard Worker 		.msg_controllen	= 0,
53*49cdfc7eSAndroid Build Coastguard Worker 		.msg_flags	= 0,
54*49cdfc7eSAndroid Build Coastguard Worker 	};
55*49cdfc7eSAndroid Build Coastguard Worker 	int R;
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 	sdr = SAFE_SOCKET(PF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_IP);
58*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_BIND(sdr, (struct sockaddr*)&addr_init, sizeof(addr_init));
59*49cdfc7eSAndroid Build Coastguard Worker 	addrlen_r = sizeof(addr_r);
60*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_GETSOCKNAME(sdr, (struct sockaddr*)&addr_r, &addrlen_r);
61*49cdfc7eSAndroid Build Coastguard Worker 	sdw = SAFE_SOCKET(PF_INET6, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_IP);
62*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_BIND(sdw, (struct sockaddr*)&addr_init, sizeof(addr_init));
63*49cdfc7eSAndroid Build Coastguard Worker 	addrlen_w = sizeof(addr_w);
64*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_GETSOCKNAME(sdw, (struct sockaddr*)&addr_w, &addrlen_w);
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 	R = sendto(sdw, msg, msglen, 0, (struct sockaddr*)&addr_r, addrlen_r);
67*49cdfc7eSAndroid Build Coastguard Worker 	if (R < 0)
68*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "sendto()");
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker 	R = recvmsg(sdr, &msghdr, MSG_PEEK);
71*49cdfc7eSAndroid Build Coastguard Worker 	if (R < 0) {
72*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TERRNO, "recvmsg(..., MSG_PEEK)");
73*49cdfc7eSAndroid Build Coastguard Worker 		return;
74*49cdfc7eSAndroid Build Coastguard Worker 	}
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "received %d bytes", R);
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker 	if ((R == bufflen) && !memcmp(msg, buff, R))
79*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "recvmsg(..., MSG_PEEK) works fine");
80*49cdfc7eSAndroid Build Coastguard Worker 	else
81*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "recvmsg(..., MSG_PEEK) failed");
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(sdw);
84*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(sdr);
85*49cdfc7eSAndroid Build Coastguard Worker }
86*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)87*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
88*49cdfc7eSAndroid Build Coastguard Worker {
89*49cdfc7eSAndroid Build Coastguard Worker 	if (sdw > 0)
90*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(sdw);
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 	if (sdr > 0)
93*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(sdr);
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
97*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_recvmsg,
98*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
99*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
100*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "197c949e7798"},
101*49cdfc7eSAndroid Build Coastguard Worker 		{}
102*49cdfc7eSAndroid Build Coastguard Worker 	}
103*49cdfc7eSAndroid Build Coastguard Worker };
104