Home | History | Annotate | Line # | Download | only in sys
t_rfc6056.c revision 1.1.2.1
      1  1.1.2.1      yamt /* $NetBSD: t_rfc6056.c,v 1.1.2.1 2011/11/10 14:31:53 yamt Exp $ */
      2      1.1  christos 
      3  1.1.2.1      yamt /*-
      4  1.1.2.1      yamt  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  1.1.2.1      yamt  * All rights reserved.
      6  1.1.2.1      yamt  *
      7  1.1.2.1      yamt  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1.2.1      yamt  * by Christos Zoulas.
      9  1.1.2.1      yamt  *
     10  1.1.2.1      yamt  * Redistribution and use in source and binary forms, with or without
     11  1.1.2.1      yamt  * modification, are permitted provided that the following conditions
     12  1.1.2.1      yamt  * are met:
     13  1.1.2.1      yamt  * 1. Redistributions of source code must retain the above copyright
     14  1.1.2.1      yamt  *    notice, this list of conditions and the following disclaimer.
     15  1.1.2.1      yamt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1.2.1      yamt  *    notice, this list of conditions and the following disclaimer in the
     17  1.1.2.1      yamt  *    documentation and/or other materials provided with the distribution.
     18  1.1.2.1      yamt  *
     19  1.1.2.1      yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1.2.1      yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1.2.1      yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1.2.1      yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1.2.1      yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1.2.1      yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1.2.1      yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1.2.1      yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1.2.1      yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1.2.1      yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1.2.1      yamt  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1.2.1      yamt  */
     31      1.1  christos #include <sys/cdefs.h>
     32  1.1.2.1      yamt __RCSID("$NetBSD: t_rfc6056.c,v 1.1.2.1 2011/11/10 14:31:53 yamt Exp $");
     33      1.1  christos 
     34      1.1  christos #include <sys/types.h>
     35      1.1  christos #include <sys/socket.h>
     36      1.1  christos #include <netinet/in.h>
     37      1.1  christos #include <netinet/udp.h>
     38      1.1  christos #include <arpa/inet.h>
     39      1.1  christos #include <string.h>
     40      1.1  christos #include <strings.h>
     41      1.1  christos #include <stdio.h>
     42      1.1  christos #include <unistd.h>
     43      1.1  christos #include <errno.h>
     44      1.1  christos #include <stdlib.h>
     45      1.1  christos #include <netdb.h>
     46      1.1  christos #include <err.h>
     47      1.1  christos 
     48      1.1  christos #include <atf-c.h>
     49      1.1  christos 
     50      1.1  christos static void
     51      1.1  christos test(const char *hostname, const char *service, int family, int al)
     52      1.1  christos {
     53      1.1  christos 	static const char hello[] = "hello\n";
     54      1.1  christos 	int s, error;
     55      1.1  christos 	struct sockaddr_storage ss;
     56      1.1  christos 	struct addrinfo hints, *res;
     57      1.1  christos 
     58      1.1  christos 	memset(&hints, 0, sizeof(hints));
     59      1.1  christos 	hints.ai_family = family;
     60      1.1  christos 	hints.ai_socktype = SOCK_DGRAM;
     61      1.1  christos 
     62      1.1  christos 	error = getaddrinfo(hostname, service, &hints, &res);
     63      1.1  christos 	if (error)
     64      1.1  christos 		errx(EXIT_FAILURE, "Cannot get address for %s (%s)",
     65      1.1  christos 		    hostname, gai_strerror(error));
     66      1.1  christos 
     67      1.1  christos 	s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
     68      1.1  christos 	if (s == -1)
     69      1.1  christos 		err(EXIT_FAILURE, "socket");
     70      1.1  christos 
     71      1.1  christos 	if (setsockopt(s, IPPROTO_UDP, UDP_RFC6056ALGO, &al, sizeof(al)) == -1)
     72      1.1  christos 		err(EXIT_FAILURE, "setsockopt");
     73      1.1  christos 
     74      1.1  christos 	memset(&ss, 0, sizeof(ss));
     75      1.1  christos 	ss.ss_len = res->ai_addrlen;
     76      1.1  christos 	ss.ss_family = res->ai_family;
     77      1.1  christos 
     78      1.1  christos 	if (bind(s, (struct sockaddr *)&ss, ss.ss_len) == -1)
     79      1.1  christos 		err(EXIT_FAILURE, "bind");
     80      1.1  christos 
     81      1.1  christos 	if (sendto(s, hello, sizeof(hello) - 1, 0,
     82      1.1  christos 	    res->ai_addr, res->ai_addrlen) == -1)
     83      1.1  christos 		err(EXIT_FAILURE, "sendto");
     84      1.1  christos 
     85      1.1  christos 	if (close(s) == -1)
     86      1.1  christos 		err(EXIT_FAILURE, "close");
     87      1.1  christos 
     88      1.1  christos 	s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
     89      1.1  christos 	if (s == -1)
     90      1.1  christos 		err(EXIT_FAILURE, "socket");
     91      1.1  christos 
     92      1.1  christos 	if (setsockopt(s, IPPROTO_UDP, UDP_RFC6056ALGO, &al, sizeof(al)) == -1)
     93      1.1  christos 		err(EXIT_FAILURE, "setsockopt");
     94      1.1  christos 
     95      1.1  christos 	if (connect(s, res->ai_addr, res->ai_addrlen) == -1)
     96      1.1  christos 		err(EXIT_FAILURE, "connect");
     97      1.1  christos 
     98      1.1  christos 	if (send(s, hello, sizeof(hello) - 1, 0) == -1)
     99      1.1  christos 		err(EXIT_FAILURE, "send");
    100      1.1  christos 
    101      1.1  christos 	if (close(s) == -1)
    102      1.1  christos 		err(EXIT_FAILURE, "close");
    103      1.1  christos 
    104      1.1  christos 	freeaddrinfo(res);
    105      1.1  christos }
    106      1.1  christos 
    107      1.1  christos ATF_TC(inet4);
    108      1.1  christos ATF_TC_HEAD(inet4, tc)
    109      1.1  christos {
    110      1.1  christos 	atf_tc_set_md_var(tc, "descr", "Checks random port allocation "
    111      1.1  christos 	    "for ipv4");
    112      1.1  christos }
    113      1.1  christos 
    114      1.1  christos ATF_TC_BODY(inet4, tc)
    115      1.1  christos {
    116      1.1  christos 	for (int i = 0; i < 6; i++)
    117      1.1  christos 		test("localhost", "http", AF_INET, i);
    118      1.1  christos }
    119      1.1  christos 
    120      1.1  christos ATF_TC(inet6);
    121      1.1  christos ATF_TC_HEAD(inet6, tc)
    122      1.1  christos {
    123      1.1  christos 	atf_tc_set_md_var(tc, "descr", "Checks random port allocation "
    124      1.1  christos 	    "for ipv6");
    125      1.1  christos }
    126      1.1  christos 
    127      1.1  christos ATF_TC_BODY(inet6, tc)
    128      1.1  christos {
    129      1.1  christos 	for (int i = 0; i < 6; i++)
    130      1.1  christos 		test("localhost", "http", AF_INET6, i);
    131      1.1  christos }
    132      1.1  christos 
    133      1.1  christos ATF_TP_ADD_TCS(tp)
    134      1.1  christos {
    135      1.1  christos         ATF_TP_ADD_TC(tp, inet4);
    136      1.1  christos         ATF_TP_ADD_TC(tp, inet6);
    137      1.1  christos 
    138      1.1  christos 	return atf_no_error();
    139      1.1  christos }
    140