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
62 char *TCID = __FILE__;
63 int TST_TOTAL = 10;
64 int TST_CNT = 0;
65
66 #define SK_MAX 10
67
68 int
main(int argc,char * argv[])69 main(int argc, char *argv[])
70 {
71 int error,i;
72 socklen_t len;
73 int sk,lstn_sk,clnt_sk[SK_MAX],acpt_sk[SK_MAX],pf_class;
74 int sk1,clnt2_sk;
75
76 struct sockaddr_in conn_addr,lstn_addr,acpt_addr;
77 struct sockaddr *tmp_addr;
78
79 /* Rather than fflush() throughout the code, set stdout to
80 * be unbuffered.
81 */
82 setvbuf(stdout, NULL, _IONBF, 0);
83 setvbuf(stderr, NULL, _IONBF, 0);
84
85 pf_class = PF_INET;
86
87 sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
88 sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
89
90 /*Creating a listen socket*/
91 lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
92
93 /*Creating a regular socket*/
94 for (i = 0 ; i < SK_MAX ; i++)
95 clnt_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
96
97 clnt2_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
98
99 conn_addr.sin_family = AF_INET;
100 conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
101 conn_addr.sin_port = htons(SCTP_TESTPORT_1);
102
103 lstn_addr.sin_family = AF_INET;
104 lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
105 lstn_addr.sin_port = htons(SCTP_TESTPORT_1);
106
107 /*Binding the listen socket*/
108 test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));
109
110 /*Listening the socket*/
111 test_listen(lstn_sk, SK_MAX-1);
112
113
114 /*sctp_connectx () TEST1: Bad socket descriptor, EBADF Expected error*/
115 len = sizeof(struct sockaddr_in);
116 error = sctp_connectx(-1, (struct sockaddr *) &conn_addr, 1, NULL);
117 if (error != -1 || errno != EBADF)
118 tst_brkm(TBROK, tst_exit, "sctp_connectx with bad socket "
119 "descriptor error:%d, errno:%d", error, errno);
120
121 tst_resm(TPASS, "sctp_connectx() with bad socket descriptor - EBADF");
122
123 /*sctp_connectx () TEST2: Invalid socket, ENOTSOCK Expected error*/
124 error = sctp_connectx(0, (struct sockaddr *) &conn_addr, 1, NULL);
125 if (error != -1 || errno != ENOTSOCK)
126 tst_brkm(TBROK, tst_exit, "sctp_connectx with invalid socket "
127 "error:%d, errno:%d", error, errno);
128
129 tst_resm(TPASS, "sctp_connectx() with invalid socket - ENOTSOCK");
130
131 /*sctp_connectx () TEST3: Invalid address, EINVAL Expected error*/
132 tmp_addr = (struct sockaddr *) malloc(sizeof(struct sockaddr) - 1);
133 tmp_addr->sa_family = AF_INET;
134 error = sctp_connectx(sk, tmp_addr, 1, NULL);
135 if (error != -1 || errno != EINVAL)
136 tst_brkm(TBROK, tst_exit, "sctp_connectx with invalid address "
137 "error:%d, errno:%d", error, errno);
138
139 tst_resm(TPASS, "sctp_connectx() with invalid address - EINVAL");
140
141 /*sctp_connectx () TEST4: Invalid address length, EINVAL Expected error*/
142 error = sctp_connectx(sk, (struct sockaddr *) &conn_addr, 0, NULL);
143 if (error != -1 || errno != EINVAL)
144 tst_brkm(TBROK, tst_exit, "sctp_connectx with invalid address length "
145 "error:%d, errno:%d", error, errno);
146
147 tst_resm(TPASS, "sctp_connectx() with invalid address length - EINVAL");
148
149 /*sctp_connectx () TEST5: Invalid address family, EINVAL Expect error*/
150 conn_addr.sin_family = 9090; /*Assigning invalid address family*/
151 error = sctp_connectx(sk, (struct sockaddr *) &conn_addr, 1, NULL);
152 if (error != -1 || errno != EINVAL)
153 tst_brkm(TBROK, tst_exit, "sctp_connectx with invalid address family "
154 "error:%d, errno:%d", error, errno);
155
156 tst_resm(TPASS, "sctp_connectx() with invalid address family - EINVAL");
157
158 conn_addr.sin_family = AF_INET;
159
160 /*sctp_connectx () TEST6: Blocking sctp_connectx, should pass*/
161 /*All the be below blocking sctp_connectx should pass as socket will be
162 listening SK_MAX clients*/
163 for (i = 0 ; i < SK_MAX ; i++) {
164 error = sctp_connectx(clnt_sk[i], (struct sockaddr *)&conn_addr,
165 1, NULL);
166 if (error < 0)
167 tst_brkm(TBROK, tst_exit, "valid blocking sctp_connectx "
168 "error:%d, errno:%d", error, errno);
169 }
170
171 tst_resm(TPASS, "valid blocking sctp_connectx() - SUCCESS");
172
173 /*sctp_connectx () TEST7: sctp_connectx when accept queue is full, ECONNREFUSED
174 Expect error*/
175 /*Now that accept queue is full, the below sctp_connectx should fail*/
176 error = sctp_connectx(clnt2_sk, (struct sockaddr *) &conn_addr, 1, NULL);
177 if (error != -1 || errno != ECONNREFUSED)
178 tst_brkm(TBROK, tst_exit, "sctp_connectx when accept queue is full "
179 "error:%d, errno:%d", error, errno);
180
181 tst_resm(TPASS, "sctp_connectx() when accept queue is full - ECONNREFUSED");
182
183 /*Calling a accept first to estblish the pending sctp_connectxions*/
184 for (i=0 ; i < SK_MAX ; i++)
185 acpt_sk[i] = test_accept(lstn_sk,
186 (struct sockaddr *) &acpt_addr, &len);
187
188 /*sctp_connectx () TEST8: from a listening socket, EISCONN Expect error*/
189 error = sctp_connectx(lstn_sk, (struct sockaddr *) &lstn_addr, 1, NULL);
190 if (error != -1 || errno != EISCONN)
191 tst_brkm(TBROK, tst_exit, "sctp_connectx on a listening socket "
192 "error:%d, errno:%d", error, errno);
193
194 tst_resm(TPASS, "sctp_connectx() on a listening socket - EISCONN");
195
196 /*sctp_connectx() TEST9: On established socket, EISCONN Expect error*/
197 i=0;
198 error = sctp_connectx(acpt_sk[i], (struct sockaddr *) &lstn_addr, 1, NULL);
199 if (error != -1 || errno != EISCONN)
200 tst_brkm(TBROK, tst_exit, "sctp_connectx on an established socket "
201 "error:%d, errno:%d", error, errno);
202
203 tst_resm(TPASS, "sctp_connectx() on an established socket - EISCONN");
204
205 for (i = 0 ; i < 4 ; i++) {
206 close(clnt_sk[i]);
207 close(acpt_sk[i]);
208 }
209
210 /* sctp_connectx() TEST10: Re-establish an association that is closed.
211 * should succeed.
212 */
213 error = sctp_connectx(sk1, (struct sockaddr *)&conn_addr, 1, NULL);
214 if (error < 0)
215 tst_brkm(TBROK, tst_exit, "Re-establish an association that "
216 "is closed error:%d, errno:%d", error, errno);
217
218 tst_resm(TPASS, "sctp_connectx() to re-establish a closed association - "
219 "SUCCESS");
220
221 close(sk);
222 close(sk1);
223 close(lstn_sk);
224
225 return 0;
226 }
227