• 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 file has test cases to test the sctp_getladdrs (), sctp_freealddrs (),
6  * sctp_getpaddrs (), sctp_freeapaddrs () for 1-1 style sockets
7  *
8  * sctp_getladdrs () Tests:
9  * -----------------------
10  * TEST1: Bad socket descriptor
11  * TEST2: Invalid socket
12  * TEST3: Socket of different protocol
13  * TEST4: Getting the local addresses
14  *
15  * sctp_freealddrs () Tests:
16  * ------------------------
17  * TEST5: Freeing the local address
18  *
19  * sctp_getpaddrs () Tests:
20  * -----------------------
21  * TEST6: Bad socket descriptor
22  * TEST7: Invalid socket
23  * TEST8: Socket of different protocol
24  * TEST9: Getting the peers addresses
25  *
26  * sctp_freeapddrs () Tests:
27  * ------------------------
28  * TEST10: Freeing the peer's address
29  *
30  * The SCTP implementation is free software;
31  * you can redistribute it and/or modify it under the terms of
32  * the GNU General Public License as published by
33  * the Free Software Foundation; either version 2, or (at your option)
34  * any later version.
35  *
36  * The SCTP implementation is distributed in the hope that it
37  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
38  *                 ************************
39  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
40  * See the GNU General Public License for more details.
41  *
42  * You should have received a copy of the GNU General Public License
43  * along with GNU CC; see the file COPYING.  If not, write to
44  * the Free Software Foundation, 59 Temple Place - Suite 330,
45  * Boston, MA 02111-1307, USA.
46  *
47  * Please send any bug reports or fixes you make to the
48  * email address(es):
49  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
50  *
51  * Or submit a bug report through the following website:
52  *    http://www.sf.net/projects/lksctp
53  *
54  * Any bugs reported given to us we will try to fix... any fixes shared will
55  * be incorporated into the next SCTP release.
56  *
57  */
58 
59 #include <stdio.h>
60 #include <unistd.h>
61 #include <fcntl.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <sys/types.h>
65 #include <sys/socket.h>
66 #include <netinet/in.h>         /* for sockaddr_in */
67 #include <arpa/inet.h>
68 #include <errno.h>
69 #include <netinet/sctp.h>
70 #include <sys/uio.h>
71 #include <sctputil.h>
72 #include "tst_kernel.h"
73 
74 char *TCID = __FILE__;
75 int TST_TOTAL = 10;
76 int TST_CNT = 0;
77 
78 int
main(void)79 main(void)
80 {
81         int error;
82 	socklen_t len;
83 	int lstn_sk,clnt_sk,acpt_sk,pf_class,sk1;
84 	struct msghdr outmessage;
85         struct msghdr inmessage;
86         char *message = "hello, world!\n";
87         struct iovec iov_rcv;
88         struct sctp_sndrcvinfo *sinfo;
89         int msg_count;
90         char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
91         struct cmsghdr *cmsg;
92         struct iovec out_iov;
93         char * buffer_rcv;
94 	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
95 	struct sockaddr *laddrs, *paddrs;
96 	int fd, err_no = 0;
97 	char filename[21];
98 
99         struct sockaddr_in conn_addr,lstn_addr,acpt_addr;
100 	struct sockaddr_in *addr;
101 
102 	if (tst_check_driver("sctp"))
103 		tst_brkm(TCONF, tst_exit, "sctp driver not available");
104 
105 	/* Rather than fflush() throughout the code, set stdout to
106          * be unbuffered.
107          */
108         setvbuf(stdout, NULL, _IONBF, 0);
109         setvbuf(stderr, NULL, _IONBF, 0);
110 
111         pf_class = PF_INET;
112 
113 	/*Creating a regular socket*/
114 	clnt_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
115 
116 	/*Creating a listen socket*/
117         lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
118 
119 	conn_addr.sin_family = AF_INET;
120         conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
121         conn_addr.sin_port = htons(SCTP_TESTPORT_1);
122 
123 	lstn_addr.sin_family = AF_INET;
124         lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
125         lstn_addr.sin_port = htons(SCTP_TESTPORT_1);
126 
127 	/*Binding the listen socket*/
128 	test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));
129 
130 	/*Listening many sockets as we are calling too many connect here*/
131 	test_listen(lstn_sk, 1);
132 
133 	len = sizeof(struct sockaddr_in);
134 
135 	test_connect(clnt_sk, (struct sockaddr *) &conn_addr, len);
136 
137 	acpt_sk = test_accept(lstn_sk, (struct sockaddr *) &acpt_addr, &len);
138 
139 	memset(&inmessage, 0, sizeof(inmessage));
140         buffer_rcv = malloc(REALLY_BIG);
141 
142         iov_rcv.iov_base = buffer_rcv;
143         iov_rcv.iov_len = REALLY_BIG;
144         inmessage.msg_iov = &iov_rcv;
145         inmessage.msg_iovlen = 1;
146         inmessage.msg_control = incmsg;
147         inmessage.msg_controllen = sizeof(incmsg);
148 
149         msg_count = strlen(message) + 1;
150 
151 	memset(&outmessage, 0, sizeof(outmessage));
152         outmessage.msg_name = &lstn_addr;
153         outmessage.msg_namelen = sizeof(lstn_addr);
154         outmessage.msg_iov = &out_iov;
155         outmessage.msg_iovlen = 1;
156         outmessage.msg_control = outcmsg;
157         outmessage.msg_controllen = sizeof(outcmsg);
158         outmessage.msg_flags = 0;
159 
160         cmsg = CMSG_FIRSTHDR(&outmessage);
161         cmsg->cmsg_level = IPPROTO_SCTP;
162         cmsg->cmsg_type = SCTP_SNDRCV;
163         cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
164         outmessage.msg_controllen = cmsg->cmsg_len;
165         sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
166         memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
167 
168         outmessage.msg_iov->iov_base = message;
169         outmessage.msg_iov->iov_len = msg_count;
170 
171 	test_sendmsg(clnt_sk, &outmessage, MSG_NOSIGNAL, msg_count);
172 
173 	test_recvmsg(acpt_sk, &inmessage, MSG_NOSIGNAL);
174 
175 	/*sctp_getladdrs() TEST1: Bad socket descriptor, EBADF Expected error*/
176 	error = sctp_getladdrs(-1, 0, &laddrs);
177 	if (error != -1 || errno != EBADF)
178 		tst_brkm(TBROK, tst_exit, "sctp_getladdrs with a bad socket "
179 			 "descriptor error:%d, errno:%d", error, errno);
180 
181 	tst_resm(TPASS, "sctp_getladdrs() with a bad socket descriptor - "
182 		 "EBADF");
183 
184 	/*sctp_getladdrs() TEST2: Invalid socket, ENOTSOCK Expected error*/
185 	strcpy(filename, "/tmp/sctptest.XXXXXX");
186 	fd = mkstemp(filename);
187 	if (fd == -1)
188 		tst_brkm(TBROK, tst_exit, "Failed to mkstemp %s: %s",
189 				filename, strerror(errno));
190 	error = sctp_getladdrs(fd, 0, &laddrs);
191 	if (error == -1)
192 		err_no = errno;
193 	close(fd);
194 	unlink(filename);
195 	if (error != -1 || err_no != ENOTSOCK)
196 		tst_brkm(TBROK, tst_exit, "sctp_getladdrs with invalid socket "
197 			 "error:%d, errno:%d", error, err_no);
198 
199 	tst_resm(TPASS, "sctp_getladdrs() with invalid socket - ENOTSOCK");
200 
201 	/*sctp_getladdrs() TEST3: socket of different protocol
202 	EOPNOTSUPP Expected error*/
203         sk1 = socket(pf_class, SOCK_STREAM, IPPROTO_IP);
204 	error = sctp_getladdrs(sk1, 0, &laddrs);
205 	if (error != -1 || errno != EOPNOTSUPP)
206 		tst_brkm(TBROK, tst_exit, "sctp_getladdrs with socket of "
207 			 "different protocol error:%d, errno:%d", error, errno);
208 
209 	tst_resm(TPASS, "sctp_getladdrs() with socket of different protocol - "
210 		 "EOPNOTSUPP");
211 
212 	/*sctp_getladdrs() TEST4: Getting the local addresses*/
213 	error = sctp_getladdrs(lstn_sk, 0, &laddrs);
214 	if (error < 0)
215 		tst_brkm(TBROK, tst_exit, "sctp_getladdrs with valid socket "
216 			 "error:%d, errno:%d", error, errno);
217 
218 	addr = (struct sockaddr_in *)laddrs;
219 	if (addr->sin_port != lstn_addr.sin_port ||
220 	    addr->sin_family != lstn_addr.sin_family ||
221 	    addr->sin_addr.s_addr != lstn_addr.sin_addr.s_addr)
222 		tst_brkm(TBROK, tst_exit, "sctp_getladdrs comparision failed");
223 
224 	tst_resm(TPASS, "sctp_getladdrs() - SUCCESS");
225 
226 	/*sctp_freealddrs() TEST5: freeing the local address*/
227 	if ((sctp_freeladdrs(laddrs)) < 0)
228 		tst_brkm(TBROK, tst_exit, "sctp_freeladdrs "
229 			 "error:%d, errno:%d", error, errno);
230 
231 	tst_resm(TPASS, "sctp_freeladdrs() - SUCCESS");
232 
233 	/*sctp_getpaddrs() TEST6: Bad socket descriptor, EBADF Expected error*/
234 	error = sctp_getpaddrs(-1, 0, &paddrs);
235 	if (error != -1 || errno != EBADF)
236 		tst_brkm(TBROK, tst_exit, "sctp_getpaddrs with a bad socket "
237 			 "descriptor error:%d, errno:%d", error, errno);
238 
239 	tst_resm(TPASS, "sctp_getpaddrs() with a bad socket descriptor - "
240 		 "EBADF");
241 
242 	/*sctp_getpaddrs() TEST7: Invalid socket, ENOTSOCK Expected error*/
243 	strcpy(filename, "/tmp/sctptest.XXXXXX");
244 	fd = mkstemp(filename);
245 	if (fd == -1)
246 		tst_brkm(TBROK, tst_exit, "Failed to mkstemp %s: %s",
247 				filename, strerror(errno));
248 	error = sctp_getpaddrs(fd, 0, &paddrs);
249 	if (error == -1)
250 		err_no = errno;
251 	close(fd);
252 	unlink(filename);
253 	if (error != -1 || err_no != ENOTSOCK)
254 		tst_brkm(TBROK, tst_exit, "sctp_getpaddrs with invalid socket "
255 			 "error:%d, errno:%d", error, err_no);
256 
257 	tst_resm(TPASS, "sctp_getpaddrs() with invalid socket - ENOTSOCK");
258 
259 	/*sctp_getpaddrs() TEST8: socket of different protocol
260 	EOPNOTSUPP Expected error*/
261 	error = sctp_getpaddrs(sk1, 0, &laddrs);
262 	if (error != -1 || errno != EOPNOTSUPP)
263 		tst_brkm(TBROK, tst_exit, "sctp_getpaddrs with socket of "
264 			 "different protocol error:%d, errno:%d", error, errno);
265 
266 	tst_resm(TPASS, "sctp_getpaddrs() with socket of different protocol - "
267 		 "EOPNOTSUPP");
268 
269 	/*sctp_getpaddrs() TEST9: Getting the peer addresses*/
270 	error = sctp_getpaddrs(acpt_sk, 0, &paddrs);
271 	if (error < 0)
272 		tst_brkm(TBROK, tst_exit, "sctp_getpaddrs with valid socket "
273 			 "error:%d, errno:%d", error, errno);
274 
275 	addr = (struct sockaddr_in *)paddrs;
276 	if (addr->sin_port != acpt_addr.sin_port ||
277             addr->sin_family != acpt_addr.sin_family ||
278             addr->sin_addr.s_addr != acpt_addr.sin_addr.s_addr)
279 		tst_brkm(TBROK, tst_exit, "sctp_getpaddrs comparision failed");
280 
281 	tst_resm(TPASS, "sctp_getpaddrs() - SUCCESS");
282 
283 	/*sctp_freeapddrs() TEST10: freeing the peer address*/
284 	if ((sctp_freepaddrs(paddrs)) < 0)
285 		tst_brkm(TBROK, tst_exit, "sctp_freepaddrs "
286 			 "error:%d, errno:%d", error, errno);
287 
288 	tst_resm(TPASS, "sctp_freepaddrs() - SUCCESS");
289 
290 	close(clnt_sk);
291 
292 	return 0;
293 }
294