xref: /netbsd-src/tests/lib/libc/sys/t_connect.c (revision 180ffa9edbf6b6765808c41b6f6cb5ae0ab70feb)
1*180ffa9eSchristos /*	$NetBSD: t_connect.c,v 1.3 2017/01/13 20:09:48 christos Exp $	*/
256b331c0Sjruoho /*
356b331c0Sjruoho  * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
456b331c0Sjruoho  * All rights reserved.
556b331c0Sjruoho  *
656b331c0Sjruoho  * Redistribution and use in source and binary forms, with or without
756b331c0Sjruoho  * modification, are permitted provided that the following conditions
856b331c0Sjruoho  * are met:
956b331c0Sjruoho  * 1. Redistributions of source code must retain the above copyright
1056b331c0Sjruoho  *    notice, this list of conditions and the following disclaimer.
1156b331c0Sjruoho  * 2. Redistributions in binary form must reproduce the above copyright
1256b331c0Sjruoho  *    notice, this list of conditions and the following disclaimer in the
1356b331c0Sjruoho  *    documentation and/or other materials provided with the distribution.
1456b331c0Sjruoho  *
1556b331c0Sjruoho  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
1656b331c0Sjruoho  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1756b331c0Sjruoho  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1856b331c0Sjruoho  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1956b331c0Sjruoho  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
2056b331c0Sjruoho  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2156b331c0Sjruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2256b331c0Sjruoho  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2356b331c0Sjruoho  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2456b331c0Sjruoho  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2556b331c0Sjruoho  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2656b331c0Sjruoho  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2756b331c0Sjruoho  */
2856b331c0Sjruoho 
29*180ffa9eSchristos #include <sys/socket.h>
3056b331c0Sjruoho #include <err.h>
3156b331c0Sjruoho #include <errno.h>
3256b331c0Sjruoho #include <string.h>
3356b331c0Sjruoho #include <unistd.h>
3456b331c0Sjruoho 
3556b331c0Sjruoho #include <arpa/inet.h>
3656b331c0Sjruoho #include <netinet/in.h>
3756b331c0Sjruoho 
3856b331c0Sjruoho #include <atf-c.h>
3956b331c0Sjruoho 
4056b331c0Sjruoho ATF_TC(connect_low_port);
ATF_TC_HEAD(connect_low_port,tc)4156b331c0Sjruoho ATF_TC_HEAD(connect_low_port, tc)
4256b331c0Sjruoho {
4356b331c0Sjruoho 	atf_tc_set_md_var(tc, "descr", "Checks that low-port allocation "
4456b331c0Sjruoho 	    "works");
4556b331c0Sjruoho 	atf_tc_set_md_var(tc, "require.user", "root");
4656b331c0Sjruoho }
ATF_TC_BODY(connect_low_port,tc)4756b331c0Sjruoho ATF_TC_BODY(connect_low_port, tc)
4856b331c0Sjruoho {
4956b331c0Sjruoho 	struct sockaddr_in sin, sinlist;
5056b331c0Sjruoho 	int sd, val, slist;
5156b331c0Sjruoho 	socklen_t slen;
5256b331c0Sjruoho 
5356b331c0Sjruoho 	slist = socket(AF_INET, SOCK_STREAM, 0);
5456b331c0Sjruoho 	sd = socket(AF_INET, SOCK_STREAM, 0);
5556b331c0Sjruoho 
56*180ffa9eSchristos 	ATF_REQUIRE(sd > 0);
57*180ffa9eSchristos 	ATF_REQUIRE(slist > 0);
58*180ffa9eSchristos 
5956b331c0Sjruoho 	/* bind listening socket */
6056b331c0Sjruoho 	memset(&sinlist, 0, sizeof(sinlist));
6156b331c0Sjruoho 	sinlist.sin_family = AF_INET;
6256b331c0Sjruoho 	sinlist.sin_port = htons(31522);
6356b331c0Sjruoho 	sinlist.sin_addr.s_addr = inet_addr("127.0.0.1");
6456b331c0Sjruoho 
6556b331c0Sjruoho 	ATF_REQUIRE_EQ(bind(slist,
6656b331c0Sjruoho 	    (struct sockaddr *)&sinlist, sizeof(sinlist)), 0);
6756b331c0Sjruoho 	ATF_REQUIRE_EQ(listen(slist, 1), 0);
6856b331c0Sjruoho 
6956b331c0Sjruoho 	val = IP_PORTRANGE_LOW;
7056b331c0Sjruoho 	if (setsockopt(sd, IPPROTO_IP, IP_PORTRANGE, &val,
7156b331c0Sjruoho 	    sizeof(val)) == -1)
7256b331c0Sjruoho 		atf_tc_fail("setsockopt failed: %s", strerror(errno));
7356b331c0Sjruoho 
7456b331c0Sjruoho 	memset(&sin, 0, sizeof(sin));
7556b331c0Sjruoho 
7656b331c0Sjruoho 	sin.sin_port = htons(31522);
7756b331c0Sjruoho 	sin.sin_addr.s_addr = inet_addr("127.0.0.1");
7856b331c0Sjruoho 	sin.sin_family = AF_INET;
7956b331c0Sjruoho 
8056b331c0Sjruoho 	if (connect(sd, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
8156b331c0Sjruoho 		int serrno = errno;
8256b331c0Sjruoho 		atf_tc_fail("connect failed: %s%s",
8356b331c0Sjruoho 		    strerror(serrno),
8456b331c0Sjruoho 		    serrno != EACCES ? "" :
8556b331c0Sjruoho 		    " (see http://mail-index.netbsd.org/"
8656b331c0Sjruoho 		    "source-changes/2007/12/16/0011.html)");
8756b331c0Sjruoho 	}
8856b331c0Sjruoho 
8956b331c0Sjruoho 	slen = sizeof(sin);
9056b331c0Sjruoho 	ATF_REQUIRE_EQ(getsockname(sd, (struct sockaddr *)&sin, &slen), 0);
9156b331c0Sjruoho 	ATF_REQUIRE_EQ(slen, sizeof(sin));
9256b331c0Sjruoho 	ATF_REQUIRE(ntohs(sin.sin_port) <= IPPORT_RESERVEDMAX);
9356b331c0Sjruoho 
9456b331c0Sjruoho 	close(sd);
95*180ffa9eSchristos 	close(slist);
9656b331c0Sjruoho }
9756b331c0Sjruoho 
98f0da3f11Srtr ATF_TC(connect_foreign_family);
ATF_TC_HEAD(connect_foreign_family,tc)99f0da3f11Srtr ATF_TC_HEAD(connect_foreign_family, tc)
100f0da3f11Srtr {
101f0da3f11Srtr 	atf_tc_set_md_var(tc, "descr", "Checks that connecting a socket "
102f0da3f11Srtr 	    "with a different address family fails");
103f0da3f11Srtr }
ATF_TC_BODY(connect_foreign_family,tc)104f0da3f11Srtr ATF_TC_BODY(connect_foreign_family, tc)
105f0da3f11Srtr {
106f0da3f11Srtr 	struct sockaddr_in addr;
107f0da3f11Srtr 
108f0da3f11Srtr 	/* addr.sin_family = AF_UNSPEC = 0 */
109f0da3f11Srtr 	memset(&addr, 0, sizeof(addr));
110f0da3f11Srtr 
111f0da3f11Srtr 	/*
112f0da3f11Srtr 	 * it is not necessary to initialize sin_{addr,port} since
113f0da3f11Srtr 	 * those structure members shall not be accessed if connect
114f0da3f11Srtr 	 * fails correctly.
115f0da3f11Srtr 	 */
116f0da3f11Srtr 
117f0da3f11Srtr 	int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
118f0da3f11Srtr 	ATF_REQUIRE(sock != -1);
119f0da3f11Srtr 
120f0da3f11Srtr 	ATF_REQUIRE(-1 == connect(sock, (struct sockaddr *)&addr, sizeof(addr)));
121f0da3f11Srtr 	ATF_REQUIRE(EAFNOSUPPORT == errno);
122f0da3f11Srtr 
123f0da3f11Srtr 	close(sock);
124f0da3f11Srtr }
125f0da3f11Srtr 
ATF_TP_ADD_TCS(tp)12656b331c0Sjruoho ATF_TP_ADD_TCS(tp)
12756b331c0Sjruoho {
12856b331c0Sjruoho 
12956b331c0Sjruoho 	ATF_TP_ADD_TC(tp, connect_low_port);
130f0da3f11Srtr 	ATF_TP_ADD_TC(tp, connect_foreign_family);
13156b331c0Sjruoho 
13256b331c0Sjruoho 	return atf_no_error();
13356b331c0Sjruoho }
134