1 /* $NetBSD: t_rfc6056.c,v 1.3 2012/06/22 14:54:35 christos Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_rfc6056.c,v 1.3 2012/06/22 14:54:35 christos Exp $");
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>
51 test(const char *hostname
, const char *service
, int family
, int al
)
53 static const char hello
[] = "hello\n";
54 int s
, error
, proto
, option
;
55 struct sockaddr_storage ss
;
56 struct addrinfo hints
, *res
;
58 memset(&hints
, 0, sizeof(hints
));
59 hints
.ai_family
= family
;
60 hints
.ai_socktype
= SOCK_DGRAM
;
69 option
= IPV6_PORTALGO
;
75 error
= getaddrinfo(hostname
, service
, &hints
, &res
);
77 errx(EXIT_FAILURE
, "Cannot get address for %s (%s)",
78 hostname
, gai_strerror(error
));
80 s
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
82 err(EXIT_FAILURE
, "socket");
84 if (setsockopt(s
, proto
, option
, &al
, sizeof(al
)) == -1)
85 err(EXIT_FAILURE
, "setsockopt");
87 memset(&ss
, 0, sizeof(ss
));
88 ss
.ss_len
= res
->ai_addrlen
;
89 ss
.ss_family
= res
->ai_family
;
91 if (bind(s
, (struct sockaddr
*)&ss
, ss
.ss_len
) == -1)
92 err(EXIT_FAILURE
, "bind");
94 if (sendto(s
, hello
, sizeof(hello
) - 1, 0,
95 res
->ai_addr
, res
->ai_addrlen
) == -1)
96 err(EXIT_FAILURE
, "sendto");
99 err(EXIT_FAILURE
, "close");
101 s
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
103 err(EXIT_FAILURE
, "socket");
105 if (setsockopt(s
, proto
, option
, &al
, sizeof(al
)) == -1)
106 err(EXIT_FAILURE
, "setsockopt");
108 if (connect(s
, res
->ai_addr
, res
->ai_addrlen
) == -1)
109 err(EXIT_FAILURE
, "connect");
111 if (send(s
, hello
, sizeof(hello
) - 1, 0) == -1)
112 err(EXIT_FAILURE
, "send");
115 err(EXIT_FAILURE
, "close");
121 ATF_TC_HEAD(inet4
, tc
)
123 atf_tc_set_md_var(tc
, "descr", "Checks random port allocation "
127 ATF_TC_BODY(inet4
, tc
)
129 for (int i
= 0; i
< 6; i
++)
130 test("localhost", "http", AF_INET
, i
);
134 ATF_TC_HEAD(inet6
, tc
)
136 atf_tc_set_md_var(tc
, "descr", "Checks random port allocation "
140 ATF_TC_BODY(inet6
, tc
)
142 for (int i
= 0; i
< 6; i
++)
143 test("localhost", "http", AF_INET6
, i
);
148 ATF_TP_ADD_TC(tp
, inet4
);
149 ATF_TP_ADD_TC(tp
, inet6
);
151 return atf_no_error();