• 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 Non-Blocking mode of connect(),
6  * accept() and recvmsg() calls.
7  *
8  * TEST1: Non blocking accept return EAGAIN if connect is not called
9  * TEST2: Non blocking connect should return EINPROGRESS
10  * TEST3: accept() passes when connect called in Non-blocking mode
11  * TEST4: Non blocking recvmsg should return EAGAIN
12  * TEST5: recvmsg() should succeed if data present to receive in non blocking
13  *	  mode
14  *
15  * The SCTP implementation is free software;
16  * you can redistribute it and/or modify it under the terms of
17  * the GNU General Public License as published by
18  * the Free Software Foundation; either version 2, or (at your option)
19  * any later version.
20  *
21  * The SCTP implementation is distributed in the hope that it
22  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
23  *                 ************************
24  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
25  * See the GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with GNU CC; see the file COPYING.  If not, write to
29  * the Free Software Foundation, 59 Temple Place - Suite 330,
30  * Boston, MA 02111-1307, USA.
31  *
32  * Please send any bug reports or fixes you make to the
33  * email address(es):
34  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
35  *
36  * Or submit a bug report through the following website:
37  *    http://www.sf.net/projects/lksctp
38  *
39  * Any bugs reported given to us we will try to fix... any fixes shared will
40  * be incorporated into the next SCTP release
41  *
42  */
43 
44 #include <stdio.h>
45 #include <unistd.h>
46 #include <fcntl.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>         /* for sockaddr_in */
52 #include <arpa/inet.h>
53 #include <errno.h>
54 #include <netinet/sctp.h>
55 #include <sys/uio.h>
56 #include <linux/socket.h>
57 #include <sctputil.h>
58 #include "tst_kernel.h"
59 
60 char *TCID = __FILE__;
61 int TST_TOTAL = 5;
62 int TST_CNT = 0;
63 
64 int
main(void)65 main(void)
66 {
67         int error,msg_count;
68 	socklen_t len;
69 	int sk,pf_class,lstn_sk,acpt_sk,flag,cflag,sflag;
70 	struct msghdr outmessage;
71 	struct msghdr inmessage;
72         char *message = "hello, world!\n";
73         struct iovec iov_rcv;
74 	struct sctp_sndrcvinfo *sinfo;
75 	char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
76 	struct cmsghdr *cmsg;
77         struct iovec out_iov;
78 	char * buffer_rcv;
79 	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
80 
81         struct sockaddr_in conn_addr,lstn_addr,svr_addr;
82 
83 	if (tst_check_driver("sctp"))
84 		tst_brkm(TCONF, tst_exit, "sctp driver not available");
85 
86 	/* Rather than fflush() throughout the code, set stdout to
87          * be unbufferd
88          */
89         setvbuf(stdout, NULL, _IONBF, 0);
90         setvbuf(stderr, NULL, _IONBF, 0);
91 
92         pf_class = PF_INET;
93 
94         sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
95 
96         lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
97 
98 	conn_addr.sin_family = AF_INET;
99         conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
100         conn_addr.sin_port = htons(SCTP_TESTPORT_1);
101 
102 	lstn_addr.sin_family = AF_INET;
103         lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
104         lstn_addr.sin_port = htons(SCTP_TESTPORT_1);
105 
106 	/*Binding the listen socket*/
107         test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));
108 
109         /*Listening the socket*/
110         test_listen(lstn_sk, 10);
111 
112 	len = sizeof(struct sockaddr_in);
113 	flag = MSG_NOSIGNAL;
114 
115 	/*Setting server socket non-blocking*/
116 	sflag = fcntl(lstn_sk, F_GETFL, 0);
117 	if (sflag < 0)
118 		tst_brkm(TBROK, tst_exit, "fcnt F_GETFL failed "
119                          "sflag:%d, errno:%d", sflag, errno);
120 
121 	error = fcntl(lstn_sk, F_SETFL, sflag | O_NONBLOCK);
122 	if (error < 0)
123 		tst_brkm(TBROK, tst_exit, "fcnt F_SETFL failed "
124                          "error:%d, errno:%d", error, errno);
125 
126 	/* TEST1: accept should return EAGAIN instead blocking. */
127 	error = accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
128 	if (error != -1 || errno != EAGAIN)
129 		tst_brkm(TBROK, tst_exit, "non-blocking accept "
130                          "error:%d, errno:%d", error, errno);
131 
132 	tst_resm(TPASS, "non-blocking accept() - EAGAIN");
133 
134 	/* TEST2: Non Block connect should return EINPROGRESS */
135 	/*Set client socket as non-blocking*/
136 	cflag = fcntl(sk, F_GETFL, 0);
137 	if (cflag < 0)
138 		tst_brkm(TBROK, tst_exit, "fcnt F_GETFL failed "
139                          "cflag:%d, errno:%d", cflag, errno);
140 
141 	error = fcntl(sk, F_SETFL, sflag | O_NONBLOCK);
142 	if (error < 0)
143 		tst_brkm(TBROK, tst_exit, "fcnt F_SETFL failed "
144                          "error:%d, errno:%d", error, errno);
145 
146 	error = connect(sk, (const struct sockaddr *) &conn_addr, len);
147 	if (error != -1 || errno != EINPROGRESS)
148 		tst_brkm(TBROK, tst_exit, "non-blocking connect "
149                          "error:%d, errno:%d", error, errno);
150 
151 	tst_resm(TPASS, "non-blocking connect() - EINPROGRESS");
152 
153 	/* TEST3: Now that connect() called, accept will succeed */
154 	acpt_sk = accept(lstn_sk, (struct sockaddr *)&svr_addr, &len);
155 	if (acpt_sk < 0)
156 		tst_brkm(TBROK, tst_exit, "accept after a non-blocking connect "
157                          "error:%d, errno:%d", error, errno);
158 
159 	tst_resm(TPASS, "accept() after a non-blocking connect - SUCCESS");
160 
161 	memset(&outmessage, 0, sizeof(outmessage));
162         outmessage.msg_name = &svr_addr;
163         outmessage.msg_namelen = sizeof(svr_addr);
164         outmessage.msg_iov = &out_iov;
165         outmessage.msg_iovlen = 1;
166         outmessage.msg_control = outcmsg;
167         outmessage.msg_controllen = sizeof(outcmsg);
168         outmessage.msg_flags = 0;
169 
170 	cmsg = CMSG_FIRSTHDR(&outmessage);
171         cmsg->cmsg_level = IPPROTO_SCTP;
172         cmsg->cmsg_type = SCTP_SNDRCV;
173         cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
174         outmessage.msg_controllen = cmsg->cmsg_len;
175 	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
176         memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
177 
178         outmessage.msg_iov->iov_base = message;
179         outmessage.msg_iov->iov_len = strlen(message) + 1;
180 
181 	memset(&inmessage, 0, sizeof(inmessage));
182         buffer_rcv = malloc(REALLY_BIG);
183 
184         iov_rcv.iov_base = buffer_rcv;
185         iov_rcv.iov_len = REALLY_BIG;
186         inmessage.msg_iov = &iov_rcv;
187         inmessage.msg_iovlen = 1;
188         inmessage.msg_control = incmsg;
189         inmessage.msg_controllen = sizeof(incmsg);
190 
191 	msg_count = strlen(message) + 1;
192 
193 	/* TEST4: recvmsg() should return EAGAIN instead blocking */
194 	error = recvmsg(sk, &inmessage, MSG_WAITALL);
195 	if ( error != -1 || errno != EAGAIN)
196 		tst_brkm(TBROK, tst_exit, "non-blocking recvmsg "
197                          "error:%d, errno:%d", error, errno);
198 
199 	tst_resm(TPASS, "non-blocking recvmsg() - EAGAIN");
200 
201 	test_sendmsg(acpt_sk, &outmessage, flag, msg_count);
202 
203 	/* TEST5: recvmsg() should succeed now as data is available. */
204 	error = test_recvmsg(sk, &inmessage, flag);
205         test_check_msg_data(&inmessage, error, msg_count, MSG_EOR, 0, 0);
206 
207 	tst_resm(TPASS, "non-blocking recvmsg() when data is available - "
208 		 "SUCCESS");
209 
210 	close(lstn_sk);
211 	close(acpt_sk);
212 	return 0;
213 }
214