• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *    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>
51 #include <sys/errno.h>
52 #include <errno.h>
53 #include <netinet/sctp.h>
54 #include <sctputil.h>
55 
56 char *TCID = __FILE__;
57 int TST_TOTAL = 1;
58 int TST_CNT = 0;
59 
60 #define MAX_CLIENTS 10
61 
62 int
main(int argc,char * argv[])63 main(int argc, char *argv[])
64 {
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;
75         struct iovec out_iov;
76         int error;
77 	uint32_t ppid;
78 	uint32_t stream;
79 	struct sctp_assoc_change *sac;
80 	char *big_buffer;
81 	int i;
82         char *message = "hello, world!\n";
83 	struct sctp_status status;
84 	socklen_t status_len;
85 
86         /* Rather than fflush() throughout the code, set stdout to
87 	 * be unbuffered.
88 	 */
89 	setvbuf(stdout, NULL, _IONBF, 0);
90 
91 	/* Create and bind the server socket.  */
92         svr_sk = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
93 
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 	/* 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));
196 	sinfo->sinfo_flags |= SCTP_EOF;
197 
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)
209 			tst_brkm(TBROK, tst_exit,
210 				 "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);
220 		error = getsockopt(svr_sk, SOL_SCTP, SCTP_STATUS,
221 				   &status, &status_len);
222 		if ((error != -1) && (errno != EINVAL))
223 			tst_brkm(TBROK, tst_exit,
224 				 "getsockopt(SCTP_STATUS) "
225 				 "error:%d errno:%d", error, errno);
226 	}
227 
228 	close(svr_sk);
229 
230         /* Get the shutdown complete notification. */
231 	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 
242 	tst_resm(TPASS, "Graceful shutdown of associations using SCTP_EOF");
243 
244         /* Indicate successful completion.  */
245         return 0;
246 }
247