xref: /freebsd-src/contrib/netbsd-tests/lib/libc/sys/t_connect.c (revision 63d1fd5970ec814904aa0f4580b10a0d302d08b2)
1 /*	$NetBSD: t_connect.c,v 1.3 2017/01/13 20:09:48 christos Exp $	*/
2 /*
3  * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
16  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/socket.h>
30 #include <err.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <unistd.h>
34 
35 #include <arpa/inet.h>
36 #include <netinet/in.h>
37 
38 #include <atf-c.h>
39 
40 #ifdef __FreeBSD__
41 #include <sys/socket.h>
42 #endif
43 
44 ATF_TC(connect_low_port);
45 ATF_TC_HEAD(connect_low_port, tc)
46 {
47 	atf_tc_set_md_var(tc, "descr", "Checks that low-port allocation "
48 	    "works");
49 	atf_tc_set_md_var(tc, "require.user", "root");
50 }
51 ATF_TC_BODY(connect_low_port, tc)
52 {
53 	struct sockaddr_in sin, sinlist;
54 	int sd, val, slist;
55 	socklen_t slen;
56 
57 	slist = socket(AF_INET, SOCK_STREAM, 0);
58 	sd = socket(AF_INET, SOCK_STREAM, 0);
59 
60 	ATF_REQUIRE(sd > 0);
61 	ATF_REQUIRE(slist > 0);
62 
63 	/* bind listening socket */
64 	memset(&sinlist, 0, sizeof(sinlist));
65 	sinlist.sin_family = AF_INET;
66 	sinlist.sin_port = htons(31522);
67 	sinlist.sin_addr.s_addr = inet_addr("127.0.0.1");
68 
69 	ATF_REQUIRE_EQ(bind(slist,
70 	    (struct sockaddr *)&sinlist, sizeof(sinlist)), 0);
71 	ATF_REQUIRE_EQ(listen(slist, 1), 0);
72 
73 	val = IP_PORTRANGE_LOW;
74 	if (setsockopt(sd, IPPROTO_IP, IP_PORTRANGE, &val,
75 	    sizeof(val)) == -1)
76 		atf_tc_fail("setsockopt failed: %s", strerror(errno));
77 
78 	memset(&sin, 0, sizeof(sin));
79 
80 	sin.sin_port = htons(31522);
81 	sin.sin_addr.s_addr = inet_addr("127.0.0.1");
82 	sin.sin_family = AF_INET;
83 
84 	if (connect(sd, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
85 		int serrno = errno;
86 		atf_tc_fail("connect failed: %s%s",
87 		    strerror(serrno),
88 		    serrno != EACCES ? "" :
89 		    " (see http://mail-index.netbsd.org/"
90 		    "source-changes/2007/12/16/0011.html)");
91 	}
92 
93 	slen = sizeof(sin);
94 	ATF_REQUIRE_EQ(getsockname(sd, (struct sockaddr *)&sin, &slen), 0);
95 	ATF_REQUIRE_EQ(slen, sizeof(sin));
96 	ATF_REQUIRE(ntohs(sin.sin_port) <= IPPORT_RESERVEDMAX);
97 
98 	close(sd);
99 	close(slist);
100 }
101 
102 ATF_TC(connect_foreign_family);
103 ATF_TC_HEAD(connect_foreign_family, tc)
104 {
105 	atf_tc_set_md_var(tc, "descr", "Checks that connecting a socket "
106 	    "with a different address family fails");
107 }
108 ATF_TC_BODY(connect_foreign_family, tc)
109 {
110 	struct sockaddr_in addr;
111 
112 	/* addr.sin_family = AF_UNSPEC = 0 */
113 	memset(&addr, 0, sizeof(addr));
114 
115 	/*
116 	 * it is not necessary to initialize sin_{addr,port} since
117 	 * those structure members shall not be accessed if connect
118 	 * fails correctly.
119 	 */
120 
121 	int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
122 	ATF_REQUIRE(sock != -1);
123 
124 	ATF_REQUIRE(-1 == connect(sock, (struct sockaddr *)&addr, sizeof(addr)));
125 	ATF_REQUIRE(EAFNOSUPPORT == errno);
126 
127 	close(sock);
128 }
129 
130 ATF_TP_ADD_TCS(tp)
131 {
132 
133 	ATF_TP_ADD_TC(tp, connect_low_port);
134 	ATF_TP_ADD_TC(tp, connect_foreign_family);
135 
136 	return atf_no_error();
137 }
138