1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  */
11 
12 /*! \file */
13 #include <config.h>
14 
15 #include <stdio.h>
16 
17 #include <isc/net.h>
18 #include <isc/print.h>
19 
20 #include <lwres/netdb.h>
21 
22 static void
print_he(struct hostent * he,int error,const char * fun,const char * name)23 print_he(struct hostent *he, int error, const char *fun, const char *name) {
24 	char **c;
25 	int i;
26 
27 	if (he != NULL) {
28 		 printf("%s(%s):\n", fun, name);
29 		 printf("\tname = %s\n", he->h_name);
30 		 printf("\taddrtype = %d\n", he->h_addrtype);
31 		 printf("\tlength = %d\n", he->h_length);
32 		 c = he->h_aliases;
33 		 i = 1;
34 		 while (*c != NULL) {
35 			printf("\talias[%d] = %s\n", i, *c);
36 			i++;
37 			c++;
38 		 }
39 		 c = he->h_addr_list;
40 		 i = 1;
41 		 while (*c != NULL) {
42 			char buf[128];
43 			inet_ntop(he->h_addrtype, *c, buf, sizeof(buf));
44 			printf("\taddress[%d] = %s\n", i, buf);
45 			c++;
46 			i++;
47 		}
48 	} else {
49 		printf("%s(%s): error = %d (%s)\n", fun, name, error,
50 		       hstrerror(error));
51 	}
52 }
53 
54 int
main(int argc,char ** argv)55 main(int argc, char **argv) {
56 	struct hostent *he;
57 	int error;
58 
59 	(void)argc;
60 
61 	while (argv[1] != NULL) {
62 		he = gethostbyname(argv[1]);
63 		print_he(he, h_errno, "gethostbyname", argv[1]);
64 
65 		he = getipnodebyname(argv[1], AF_INET6, AI_DEFAULT|AI_ALL,
66 				     &error);
67 		print_he(he, error, "getipnodebyname", argv[1]);
68 		if (he != NULL)
69 			freehostent(he);
70 
71 		he = getipnodebyname(argv[1], AF_INET6, AI_DEFAULT,
72 				     &error);
73 		print_he(he, error, "getipnodebyname", argv[1]);
74 		if (he != NULL)
75 			freehostent(he);
76 		argv++;
77 	}
78 	return (0);
79 }
80