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