xref: /minix/external/bsd/bind/dist/bin/tests/inter_test.c (revision bb9622b5)
1 /*	$NetBSD: inter_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007, 2008  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 2000, 2001, 2003  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: inter_test.c,v 1.16 2008/03/20 23:47:00 tbox Exp  */
21 
22 /*! \file */
23 #include <config.h>
24 
25 #include <stdlib.h>
26 
27 #include <isc/interfaceiter.h>
28 #include <isc/mem.h>
29 #include <isc/util.h>
30 
31 int
32 main(int argc, char **argv) {
33 	isc_mem_t *mctx = NULL;
34 	isc_interfaceiter_t *iter = NULL;
35 	isc_interface_t ifdata;
36 	isc_result_t result;
37 	const char * res;
38 	char buf[128];
39 
40 	UNUSED(argc);
41 	UNUSED(argv);
42 
43 	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
44 	result = isc_interfaceiter_create(mctx, &iter);
45 	if (result != ISC_R_SUCCESS)
46 		goto cleanup;
47 	result = isc_interfaceiter_first(iter);
48 	while (result == ISC_R_SUCCESS) {
49 		result = isc_interfaceiter_current(iter, &ifdata);
50 		if (result != ISC_R_SUCCESS) {
51 			fprintf(stdout, "isc_interfaceiter_current: %s",
52 				isc_result_totext(result));
53 			continue;
54 		}
55 		fprintf(stdout, "%s %d %x\n", ifdata.name, ifdata.af,
56 			ifdata.flags);
57 		INSIST(ifdata.af == AF_INET || ifdata.af == AF_INET6);
58 		res = inet_ntop(ifdata.af, &ifdata.address.type, buf,
59 				sizeof(buf));
60 		if (ifdata.address.zone != 0)
61 			fprintf(stdout, "address = %s (zone %u)\n",
62 				res == NULL ? "BAD" : res,
63 				ifdata.address.zone);
64 		else
65 			fprintf(stdout, "address = %s\n",
66 				res == NULL ? "BAD" : res);
67 		INSIST(ifdata.address.family == ifdata.af);
68 		res = inet_ntop(ifdata.af, &ifdata.netmask.type, buf,
69 				sizeof(buf));
70 		fprintf(stdout, "netmask = %s\n", res == NULL ? "BAD" : res);
71 		INSIST(ifdata.netmask.family == ifdata.af);
72 		if ((ifdata.flags & INTERFACE_F_POINTTOPOINT) != 0) {
73 			res = inet_ntop(ifdata.af, &ifdata.dstaddress.type,
74 					 buf, sizeof(buf));
75 			fprintf(stdout, "dstaddress = %s\n",
76 				res == NULL ? "BAD" : res);
77 
78 			INSIST(ifdata.dstaddress.family == ifdata.af);
79 		}
80 		result = isc_interfaceiter_next(iter);
81 		if (result != ISC_R_SUCCESS && result != ISC_R_NOMORE) {
82 			fprintf(stdout, "isc_interfaceiter_next: %s",
83 				isc_result_totext(result));
84 			continue;
85 		}
86 	}
87 	isc_interfaceiter_destroy(&iter);
88 
89 	fprintf(stdout, "\nPass 2\n\n");
90 
91 	result = isc_interfaceiter_create(mctx, &iter);
92 	if (result != ISC_R_SUCCESS)
93 		goto cleanup;
94 	result = isc_interfaceiter_first(iter);
95 	while (result == ISC_R_SUCCESS) {
96 		result = isc_interfaceiter_current(iter, &ifdata);
97 		if (result != ISC_R_SUCCESS) {
98 			fprintf(stdout, "isc_interfaceiter_current: %s",
99 				isc_result_totext(result));
100 			continue;
101 		}
102 		fprintf(stdout, "%s %d %x\n", ifdata.name, ifdata.af,
103 			ifdata.flags);
104 		INSIST(ifdata.af == AF_INET || ifdata.af == AF_INET6);
105 		res = inet_ntop(ifdata.af, &ifdata.address.type, buf,
106 				sizeof(buf));
107 		if (ifdata.address.zone != 0)
108 			fprintf(stdout, "address = %s (zone %u)\n",
109 				res == NULL ? "BAD" : res,
110 				ifdata.address.zone);
111 		else
112 			fprintf(stdout, "address = %s\n",
113 				res == NULL ? "BAD" : res);
114 		INSIST(ifdata.address.family == ifdata.af);
115 		res = inet_ntop(ifdata.af, &ifdata.netmask.type, buf,
116 				sizeof(buf));
117 		fprintf(stdout, "netmask = %s\n", res == NULL ? "BAD" : res);
118 		INSIST(ifdata.netmask.family == ifdata.af);
119 		if ((ifdata.flags & INTERFACE_F_POINTTOPOINT) != 0) {
120 			res = inet_ntop(ifdata.af, &ifdata.dstaddress.type,
121 					 buf, sizeof(buf));
122 			fprintf(stdout, "dstaddress = %s\n",
123 				res == NULL ? "BAD" : res);
124 
125 			INSIST(ifdata.dstaddress.family == ifdata.af);
126 		}
127 		result = isc_interfaceiter_next(iter);
128 		if (result != ISC_R_SUCCESS && result != ISC_R_NOMORE) {
129 			fprintf(stdout, "isc_interfaceiter_next: %s",
130 				isc_result_totext(result));
131 			continue;
132 		}
133 	}
134 	isc_interfaceiter_destroy(&iter);
135  cleanup:
136 	isc_mem_destroy(&mctx);
137 
138 	return (0);
139 }
140