• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SCTP kernel Implementation
2  * Copyright (c) 2003 Hewlett-Packard Development Company, L.P
3  * (C) Copyright IBM Corp. 2004
4  *
5  * This file has test cases to test the sendmsg() call for 1-1 style sockets
6  *
7  * TEST1: Bad socket descriptor
8  * TEST2: Invalid socket
9  * TEST3: On a listening socket
10  * TEST4: Invalid iovec pointer
11  * TEST5: Invalid iovec length
12  * TEST6: Invalid msghdr pointer
13  * TEST7: Invalid sinfo flags
14  * TEST8: SCTP_EOF flag set
15  * TEST9: SCTP_ABORT flag set
16  * TEST10: On a closed association
17  *
18  * TEST11: Sending data from server socket to client socket
19  * TEST12: Sending data from client socket to server socket
20  * TEST13: Sending data from unconnected client to server
21  * TEST14: Sending a message on SHUT_RD socket
22  *
23  * The SCTP implementation is free software;
24  * you can redistribute it and/or modify it under the terms of
25  * the GNU General Public License as published by
26  * the Free Software Foundation; either version 2, or (at your option)
27  * any later version.
28  *
29  * The SCTP implementation is distributed in the hope that it
30  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
31  *                 ************************
32  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
33  * See the GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with GNU CC; see the file COPYING.  If not, write to
37  * the Free Software Foundation, 59 Temple Place - Suite 330,
38  * Boston, MA 02111-1307, USA.
39  *
40  * Please send any bug reports or fixes you make to the
41  * email address(es):
42  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
43  *
44  * Or submit a bug report through the following website:
45  *    http://www.sf.net/projects/lksctp
46  *
47  * Any bugs reported given to us we will try to fix... any fixes shared will
48  * be incorporated into the next SCTP release
49  *
50  */
51 
52 #include <stdio.h>
53 #include <unistd.h>
54 #include <fcntl.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <sys/types.h>
58 #include <sys/socket.h>
59 #include <netinet/in.h>         /* for sockaddr_in */
60 #include <arpa/inet.h>
61 #include <errno.h>
62 #include <netinet/sctp.h>
63 #include <sys/uio.h>
64 #include <linux/socket.h>
65 #include <sctputil.h>
66 #include "tst_kernel.h"
67 
68 char *TCID = __FILE__;
69 int TST_TOTAL = 14;
70 int TST_CNT = 0;
71 
72 int
main(void)73 main(void)
74 {
75         socklen_t len;
76 	int msg_count;
77 	int sk,sk1,pf_class,lstn_sk,acpt_sk,acpt1_sk, flag;
78 	struct msghdr outmessage;
79         char *message = "hello, world!\n";
80 	struct sctp_sndrcvinfo *sinfo;
81         int count;
82 	char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
83 	struct cmsghdr *cmsg;
84         struct iovec out_iov;
85 	struct msghdr inmessage;
86 	char * buffer_rcv;
87         struct sockaddr_in conn_addr,lstn_addr,svr_addr;
88         struct iovec iov_rcv;
89 	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
90 	int fd, err_no = 0;
91 	char filename[21];
92 
93 	if (tst_check_driver("sctp"))
94 		tst_brkm(TCONF, tst_exit, "sctp driver not available");
95 
96 	/* Rather than fflush() throughout the code, set stdout to
97          * be unbuffered.
98          */
99         setvbuf(stdout, NULL, _IONBF, 0);
100         setvbuf(stderr, NULL, _IONBF, 0);
101 
102         pf_class = PF_INET;
103 
104         sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
105 
106         sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
107 
108         lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
109 
110 	conn_addr.sin_family = AF_INET;
111         conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
112         conn_addr.sin_port = htons(SCTP_TESTPORT_1);
113 
114 	lstn_addr.sin_family = AF_INET;
115         lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
116         lstn_addr.sin_port = htons(SCTP_TESTPORT_1);
117 
118 	/*Binding the listen socket*/
119         test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));
120 
121         /*Listening the socket*/
122         test_listen(lstn_sk, 10);
123 
124 	len = sizeof(struct sockaddr_in);
125 
126 	test_connect(sk, (struct sockaddr *) &conn_addr, len);
127 
128 	acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
129 
130 	memset(&outmessage, 0, sizeof(outmessage));
131         outmessage.msg_name = &conn_addr;
132         outmessage.msg_namelen = sizeof(conn_addr);
133         outmessage.msg_iov = &out_iov;
134         outmessage.msg_iovlen = 1;
135         outmessage.msg_control = outcmsg;
136         outmessage.msg_controllen = sizeof(outcmsg);
137         outmessage.msg_flags = 0;
138 
139 	cmsg = CMSG_FIRSTHDR(&outmessage);
140         cmsg->cmsg_level = IPPROTO_SCTP;
141         cmsg->cmsg_type = SCTP_SNDRCV;
142         cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
143         outmessage.msg_controllen = cmsg->cmsg_len;
144 	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
145         memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
146 
147         outmessage.msg_iov->iov_base = message;
148         outmessage.msg_iov->iov_len = strlen(message) + 1;
149 
150 	flag = MSG_NOSIGNAL;
151 	/*sendmsg () TEST1: Bad socket descriptor, EBADF Expected error*/
152 	count = sendmsg(-1, &outmessage, flag);
153 	if (count != -1 || errno != EBADF)
154 		tst_brkm(TBROK, tst_exit, "sendmsg with a bad socket "
155 			 "descriptor count:%d, errno:%d", count, errno);
156 
157 	tst_resm(TPASS, "sendmsg() with a bad socket descriptor - EBADF");
158 
159 	/*sendmsg () TEST2: Invalid socket, ENOTSOCK Expected error*/
160 	strcpy(filename, "/tmp/sctptest.XXXXXX");
161 	fd = mkstemp(filename);
162 	if (fd == -1)
163 		tst_brkm(TBROK, tst_exit, "Failed to mkstemp %s: %s",
164 				filename, strerror(errno));
165 	count = sendmsg(fd, &outmessage, flag);
166 	if (count == -1)
167 		err_no = errno;
168 	close(fd);
169 	unlink(filename);
170 	if (count != -1 || err_no != ENOTSOCK)
171 		tst_brkm(TBROK, tst_exit, "sendmsg with invalid socket "
172 			 "count:%d, errno:%d", count, err_no);
173 
174 	tst_resm(TPASS, "sendmsg() with invalid socket - ENOTSOCK");
175 
176 	/*sendmsg () TEST3: sendmsg on listening socket, EPIPE Expected error*/
177 	count = sendmsg(lstn_sk, &outmessage, flag);
178 	if (count != -1 || errno != EPIPE)
179 		tst_brkm(TBROK, tst_exit, "sendmsg on a listening socket "
180 			 "count:%d, errno:%d", count, errno);
181 
182 	tst_resm(TPASS, "sendmsg() on a listening socket - EPIPE");
183 
184 	/*sendmsg () TEST4: Invalid iovec pointer EFAULT, Expected error*/
185 	outmessage.msg_iov = (struct iovec *)-1;
186 	count = sendmsg(sk, &outmessage, flag);
187 	if (count != -1 || errno != EFAULT)
188 		tst_brkm(TBROK, tst_exit, "sendmsg with invalid iovec "
189 			 "pointer count:%d, errno:%d", count, errno);
190 
191 	tst_resm(TPASS, "sendmsg() with invalid iovec ptr - EFAULT");
192 
193 	outmessage.msg_iov = &out_iov;
194 
195 	/*sendmsg () TEST5: Invalid iovec count EINVAL, Expected error*/
196         outmessage.msg_iovlen = 0;
197 	count = sendmsg(sk, &outmessage, flag);
198 	if (count != -1 || errno != EINVAL)
199 		tst_brkm(TBROK, tst_exit, "sendmsg with invalid iovec "
200 			 "length count:%d, errno:%d", count, errno);
201 
202 	tst_resm(TPASS, "sendmsg() with invalid iovec length - EINVAL");
203 
204 	outmessage.msg_iovlen = 1;
205 
206 	/*sendmsg () TEST6: Invalid msghdr pointer EFAULT, Expected error*/
207 	count = sendmsg(sk, (struct msghdr *)-1, flag);
208 	if (count != -1 || errno != EFAULT)
209 		tst_brkm(TBROK, tst_exit, "sendmsg with invalid msghdr "
210 			 "pointer count:%d, errno:%d", count, errno);
211 
212 	tst_resm(TPASS, "sendmsg() with invalid msghdr ptr - EFAULT");
213 
214 	/*sendmsg () TEST7: Invalid sinfo flag EINVAL, Expected error*/
215 	sinfo->sinfo_flags = 999;
216 	count = sendmsg(sk, &outmessage, -1);
217 	if (count != -1 || errno != EINVAL)
218 		tst_brkm(TBROK, tst_exit, "sendmsg with invalid sinfo "
219 			 "flags count:%d, errno:%d", count, errno);
220 
221 	tst_resm(TPASS, "sendmsg() with invalid sinfo flags - EINVAL");
222 
223 	/*sendmsg () TEST8: SCTP_EOF flag EINVAL, Expected error*/
224 	sinfo->sinfo_flags = SCTP_EOF;
225 	count = sendmsg(sk, &outmessage, flag);
226 	if (count != -1 || errno != EINVAL)
227 		tst_brkm(TBROK, tst_exit, "sendmsg with SCTP_EOF flag "
228 			 "count:%d, errno:%d", count, errno);
229 
230 	tst_resm(TPASS, "sendmsg() with SCTP_EOF flag - EINVAL");
231 
232 	/*sendmsg () TEST9: SCTP_ABORT flag EINVAL, Expected error*/
233 	sinfo->sinfo_flags = SCTP_ABORT;
234 	count = sendmsg(sk, &outmessage, flag);
235 	if (count != -1 || errno != EINVAL)
236 		tst_brkm(TBROK, tst_exit, "sendmsg with SCTP_ABORT flag "
237 			 "count:%d, errno:%d", count, errno);
238 
239 	tst_resm(TPASS, "sendmsg() with SCTP_ABORT flag - EINVAL");
240 
241 	sinfo->sinfo_flags = 0;
242 
243 	test_connect(sk1, (struct sockaddr *) &lstn_addr, len);
244 
245 	test_sendmsg(sk1, &outmessage, flag, strlen(message)+1);
246 
247 	close(sk1);
248 	acpt1_sk = test_accept(lstn_sk, (struct sockaddr *)&conn_addr, &len);
249 
250 	/*sendmsg () TEST10:sendmsg on closed association, EPIPE Expected error*/
251 	count = sendmsg(acpt1_sk, &outmessage, flag);
252 	if (count != -1 || errno != EPIPE)
253 		tst_brkm(TBROK, tst_exit, "sendmsg on a closed association "
254 			 "count:%d, errno:%d", count, errno);
255 
256 	tst_resm(TPASS, "sendmsg() on a closed association - EPIPE");
257 
258 	close(acpt1_sk);
259 	close(sk);
260 	close(lstn_sk);
261 	close(acpt_sk);
262 
263         sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
264 
265         lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
266 
267 	conn_addr.sin_family = AF_INET;
268         conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
269         conn_addr.sin_port = htons(SCTP_TESTPORT_1);
270 
271 	lstn_addr.sin_family = AF_INET;
272         lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
273         lstn_addr.sin_port = htons(SCTP_TESTPORT_1);
274 
275 	/*Binding the listen socket*/
276         test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));
277 
278         /*Listening the socket*/
279         test_listen(lstn_sk, 10);
280 
281 	len = sizeof(struct sockaddr_in);
282 	flag = MSG_NOSIGNAL;
283 
284 	test_connect(sk, (struct sockaddr *) &conn_addr, len);
285 
286 	acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
287 
288 	memset(&outmessage, 0, sizeof(outmessage));
289         outmessage.msg_name = &svr_addr;
290         outmessage.msg_namelen = sizeof(svr_addr);
291         outmessage.msg_iov = &out_iov;
292         outmessage.msg_iovlen = 1;
293         outmessage.msg_control = outcmsg;
294         outmessage.msg_controllen = sizeof(outcmsg);
295         outmessage.msg_flags = 0;
296 
297 	cmsg = CMSG_FIRSTHDR(&outmessage);
298         cmsg->cmsg_level = IPPROTO_SCTP;
299         cmsg->cmsg_type = SCTP_SNDRCV;
300         cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
301         outmessage.msg_controllen = cmsg->cmsg_len;
302 	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
303         memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
304 
305         outmessage.msg_iov->iov_base = message;
306         outmessage.msg_iov->iov_len = strlen(message) + 1;
307 
308 	memset(&inmessage, 0, sizeof(inmessage));
309         buffer_rcv = malloc(REALLY_BIG);
310 
311         iov_rcv.iov_base = buffer_rcv;
312         iov_rcv.iov_len = REALLY_BIG;
313         inmessage.msg_iov = &iov_rcv;
314         inmessage.msg_iovlen = 1;
315         inmessage.msg_control = incmsg;
316         inmessage.msg_controllen = sizeof(incmsg);
317 
318 	msg_count = strlen(message) + 1;
319 
320 	/*sendmsg() TEST11: Sending data from server socket to client socket*/
321 	count = sendmsg(acpt_sk, &outmessage, flag);
322 	if (count != msg_count)
323 		tst_brkm(TBROK, tst_exit, "sendmsg from accept socket to "
324 			 "client count:%d, errno:%d", count, errno);
325 
326 	tst_resm(TPASS, "sendmsg() from accept socket to client - SUCCESS");
327 
328 	count = test_recvmsg(sk, &inmessage, flag);
329         test_check_msg_data(&inmessage, count, msg_count, MSG_EOR, 0, 0);
330 
331         outmessage.msg_name = &conn_addr;
332         outmessage.msg_namelen = sizeof(conn_addr);
333 	/*sendmsg() TEST12: Sending data from client socket to server socket*/
334 	count = sendmsg(sk, &outmessage, flag);
335 	if (count != msg_count)
336 		tst_brkm(TBROK, tst_exit, "sendmsg from client to server "
337                          "count:%d, errno:%d", count, errno);
338 
339 	tst_resm(TPASS, "sendmsg() from client to server - SUCCESS");
340 
341 	count = test_recvmsg(acpt_sk, &inmessage, flag);
342         test_check_msg_data(&inmessage, count, msg_count, MSG_EOR, 0, 0);
343 
344         outmessage.msg_name = &conn_addr;
345         outmessage.msg_namelen = sizeof(conn_addr);
346 	close(sk);
347 	close(acpt_sk);
348 	sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
349 
350 	/*sendmsg() TEST13: Sending data from unconnected client socket to
351 	server socket*/
352 	count = sendmsg(sk1, &outmessage, flag);
353 	if (count != msg_count)
354 		tst_brkm(TBROK, tst_exit, "sendmsg from unconnected client to "
355 			 "server count:%d, errno:%d", count, errno);
356 
357 	tst_resm(TPASS, "sendmsg() from unconnected clt to server - SUCCESS");
358 
359 	acpt_sk = test_accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
360 
361 	count = test_recvmsg(acpt_sk, &inmessage, flag);
362         test_check_msg_data(&inmessage, count, msg_count, MSG_EOR, 0, 0);
363 
364 	test_shutdown(sk1, SHUT_RD);
365 
366 	/*sendmsg() TEST14: Sending a message on SHUT_RD socket*/
367 	count = sendmsg(sk1, &outmessage, flag);
368 	if (count != msg_count)
369 		tst_brkm(TBROK, tst_exit, "sendmsg on a SHUT_RD socket "
370                          "count:%d, errno:%d", count, errno);
371 
372 	tst_resm(TPASS, "sendmsg() on a SHUT_RD socket - SUCCESS");
373 
374 	count = test_recvmsg(acpt_sk, &inmessage, flag);
375         test_check_msg_data(&inmessage, count, msg_count, MSG_EOR, 0, 0);
376 
377 	close(sk1);
378 	close(lstn_sk);
379 	close(acpt_sk);
380 	return 0;
381 }
382