blob: 5b9a79040ceba8b45f58f126930264f358503d21 [file] [log] [blame]
robbiew3e3319c2002-01-23 19:30:57 +00001/* host - print information about a host
2 * originally written by Paul Vixie @DEC WRL, January 1989
3 */
4
5/* DECWRL Header: host.c,v 1.1 89/04/05 15:41:12 vixie Locked $ */
6
robbiew3e3319c2002-01-23 19:30:57 +00007#include <sys/param.h>
8#include <sys/socket.h>
9#include <netinet/in.h>
10#include <arpa/inet.h>
11#include <arpa/nameser.h>
12
13#include <stdio.h>
14#include <resolv.h>
15#include <netdb.h>
16#include <syslog.h>
robbiew779b3ec2002-03-27 22:54:09 +000017#include <string.h>
18#include <stdlib.h>
robbiew3e3319c2002-01-23 19:30:57 +000019
20#ifndef LOG_PERROR
21#define LOG_PERROR 0
22#endif
23
Wanlong Gao354ebb42012-12-07 10:10:04 +080024int main(argc, argv)
25int argc;
26char **argv;
robbiew3e3319c2002-01-23 19:30:57 +000027{
28 u_char b_addr[IN6ADDRSZ];
29 struct hostent *host;
30 char **ap, **cp, *arg;
31 const char *prog = "amnesia";
32 int af = AF_INET;
33 int size = INADDRSZ;
34 int force = 0;
35
36 if (argc < 1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080037usage:
robbiew3e3319c2002-01-23 19:30:57 +000038 printf("usage: %s [-d] [-6] [-f] (hostname|ipaddr)\n", prog);
39 exit(1);
40 }
Wanlong Gao354ebb42012-12-07 10:10:04 +080041 prog = *argv++;
42 argc--;
robbiew3e3319c2002-01-23 19:30:57 +000043#ifdef LOG_USER
44 openlog(prog, LOG_PERROR, LOG_USER);
45#else
46 openlog(prog, LOG_PERROR);
47#endif
48 res_init();
49
50 if (argc >= 1 && !strcmp(*argv, "-d")) {
51 _res.options |= RES_DEBUG;
52 argv++, argc--;
53 }
54 if (argc >= 1 && !strcmp(*argv, "-6")) {
55 af = AF_INET6, size = IN6ADDRSZ;
56 _res.options |= RES_USE_INET6;
57 argv++, argc--;
58 }
59 if (argc >= 1 && !strcmp(*argv, "-f")) {
60 force++;
61 argv++, argc--;
62 }
63
64 if (argc < 1)
65 goto usage;
Wanlong Gao354ebb42012-12-07 10:10:04 +080066 arg = *argv++;
67 argc--;
robbiew3e3319c2002-01-23 19:30:57 +000068
69 if (inet_pton(af, arg, b_addr)) {
70 char p[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
71
72 printf("[%s]\n", inet_ntop(af, b_addr, p, sizeof p));
Wanlong Gao354ebb42012-12-07 10:10:04 +080073 if (!(host = gethostbyaddr((char *)b_addr, size, af))) {
robbiew3e3319c2002-01-23 19:30:57 +000074 herror("gethostbyaddr");
75 exit(1);
76 }
77 } else {
78 printf("{%s}\n", arg);
79 if (force)
80 host = gethostbyname2(arg, af);
81 else
82 host = gethostbyname(arg);
83 if (!host) {
84 herror("gethostbyname*");
85 exit(1);
86 }
87 }
88 printf("name: %s\n", host->h_name);
89 if (host->h_aliases && *host->h_aliases) {
90 printf("aliases:");
Wanlong Gao354ebb42012-12-07 10:10:04 +080091 for (cp = (char **)host->h_aliases; *cp; cp++)
robbiew3e3319c2002-01-23 19:30:57 +000092 printf(" %s", *cp);
93 printf("\n");
94 }
95 if (host->h_addr_list && *host->h_addr_list) {
96 printf("addresses:");
97 for (ap = host->h_addr_list; *ap; ap++) {
98 char p[sizeof
Wanlong Gao354ebb42012-12-07 10:10:04 +080099 "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
robbiew3e3319c2002-01-23 19:30:57 +0000100
101 printf(" %s", inet_ntop(host->h_addrtype,
102 *ap, p, sizeof p));
103 }
104 printf("\n");
105 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800106 exit(0);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700107}