Imported Upstream version 1.0.10
[platform/upstream/lksctp-tools.git] / src / func_tests / test_assoc_abort.c
1 /* SCTP kernel Implementation
2  * (C) Copyright IBM Corp. 2001, 2003
3  * Copyright (c) 1999-2000 Cisco, Inc.
4  * Copyright (c) 1999-2001 Motorola, Inc.
5  * Copyright (c) 2001 Intel Corp.
6  * Copyright (c) 2001 Nokia, Inc.
7  *
8  * The SCTP implementation is free software;
9  * you can redistribute it and/or modify it under the terms of
10  * the GNU General Public License as published by
11  * the Free Software Foundation; either version 2, or (at your option)
12  * any later version.
13  *
14  * The SCTP implementation is distributed in the hope that it
15  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
16  *                 ************************
17  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18  * See the GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with GNU CC; see the file COPYING.  If not, write to
22  * the Free Software Foundation, 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  *
25  * Please send any bug reports or fixes you make to the
26  * email address(es):
27  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
28  *
29  * Or submit a bug report through the following website:
30  *    http://www.sf.net/projects/lksctp
31  *
32  * Any bugs reported to us we will try to fix... any fixes shared will
33  * be incorporated into the next SCTP release.
34  *
35  * Written or modified by:
36  *    Ardelle Fan <ardelle.fan@intle.com>
37  *    Sridhar Samudrala <sri@us.ibm.com>
38  */
39
40 /* This is a functional test to verify the ungraceful abort of an
41  * association.
42  */
43
44 #include <stdio.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 #include <sys/uio.h>
51 #include <netinet/in.h>
52 #include <sys/errno.h>
53 #include <errno.h>
54 #include <netinet/sctp.h>
55 #include <sctputil.h>
56
57 char *TCID = __FILE__;
58 int TST_TOTAL = 1;
59 int TST_CNT = 0;
60
61 #define MAX_CLIENTS 10
62
63 int
64 main(int argc, char *argv[])
65 {
66         int svr_sk, clt_sk[MAX_CLIENTS];
67         sockaddr_storage_t svr_loop, clt_loop[MAX_CLIENTS];
68         sctp_assoc_t svr_associd[MAX_CLIENTS];
69         struct iovec iov;
70         struct msghdr inmessage;
71         struct msghdr outmessage;
72         char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
73         char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
74         struct cmsghdr *cmsg;
75         struct sctp_sndrcvinfo *sinfo;
76         struct iovec out_iov;
77         int error;
78         uint32_t ppid;
79         uint32_t stream;
80         struct sctp_assoc_change *sac;
81         char *big_buffer;
82         int i;
83         char *message = "hello, world!\n";
84         struct sctp_status status;
85         socklen_t status_len;
86
87         /* Rather than fflush() throughout the code, set stdout to 
88          * be unbuffered.  
89          */ 
90         setvbuf(stdout, NULL, _IONBF, 0); 
91
92         /* Create and bind the server socket.  */
93         svr_sk = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
94         svr_loop.v4.sin_family = AF_INET;
95         svr_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
96         svr_loop.v4.sin_port = htons(SCTP_TESTPORT_1);
97         test_bind(svr_sk, &svr_loop.sa, sizeof(svr_loop));
98
99         /* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
100         test_enable_assoc_change(svr_sk);
101
102         /* Mark server socket as being able to accept new associations.  */
103         test_listen(svr_sk, 1);
104
105         /* Create and bind all the client sockets.  */
106         for (i = 0; i < MAX_CLIENTS; i++) {
107                 clt_sk[i] = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
108
109                 clt_loop[i].v4.sin_family = AF_INET;
110                 clt_loop[i].v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
111                 clt_loop[i].v4.sin_port = htons(SCTP_TESTPORT_2 + i);
112                 test_bind(clt_sk[i], &clt_loop[i].sa, sizeof(clt_loop[i]));
113
114                 test_enable_assoc_change(clt_sk[i]);
115         }
116
117         /* Build up a msghdr structure we can use for all sending.  */
118         outmessage.msg_name = &svr_loop;
119         outmessage.msg_namelen = sizeof(svr_loop);
120         outmessage.msg_iov = &out_iov;
121         outmessage.msg_iovlen = 1;
122         outmessage.msg_control = outcmsg;
123         outmessage.msg_controllen = sizeof(outcmsg);
124         outmessage.msg_flags = 0;
125         cmsg = CMSG_FIRSTHDR(&outmessage);
126         cmsg->cmsg_level = IPPROTO_SCTP;
127         cmsg->cmsg_type = SCTP_SNDRCV;
128         cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
129         outmessage.msg_controllen = cmsg->cmsg_len;
130         sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
131         memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
132         ppid = rand(); /* Choose an arbitrary value. */
133         stream = 1; 
134         sinfo->sinfo_ppid = ppid;
135         sinfo->sinfo_stream = stream;
136         out_iov.iov_base = message;
137         out_iov.iov_len = strlen(message) + 1;
138         
139         /* Send the first message from all the clients to the server.  This 
140          * will create the associations.  
141          */
142         for (i = 0; i < MAX_CLIENTS; i++)
143                 test_sendmsg(clt_sk[i], &outmessage, 0, strlen(message) + 1);
144         
145         /* Initialize inmessage for all receives. */
146         big_buffer = test_malloc(REALLY_BIG);
147         memset(&inmessage, 0, sizeof(inmessage));       
148         iov.iov_base = big_buffer;
149         iov.iov_len = REALLY_BIG;
150         inmessage.msg_iov = &iov;
151         inmessage.msg_iovlen = 1;
152         inmessage.msg_control = incmsg;
153
154         /* Get the communication up message on all client sockets.  */
155         for (i = 0; i < MAX_CLIENTS; i++) {
156                 inmessage.msg_controllen = sizeof(incmsg);
157                 error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL);
158                 test_check_msg_notification(&inmessage, error,
159                                             sizeof(struct sctp_assoc_change),
160                                             SCTP_ASSOC_CHANGE, SCTP_COMM_UP);   
161         }
162
163         /* Get the communication up message and the data message on the
164          * server sockets for all the clients.  
165          */
166         for (i = 0; i < MAX_CLIENTS; i++) {
167                 inmessage.msg_controllen = sizeof(incmsg);
168                 error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
169                 test_check_msg_notification(&inmessage, error,
170                                             sizeof(struct sctp_assoc_change),
171                                             SCTP_ASSOC_CHANGE, SCTP_COMM_UP);   
172
173                 inmessage.msg_controllen = sizeof(incmsg);
174                 error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
175                 test_check_msg_data(&inmessage, error, strlen(message) + 1, 
176                                     MSG_EOR, stream, ppid);
177                 sac = (struct sctp_assoc_change *)iov.iov_base;
178                 svr_associd[i] = sac->sac_assoc_id;
179         }
180
181         outmessage.msg_name = NULL;
182         outmessage.msg_namelen = 0;
183         outmessage.msg_iov = NULL;
184         outmessage.msg_iovlen = 0;
185         outmessage.msg_control = outcmsg;
186         outmessage.msg_controllen = sizeof(outcmsg);
187         outmessage.msg_flags = 0;
188         cmsg = CMSG_FIRSTHDR(&outmessage);
189         cmsg->cmsg_level = IPPROTO_SCTP;
190         cmsg->cmsg_type = SCTP_SNDRCV;
191         cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
192         outmessage.msg_controllen = cmsg->cmsg_len;
193         sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
194         memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
195         sinfo->sinfo_flags |= SCTP_ABORT;
196
197         /* Shutdown all the associations of the server socket in a loop.  */
198         for (i = 0; i < MAX_CLIENTS; i++) {
199                 sinfo->sinfo_assoc_id = svr_associd[i];
200
201                 /* Verify that the association is present. */
202                 memset(&status, 0, sizeof(struct sctp_status));
203                 status.sstat_assoc_id = sinfo->sinfo_assoc_id;
204                 status_len = sizeof(struct sctp_status);
205                 error = getsockopt(svr_sk, SOL_SCTP, SCTP_STATUS,
206                                    &status, &status_len);
207                 if (error)
208                         tst_brkm(TBROK, tst_exit,
209                                  "getsockopt(SCTP_STATUS): %s",
210                                  strerror(errno));
211
212                 /* Call sendmsg() to abort the association.  */
213                 test_sendmsg(svr_sk, &outmessage, 0, 0);
214
215                 /* Verify that the association is no longer present.  */
216                 memset(&status, 0, sizeof(struct sctp_status));
217                 status.sstat_assoc_id = sinfo->sinfo_assoc_id;
218                 status_len = sizeof(struct sctp_status);
219                 error = getsockopt(svr_sk, SOL_SCTP, SCTP_STATUS, 
220                                    &status, &status_len);
221                 if ((error != -1) && (errno != EINVAL))
222                         tst_brkm(TBROK, tst_exit,
223                                  "getsockopt(SCTP_STATUS) "
224                                  "error:%d errno:%d", error, errno);
225         }
226
227         close(svr_sk);
228
229         /* Get the COMM_LOST notification. */
230         for (i = 0; i < MAX_CLIENTS; i++) {
231                 inmessage.msg_controllen = sizeof(incmsg);
232                 error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL);
233                 test_check_msg_notification(&inmessage, error,
234                                             sizeof(struct sctp_assoc_change)+4,
235                                             SCTP_ASSOC_CHANGE, SCTP_COMM_LOST); 
236
237                 close(clt_sk[i]);
238         }
239
240         tst_resm(TPASS, "ABORT an association using SCTP_ABORT"); 
241
242         /* Indicate successful completion.  */
243         return 0;
244 }