xref: /minix/external/bsd/bind/dist/bin/tests/gxba_test.c (revision bb9622b5)
1 /*	$NetBSD: gxba_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 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: gxba_test.c,v 1.13 2007/06/19 23:46:59 tbox Exp  */
21 
22 /*! \file */
23 #include <config.h>
24 
25 #include <stdio.h>
26 
27 #include <isc/net.h>
28 
29 #include <lwres/netdb.h>
30 
31 static void
32 print_he(struct hostent *he, int error, const char *fun, const char *name) {
33 	char **c;
34 	int i;
35 
36 	if (he != NULL) {
37 		 printf("%s(%s):\n", fun, name);
38 		 printf("\tname = %s\n", he->h_name);
39 		 printf("\taddrtype = %d\n", he->h_addrtype);
40 		 printf("\tlength = %d\n", he->h_length);
41 		 c = he->h_aliases;
42 		 i = 1;
43 		 while (*c != NULL) {
44 			printf("\talias[%d] = %s\n", i, *c);
45 			i++;
46 			c++;
47 		 }
48 		 c = he->h_addr_list;
49 		 i = 1;
50 		 while (*c != NULL) {
51 			char buf[128];
52 			inet_ntop(he->h_addrtype, *c, buf, sizeof(buf));
53 			printf("\taddress[%d] = %s\n", i, buf);
54 			c++;
55 			i++;
56 		}
57 	} else {
58 		printf("%s(%s): error = %d (%s)\n", fun, name, error,
59 		       hstrerror(error));
60 	}
61 }
62 
63 int
64 main(int argc, char **argv) {
65 	struct hostent *he;
66 	int error;
67 	struct in_addr in_addr;
68 	struct in6_addr in6_addr;
69 	void *addr;
70 	int af;
71 	size_t len;
72 
73 	(void)argc;
74 
75 	while (argv[1] != NULL) {
76 		if (inet_pton(AF_INET, argv[1], &in_addr) == 1) {
77 			af = AF_INET;
78 			addr = &in_addr;
79 			len = sizeof(in_addr);
80 		} else if (inet_pton(AF_INET6, argv[1], &in6_addr) == 1) {
81 			af = AF_INET6;
82 			addr = &in6_addr;
83 			len = sizeof(in6_addr);
84 		} else {
85 			printf("unable to convert \"%s\" to an address\n",
86 			       argv[1]);
87 			argv++;
88 			continue;
89 		}
90 		he = gethostbyaddr(addr, len, af);
91 		print_he(he, h_errno, "gethostbyaddr", argv[1]);
92 
93 		he = getipnodebyaddr(addr, len, af, &error);
94 		print_he(he, error, "getipnodebyaddr", argv[1]);
95 		if (he != NULL)
96 			freehostent(he);
97 		argv++;
98 	}
99 	return (0);
100 }
101