• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SCTP kernel Implementation
2  * (C) Copyright IBM Corp. 2004
3  * Copyright (c) 1999-2001 Motorola, Inc.
4  *
5  * The SCTP implementation is free software;
6  * you can redistribute it and/or modify it under the terms of
7  * the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * The SCTP implementation is distributed in the hope that it
12  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
13  *                 ************************
14  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  * See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU CC; see the file COPYING.  If not, write to
19  * the Free Software Foundation, 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * Please send any bug reports or fixes you make to the
23  * email address(es):
24  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
25  *
26  * Or submit a bug report through the following website:
27  *    http://www.sf.net/projects/lksctp
28  *
29  * Any bugs reported given to us we will try to fix... any fixes shared will
30  * be incorporated into the next SCTP release.
31  *
32  * Written or modified by:
33  *    Sridhar Samudrala		<sri@us.ibm.com>
34  */
35 
36 /* This is a kernel test to verify getsockname() and getpeername() interfaces
37  * for single-homed one-to-one style sockets.
38  */
39 
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <fcntl.h>
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <sys/uio.h>
48 #include <netinet/in.h>
49 #include <errno.h>
50 #include <netinet/sctp.h>
51 #include <sctputil.h>
52 #include "tst_kernel.h"
53 
54 char *TCID = __FILE__;
55 int TST_TOTAL = 13;
56 int TST_CNT = 0;
57 
58 #define MAX_CLIENTS 10
59 
60 int
main(void)61 main(void)
62 {
63 	int clt_sk, svr_sk, accept_sk;
64 	sockaddr_storage_t svr_loop, accept_loop;
65 	sockaddr_storage_t svr_local_addr, svr_peer_addr;
66 	sockaddr_storage_t clt_local_addr, clt_peer_addr;
67 	socklen_t len;
68 	int error;
69 	int pf_class;
70 	int fd, err_no = 0;
71 	char filename[21];
72 
73 	if (tst_check_driver("sctp"))
74 		tst_brkm(TCONF, tst_exit, "sctp driver not available");
75 
76         /* Rather than fflush() throughout the code, set stdout to
77 	 * be unbuffered.
78 	 */
79 	setvbuf(stdout, NULL, _IONBF, 0);
80 
81 	/* Initialize the server and client addresses. */
82 #if TEST_V6
83 	pf_class = PF_INET6;
84         svr_loop.v6.sin6_family = AF_INET6;
85         svr_loop.v6.sin6_addr = (struct in6_addr)SCTP_IN6ADDR_ANY_INIT;
86         svr_loop.v6.sin6_port = htons(SCTP_TESTPORT_1);
87 #else
88 	pf_class = PF_INET;
89 	svr_loop.v4.sin_family = AF_INET;
90 	svr_loop.v4.sin_addr.s_addr = INADDR_ANY;
91 	svr_loop.v4.sin_port = htons(SCTP_TESTPORT_1);
92 #endif
93 
94 	/* Create and bind the listening server socket.  */
95         svr_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
96 	test_bind(svr_sk, &svr_loop.sa, sizeof(svr_loop));
97 
98 	memset(&svr_local_addr, 0, sizeof(svr_local_addr));
99 	len = sizeof(svr_local_addr);
100 	/* Verify that getsockname() on an unconnected socket works fine. */
101 	error = getsockname(svr_sk, (struct sockaddr *)&svr_local_addr, &len);
102 	if (0 != error)
103 		tst_brkm(TBROK, tst_exit, "getsockname: %s", strerror(errno));
104 
105 	tst_resm(TPASS, "getsockname on an unconnected socket");
106 
107 	memset(&svr_peer_addr, 0, sizeof(svr_peer_addr));
108 	len = sizeof(svr_peer_addr);
109 	/* Verify that getpeername() on an unconnected socket fails. */
110 	error = getpeername(svr_sk, (struct sockaddr *)&svr_peer_addr, &len);
111 	if ((-1 != error) || (ENOTCONN != errno))
112 		tst_brkm(TBROK, tst_exit, "getpeername on an unconnected "
113 			 "socket error:%d, errno:%d", error, errno);
114 
115 	tst_resm(TPASS, "getpeername on an unconnected socket");
116 
117 	/* Mark svr_sk as being able to accept new associations.  */
118 	test_listen(svr_sk, 5);
119 
120 	/* Create the client socket.  */
121 	clt_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
122 
123 	/* Do a blocking connect from clt_sk to svr_sk */
124 #if TEST_V6
125 	svr_loop.v6.sin6_addr = in6addr_loopback;
126 #else
127 	svr_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
128 #endif
129 	test_connect(clt_sk, &svr_loop.sa, sizeof(svr_loop));
130 
131 	memset(&clt_local_addr, 0, sizeof(clt_local_addr));
132 	len = sizeof(clt_local_addr);
133 	/* Get the client's local address. */
134 	error = getsockname(clt_sk, (struct sockaddr *)&clt_local_addr, &len);
135 	if (0 != error)
136 		tst_brkm(TBROK, tst_exit, "getsockname on a connected client "
137 			 "socket: %s", strerror(errno));
138 
139 	tst_resm(TPASS, "getsockname on a connected client socket");
140 
141 	memset(&clt_peer_addr, 0, sizeof(clt_peer_addr));
142 	len = sizeof(clt_peer_addr);
143 	/* Get the client's peer address. */
144 	error = getpeername(clt_sk, (struct sockaddr *)&clt_peer_addr, &len);
145 	if (0 != error)
146 		tst_brkm(TBROK, tst_exit, "getpeername on a connected client "
147 			 "socket: %s", strerror(errno));
148 
149 	tst_resm(TPASS, "getpeername on a connected client socket");
150 
151 	/* Extract the association on the listening socket as a new socket. */
152 	len = sizeof(accept_loop);
153 	accept_sk = test_accept(svr_sk, &accept_loop.sa, &len);
154 
155 	memset(&svr_local_addr, 0, sizeof(svr_local_addr));
156 	len = sizeof(svr_local_addr);
157 	/* Get the server's local address. */
158 	error = getsockname(accept_sk, (struct sockaddr *)&svr_local_addr,
159 				&len);
160 	if (0 != error)
161 		tst_brkm(TBROK, tst_exit, "getsockname on a connected server "
162 			 "socket: %s", strerror(errno));
163 
164 	tst_resm(TPASS, "getsockname on a connected server socket");
165 
166 	memset(&svr_peer_addr, 0, sizeof(svr_peer_addr));
167 	len = sizeof(svr_peer_addr);
168 	/* Get the server's peer address. */
169 	error = getpeername(accept_sk, (struct sockaddr *)&svr_peer_addr,
170 				&len);
171 	if (0 != error)
172 		tst_brkm(TBROK, tst_exit, "getpeername on a connected server "
173 			 "socket: %s", strerror(errno));
174 
175 	tst_resm(TPASS, "getpeername on a connected server socket");
176 
177 	if (svr_local_addr.v4.sin_port != clt_peer_addr.v4.sin_port)
178 		tst_brkm(TBROK, tst_exit, "Server's local port(%d) doesn't "
179 			 "match Client's peer port(%d)\n",
180 			 svr_local_addr.v4.sin_port, clt_peer_addr.v4.sin_port);
181 
182 	if (svr_peer_addr.v4.sin_port != clt_local_addr.v4.sin_port)
183 		tst_brkm(TBROK, tst_exit, "Server's peer port(%d) doesn't "
184 			 "match Client's local port(%d)\n",
185 			 svr_peer_addr.v4.sin_port, clt_local_addr.v4.sin_port);
186 #if TEST_V6
187 	if (memcmp(&svr_local_addr, &clt_peer_addr, len) != 0)
188 		tst_brkm(TBROK, tst_exit, "Server's local address and client's "
189 			 "peer addresses do not match\n");
190 
191 	if (memcmp(&svr_peer_addr, &clt_local_addr, len) != 0)
192 		tst_brkm(TBROK, tst_exit, "Server's peer address and client's "
193 			 "local addresses do not match\n");
194 #else
195 	if (svr_local_addr.v4.sin_addr.s_addr !=
196 		 		clt_peer_addr.v4.sin_addr.s_addr)
197 		tst_brkm(TBROK, tst_exit, "Server's local address and client's "
198 			 "peer addresses do not match\n");
199 	if (svr_peer_addr.v4.sin_addr.s_addr !=
200 		 		clt_local_addr.v4.sin_addr.s_addr)
201 		tst_brkm(TBROK, tst_exit, "Server's peer address and client's "
202 			 "local addresses do not match\n");
203 #endif
204 	tst_resm(TPASS, "getsockname/getpeername server/client match");
205 
206 	memset(&clt_local_addr, 0, sizeof(clt_local_addr));
207 	len = sizeof(clt_local_addr);
208 	/*getsockname():  Bad socket descriptor, EBADF expected error*/
209 	error = getsockname(-1, (struct sockaddr *)&clt_local_addr, &len);
210 	if (error != -1 || errno != EBADF)
211 		tst_brkm(TBROK, tst_exit, "getsockname on a bad socket "
212 			 "descriptor. error:%d errno:%d", error, errno);
213 
214 	tst_resm(TPASS, "getsockname on a bad socket descriptor - EBADF");
215 
216 	/*getsockname(): Invalid socket, ENOTSOCK expected error*/
217 	strcpy(filename, "/tmp/sctptest.XXXXXX");
218 	fd = mkstemp(filename);
219 	if (fd == -1)
220 		tst_brkm(TBROK, tst_exit, "Failed to mkstemp %s: %s",
221 				filename, strerror(errno));
222 	error = getsockname(fd, (struct sockaddr *)&clt_local_addr, &len);
223 	if (error == -1)
224 		err_no = errno;
225 	close(fd);
226 	unlink(filename);
227 	if (error != -1 || err_no != ENOTSOCK)
228 		tst_brkm(TBROK, tst_exit, "getsockname on an invalid socket "
229 			 "error:%d errno:%d", error, err_no);
230 
231 	tst_resm(TPASS, "getsockname on an invalid socket - ENOTSOCK");
232 
233 	/*getsockname(): Invalid structure, EFAULT expected error*/
234 	error = getsockname(clt_sk, (struct sockaddr *)-1, &len);
235 	if (error != -1 || errno != EFAULT)
236 		tst_brkm(TBROK, tst_exit, "getsockname with invalid buffer "
237 			 "error:%d errno:%d", error, errno);
238 
239 	tst_resm(TPASS, "getsockname with invalid buffer - EFAULT");
240 
241 	memset(&clt_peer_addr, 0, sizeof(clt_peer_addr));
242 	len = sizeof(clt_peer_addr);
243 	/*getpeername():  Bad socket descriptor, EBADF expected error*/
244 	error = getpeername(-1, (struct sockaddr *)&clt_local_addr, &len);
245 	if (error != -1 || errno != EBADF)
246 		tst_brkm(TBROK, tst_exit, "getpeername on a bad socket "
247 			 "descriptor. error:%d errno:%d", error, errno);
248 
249 	tst_resm(TPASS, "getpeername on a bad socket descriptor - EBADF");
250 
251 	/*getpeername(): Invalid socket, ENOTSOCK expected error*/
252 	strcpy(filename, "/tmp/sctptest.XXXXXX");
253 	fd = mkstemp(filename);
254 	if (fd == -1)
255 		tst_brkm(TBROK, tst_exit, "Failed to mkstemp %s: %s",
256 				filename, strerror(errno));
257 	error = getpeername(fd, (struct sockaddr *)&clt_local_addr, &len);
258 	if (error == -1)
259 		err_no = errno;
260 	close(fd);
261 	unlink(filename);
262 	if (error != -1 || err_no != ENOTSOCK)
263 		tst_brkm(TBROK, tst_exit, "getpeername on an invalid socket "
264 			 "error:%d errno:%d", error, err_no);
265 
266 	tst_resm(TPASS, "getpeername on an invalid socket - ENOTSOCK");
267 
268 	/*getpeername(): Invalid structure, EFAULT expected error*/
269 	error = getpeername(clt_sk, (struct sockaddr *)-1, &len);
270 	if (error != -1 || errno != EFAULT)
271 		tst_brkm(TBROK, tst_exit, "getpeername with invalid buffer "
272 			 "error:%d errno:%d", error, errno);
273 
274 	tst_resm(TPASS, "getpeername with invalid buffer - EFAULT");
275 
276 	close(clt_sk);
277 	close(svr_sk);
278 	close(accept_sk);
279 
280         /* Indicate successful completion.  */
281 	return 0;
282 }
283