Home | History | Annotate | Line # | Download | only in sys
t_connect.c revision 1.1.2.2
      1  1.1.2.2  yamt /*	$NetBSD: t_connect.c,v 1.1.2.2 2011/11/10 14:31:52 yamt Exp $	*/
      2  1.1.2.2  yamt /*
      3  1.1.2.2  yamt  * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
      4  1.1.2.2  yamt  * All rights reserved.
      5  1.1.2.2  yamt  *
      6  1.1.2.2  yamt  * Redistribution and use in source and binary forms, with or without
      7  1.1.2.2  yamt  * modification, are permitted provided that the following conditions
      8  1.1.2.2  yamt  * are met:
      9  1.1.2.2  yamt  * 1. Redistributions of source code must retain the above copyright
     10  1.1.2.2  yamt  *    notice, this list of conditions and the following disclaimer.
     11  1.1.2.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1.2.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     13  1.1.2.2  yamt  *    documentation and/or other materials provided with the distribution.
     14  1.1.2.2  yamt  *
     15  1.1.2.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
     16  1.1.2.2  yamt  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     17  1.1.2.2  yamt  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  1.1.2.2  yamt  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1.2.2  yamt  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
     20  1.1.2.2  yamt  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1.2.2  yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     22  1.1.2.2  yamt  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1.2.2  yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     24  1.1.2.2  yamt  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     25  1.1.2.2  yamt  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     26  1.1.2.2  yamt  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1.2.2  yamt  */
     28  1.1.2.2  yamt 
     29  1.1.2.2  yamt #include <err.h>
     30  1.1.2.2  yamt #include <errno.h>
     31  1.1.2.2  yamt #include <string.h>
     32  1.1.2.2  yamt #include <unistd.h>
     33  1.1.2.2  yamt 
     34  1.1.2.2  yamt #include <arpa/inet.h>
     35  1.1.2.2  yamt #include <netinet/in.h>
     36  1.1.2.2  yamt 
     37  1.1.2.2  yamt #include <atf-c.h>
     38  1.1.2.2  yamt 
     39  1.1.2.2  yamt ATF_TC(connect_low_port);
     40  1.1.2.2  yamt ATF_TC_HEAD(connect_low_port, tc)
     41  1.1.2.2  yamt {
     42  1.1.2.2  yamt 	atf_tc_set_md_var(tc, "descr", "Checks that low-port allocation "
     43  1.1.2.2  yamt 	    "works");
     44  1.1.2.2  yamt 	atf_tc_set_md_var(tc, "require.user", "root");
     45  1.1.2.2  yamt }
     46  1.1.2.2  yamt ATF_TC_BODY(connect_low_port, tc)
     47  1.1.2.2  yamt {
     48  1.1.2.2  yamt 	struct sockaddr_in sin, sinlist;
     49  1.1.2.2  yamt 	int sd, val, slist;
     50  1.1.2.2  yamt 	socklen_t slen;
     51  1.1.2.2  yamt 
     52  1.1.2.2  yamt 	slist = socket(AF_INET, SOCK_STREAM, 0);
     53  1.1.2.2  yamt 	sd = socket(AF_INET, SOCK_STREAM, 0);
     54  1.1.2.2  yamt 
     55  1.1.2.2  yamt 	/* bind listening socket */
     56  1.1.2.2  yamt 	memset(&sinlist, 0, sizeof(sinlist));
     57  1.1.2.2  yamt 	sinlist.sin_family = AF_INET;
     58  1.1.2.2  yamt 	sinlist.sin_port = htons(31522);
     59  1.1.2.2  yamt 	sinlist.sin_addr.s_addr = inet_addr("127.0.0.1");
     60  1.1.2.2  yamt 
     61  1.1.2.2  yamt 	ATF_REQUIRE_EQ(bind(slist,
     62  1.1.2.2  yamt 	    (struct sockaddr *)&sinlist, sizeof(sinlist)), 0);
     63  1.1.2.2  yamt 	ATF_REQUIRE_EQ(listen(slist, 1), 0);
     64  1.1.2.2  yamt 
     65  1.1.2.2  yamt 	val = IP_PORTRANGE_LOW;
     66  1.1.2.2  yamt 	if (setsockopt(sd, IPPROTO_IP, IP_PORTRANGE, &val,
     67  1.1.2.2  yamt 	    sizeof(val)) == -1)
     68  1.1.2.2  yamt 		atf_tc_fail("setsockopt failed: %s", strerror(errno));
     69  1.1.2.2  yamt 
     70  1.1.2.2  yamt 	memset(&sin, 0, sizeof(sin));
     71  1.1.2.2  yamt 
     72  1.1.2.2  yamt 	sin.sin_port = htons(31522);
     73  1.1.2.2  yamt 	sin.sin_addr.s_addr = inet_addr("127.0.0.1");
     74  1.1.2.2  yamt 	sin.sin_family = AF_INET;
     75  1.1.2.2  yamt 
     76  1.1.2.2  yamt 	if (connect(sd, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
     77  1.1.2.2  yamt 		int serrno = errno;
     78  1.1.2.2  yamt 		atf_tc_fail("connect failed: %s%s",
     79  1.1.2.2  yamt 		    strerror(serrno),
     80  1.1.2.2  yamt 		    serrno != EACCES ? "" :
     81  1.1.2.2  yamt 		    " (see http://mail-index.netbsd.org/"
     82  1.1.2.2  yamt 		    "source-changes/2007/12/16/0011.html)");
     83  1.1.2.2  yamt 	}
     84  1.1.2.2  yamt 
     85  1.1.2.2  yamt 	slen = sizeof(sin);
     86  1.1.2.2  yamt 	ATF_REQUIRE_EQ(getsockname(sd, (struct sockaddr *)&sin, &slen), 0);
     87  1.1.2.2  yamt 	ATF_REQUIRE_EQ(slen, sizeof(sin));
     88  1.1.2.2  yamt 	ATF_REQUIRE(ntohs(sin.sin_port) <= IPPORT_RESERVEDMAX);
     89  1.1.2.2  yamt 
     90  1.1.2.2  yamt 	close(sd);
     91  1.1.2.2  yamt }
     92  1.1.2.2  yamt 
     93  1.1.2.2  yamt ATF_TP_ADD_TCS(tp)
     94  1.1.2.2  yamt {
     95  1.1.2.2  yamt 
     96  1.1.2.2  yamt 	ATF_TP_ADD_TC(tp, connect_low_port);
     97  1.1.2.2  yamt 
     98  1.1.2.2  yamt 	return atf_no_error();
     99  1.1.2.2  yamt }
    100