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