1 /* SCTP kernel Implementation
2 * Copyright (c) 2003 Hewlett-Packard Development Company, L.P
3 * (C) Copyright IBM Corp. 2004
5 * This file has test cases to test the connect () call for 1-1 style sockets
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.
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)
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.
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.
35 * Please send any bug reports or fixes you make to the
37 * lksctp developers <lksctp-developers@lists.sourceforge.net>
39 * Or submit a bug report through the following website:
40 * http://www.sf.net/projects/lksctp
42 * Any bugs reported given to us we will try to fix... any fixes shared will
43 * be incorporated into the next SCTP release
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>
59 #include <netinet/sctp.h>
62 char *TCID = __FILE__;
69 main(int argc, char *argv[])
73 int sk,lstn_sk,clnt_sk[SK_MAX],acpt_sk[SK_MAX],pf_class;
76 struct sockaddr_in conn_addr,lstn_addr,acpt_addr;
78 /* Rather than fflush() throughout the code, set stdout to
81 setvbuf(stdout, NULL, _IONBF, 0);
82 setvbuf(stderr, NULL, _IONBF, 0);
86 sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
87 sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
89 /*Creating a listen socket*/
90 lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
92 /*Creating a regular socket*/
93 for (i = 0 ; i < SK_MAX ; i++)
94 clnt_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
96 clnt2_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
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);
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);
106 /*Binding the listen socket*/
107 test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));
109 /*Listening the socket*/
110 test_listen(lstn_sk, SK_MAX-1);
113 /*connect () TEST1: Bad socket descriptor, EBADF Expected error*/
114 len = sizeof(struct sockaddr_in);
115 error = connect(-1, (const struct sockaddr *) &conn_addr, len);
116 if (error != -1 || errno != EBADF)
117 tst_brkm(TBROK, tst_exit, "connect with bad socket "
118 "descriptor error:%d, errno:%d", error, errno);
120 tst_resm(TPASS, "connect() with bad socket descriptor - EBADF");
122 /*connect () TEST2: Invalid socket, ENOTSOCK Expected error*/
123 error = connect(0, (const struct sockaddr *) &conn_addr, len);
124 if (error != -1 || errno != ENOTSOCK)
125 tst_brkm(TBROK, tst_exit, "connect with invalid socket "
126 "error:%d, errno:%d", error, errno);
128 tst_resm(TPASS, "connect() with invalid socket - ENOTSOCK");
130 /*connect () TEST3: Invalid address, EFAULT Expected error*/
131 error = connect(sk, (const struct sockaddr *) -1, len);
132 if (error != -1 || errno != EFAULT)
133 tst_brkm(TBROK, tst_exit, "connect with invalid address "
134 "error:%d, errno:%d", error, errno);
136 tst_resm(TPASS, "connect() with invalid address - EFAULT");
138 /*connect () TEST4: Invalid address length, EINVAL Expected error*/
139 error = connect(sk, (const struct sockaddr *) &conn_addr, (len - 3));
140 if (error != -1 || errno != EINVAL)
141 tst_brkm(TBROK, tst_exit, "connect with invalid address length "
142 "error:%d, errno:%d", error, errno);
144 tst_resm(TPASS, "connect() with invalid address length - EINVAL");
146 /*connect () TEST5: Invalid address family, EINVAL Expect error*/
147 conn_addr.sin_family = 9090; /*Assigning invalid address family*/
148 error = connect(sk, (const struct sockaddr *) &conn_addr, len);
149 if (error != -1 || errno != EINVAL)
150 tst_brkm(TBROK, tst_exit, "connect with invalid address family "
151 "error:%d, errno:%d", error, errno);
153 tst_resm(TPASS, "connect() with invalid address family - EINVAL");
155 conn_addr.sin_family = AF_INET;
157 /*connect () TEST6: Blocking connect, should pass*/
158 /*All the be below blocking connect should pass as socket will be
159 listening SK_MAX clients*/
160 for (i = 0 ; i < SK_MAX ; i++) {
161 error = connect(clnt_sk[i], (const struct sockaddr *)&conn_addr,
164 tst_brkm(TBROK, tst_exit, "valid blocking connect "
165 "error:%d, errno:%d", error, errno);
168 tst_resm(TPASS, "valid blocking connect() - SUCCESS");
170 /*connect () TEST7: connect when accept queue is full, ECONNREFUSED
172 /*Now that accept queue is full, the below connect should fail*/
173 error = connect(clnt2_sk, (const struct sockaddr *) &conn_addr, len);
174 if (error != -1 || errno != ECONNREFUSED)
175 tst_brkm(TBROK, tst_exit, "connect when accept queue is full "
176 "error:%d, errno:%d", error, errno);
178 tst_resm(TPASS, "connect() when accept queue is full - ECONNREFUSED");
180 /*Calling a accept first to estblish the pending connections*/
181 for (i=0 ; i < SK_MAX ; i++)
182 acpt_sk[i] = test_accept(lstn_sk,
183 (struct sockaddr *) &acpt_addr, &len);
185 /*connect () TEST8: from a listening socket, EISCONN Expect error*/
186 error = connect(lstn_sk, (const struct sockaddr *) &lstn_addr, len);
187 if (error != -1 || errno != EISCONN)
188 tst_brkm(TBROK, tst_exit, "connect on a listening socket "
189 "error:%d, errno:%d", error, errno);
191 tst_resm(TPASS, "connect() on a listening socket - EISCONN");
193 /*connect() TEST9: On established socket, EISCONN Expect error*/
195 error = connect(acpt_sk[i], (const struct sockaddr *) &lstn_addr, len);
196 if (error != -1 || errno != EISCONN)
197 tst_brkm(TBROK, tst_exit, "connect on an established socket "
198 "error:%d, errno:%d", error, errno);
200 tst_resm(TPASS, "connect() on an established socket - EISCONN");
202 for (i = 0 ; i < 4 ; i++) {
207 /* connect() TEST10: Re-establish an association that is closed.
210 error = connect(sk1, (const struct sockaddr *)&conn_addr, len);
212 tst_brkm(TBROK, tst_exit, "Re-establish an association that "
213 "is closed error:%d, errno:%d", error, errno);
215 tst_resm(TPASS, "connect() to re-establish a closed association - "