1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2001
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * Test Name: connect01
22  *
23  * Test Description:
24  *  Verify that connect() returns the proper errno for various failure cases
25  *
26  * Usage:  <for command-line>
27  *  connect01 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
28  *     where,  -c n : Run n copies concurrently.
29  *             -e   : Turn on errno logging.
30  *	       -i n : Execute test n times.
31  *	       -I x : Execute test for x seconds.
32  *	       -P x : Pause for x seconds between iterations.
33  *	       -t   : Turn on syscall timing.
34  *
35  * HISTORY
36  *	07/2001 Ported by Wayne Boyer
37  *
38  * RESTRICTIONS:
39  *  None.
40  *
41  */
42 
43 #include <stdio.h>
44 #include <unistd.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 #include <sys/signal.h>
51 #include <sys/un.h>
52 
53 #include <netinet/in.h>
54 
55 #include "test.h"
56 #include "safe_macros.h"
57 
58 char *TCID = "connect01";
59 int testno;
60 
61 int s, s2;			/* socket descriptor */
62 struct sockaddr_in sin1, sin2, sin3, sin4;
63 static int sfd;			/* shared between start_server and do_child */
64 
65 void setup(void), setup0(void), setup1(void), setup2(void),
66 cleanup(void), cleanup0(void), cleanup1(void), do_child(void);
67 
68 static pid_t start_server(struct sockaddr_in *);
69 
70 struct test_case_t {		/* test case structure */
71 	int domain;		/* PF_INET, PF_UNIX, ... */
72 	int type;		/* SOCK_STREAM, SOCK_DGRAM ... */
73 	int proto;		/* protocol number (usually 0 = default) */
74 	struct sockaddr *sockaddr;	/* socket address buffer */
75 	int salen;		/* connect's 3rd argument */
76 	int retval;		/* syscall return value */
77 	int experrno;		/* expected errno */
78 	void (*setup) (void);
79 	void (*cleanup) (void);
80 	char *desc;
81 } tdat[] = {
82 	{
83 	PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&sin1,
84 		    sizeof(struct sockaddr_in), -1, EBADF, setup0,
85 		    cleanup0, "bad file descriptor"},
86 #ifndef UCLINUX
87 	    /* Skip since uClinux does not implement memory protection */
88 	{
89 	PF_INET, SOCK_STREAM, 0, (struct sockaddr *)-1,
90 		    sizeof(struct sockaddr_in), -1, EFAULT, setup1,
91 		    cleanup1, "invalid socket buffer"},
92 #endif
93 	{
94 	PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&sin1,
95 		    3, -1, EINVAL, setup1, cleanup1, "invalid salen"}, {
96 	0, 0, 0, (struct sockaddr *)&sin1,
97 		    sizeof(sin1), -1, ENOTSOCK, setup0, cleanup0,
98 		    "invalid socket"}
99 	, {
100 	PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&sin1,
101 		    sizeof(sin1), -1, EISCONN, setup2, cleanup1,
102 		    "already connected"}
103 	, {
104 	PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&sin2,
105 		    sizeof(sin2), -1, ECONNREFUSED, setup1, cleanup1,
106 		    "connection refused"}
107 	, {
108 	PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&sin4,
109 		    sizeof(sin4), -1, EAFNOSUPPORT, setup1, cleanup1,
110 		    "invalid address family"}
111 ,};
112 
113 int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
114 
115 #ifdef UCLINUX
116 static char *argv0;
117 #endif
118 
119 /**
120  * bionic's connect() implementation calls netdClientInitConnect() before
121  * sending the request to the kernel.  We need to bypass this, or the test will
122  * segfault during the addr = (struct sockaddr *)-1 testcase. We had cases where
123  * tests started to segfault on glibc upgrade or in special conditions where
124  * libc had to convert structure layouts between 32bit/64bit userspace/kernel =>
125  * safer to call the raw syscall regardless of the libc implementation.
126  */
127 #include "lapi/syscalls.h"
128 
sys_connect(int sockfd,const struct sockaddr * addr,socklen_t addrlen)129 static int sys_connect(int sockfd, const struct sockaddr *addr,
130 		socklen_t addrlen)
131 {
132 	return ltp_syscall(__NR_connect, sockfd, addr, addrlen);
133 }
134 
135 #define connect(sockfd, addr, addrlen) sys_connect(sockfd, addr, addrlen)
136 
main(int argc,char * argv[])137 int main(int argc, char *argv[])
138 {
139 	int lc;
140 
141 	tst_parse_opts(argc, argv, NULL, NULL);
142 #ifdef UCLINUX
143 	argv0 = argv[0];
144 	maybe_run_child(&do_child, "d", &sfd);
145 #endif
146 
147 	setup();
148 
149 	for (lc = 0; TEST_LOOPING(lc); ++lc) {
150 		tst_count = 0;
151 		for (testno = 0; testno < TST_TOTAL; ++testno) {
152 			tdat[testno].setup();
153 
154 			TEST(connect
155 			     (s, tdat[testno].sockaddr, tdat[testno].salen));
156 
157 			if (TEST_RETURN != tdat[testno].retval ||
158 			    (TEST_RETURN < 0 &&
159 			     TEST_ERRNO != tdat[testno].experrno)) {
160 				tst_resm(TFAIL, "%s ; returned"
161 					 " %ld (expected %d), errno %d (expected"
162 					 " %d)", tdat[testno].desc,
163 					 TEST_RETURN, tdat[testno].retval,
164 					 TEST_ERRNO, tdat[testno].experrno);
165 			} else {
166 				tst_resm(TPASS, "%s successful",
167 					 tdat[testno].desc);
168 			}
169 			tdat[testno].cleanup();
170 		}
171 	}
172 	cleanup();
173 
174 	tst_exit();
175 }
176 
177 pid_t pid;
178 
setup(void)179 void setup(void)
180 {
181 	TEST_PAUSE;		/* if -p option specified */
182 
183 	pid = start_server(&sin1);
184 
185 	sin2.sin_family = AF_INET;
186 	/* this port must be unused! */
187 	sin2.sin_port = tst_get_unused_port(NULL, AF_INET, SOCK_STREAM);
188 	sin2.sin_addr.s_addr = INADDR_ANY;
189 
190 	sin3.sin_family = AF_INET;
191 	sin3.sin_port = 0;
192 	/* assumes no route to this network! */
193 	sin3.sin_addr.s_addr = htonl(0x0AFFFEFD);
194 
195 	sin4.sin_family = 47;	/* bogus address family */
196 	sin4.sin_port = 0;
197 	sin4.sin_addr.s_addr = htonl(0x0AFFFEFD);
198 
199 }
200 
cleanup(void)201 void cleanup(void)
202 {
203 	(void)kill(pid, SIGKILL);
204 
205 }
206 
setup0(void)207 void setup0(void)
208 {
209 	if (tdat[testno].experrno == EBADF)
210 		s = 400;	/* anything not an open file */
211 	else if ((s = open("/dev/null", O_WRONLY)) == -1)
212 		tst_brkm(TBROK | TERRNO, cleanup, "open(/dev/null) failed");
213 
214 }
215 
cleanup0(void)216 void cleanup0(void)
217 {
218 	close(s);
219 	s = -1;
220 }
221 
setup1(void)222 void setup1(void)
223 {
224 	s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
225 		        tdat[testno].proto);
226 }
227 
cleanup1(void)228 void cleanup1(void)
229 {
230 	(void)close(s);
231 	s = -1;
232 }
233 
setup2(void)234 void setup2(void)
235 {
236 	setup1();		/* get a socket in s */
237 	SAFE_CONNECT(cleanup, s, (const struct sockaddr *)&sin1, sizeof(sin1));
238 }
239 
start_server(struct sockaddr_in * sin0)240 pid_t start_server(struct sockaddr_in *sin0)
241 {
242 	pid_t pid;
243 	socklen_t slen = sizeof(*sin0);
244 
245 	sin0->sin_family = AF_INET;
246 	sin0->sin_port = 0; /* pick random free port */
247 	sin0->sin_addr.s_addr = INADDR_ANY;
248 
249 	sfd = socket(PF_INET, SOCK_STREAM, 0);
250 	if (sfd < 0) {
251 		tst_brkm(TBROK | TERRNO, cleanup, "server socket failed");
252 		return -1;
253 	}
254 	if (bind(sfd, (struct sockaddr *)sin0, sizeof(*sin0)) < 0) {
255 		tst_brkm(TBROK | TERRNO, cleanup, "server bind failed");
256 		return -1;
257 	}
258 	if (listen(sfd, 10) < 0) {
259 		tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
260 		return -1;
261 	}
262 	SAFE_GETSOCKNAME(cleanup, sfd, (struct sockaddr *)sin0, &slen);
263 
264 	switch ((pid = FORK_OR_VFORK())) {
265 	case 0:		/* child */
266 #ifdef UCLINUX
267 		self_exec(argv0, "d", sfd);
268 #else
269 		do_child();
270 #endif
271 		break;
272 	case -1:
273 		tst_brkm(TBROK | TERRNO, cleanup, "server fork failed");
274 		/* fall through */
275 	default:		/* parent */
276 		(void)close(sfd);
277 		return pid;
278 	}
279 
280 	return -1;
281 }
282 
do_child(void)283 void do_child(void)
284 {
285 	struct sockaddr_in fsin;
286 	fd_set afds, rfds;
287 	int nfds, cc, fd;
288 	char c;
289 
290 	FD_ZERO(&afds);
291 	FD_SET(sfd, &afds);
292 
293 	nfds = sfd + 1;
294 
295 	/* accept connections until killed */
296 	while (1) {
297 		socklen_t fromlen;
298 
299 		memcpy(&rfds, &afds, sizeof(rfds));
300 
301 		if (select(nfds, &rfds, NULL, NULL,
302 			   NULL) < 0)
303 			if (errno != EINTR)
304 				exit(1);
305 		if (FD_ISSET(sfd, &rfds)) {
306 			int newfd;
307 
308 			fromlen = sizeof(fsin);
309 			newfd = accept(sfd, (struct sockaddr *)&fsin, &fromlen);
310 			if (newfd >= 0) {
311 				FD_SET(newfd, &afds);
312 				nfds = MAX(nfds, newfd + 1);
313 			}
314 		}
315 		for (fd = 0; fd < nfds; ++fd)
316 			if (fd != sfd && FD_ISSET(fd, &rfds)) {
317 				if ((cc = read(fd, &c, 1)) == 0) {
318 					(void)close(fd);
319 					FD_CLR(fd, &afds);
320 				}
321 			}
322 	}
323 }
324