• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SCTP kernel Implementation
2  * Copyright (c) 2003 Hewlett-Packard Development Company, L.P
3  * (C) Copyright IBM Corp. 2004
4  *
5  * This test tests the events for 1-1 style sockets.
6  *
7  * The SCTP implementation is free software;
8  * you can redistribute it and/or modify it under the terms of
9  * the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * The SCTP implementation is distributed in the hope that it
14  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
15  *                 ************************
16  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17  * See the GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GNU CC; see the file COPYING.  If not, write to
21  * the Free Software Foundation, 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  *
24  * Please send any bug reports or fixes you make to the
25  * email address(es):
26  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
27  *
28  * Or submit a bug report through the following website:
29  *    http://www.sf.net/projects/lksctp
30  *
31  *
32  * Any bugs reported given to us we will try to fix... any fixes shared will
33  * be incorporated into the next SCTP release.
34  *
35  */
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <stdlib.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>         /* needed by linux/sctp.h */
41 #include <sys/uio.h>
42 #include <netinet/in.h>         /* for sockaddr_in */
43 #include <errno.h>
44 #include <netinet/sctp.h>
45 #include <sctputil.h>
46 #include <string.h>
47 #include "tst_kernel.h"
48 
49 char *TCID = __FILE__;
50 int TST_TOTAL = 4;
51 int TST_CNT = 0;
52 
53 int
main(void)54 main(void)
55 {
56 	int svr_sk, clt_sk,acpt_sk;
57 	struct sockaddr_in svr_loop, clt_loop,acpt_loop;
58 	struct iovec iov, out_iov;
59 	struct msghdr inmessage, outmessage;
60 	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
61 	char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
62 	int error;
63 	socklen_t len;
64 	char *big_buffer;
65 	struct sctp_event_subscribe event;
66 	struct cmsghdr *cmsg;
67 	struct sctp_sndrcvinfo *sinfo;
68 	char *message = "hello, world!\n";
69 	uint32_t ppid;
70 	uint32_t stream;
71 
72 	if (tst_check_driver("sctp"))
73 		tst_brkm(TCONF, tst_exit, "sctp driver not available");
74 
75         /* Rather than fflush() throughout the code, set stdout to
76 	 * be unbuffered.
77 	 */
78 	setvbuf(stdout, NULL, _IONBF, 0);
79 
80 	/* Initialize the server and client addresses. */
81 	svr_loop.sin_family = AF_INET;
82 	svr_loop.sin_addr.s_addr = SCTP_IP_LOOPBACK;
83 	svr_loop.sin_port = htons(SCTP_TESTPORT_1);
84 
85 	clt_loop.sin_family = AF_INET;
86 	clt_loop.sin_addr.s_addr = SCTP_IP_LOOPBACK;
87 	clt_loop.sin_port = htons(SCTP_TESTPORT_1);
88 
89 	/* Create and bind the server socket.  */
90         svr_sk = test_socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
91 	test_bind(svr_sk, (struct sockaddr *) &svr_loop, sizeof(svr_loop));
92 
93 	/* Mark server socket as being able to accept new associations.  */
94 	test_listen(svr_sk, 3);
95 
96 	/* Create the client socket.  */
97 	clt_sk = test_socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
98 
99 	event.sctp_data_io_event = 1;
100 	event.sctp_association_event = 1;
101 	event.sctp_shutdown_event = 1;
102 	len = sizeof(struct sctp_event_subscribe);
103 	test_setsockopt(svr_sk, SCTP_EVENTS, &event, len);
104 	test_setsockopt(clt_sk, SCTP_EVENTS, &event, len);
105 
106 	len = sizeof(struct sockaddr_in);
107 	test_connect(clt_sk, (struct sockaddr *) &clt_loop, len);
108 
109 	acpt_sk = test_accept(svr_sk, (struct sockaddr *) &acpt_loop, &len);
110 
111 	/* Build up a msghdr structure we can use for all sending.  */
112 	memset(&outmessage, 0, sizeof(outmessage));
113 	outmessage.msg_name = &svr_loop;
114 	outmessage.msg_namelen = sizeof(svr_loop);
115 	outmessage.msg_iov = &out_iov;
116 	outmessage.msg_iovlen = 1;
117 	outmessage.msg_control = outcmsg;
118 	outmessage.msg_controllen = sizeof(outcmsg);
119 	outmessage.msg_flags = 0;
120 
121 	cmsg = CMSG_FIRSTHDR(&outmessage);
122 	cmsg->cmsg_level = IPPROTO_SCTP;
123 	cmsg->cmsg_type = SCTP_SNDRCV;
124 	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
125 	outmessage.msg_controllen = cmsg->cmsg_len;
126 	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
127 	memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
128 	ppid = rand(); /* Choose an arbitrary value. */
129 	stream = 1;
130 
131 	sinfo->sinfo_ppid = ppid;
132 	sinfo->sinfo_stream = stream;
133 
134 	outmessage.msg_iov->iov_base = message;
135 	outmessage.msg_iov->iov_len = (strlen(message) + 1);
136 
137 	/* Send . This will create the association*/
138 	test_sendmsg(clt_sk, &outmessage, 0, strlen(message)+1);
139 
140         memset(&inmessage, 0, sizeof(inmessage));
141 	/* NOW initialize inmessage with enough space for DATA... */
142 	big_buffer = malloc(REALLY_BIG);
143 	if (!big_buffer) { DUMP_CORE; }
144 
145 	/* Let's do a test to do a recvmsg when we are not listening and
146 	 * when we have no associations.
147 	 */
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 	inmessage.msg_controllen = sizeof(incmsg);
154 
155 	error = test_recvmsg(clt_sk, &inmessage, MSG_WAITALL);
156 	test_check_msg_notification(&inmessage,
157                                     error,
158                                     sizeof(struct sctp_assoc_change),
159                                     SCTP_ASSOC_CHANGE,
160                                     SCTP_COMM_UP);
161 
162 	tst_resm(TPASS, "COMM_UP notification on client socket - SUCCESS");
163 
164 	error = test_recvmsg(acpt_sk, &inmessage, MSG_WAITALL);
165 	test_check_msg_notification(&inmessage,
166                                     error,
167                                     sizeof(struct sctp_assoc_change),
168                                     SCTP_ASSOC_CHANGE,
169                                     SCTP_COMM_UP);
170 
171 	tst_resm(TPASS, "COMM_UP notification on server socket - SUCCESS");
172 
173 	inmessage.msg_control = incmsg;
174 	inmessage.msg_controllen = sizeof(incmsg);
175 	error = test_recvmsg(acpt_sk, &inmessage, MSG_WAITALL);
176         test_check_msg_data(&inmessage, error, strlen(message) + 1,
177                             MSG_EOR, stream, ppid);
178 
179 	tst_resm(TPASS, "Data message on server socket - SUCCESS");
180 
181 	close(clt_sk);
182 	error = test_recvmsg(acpt_sk, &inmessage, MSG_WAITALL);
183 	test_check_msg_notification(&inmessage,
184                                     error,
185                                     sizeof(struct sctp_shutdown_event),
186                                     SCTP_SHUTDOWN_EVENT,
187                                     0);
188 
189 	tst_resm(TPASS, "SHUTDOWN notification on accepted socket - SUCCESS");
190 	close(svr_sk);
191 	close(acpt_sk);
192 
193 	return 0;
194 }
195