1 /* $NetBSD: h_hostent.c,v 1.2 2014/01/09 02:18:10 christos Exp $ */
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: h_hostent.c,v 1.2 2014/01/09 02:18:10 christos Exp $");
43 #include <netinet/in.h>
44 #include <sys/types.h>
45 #include <arpa/nameser.h>
46 #include <arpa/inet.h>
50 extern const char *__res_conf_name
;
53 phostent(const struct hostent
*h
)
57 const int af
= h
->h_length
== NS_INADDRSZ
? AF_INET
: AF_INET6
;
59 printf("name=%s, length=%d, addrtype=%d, aliases=[",
60 h
->h_name
, h
->h_length
, h
->h_addrtype
);
62 for (i
= 0; h
->h_aliases
[i
]; i
++)
63 printf("%s%s", i
== 0 ? "" : " ", h
->h_aliases
[i
]);
65 printf("] addr_list=[");
67 for (i
= 0; h
->h_addr_list
[i
]; i
++)
68 printf("%s%s", i
== 0 ? "" : " ", inet_ntop(af
,
69 h
->h_addr_list
[i
], buf
, (socklen_t
)sizeof(buf
)));
77 (void)fprintf(stderr
, "Usage: %s [-f <hostsfile>] "
78 "[-t <any|dns|nis|files>] "
79 "[-46a] <name|address>\n", getprogname());
84 getby(int (*f
)(void *, void *, va_list), struct getnamaddr
*info
, ...)
90 e
= (*f
)(info
, NULL
, ap
);
97 printf("error %d\n", e
);
103 geta(struct hostent
*hp
) {
105 printf("error %d\n", h_errno
);
111 main(int argc
, char *argv
[])
113 int (*f
)(void *, void *, va_list) = NULL
;
114 const char *type
= "any";
115 int c
, af
, e
, byaddr
, len
;
117 struct getnamaddr info
;
125 info
.buflen
= sizeof(buf
);
128 while ((c
= getopt(argc
, argv
, "46af:r:t:")) != -1) {
140 _hf_sethostsfile(optarg
);
143 __res_conf_name
= optarg
;
163 f
= byaddr
? _dns_gethtbyaddr
: _dns_gethtbyname
;
167 f
= byaddr
? _yp_gethtbyaddr
: _yp_gethtbyname
;
171 f
= byaddr
? _hf_gethtbyaddr
: _hf_gethtbyname
;
174 errx(EXIT_FAILURE
, "Unknown db type `%s'", type
);
178 struct in6_addr addr
;
179 af
= strchr(*argv
, ':') ? AF_INET6
: AF_INET
;
180 len
= af
== AF_INET
? NS_INADDRSZ
: NS_IN6ADDRSZ
;
181 if (inet_pton(af
, *argv
, &addr
) == -1)
182 err(EXIT_FAILURE
, "Can't parse `%s'", *argv
);
184 geta(gethostbyaddr((const char *)&addr
, len
, af
));
186 getby(f
, &info
, &addr
, len
, af
);
189 geta(gethostbyname2(*argv
, af
));
191 getby(f
, &info
, *argv
, len
, af
);