robbiew | 3e3319c | 2002-01-23 19:30:57 +0000 | [diff] [blame] | 1 | /* 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 | |
robbiew | 3e3319c | 2002-01-23 19:30:57 +0000 | [diff] [blame] | 7 | #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> |
robbiew | 779b3ec | 2002-03-27 22:54:09 +0000 | [diff] [blame] | 17 | #include <string.h> |
| 18 | #include <stdlib.h> |
robbiew | 3e3319c | 2002-01-23 19:30:57 +0000 | [diff] [blame] | 19 | |
| 20 | #ifndef LOG_PERROR |
| 21 | #define LOG_PERROR 0 |
| 22 | #endif |
| 23 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 24 | int main(argc, argv) |
| 25 | int argc; |
| 26 | char **argv; |
robbiew | 3e3319c | 2002-01-23 19:30:57 +0000 | [diff] [blame] | 27 | { |
| 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 37 | usage: |
robbiew | 3e3319c | 2002-01-23 19:30:57 +0000 | [diff] [blame] | 38 | printf("usage: %s [-d] [-6] [-f] (hostname|ipaddr)\n", prog); |
| 39 | exit(1); |
| 40 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 41 | prog = *argv++; |
| 42 | argc--; |
robbiew | 3e3319c | 2002-01-23 19:30:57 +0000 | [diff] [blame] | 43 | #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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 66 | arg = *argv++; |
| 67 | argc--; |
robbiew | 3e3319c | 2002-01-23 19:30:57 +0000 | [diff] [blame] | 68 | |
| 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 73 | if (!(host = gethostbyaddr((char *)b_addr, size, af))) { |
robbiew | 3e3319c | 2002-01-23 19:30:57 +0000 | [diff] [blame] | 74 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 91 | for (cp = (char **)host->h_aliases; *cp; cp++) |
robbiew | 3e3319c | 2002-01-23 19:30:57 +0000 | [diff] [blame] | 92 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 99 | "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"]; |
robbiew | 3e3319c | 2002-01-23 19:30:57 +0000 | [diff] [blame] | 100 | |
| 101 | printf(" %s", inet_ntop(host->h_addrtype, |
| 102 | *ap, p, sizeof p)); |
| 103 | } |
| 104 | printf("\n"); |
| 105 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 106 | exit(0); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 107 | } |