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