• 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 sctp_connectx () call for 1-1 style sockets
6  *
7  * TEST1: Bad socket descriptor
8  * TEST2: Invalid socket
9  * TEST3: Invalid address
10  * TEST4: Invalid address length
11  * TEST5: Invalid address family
12  * TEST6: Valid blocking sctp_connectx
13  * TEST7: Connect when accept queue is full
14  * TEST8: On a listening socket
15  * TEST9: On established socket
16  * TEST10: Connect to re-establish a closed association.
17  *
18  * The SCTP implementation is free software;
19  * you can redistribute it and/or modify it under the terms of
20  * the GNU General Public License as published by
21  * the Free Software Foundation; either version 2, or (at your option)
22  * any later version.
23  *
24  * The SCTP implementation is distributed in the hope that it
25  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
26  *                 ************************
27  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28  * See the GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with GNU CC; see the file COPYING.  If not, write to
32  * the Free Software Foundation, 59 Temple Place - Suite 330,
33  * Boston, MA 02111-1307, USA.
34  *
35  * Please send any bug reports or fixes you make to the
36  * email address(es):
37  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
38  *
39  * Or submit a bug report through the following website:
40  *    http://www.sf.net/projects/lksctp
41  *
42  * Any bugs reported given to us we will try to fix... any fixes shared will
43  * be incorporated into the next SCTP release
44  *
45  */
46 
47 #include <stdio.h>
48 #include <unistd.h>
49 #include <fcntl.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <linux/socket.h>
55 #include <netinet/in.h>         /* for sockaddr_in */
56 #include <arpa/inet.h>
57 #include <sys/errno.h>
58 #include <sys/uio.h>
59 #include <netinet/sctp.h>
60 #include "sctputil.h"
61 #include "tst_kernel.h"
62 
63 char *TCID = __FILE__;
64 int TST_TOTAL = 10;
65 int TST_CNT = 0;
66 
67 #define SK_MAX 10
68 
69 int
main(void)70 main(void)
71 {
72 	int error,i;
73 	socklen_t len;
74 	int sk,lstn_sk,clnt_sk[SK_MAX],acpt_sk[SK_MAX],pf_class;
75 	int sk1,clnt2_sk;
76 
77 	struct sockaddr_in conn_addr,lstn_addr,acpt_addr;
78 	struct sockaddr *tmp_addr;
79 
80 	if (tst_check_driver("sctp"))
81 		tst_brkm(TCONF, tst_exit, "sctp driver not available");
82 
83 	/* Rather than fflush() throughout the code, set stdout to
84 	 * be unbuffered.
85 	 */
86 	setvbuf(stdout, NULL, _IONBF, 0);
87 	setvbuf(stderr, NULL, _IONBF, 0);
88 
89 	pf_class = PF_INET;
90 
91 	sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
92 	sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
93 
94 	/*Creating a listen socket*/
95 	lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
96 
97 	/*Creating a regular socket*/
98 	for (i = 0 ; i < SK_MAX ; i++)
99 		clnt_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
100 
101 	clnt2_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
102 
103 	conn_addr.sin_family = AF_INET;
104 	conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
105 	conn_addr.sin_port = htons(SCTP_TESTPORT_1);
106 
107 	lstn_addr.sin_family = AF_INET;
108 	lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
109 	lstn_addr.sin_port = htons(SCTP_TESTPORT_1);
110 
111 	/*Binding the listen socket*/
112 	test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));
113 
114 	/*Listening the socket*/
115 	test_listen(lstn_sk, SK_MAX-1);
116 
117 
118 	/*sctp_connectx () TEST1: Bad socket descriptor, EBADF Expected error*/
119 	len = sizeof(struct sockaddr_in);
120 	error = sctp_connectx(-1, (struct sockaddr *) &conn_addr, 1, NULL);
121 	if (error != -1 || errno != EBADF)
122 		tst_brkm(TBROK, tst_exit, "sctp_connectx with bad socket "
123 			 "descriptor error:%d, errno:%d", error, errno);
124 
125 	tst_resm(TPASS, "sctp_connectx() with bad socket descriptor - EBADF");
126 
127 	/*sctp_connectx () TEST2: Invalid socket, ENOTSOCK Expected error*/
128 	error = sctp_connectx(0, (struct sockaddr *) &conn_addr, 1, NULL);
129 	if (error != -1 || errno != ENOTSOCK)
130 		tst_brkm(TBROK, tst_exit, "sctp_connectx with invalid socket "
131 	                 "error:%d, errno:%d", error, errno);
132 
133 	tst_resm(TPASS, "sctp_connectx() with invalid socket - ENOTSOCK");
134 
135 	/*sctp_connectx () TEST3: Invalid address, EINVAL Expected error*/
136 	tmp_addr = (struct sockaddr *) malloc(sizeof(struct sockaddr) - 1);
137 	tmp_addr->sa_family = AF_INET;
138 	error = sctp_connectx(sk, tmp_addr, 1, NULL);
139 	if (error != -1 || errno != EINVAL)
140 		tst_brkm(TBROK, tst_exit, "sctp_connectx with invalid address "
141 	                 "error:%d, errno:%d", error, errno);
142 
143 	tst_resm(TPASS, "sctp_connectx() with invalid address - EINVAL");
144 
145 	/*sctp_connectx () TEST4: Invalid address length, EINVAL Expected error*/
146 	error = sctp_connectx(sk, (struct sockaddr *) &conn_addr, 0, NULL);
147 	if (error != -1 || errno != EINVAL)
148 		tst_brkm(TBROK, tst_exit, "sctp_connectx with invalid address length "
149 	                 "error:%d, errno:%d", error, errno);
150 
151 	tst_resm(TPASS, "sctp_connectx() with invalid address length - EINVAL");
152 
153 	/*sctp_connectx () TEST5: Invalid address family, EINVAL Expect error*/
154 	conn_addr.sin_family = 9090; /*Assigning invalid address family*/
155 	error = sctp_connectx(sk, (struct sockaddr *) &conn_addr, 1, NULL);
156 	if (error != -1 || errno != EINVAL)
157 		tst_brkm(TBROK, tst_exit, "sctp_connectx with invalid address family "
158 	                 "error:%d, errno:%d", error, errno);
159 
160 	tst_resm(TPASS, "sctp_connectx() with invalid address family - EINVAL");
161 
162 	conn_addr.sin_family = AF_INET;
163 
164 	/*sctp_connectx () TEST6: Blocking sctp_connectx, should pass*/
165 	/*All the be below blocking sctp_connectx should pass as socket will be
166 	listening SK_MAX clients*/
167 	for (i = 0 ; i < SK_MAX ; i++) {
168 		error = sctp_connectx(clnt_sk[i], (struct sockaddr *)&conn_addr,
169 			      1, NULL);
170 		if (error < 0)
171 			tst_brkm(TBROK, tst_exit, "valid blocking sctp_connectx "
172 				 "error:%d, errno:%d", error, errno);
173 	}
174 
175 	tst_resm(TPASS, "valid blocking sctp_connectx() - SUCCESS");
176 
177 	/*sctp_connectx () TEST7: sctp_connectx when accept queue is full, ECONNREFUSED
178 	Expect error*/
179 	/*Now that accept queue is full, the below sctp_connectx should fail*/
180 	error = sctp_connectx(clnt2_sk, (struct sockaddr *) &conn_addr, 1, NULL);
181 	if (error != -1 || errno != ECONNREFUSED)
182 		tst_brkm(TBROK, tst_exit, "sctp_connectx when accept queue is full "
183 	                 "error:%d, errno:%d", error, errno);
184 
185 	tst_resm(TPASS, "sctp_connectx() when accept queue is full - ECONNREFUSED");
186 
187 	/*Calling a accept first to estblish the pending sctp_connectxions*/
188 	for (i=0 ; i < SK_MAX ; i++)
189 		acpt_sk[i] = test_accept(lstn_sk,
190 					 (struct sockaddr *) &acpt_addr, &len);
191 
192 	/*sctp_connectx () TEST8: from a listening socket, EISCONN Expect error*/
193 	error = sctp_connectx(lstn_sk, (struct sockaddr *) &lstn_addr, 1, NULL);
194 	if (error != -1 || errno != EISCONN)
195 		tst_brkm(TBROK, tst_exit, "sctp_connectx on a listening socket "
196 	                 "error:%d, errno:%d", error, errno);
197 
198 	tst_resm(TPASS, "sctp_connectx() on a listening socket - EISCONN");
199 
200 	/*sctp_connectx() TEST9: On established socket, EISCONN Expect error*/
201 	i=0;
202 	error = sctp_connectx(acpt_sk[i], (struct sockaddr *) &lstn_addr, 1, NULL);
203 	if (error != -1 || errno != EISCONN)
204 		tst_brkm(TBROK, tst_exit, "sctp_connectx on an established socket "
205 	                 "error:%d, errno:%d", error, errno);
206 
207 	tst_resm(TPASS, "sctp_connectx() on an established socket - EISCONN");
208 
209 	for (i = 0 ; i < 4 ; i++) {
210 		close(clnt_sk[i]);
211 		close(acpt_sk[i]);
212 	}
213 
214 	/* sctp_connectx() TEST10: Re-establish an association that is closed.
215 	 * should succeed.
216 	 */
217 	error = sctp_connectx(sk1, (struct sockaddr *)&conn_addr, 1, NULL);
218 	if (error < 0)
219 		tst_brkm(TBROK, tst_exit, "Re-establish an association that "
220 				 "is closed error:%d, errno:%d", error, errno);
221 
222 	tst_resm(TPASS, "sctp_connectx() to re-establish a closed association - "
223 		 "SUCCESS");
224 
225 	close(sk);
226 	close(sk1);
227 	close(lstn_sk);
228 
229 	return 0;
230 }
231