xref: /freebsd/contrib/netbsd-tests/lib/libc/sys/t_connect.c (revision 1a36faad54665288ed4eb839d2a4699ae2ead45e)
1*63d1fd59SEnji Cooper /*	$NetBSD: t_connect.c,v 1.3 2017/01/13 20:09:48 christos Exp $	*/
257718be8SEnji Cooper /*
357718be8SEnji Cooper  * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
457718be8SEnji Cooper  * All rights reserved.
557718be8SEnji Cooper  *
657718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
757718be8SEnji Cooper  * modification, are permitted provided that the following conditions
857718be8SEnji Cooper  * are met:
957718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
1057718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
1157718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
1257718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
1357718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
1457718be8SEnji Cooper  *
1557718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
1657718be8SEnji Cooper  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1757718be8SEnji Cooper  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1857718be8SEnji Cooper  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1957718be8SEnji Cooper  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
2057718be8SEnji Cooper  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2157718be8SEnji Cooper  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2257718be8SEnji Cooper  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2357718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2457718be8SEnji Cooper  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2557718be8SEnji Cooper  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2657718be8SEnji Cooper  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2757718be8SEnji Cooper  */
2857718be8SEnji Cooper 
29*63d1fd59SEnji Cooper #include <sys/socket.h>
3057718be8SEnji Cooper #include <err.h>
3157718be8SEnji Cooper #include <errno.h>
3257718be8SEnji Cooper #include <string.h>
3357718be8SEnji Cooper #include <unistd.h>
3457718be8SEnji Cooper 
3557718be8SEnji Cooper #include <arpa/inet.h>
3657718be8SEnji Cooper #include <netinet/in.h>
3757718be8SEnji Cooper 
3857718be8SEnji Cooper #include <atf-c.h>
3957718be8SEnji Cooper 
4057718be8SEnji Cooper ATF_TC(connect_low_port);
ATF_TC_HEAD(connect_low_port,tc)4157718be8SEnji Cooper ATF_TC_HEAD(connect_low_port, tc)
4257718be8SEnji Cooper {
4357718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Checks that low-port allocation "
4457718be8SEnji Cooper 	    "works");
4557718be8SEnji Cooper 	atf_tc_set_md_var(tc, "require.user", "root");
4657718be8SEnji Cooper }
ATF_TC_BODY(connect_low_port,tc)4757718be8SEnji Cooper ATF_TC_BODY(connect_low_port, tc)
4857718be8SEnji Cooper {
4957718be8SEnji Cooper 	struct sockaddr_in sin, sinlist;
5057718be8SEnji Cooper 	int sd, val, slist;
5157718be8SEnji Cooper 	socklen_t slen;
5257718be8SEnji Cooper 
5357718be8SEnji Cooper 	slist = socket(AF_INET, SOCK_STREAM, 0);
5457718be8SEnji Cooper 	sd = socket(AF_INET, SOCK_STREAM, 0);
5557718be8SEnji Cooper 
564dd13092SEnji Cooper 	ATF_REQUIRE(sd > 0);
574dd13092SEnji Cooper 	ATF_REQUIRE(slist > 0);
584dd13092SEnji Cooper 
5957718be8SEnji Cooper 	/* bind listening socket */
6057718be8SEnji Cooper 	memset(&sinlist, 0, sizeof(sinlist));
6157718be8SEnji Cooper 	sinlist.sin_family = AF_INET;
6257718be8SEnji Cooper 	sinlist.sin_port = htons(31522);
6357718be8SEnji Cooper 	sinlist.sin_addr.s_addr = inet_addr("127.0.0.1");
6457718be8SEnji Cooper 
6557718be8SEnji Cooper 	ATF_REQUIRE_EQ(bind(slist,
6657718be8SEnji Cooper 	    (struct sockaddr *)&sinlist, sizeof(sinlist)), 0);
6757718be8SEnji Cooper 	ATF_REQUIRE_EQ(listen(slist, 1), 0);
6857718be8SEnji Cooper 
6957718be8SEnji Cooper 	val = IP_PORTRANGE_LOW;
7057718be8SEnji Cooper 	if (setsockopt(sd, IPPROTO_IP, IP_PORTRANGE, &val,
7157718be8SEnji Cooper 	    sizeof(val)) == -1)
7257718be8SEnji Cooper 		atf_tc_fail("setsockopt failed: %s", strerror(errno));
7357718be8SEnji Cooper 
7457718be8SEnji Cooper 	memset(&sin, 0, sizeof(sin));
7557718be8SEnji Cooper 
7657718be8SEnji Cooper 	sin.sin_port = htons(31522);
7757718be8SEnji Cooper 	sin.sin_addr.s_addr = inet_addr("127.0.0.1");
7857718be8SEnji Cooper 	sin.sin_family = AF_INET;
7957718be8SEnji Cooper 
8057718be8SEnji Cooper 	if (connect(sd, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
8157718be8SEnji Cooper 		int serrno = errno;
8257718be8SEnji Cooper 		atf_tc_fail("connect failed: %s%s",
8357718be8SEnji Cooper 		    strerror(serrno),
8457718be8SEnji Cooper 		    serrno != EACCES ? "" :
8557718be8SEnji Cooper 		    " (see http://mail-index.netbsd.org/"
8657718be8SEnji Cooper 		    "source-changes/2007/12/16/0011.html)");
8757718be8SEnji Cooper 	}
8857718be8SEnji Cooper 
8957718be8SEnji Cooper 	slen = sizeof(sin);
9057718be8SEnji Cooper 	ATF_REQUIRE_EQ(getsockname(sd, (struct sockaddr *)&sin, &slen), 0);
9157718be8SEnji Cooper 	ATF_REQUIRE_EQ(slen, sizeof(sin));
9257718be8SEnji Cooper 	ATF_REQUIRE(ntohs(sin.sin_port) <= IPPORT_RESERVEDMAX);
9357718be8SEnji Cooper 
9457718be8SEnji Cooper 	close(sd);
954dd13092SEnji Cooper 	close(slist);
9657718be8SEnji Cooper }
9757718be8SEnji Cooper 
98640235e2SEnji Cooper ATF_TC(connect_foreign_family);
ATF_TC_HEAD(connect_foreign_family,tc)99640235e2SEnji Cooper ATF_TC_HEAD(connect_foreign_family, tc)
100640235e2SEnji Cooper {
101640235e2SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Checks that connecting a socket "
102640235e2SEnji Cooper 	    "with a different address family fails");
103640235e2SEnji Cooper }
ATF_TC_BODY(connect_foreign_family,tc)104640235e2SEnji Cooper ATF_TC_BODY(connect_foreign_family, tc)
105640235e2SEnji Cooper {
106640235e2SEnji Cooper 	struct sockaddr_in addr;
107640235e2SEnji Cooper 
108640235e2SEnji Cooper 	/* addr.sin_family = AF_UNSPEC = 0 */
109640235e2SEnji Cooper 	memset(&addr, 0, sizeof(addr));
110640235e2SEnji Cooper 
111640235e2SEnji Cooper 	/*
112640235e2SEnji Cooper 	 * it is not necessary to initialize sin_{addr,port} since
113640235e2SEnji Cooper 	 * those structure members shall not be accessed if connect
114640235e2SEnji Cooper 	 * fails correctly.
115640235e2SEnji Cooper 	 */
116640235e2SEnji Cooper 
117640235e2SEnji Cooper 	int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
118640235e2SEnji Cooper 	ATF_REQUIRE(sock != -1);
119640235e2SEnji Cooper 
120640235e2SEnji Cooper 	ATF_REQUIRE(-1 == connect(sock, (struct sockaddr *)&addr, sizeof(addr)));
121640235e2SEnji Cooper 	ATF_REQUIRE(EAFNOSUPPORT == errno);
122640235e2SEnji Cooper 
123640235e2SEnji Cooper 	close(sock);
124640235e2SEnji Cooper }
125640235e2SEnji Cooper 
ATF_TP_ADD_TCS(tp)12657718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
12757718be8SEnji Cooper {
12857718be8SEnji Cooper 
12957718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, connect_low_port);
130640235e2SEnji Cooper 	ATF_TP_ADD_TC(tp, connect_foreign_family);
13157718be8SEnji Cooper 
13257718be8SEnji Cooper 	return atf_no_error();
13357718be8SEnji Cooper }
134