1 /* $NetBSD: h_hostent.c,v 1.1 2013/08/16 15:29:45 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.1 2013/08/16 15:29:45 christos Exp $");
43 #include <netinet/in.h>
44 #include <sys/types.h>
45 #include <arpa/nameser.h>
46 #include <arpa/inet.h>
51 phostent(const struct hostent
*h
)
55 const int af
= h
->h_length
== NS_INADDRSZ
? AF_INET
: AF_INET6
;
57 printf("name=%s, length=%d, addrtype=%d, aliases=[",
58 h
->h_name
, h
->h_length
, h
->h_addrtype
);
60 for (i
= 0; h
->h_aliases
[i
]; i
++)
61 printf("%s%s", i
== 0 ? "" : " ", h
->h_aliases
[i
]);
63 printf("] addr_list=[");
65 for (i
= 0; h
->h_addr_list
[i
]; i
++)
66 printf("%s%s", i
== 0 ? "" : " ", inet_ntop(af
,
67 h
->h_addr_list
[i
], buf
, (socklen_t
)sizeof(buf
)));
75 (void)fprintf(stderr
, "Usage: %s [-f <hostsfile>] "
76 "[-t <any|dns|nis|files>] "
77 "[-46a] <name|address>\n", getprogname());
82 getby(int (*f
)(void *, void *, va_list), struct getnamaddr
*info
, ...)
88 e
= (*f
)(info
, NULL
, ap
);
95 printf("error %d\n", e
);
101 geta(struct hostent
*hp
) {
103 printf("error %d\n", h_errno
);
109 main(int argc
, char *argv
[])
111 int (*f
)(void *, void *, va_list) = NULL
;
112 const char *type
= "any";
113 int c
, af
, e
, byaddr
, len
;
115 struct getnamaddr info
;
123 info
.buflen
= sizeof(buf
);
126 while ((c
= getopt(argc
, argv
, "46af:t:")) != -1) {
141 _hf_sethostsfile(optarg
);
158 f
= byaddr
? _dns_gethtbyaddr
: _dns_gethtbyname
;
162 f
= byaddr
? _yp_gethtbyaddr
: _yp_gethtbyname
;
166 f
= byaddr
? _hf_gethtbyaddr
: _hf_gethtbyname
;
169 errx(EXIT_FAILURE
, "Unknown db type `%s'", type
);
173 struct in6_addr addr
;
174 af
= strchr(*argv
, ':') ? AF_INET6
: AF_INET
;
175 len
= af
== AF_INET
? NS_INADDRSZ
: NS_IN6ADDRSZ
;
176 if (inet_pton(af
, *argv
, &addr
) == -1)
177 err(EXIT_FAILURE
, "Can't parse `%s'", *argv
);
179 geta(gethostbyaddr((const char *)&addr
, len
, af
));
181 getby(f
, &info
, &addr
, len
, af
);
184 geta(gethostbyname2(*argv
, af
));
186 getby(f
, &info
, *argv
, len
, af
);