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