• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SCTP kernel Implementation
2  * (C) Copyright IBM Corp. 2002, 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  * Copyright (c) 2001 La Monte H.P. Yarroll
8  *
9  * The SCTP implementation is free software;
10  * you can redistribute it and/or modify it under the terms of
11  * the GNU General Public License as published by
12  * the Free Software Foundation; either version 2, or (at your option)
13  * any later version.
14  *
15  * The SCTP implementation is distributed in the hope that it
16  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
17  *                 ************************
18  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with GNU CC; see the file COPYING.  If not, write to
23  * the Free Software Foundation, 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  *
26  * Please send any bug reports or fixes you make to the
27  * email address(es):
28  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
29  *
30  * Or submit a bug report through the following website:
31  *    http://www.sf.net/projects/lksctp
32  *
33  * Any bugs reported to us we will try to fix... any fixes shared will
34  * be incorporated into the next SCTP release.
35  *
36  * Written or modified by:
37  *    La Monte H.P. Yarroll <piggy@acm.org>
38  *    Karl Knutson <karl@athena.chicago.il.us>
39  *    Jon Grimm <jgrimm@us.ibm.com>
40  *    Sridhar Samudrala <sri@us.ibm.com>
41  *    Daisy Chang <daisyc@us.ibm.com>
42  */
43 
44 /* This is a functional test to verify binding a socket with INADDRY_ANY
45  * address and send messages.
46  */
47 
48 #include <stdio.h>
49 #include <unistd.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <sys/uio.h>
55 #include <netinet/in.h>
56 #include <errno.h>
57 #include <netinet/sctp.h>
58 #include <sctputil.h>
59 #include "tst_kernel.h"
60 
61 char *TCID = __FILE__;
62 int TST_TOTAL = 2;
63 int TST_CNT = 0;
64 
65 int
main(void)66 main(void)
67 {
68         int sk1, sk2;
69         sockaddr_storage_t loop;
70         sockaddr_storage_t anyaddr;
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         struct iovec iov;
78         struct msghdr inmessage;
79         char *message = "hello, world!\n";
80         char *telephone = "Watson, come here!  I need you!\n";
81         char *telephone_resp = "I already brought your coffee...\n";
82         int error;
83 	int pf_class;
84 	uint32_t ppid;
85 	uint32_t stream;
86 	socklen_t namelen;
87 
88 	if (tst_check_driver("sctp"))
89 		tst_brkm(TCONF, tst_exit, "sctp driver not available");
90 
91         /* Rather than fflush() throughout the code, set stdout to
92 	 * be unbuffered.
93 	 */
94 	setvbuf(stdout, NULL, _IONBF, 0);
95 
96 	/* Set some basic values which depend on the address family. */
97 #if TEST_V6
98 	pf_class = PF_INET6;
99 
100         loop.v6.sin6_family = AF_INET6;
101         loop.v6.sin6_addr = (struct in6_addr)SCTP_IN6ADDR_LOOPBACK_INIT;
102         loop.v6.sin6_port = 0;
103 
104         anyaddr.v6.sin6_family = AF_INET6;
105         anyaddr.v6.sin6_addr = (struct in6_addr)SCTP_IN6ADDR_ANY_INIT;
106         anyaddr.v6.sin6_port = 0;
107 #else
108 	pf_class = PF_INET;
109 
110         loop.v4.sin_family = AF_INET;
111         loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
112         loop.v4.sin_port = 0;
113 
114         anyaddr.v4.sin_family = AF_INET;
115         anyaddr.v4.sin_addr.s_addr = INADDR_ANY;
116         anyaddr.v4.sin_port = 0;
117 #endif /* TEST_V6 */
118 
119         /* Create the two endpoints which will talk to each other.  */
120         sk1 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
121         sk2 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
122 
123 	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
124 	test_enable_assoc_change(sk1);
125 	test_enable_assoc_change(sk2);
126 
127         /* Bind these sockets to the test ports.  */
128         test_bind(sk1, &loop.sa, sizeof(loop));
129         test_bind(sk2, &anyaddr.sa, sizeof(anyaddr));
130 
131 	tst_resm(TPASS, "bind INADDR_ANY address");
132 
133  	/* Mark sk2 as being able to accept new associations */
134 	test_listen(sk2, 1);
135 
136 	/* Now use getsockaname() to retrieve the ephmeral ports. */
137 	namelen = sizeof(loop);
138 	error = getsockname(sk1, &loop.sa, &namelen);
139 	if (error != 0)
140 		tst_brkm(TBROK, tst_exit, "getsockname: %s", strerror(errno));
141 
142 	namelen = sizeof(anyaddr);
143 	error = getsockname(sk2, &anyaddr.sa, &namelen);
144 	if (error != 0)
145 		tst_brkm(TBROK, tst_exit, "getsockname: %s", strerror(errno));
146 
147 #if TEST_V6
148 	loop.v6.sin6_port = anyaddr.v6.sin6_port;
149 #else
150         loop.v4.sin_port = anyaddr.v4.sin_port;
151 #endif
152 
153         /* Send the first message.  This will create the association.  */
154         outmessage.msg_name = &loop;
155         outmessage.msg_namelen = sizeof(loop);
156         outmessage.msg_iov = &out_iov;
157         outmessage.msg_iovlen = 1;
158         outmessage.msg_control = outcmsg;
159         outmessage.msg_controllen = sizeof(outcmsg);
160         outmessage.msg_flags = 0;
161 	cmsg = CMSG_FIRSTHDR(&outmessage);
162 	cmsg->cmsg_level = IPPROTO_SCTP;
163 	cmsg->cmsg_type = SCTP_SNDRCV;
164 	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
165 	outmessage.msg_controllen = cmsg->cmsg_len;
166 	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
167 	memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
168 	ppid = rand(); /* Choose an arbitrary value. */
169 	stream = 1;
170 	sinfo->sinfo_ppid = ppid;
171 	sinfo->sinfo_stream = stream;
172         outmessage.msg_iov->iov_base = message;
173         outmessage.msg_iov->iov_len = strlen(message) + 1;
174         test_sendmsg(sk1, &outmessage, 0, strlen(message)+1);
175 
176 	/* Initialize inmessage for all receives. */
177         memset(&inmessage, 0, sizeof(inmessage));
178         iov.iov_base = test_malloc(REALLY_BIG);
179         iov.iov_len = REALLY_BIG;
180         inmessage.msg_iov = &iov;
181         inmessage.msg_iovlen = 1;
182         inmessage.msg_control = incmsg;
183 
184         /* Get the communication up message on sk2.  */
185         inmessage.msg_controllen = sizeof(incmsg);
186         error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
187 	test_check_msg_notification(&inmessage, error,
188 				    sizeof(struct sctp_assoc_change),
189 				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
190 
191         /* Get the communication up message on sk1.  */
192         inmessage.msg_controllen = sizeof(incmsg);
193         error = test_recvmsg(sk1, &inmessage, MSG_WAITALL);
194 	test_check_msg_notification(&inmessage, error,
195 				    sizeof(struct sctp_assoc_change),
196 				    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
197 
198         /* Get the first message which was sent.  */
199         inmessage.msg_controllen = sizeof(incmsg);
200         error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
201         test_check_msg_data(&inmessage, error, strlen(message) + 1,
202 			    MSG_EOR, stream, ppid);
203 
204        /* Send 2 messages.  */
205         outmessage.msg_name = &loop;
206         outmessage.msg_namelen = sizeof(loop);
207         outmessage.msg_controllen = sizeof(outcmsg);
208         outmessage.msg_flags = 0;
209 	cmsg = CMSG_FIRSTHDR(&outmessage);
210 	cmsg->cmsg_level = IPPROTO_SCTP;
211 	cmsg->cmsg_type = SCTP_SNDRCV;
212 	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
213 	outmessage.msg_controllen = cmsg->cmsg_len;
214 	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
215 	memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
216 	ppid++;
217 	stream++;
218 	sinfo->sinfo_ppid = ppid;
219 	sinfo->sinfo_stream = stream;
220 	outmessage.msg_iov->iov_base = telephone;
221         outmessage.msg_iov->iov_len = strlen(telephone) + 1;
222 	test_sendmsg(sk1, &outmessage, 0, strlen(telephone)+1);
223 
224 	outmessage.msg_iov->iov_base = telephone_resp;
225         outmessage.msg_iov->iov_len = strlen(telephone_resp) + 1;
226 	test_sendmsg(sk1, &outmessage, 0, strlen(telephone_resp)+1);
227 
228         /* Get those two messages.  */
229 	inmessage.msg_controllen = sizeof(incmsg);
230         error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
231         test_check_msg_data(&inmessage, error, strlen(telephone) + 1,
232 			    MSG_EOR, stream, ppid);
233 
234 	inmessage.msg_controllen = sizeof(incmsg);
235         error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
236         test_check_msg_data(&inmessage, error, strlen(telephone_resp) + 1,
237 			    MSG_EOR, stream, ppid);
238 
239         /* Shut down the link.  */
240         close(sk1);
241 
242         /* Get the shutdown complete notification. */
243 	inmessage.msg_controllen = sizeof(incmsg);
244         error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
245 	test_check_msg_notification(&inmessage, error,
246 				    sizeof(struct sctp_assoc_change),
247 				    SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP);
248 
249         close(sk2);
250 
251 	tst_resm(TPASS, "send msgs from a socket with INADDR_ANY bind address");
252 
253         /* Indicate successful completion.  */
254         return 0;
255 }
256