xref: /minix3/external/bsd/bind/dist/bin/tests/net/netaddr_multicast.c (revision 00b67f09dd46474d133c95011a48590a8e8f94c7)
1 /*	$NetBSD: netaddr_multicast.c,v 1.4 2014/12/10 04:37:53 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2007  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 2000, 2001  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /* Id: netaddr_multicast.c,v 1.12 2007/06/19 23:47:00 tbox Exp  */
21 
22 #include <config.h>
23 
24 #include <stdlib.h>
25 #include <stdio.h>
26 
27 #include <isc/net.h>
28 #include <isc/netaddr.h>
29 #include <isc/string.h>
30 #include <isc/types.h>
31 #include <isc/util.h>
32 
33 #include "driver.h"
34 
35 TESTDECL(netaddr_multicast);
36 
37 typedef struct {
38 	int family;
39 	const char *addr;
40 	isc_boolean_t is_multicast;
41 } t_addr_t;
42 
43 static t_addr_t addrs[] = {
44 	{ AF_INET, "1.2.3.4", ISC_FALSE },
45 	{ AF_INET, "4.3.2.1", ISC_FALSE },
46 	{ AF_INET, "224.1.1.1", ISC_TRUE },
47 	{ AF_INET, "1.1.1.244", ISC_FALSE },
48 	{ AF_INET6, "::1", ISC_FALSE },
49 	{ AF_INET6, "ff02::1", ISC_TRUE }
50 };
51 #define NADDRS (sizeof(addrs) / sizeof(t_addr_t))
52 
53 static isc_result_t to_netaddr(t_addr_t *, isc_netaddr_t *);
54 
55 static isc_result_t
to_netaddr(t_addr_t * addr,isc_netaddr_t * na)56 to_netaddr(t_addr_t *addr, isc_netaddr_t *na) {
57 	int r;
58 	struct in_addr in;
59 	struct in6_addr in6;
60 
61 	switch (addr->family) {
62 	case AF_INET:
63 		r = inet_pton(AF_INET, addr->addr, (unsigned char *)&in);
64 		if (r != 1)
65 			return (ISC_R_FAILURE);
66 		isc_netaddr_fromin(na, &in);
67 		break;
68 	case AF_INET6:
69 		r = inet_pton(AF_INET6, addr->addr, (unsigned char *)&in6);
70 		if (r != 1)
71 			return (ISC_R_FAILURE);
72 		isc_netaddr_fromin6(na, &in6);
73 		break;
74 	default:
75 		return (ISC_R_UNEXPECTED);
76 	}
77 
78 	return (ISC_R_SUCCESS);
79 }
80 
81 test_result_t
netaddr_multicast(void)82 netaddr_multicast(void) {
83 	isc_netaddr_t na;
84 	unsigned int n_fail;
85 	t_addr_t *addr;
86 	unsigned int i;
87 	isc_result_t result;
88 	isc_boolean_t tf;
89 
90 	n_fail = 0;
91 	for (i = 0; i < NADDRS; i++) {
92 		addr = &addrs[i];
93 		result = to_netaddr(addr, &na);
94 		if (result != ISC_R_SUCCESS) {
95 			printf("I:to_netaddr() returned %s on item %u\n",
96 			       isc_result_totext(result), i);
97 			return (UNKNOWN);
98 		}
99 		tf = isc_netaddr_ismulticast(&na);
100 		if (tf == addr->is_multicast) {
101 			printf("I:%s is%s multicast (PASSED)\n",
102 			       (addr->addr), (tf ? "" : " not"));
103 		} else {
104 			printf("I:%s is%s multicast (FAILED)\n",
105 			       (addr->addr), (tf ? "" : " not"));
106 			n_fail++;
107 		}
108 	}
109 
110 	if (n_fail > 0)
111 		return (FAILED);
112 
113 	return (PASSED);
114 }
115