blob: dcdb37794d41bb7269c435347092999cb70026ce [file] [log] [blame]
Simon Xu6f224942013-12-06 11:51:16 +08001/* SCTP kernel Implementation
uid5281789de9aa2003-10-23 18:42:24 +00002 * (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 *
Simon Xu6f224942013-12-06 11:51:16 +08008 * The SCTP implementation is free software;
uid5281789de9aa2003-10-23 18:42:24 +00009 * 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 *
Simon Xu6f224942013-12-06 11:51:16 +080014 * The SCTP implementation is distributed in the hope that it
uid5281789de9aa2003-10-23 18:42:24 +000015 * 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 * Sridhar Samudrala <sri@us.ibm.com>
37 */
38
39/* This is a functional test to verify the graceful shutdown of an
40 * association.
41 */
42
43#include <stdio.h>
44#include <unistd.h>
45#include <stdlib.h>
46#include <string.h>
47#include <sys/types.h>
48#include <sys/socket.h>
49#include <sys/uio.h>
50#include <netinet/in.h>
Simon Xu6f224942013-12-06 11:51:16 +080051#include <sys/errno.h>
uid5281789de9aa2003-10-23 18:42:24 +000052#include <errno.h>
53#include <netinet/sctp.h>
54#include <sctputil.h>
55
56char *TCID = __FILE__;
57int TST_TOTAL = 1;
58int TST_CNT = 0;
59
60#define MAX_CLIENTS 10
uid5281789de9aa2003-10-23 18:42:24 +000061
Simon Xu6f224942013-12-06 11:51:16 +080062int
63main(int argc, char *argv[])
uid5281789de9aa2003-10-23 18:42:24 +000064{
65 int svr_sk, clt_sk[MAX_CLIENTS];
66 sctp_assoc_t svr_associd[MAX_CLIENTS];
67 sockaddr_storage_t svr_loop, clt_loop[MAX_CLIENTS];
68 struct iovec iov;
69 struct msghdr inmessage;
70 struct msghdr outmessage;
71 char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
72 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
73 struct cmsghdr *cmsg;
74 struct sctp_sndrcvinfo *sinfo;
Simon Xu6f224942013-12-06 11:51:16 +080075 struct iovec out_iov;
76 int error;
uid5281789de9aa2003-10-23 18:42:24 +000077 uint32_t ppid;
78 uint32_t stream;
79 struct sctp_assoc_change *sac;
80 char *big_buffer;
81 int i;
Simon Xu6f224942013-12-06 11:51:16 +080082 char *message = "hello, world!\n";
uid5281789de9aa2003-10-23 18:42:24 +000083 struct sctp_status status;
84 socklen_t status_len;
85
Simon Xu6f224942013-12-06 11:51:16 +080086 /* Rather than fflush() throughout the code, set stdout to
87 * be unbuffered.
88 */
89 setvbuf(stdout, NULL, _IONBF, 0);
uid5281789de9aa2003-10-23 18:42:24 +000090
91 /* Create and bind the server socket. */
Simon Xu6f224942013-12-06 11:51:16 +080092 svr_sk = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
uid5281789de9aa2003-10-23 18:42:24 +000093
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));
Simon Xu6f224942013-12-06 11:51:16 +0800132 ppid = rand(); /* Choose an arbitrary value. */
133 stream = 1;
uid5281789de9aa2003-10-23 18:42:24 +0000134 sinfo->sinfo_ppid = ppid;
135 sinfo->sinfo_stream = stream;
136 out_iov.iov_base = message;
137 out_iov.iov_len = strlen(message) + 1;
Simon Xu6f224942013-12-06 11:51:16 +0800138
139 /* Send the first message from all the clients to the server. This
140 * will create the associations.
uid5281789de9aa2003-10-23 18:42:24 +0000141 */
142 for (i = 0; i < MAX_CLIENTS; i++)
Simon Xu6f224942013-12-06 11:51:16 +0800143 test_sendmsg(clt_sk[i], &outmessage, 0, strlen(message)+1);
144
uid5281789de9aa2003-10-23 18:42:24 +0000145 /* Initialize inmessage for all receives. */
146 big_buffer = test_malloc(REALLY_BIG);
Simon Xu6f224942013-12-06 11:51:16 +0800147 memset(&inmessage, 0, sizeof(inmessage));
uid5281789de9aa2003-10-23 18:42:24 +0000148 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),
Simon Xu6f224942013-12-06 11:51:16 +0800160 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
uid5281789de9aa2003-10-23 18:42:24 +0000161 }
162
163 /* Get the communication up message and the data message on the
Simon Xu6f224942013-12-06 11:51:16 +0800164 * server sockets for all the clients.
uid5281789de9aa2003-10-23 18:42:24 +0000165 */
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),
Simon Xu6f224942013-12-06 11:51:16 +0800171 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
uid5281789de9aa2003-10-23 18:42:24 +0000172
173 inmessage.msg_controllen = sizeof(incmsg);
174 error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
Simon Xu6f224942013-12-06 11:51:16 +0800175 test_check_msg_data(&inmessage, error, strlen(message)+1,
uid5281789de9aa2003-10-23 18:42:24 +0000176 MSG_EOR, stream, ppid);
177 sac = (struct sctp_assoc_change *)iov.iov_base;
178 svr_associd[i] = sac->sac_assoc_id;
179 }
180
181 /* Build up a msghdr structure we can use for all sending. */
182 outmessage.msg_name = NULL;
183 outmessage.msg_namelen = 0;
184 outmessage.msg_iov = NULL;
185 outmessage.msg_iovlen = 0;
186 outmessage.msg_control = outcmsg;
187 outmessage.msg_controllen = sizeof(outcmsg);
188 outmessage.msg_flags = 0;
189 cmsg = CMSG_FIRSTHDR(&outmessage);
190 cmsg->cmsg_level = IPPROTO_SCTP;
191 cmsg->cmsg_type = SCTP_SNDRCV;
192 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
193 outmessage.msg_controllen = cmsg->cmsg_len;
194 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
195 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
mridge64a341f2006-01-03 19:16:46 +0000196 sinfo->sinfo_flags |= SCTP_EOF;
uid5281789de9aa2003-10-23 18:42:24 +0000197
198 /* Shutdown all the associations of the server socket in a loop. */
199 for (i = 0; i < MAX_CLIENTS; i++) {
200 sinfo->sinfo_assoc_id = svr_associd[i];
201
202 /* Verify that the association is present. */
203 memset(&status, 0, sizeof(struct sctp_status));
204 status.sstat_assoc_id = sinfo->sinfo_assoc_id;
205 status_len = sizeof(struct sctp_status);
206 error = getsockopt(svr_sk, SOL_SCTP, SCTP_STATUS,
207 &status, &status_len);
208 if (error)
Simon Xu6f224942013-12-06 11:51:16 +0800209 tst_brkm(TBROK, tst_exit,
uid5281789de9aa2003-10-23 18:42:24 +0000210 "getsockopt(SCTP_STATUS): %s",
211 strerror(errno));
212
213 /* Call sendmsg() to shutdown the association. */
214 test_sendmsg(svr_sk, &outmessage, 0, 0);
215
216 /* Verify that the association is no longer present. */
217 memset(&status, 0, sizeof(struct sctp_status));
218 status.sstat_assoc_id = sinfo->sinfo_assoc_id;
219 status_len = sizeof(struct sctp_status);
Simon Xu6f224942013-12-06 11:51:16 +0800220 error = getsockopt(svr_sk, SOL_SCTP, SCTP_STATUS,
uid5281789de9aa2003-10-23 18:42:24 +0000221 &status, &status_len);
222 if ((error != -1) && (errno != EINVAL))
Simon Xu6f224942013-12-06 11:51:16 +0800223 tst_brkm(TBROK, tst_exit,
uid5281789de9aa2003-10-23 18:42:24 +0000224 "getsockopt(SCTP_STATUS) "
225 "error:%d errno:%d", error, errno);
226 }
227
228 close(svr_sk);
229
Simon Xu6f224942013-12-06 11:51:16 +0800230 /* Get the shutdown complete notification. */
uid5281789de9aa2003-10-23 18:42:24 +0000231 for (i = 0; i < MAX_CLIENTS; i++) {
232 inmessage.msg_controllen = sizeof(incmsg);
233 error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL);
234 test_check_msg_notification(&inmessage, error,
235 sizeof(struct sctp_assoc_change),
236 SCTP_ASSOC_CHANGE,
237 SCTP_SHUTDOWN_COMP);
238
239 close(clt_sk[i]);
240 }
241
Simon Xu6f224942013-12-06 11:51:16 +0800242 tst_resm(TPASS, "Graceful shutdown of associations using SCTP_EOF");
uid5281789de9aa2003-10-23 18:42:24 +0000243
Simon Xu6f224942013-12-06 11:51:16 +0800244 /* Indicate successful completion. */
245 return 0;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700246}