1 /* SCTP kernel Implementation
2  * (C) Copyright IBM Corp. 2002, 2003
3  * Copyright (c) 1999-2001 Motorola, Inc.
4  *
5  * This file is part of the SCTP kernel Implementation
6  *
7  * The SCTP implementation is free software;
8  * you can redistribute it and/or modify it under the terms of
9  * the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * The SCTP implementation is distributed in the hope that it
14  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
15  *                 ************************
16  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17  * See the GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GNU CC; see the file COPYING.  If not, write to
21  * the Free Software Foundation, 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  *
24  * Please send any bug reports or fixes you make to the
25  * email address(es):
26  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
27  *
28  * Or submit a bug report through the following website:
29  *    http://www.sf.net/projects/lksctp
30  *
31  * Written or modified by:
32  *    Sridhar Samudrala		<sri@us.ibm.com>
33  *
34  * Any bugs reported to us we will try to fix... any fixes shared will
35  * be incorporated into the next SCTP release.
36  */
37 
38 /* This is a kernel test to verify
39  * 1. MSG_EOR flag is set correctly when a single message is read using multiple
40  *    recvmsg() calls.
41  * 2. MSG_PEEK support.
42  */
43 
44 #include <stdio.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <fcntl.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <sys/uio.h>
52 #include <netinet/in.h>
53 #include <errno.h>
54 #include <netinet/sctp.h>
55 #include <sctputil.h>
56 
57 char *TCID = __FILE__;
58 int TST_TOTAL = 2;
59 int TST_CNT = 0;
60 
61 int
main(int argc,char * argv[])62 main(int argc, char *argv[])
63 {
64 	int svr_sk, clt_sk;
65 	struct sockaddr_in svr_loop, clt_loop;
66 	struct iovec iov, out_iov;
67 	struct msghdr inmessage, outmessage;
68 	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
69 	int error, msglen, i;
70 	char *big_buffer;
71 	void *msg_buf;
72 
73         /* Rather than fflush() throughout the code, set stdout to
74 	 * be unbuffered.
75 	 */
76 	setvbuf(stdout, NULL, _IONBF, 0);
77 
78 	/* Initialize the server and client addresses. */
79 	svr_loop.sin_family = AF_INET;
80 	svr_loop.sin_addr.s_addr = SCTP_IP_LOOPBACK;
81 	svr_loop.sin_port = htons(SCTP_TESTPORT_1);
82 	clt_loop.sin_family = AF_INET;
83 	clt_loop.sin_addr.s_addr = SCTP_IP_LOOPBACK;
84 	clt_loop.sin_port = htons(SCTP_TESTPORT_2);
85 
86 	/* Create and bind the server socket.  */
87         svr_sk = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
88 	test_bind(svr_sk, (struct sockaddr *)&svr_loop, sizeof(svr_loop));
89 
90 	/* Mark server socket as being able to accept new associations.  */
91 	test_listen(svr_sk, 1);
92 
93 	/* Create and bind the client sockets.  */
94 	clt_sk = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
95 	test_bind(clt_sk, (struct sockaddr *)&clt_loop, sizeof(clt_loop));
96 
97 	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
98 	test_enable_assoc_change(svr_sk);
99 	test_enable_assoc_change(clt_sk);
100 
101 	/* Send a message. This will create the association.  */
102 	memset(&outmessage, 0, sizeof(outmessage));
103 	outmessage.msg_name = &svr_loop;
104 	outmessage.msg_namelen = sizeof(svr_loop);
105 	outmessage.msg_iov = &out_iov;
106 	outmessage.msg_iovlen = 1;
107 	msg_buf = test_build_msg(30000);
108 	outmessage.msg_iov->iov_base = msg_buf;
109 	outmessage.msg_iov->iov_len = 30000;
110 	test_sendmsg(clt_sk, &outmessage, 0, 30000);
111 
112 	/* Initialize inmessage for all receives. */
113 	big_buffer = test_malloc(REALLY_BIG);
114         memset(&inmessage, 0, sizeof(inmessage));
115 	iov.iov_base = big_buffer;
116 	iov.iov_len = 2000;
117 	inmessage.msg_iov = &iov;
118 	inmessage.msg_iovlen = 1;
119 	inmessage.msg_control = incmsg;
120 
121 	/* Receive COMM_UP on clt_sk. */
122 	inmessage.msg_controllen = sizeof(incmsg);
123 	error = test_recvmsg(clt_sk, &inmessage, 0);
124 	test_check_msg_notification(&inmessage, error,
125 				    sizeof(struct sctp_assoc_change),
126 				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
127 
128 	/* Receive COMM_UP on svr_sk. */
129 	inmessage.msg_controllen = sizeof(incmsg);
130 	error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
131 	test_check_msg_notification(&inmessage, error,
132 				    sizeof(struct sctp_assoc_change),
133 				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
134 
135 	/* Read the 30000 byte message using multiple recvmsg() calls in a
136 	 * loop with 2000 bytes per read.
137 	 */
138 	for (i = 0, msglen = 30000; i < 15; i++, msglen-=2000) {
139 		iov.iov_len = REALLY_BIG;
140 		inmessage.msg_controllen = sizeof(incmsg);
141 		error = test_recvmsg(svr_sk, &inmessage, MSG_PEEK);
142 		test_check_msg_data(&inmessage, error, msglen,
143 				    MSG_EOR, 0, 0);
144 
145 		iov.iov_len = 2000;
146 		inmessage.msg_controllen = sizeof(incmsg);
147 		error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
148 		test_check_msg_data(&inmessage, error, 2000,
149 				    ((i==14)?MSG_EOR:0), 0, 0);
150 	}
151 
152 	tst_resm(TPASS, "recvmsg with MSG_PEEK flag");
153 	tst_resm(TPASS, "MSG_EOR in msg_flags set correctly");
154 
155 	close(svr_sk);
156 	close(clt_sk);
157 
158         /* Indicate successful completion.  */
159         return 0;
160 }
161