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