Imported Upstream version 1.0.10
[platform/upstream/lksctp-tools.git] / src / func_tests / test_connect.c
1 /* SCTP kernel Implementation
2  * (C) Copyright IBM Corp. 2002, 2003
3  * Copyright (c) 1999-2001 Motorola, Inc.
4  *
5  * The SCTP implementation is free software;
6  * you can redistribute it and/or modify it under the terms of
7  * the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * The SCTP implementation is distributed in the hope that it
12  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
13  *                 ************************
14  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  * See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU CC; see the file COPYING.  If not, write to
19  * the Free Software Foundation, 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * Please send any bug reports or fixes you make to the
23  * email address(es):
24  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
25  *
26  * Or submit a bug report through the following website:
27  *    http://www.sf.net/projects/lksctp
28  *
29  * Any bugs reported given to us we will try to fix... any fixes shared will
30  * be incorporated into the next SCTP release.
31  *
32  * Written or modified by:
33  *    Sridhar Samudrala         <sri@us.ibm.com>
34  */
35
36 /* This is a kernel test to verify the one-to-many style connect() in blocking
37  * and non-blocking modes.
38  */
39
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <fcntl.h>
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <sys/uio.h>
48 #include <netinet/in.h>
49 #include <errno.h>
50 #include <netinet/sctp.h>
51 #include <sctputil.h>
52
53 char *TCID = __FILE__;
54 int TST_TOTAL = 5;
55 int TST_CNT = 0;
56
57 int
58 main(int argc, char *argv[])
59 {
60         int svr_sk, clt_sk1, clt_sk2, peeloff_sk;
61         sctp_assoc_t svr_associd1, svr_associd2, clt_associd1, clt_associd2; 
62         sockaddr_storage_t svr_loop, clt_loop1, clt_loop2, clt_loop3;
63         struct iovec iov;
64         struct msghdr inmessage;
65         int error;
66         struct sctp_assoc_change *sac;
67         char *big_buffer;
68         int flags;
69
70         /* Rather than fflush() throughout the code, set stdout to 
71          * be unbuffered.  
72          */ 
73         setvbuf(stdout, NULL, _IONBF, 0); 
74
75         /* Initialize the server and client addresses. */ 
76         svr_loop.v4.sin_family = AF_INET;
77         svr_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
78         svr_loop.v4.sin_port = htons(SCTP_TESTPORT_1);
79         clt_loop1.v4.sin_family = AF_INET;
80         clt_loop1.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
81         clt_loop1.v4.sin_port = htons(SCTP_TESTPORT_2);
82         clt_loop2.v4.sin_family = AF_INET;
83         clt_loop2.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
84         clt_loop2.v4.sin_port = htons(SCTP_TESTPORT_2+1);
85         clt_loop3.v4.sin_family = AF_INET;
86         clt_loop3.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
87         clt_loop3.v4.sin_port = htons(SCTP_TESTPORT_2+2);
88
89         /* Create and bind the server socket.  */
90         svr_sk = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
91         test_bind(svr_sk, &svr_loop.sa, sizeof(svr_loop));
92
93         /* Mark server socket as being able to accept new associations.  */
94         test_listen(svr_sk, 1);
95
96         /* Create and bind the client sockets.  */
97         clt_sk1 = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
98         test_bind(clt_sk1, &clt_loop1.sa, sizeof(clt_loop1));
99         clt_sk2 = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
100         test_bind(clt_sk2, &clt_loop2.sa, sizeof(clt_loop2));
101
102         /* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
103         test_enable_assoc_change(svr_sk);
104         test_enable_assoc_change(clt_sk1);
105         test_enable_assoc_change(clt_sk2);
106
107         /* Set clt_sk1 as non-blocking. */
108         flags = fcntl(clt_sk1, F_GETFL, 0);
109         if (flags < 0)
110                 tst_brkm(TBROK, tst_exit, "fcntl F_GETFL: %s", strerror(errno));
111         if (fcntl(clt_sk1, F_SETFL, flags | O_NONBLOCK) < 0)
112                 tst_brkm(TBROK, tst_exit, "fcntl F_SETFL: %s", strerror(errno));
113
114         /* Do a non-blocking connect from clt_sk1 to svr_sk */      
115         error = connect(clt_sk1, &svr_loop.sa, sizeof(svr_loop));
116         /* Non-blocking connect should return immediately with EINPROGRESS. */
117         if ((error != -1) || (EINPROGRESS != errno))
118                 tst_brkm(TBROK, tst_exit, "non-blocking connect error: %d"
119                          "errno:%d", error, errno);
120
121         tst_resm(TPASS, "non-blocking connect");
122
123         /* Doing a connect on a socket to create an association that is
124          * is already established should return EISCONN.
125          */
126         error = connect(clt_sk1, &svr_loop.sa, sizeof(svr_loop));
127         if ((error != -1) || (EISCONN != errno))
128                 tst_brkm(TBROK, tst_exit, "connect on a socket to create an "
129                          "assoc that is already established error:%d errno:%d",
130                          error, errno);
131
132         tst_resm(TPASS, "connect on a socket to create an assoc that is "
133                  "already established");
134
135         /* Initialize inmessage for all receives. */
136         memset(&inmessage, 0, sizeof(inmessage));
137         big_buffer = test_malloc(REALLY_BIG);
138         iov.iov_base = big_buffer;
139         iov.iov_len = REALLY_BIG;
140         inmessage.msg_iov = &iov;
141         inmessage.msg_iovlen = 1;
142         inmessage.msg_control = NULL;
143
144         /* Get COMM_UP on clt_sk1 */
145         error = test_recvmsg(clt_sk1, &inmessage, MSG_WAITALL);
146         test_check_msg_notification(&inmessage, error,
147                                     sizeof(struct sctp_assoc_change),
148                                     SCTP_ASSOC_CHANGE, SCTP_COMM_UP);   
149         sac = (struct sctp_assoc_change *)iov.iov_base;
150         clt_associd1 = sac->sac_assoc_id;
151
152         /* Get COMM_UP on svr_sk */
153         error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
154         test_check_msg_notification(&inmessage, error,
155                                     sizeof(struct sctp_assoc_change),
156                                     SCTP_ASSOC_CHANGE, SCTP_COMM_UP);   
157         sac = (struct sctp_assoc_change *)iov.iov_base;
158         svr_associd1 = sac->sac_assoc_id;
159
160         /* Do a blocking connect from clt_sk2 to svr_sk. 
161          * Blocking connect should block until the association is established
162          * and return success.
163          */
164         test_connect(clt_sk2, &svr_loop.sa, sizeof(svr_loop));
165
166         /* Get COMM_UP on clt_sk2 */
167         error = test_recvmsg(clt_sk2, &inmessage, MSG_WAITALL);
168         test_check_msg_notification(&inmessage, error,
169                                     sizeof(struct sctp_assoc_change),
170                                     SCTP_ASSOC_CHANGE, SCTP_COMM_UP);   
171         sac = (struct sctp_assoc_change *)iov.iov_base;
172         clt_associd2 = sac->sac_assoc_id;
173
174         /* Get COMM_UP on svr_sk */
175         error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
176         test_check_msg_notification(&inmessage, error,
177                                     sizeof(struct sctp_assoc_change),
178                                     SCTP_ASSOC_CHANGE, SCTP_COMM_UP);   
179         sac = (struct sctp_assoc_change *)iov.iov_base;
180         svr_associd2 = sac->sac_assoc_id;
181
182         tst_resm(TPASS, "blocking connect");
183
184         peeloff_sk = test_sctp_peeloff(svr_sk, svr_associd1); 
185
186         /* Doing a connect on a peeled off socket should fail. */
187         error = connect(peeloff_sk, &clt_loop3.sa, sizeof(clt_loop3));
188         if ((error != -1) || (EISCONN != errno))
189                 tst_brkm(TBROK, tst_exit, "connect on a peeled off socket "
190                          "error:%d, errno:%d", error, errno);
191
192         tst_resm(TPASS, "connect on a peeled off socket");
193
194         /* Trying to create an association on a socket that matches an 
195          * existing peeled-off association should fail.
196          */
197         error = connect(svr_sk, &clt_loop1.sa, sizeof(clt_loop1));
198         if ((error != -1) || (EADDRNOTAVAIL != errno))
199                 tst_brkm(TBROK, tst_exit, "connect to create an assoc that "
200                          "matches a peeled off assoc error:%d errno:%d",
201                          error, errno);
202
203         tst_resm(TPASS, "connect to create an assoc that matches a peeled off "
204                  "assoc");
205
206         close(svr_sk);
207         close(clt_sk1);
208         close(clt_sk2);
209         close(peeloff_sk);
210
211         /* Indicate successful completion.  */
212         return 0; 
213 }