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.
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)
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.
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.
25 * Please send any bug reports or fixes you make to the
27 * lksctp developers <lksctp-developers@lists.sourceforge.net>
29 * Or submit a bug report through the following website:
30 * http://www.sf.net/projects/lksctp
32 * Any bugs reported to us we will try to fix... any fixes shared will
33 * be incorporated into the next SCTP release.
35 * Written or modified by:
36 * Sridhar Samudrala <sri@us.ibm.com>
39 /* This is a Functional test to verify the new SCTP interface sctp_peeloff()
40 * that can be used to branch off an association into a separate socket.
47 #include <sys/types.h>
48 #include <sys/socket.h>
50 #include <netinet/in.h>
52 #include <netinet/sctp.h>
55 char *TCID = __FILE__;
59 #define MAX_CLIENTS 10
62 main(int argc, char *argv[])
64 int svr_sk, clt_sk[MAX_CLIENTS], peeloff_sk[MAX_CLIENTS];
65 sctp_assoc_t svr_associd[MAX_CLIENTS], clt_associd[MAX_CLIENTS];
66 sockaddr_storage_t svr_loop, clt_loop[MAX_CLIENTS];
68 struct msghdr inmessage;
69 struct msghdr outmessage;
70 char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
71 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
73 struct sctp_sndrcvinfo *sinfo;
78 struct sctp_assoc_change *sac;
81 char *message = "hello, world!\n";
84 /* Rather than fflush() throughout the code, set stdout to
87 setvbuf(stdout, NULL, _IONBF, 0);
91 svr_loop.v6.sin6_family = AF_INET6;
92 svr_loop.v6.sin6_addr = in6addr_loopback;
93 svr_loop.v6.sin6_port = htons(SCTP_TESTPORT_1);
96 svr_loop.v4.sin_family = AF_INET;
97 svr_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
98 svr_loop.v4.sin_port = htons(SCTP_TESTPORT_1);
101 /* Create and bind the server socket. */
102 svr_sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
103 test_bind(svr_sk, &svr_loop.sa, sizeof(svr_loop));
105 /* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
106 test_enable_assoc_change(svr_sk);
108 /* Mark server socket as being able to accept new associations. */
109 test_listen(svr_sk, 1);
111 /* Create and bind all the client sockets. */
112 for (i = 0; i < MAX_CLIENTS; i++) {
113 clt_sk[i] = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
115 clt_loop[i].v6.sin6_family = AF_INET6;
116 clt_loop[i].v6.sin6_addr = in6addr_loopback;
117 clt_loop[i].v6.sin6_port = htons(SCTP_TESTPORT_2 + i);
119 clt_loop[i].v4.sin_family = AF_INET;
120 clt_loop[i].v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
121 clt_loop[i].v4.sin_port = htons(SCTP_TESTPORT_2 + i);
123 test_bind(clt_sk[i], &clt_loop[i].sa, sizeof(clt_loop[i]));
125 test_enable_assoc_change(clt_sk[i]);
128 /* Send the first message from all the clients to the server. This
129 * will create the associations.
131 outmessage.msg_name = &svr_loop;
132 outmessage.msg_namelen = sizeof(svr_loop);
133 outmessage.msg_iov = &out_iov;
134 outmessage.msg_iovlen = 1;
135 outmessage.msg_control = outcmsg;
136 outmessage.msg_controllen = sizeof(outcmsg);
137 outmessage.msg_flags = 0;
138 cmsg = CMSG_FIRSTHDR(&outmessage);
139 cmsg->cmsg_level = IPPROTO_SCTP;
140 cmsg->cmsg_type = SCTP_SNDRCV;
141 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
142 outmessage.msg_controllen = cmsg->cmsg_len;
143 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
144 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
145 ppid = rand(); /* Choose an arbitrary value. */
147 sinfo->sinfo_ppid = ppid;
148 sinfo->sinfo_stream = stream;
149 outmessage.msg_iov->iov_base = message;
150 outmessage.msg_iov->iov_len = strlen(message) + 1;
151 for (i = 0; i < MAX_CLIENTS; i++)
152 test_sendmsg(clt_sk[i], &outmessage, 0,
155 /* Initialize inmessage for all receives. */
156 big_buffer = test_malloc(REALLY_BIG);
157 memset(&inmessage, 0, sizeof(inmessage));
158 iov.iov_base = big_buffer;
159 iov.iov_len = REALLY_BIG;
160 inmessage.msg_iov = &iov;
161 inmessage.msg_iovlen = 1;
162 inmessage.msg_control = incmsg;
164 /* Get the communication up message on all client sockets. */
165 for (i = 0; i < MAX_CLIENTS; i++) {
166 inmessage.msg_controllen = sizeof(incmsg);
167 error = test_recvmsg(clt_sk[i], &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_associd[i] = sac->sac_assoc_id;
175 /* Get the communication up message and the data message on the
176 * server sockets for all the clients.
178 for (i = 0; i < MAX_CLIENTS; i++) {
179 inmessage.msg_controllen = sizeof(incmsg);
180 error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
181 test_check_msg_notification(&inmessage, error,
182 sizeof(struct sctp_assoc_change),
183 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
184 sac = (struct sctp_assoc_change *)iov.iov_base;
185 svr_associd[i] = sac->sac_assoc_id;
187 inmessage.msg_controllen = sizeof(incmsg);
188 error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
189 test_check_msg_data(&inmessage, error, strlen(message) + 1,
190 MSG_EOR, stream, ppid);
193 /* Branch off all the associations on the server socket to separate
194 * individual sockets.
196 for (i = 0; i < MAX_CLIENTS; i++)
197 peeloff_sk[i] = test_sctp_peeloff(svr_sk, svr_associd[i]);
199 tst_resm(TPASS, "sctp_peeloff");
202 /* Verify that a peeled off socket is not allowed to do a listen(). */
203 error = listen(peeloff_sk[0], 1);
205 tst_brkm(TBROK, tst_exit, "listen on a peeled off socket "
206 "error: %d, errno: %d", error, errno);
208 tst_resm(TPASS, "listen on a peeled off socket");
211 /* Verify that an association cannot be branched off an already
214 if ((-1 != sctp_peeloff(peeloff_sk[0], svr_associd[0])) ||
216 tst_brkm(TBROK, tst_exit, "sctp_peeloff on a peeled off "
217 "socket error:%d, errno:%d",
220 tst_resm(TPASS, "sctp_peeloff on a peeled off socket");
222 /* Send a message from all the client sockets to the server socket. */
223 for (i = 0; i < MAX_CLIENTS; i++)
224 test_sendmsg(clt_sk[i], &outmessage, 0, strlen(message)+1);
226 /* Receive the sent messages on the peeled off server sockets. */
227 for (i = 0; i < MAX_CLIENTS; i++) {
228 inmessage.msg_controllen = sizeof(incmsg);
229 error = test_recvmsg(peeloff_sk[i], &inmessage, MSG_WAITALL);
230 test_check_msg_data(&inmessage, error, strlen(message) + 1,
231 MSG_EOR, stream, ppid);
234 tst_resm(TPASS, "Receive msgs on peeled off sockets");
236 /* Send a message from all the peeled off server sockets to the client
239 for (i = 0; i < MAX_CLIENTS; i++) {
240 outmessage.msg_name = &clt_loop[i];
241 outmessage.msg_namelen = sizeof(clt_loop[i]);
242 test_sendmsg(peeloff_sk[i], &outmessage, 0, strlen(message)+1);
245 /* Receive the messages sent from the peeled of server sockets on
246 * the client sockets.
248 for (i = 0; i < MAX_CLIENTS; i++) {
249 inmessage.msg_controllen = sizeof(incmsg);
250 error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL);
251 test_check_msg_data(&inmessage, error, strlen(message) + 1,
252 MSG_EOR, stream, ppid);
255 tst_resm(TPASS, "Send msgs on peeled off sockets");
258 /* Verify that a peeled-off socket cannot initialize a new
259 * association by trying to send a message to a client that is not
260 * associated with the peeled-off socket.
261 * The message is sent to the client that is associated with the
264 outmessage.msg_name = &clt_loop[1];
265 outmessage.msg_namelen = sizeof(clt_loop[1]);
266 test_sendmsg(peeloff_sk[0], &outmessage, 0, strlen(message)+1);
268 inmessage.msg_controllen = sizeof(incmsg);
269 error = test_recvmsg(clt_sk[0], &inmessage, MSG_WAITALL);
270 test_check_msg_data(&inmessage, error, strlen(message) + 1,
271 MSG_EOR, stream, ppid);
273 tst_resm(TPASS, "peeled off socket cannot initialize a new assoc");
277 /* Close all the peeled off server sockets. */
278 for (i = 0; i < MAX_CLIENTS; i++)
279 close(peeloff_sk[i]);
281 /* Get the shutdown complete notification from all the client
284 for (i = 0; i < MAX_CLIENTS; i++) {
285 inmessage.msg_controllen = sizeof(incmsg);
286 error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL);
287 test_check_msg_notification(&inmessage, error,
288 sizeof(struct sctp_assoc_change),
295 /* Indicate successful completion. */