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