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 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> 17 #include <string.h> 18 #include <stdlib.h> 19 20 #ifndef LOG_PERROR 21 #define LOG_PERROR 0 22 #endif 23 24 int main(argc, argv) 25 int argc; 26 char **argv; 27 { 28 unsigned 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) { 37 usage: 38 printf("usage: %s [-d] [-6] [-f] (hostname|ipaddr)\n", prog); 39 exit(1); 40 } 41 prog = *argv++; 42 argc--; 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; 66 arg = *argv++; 67 argc--; 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)); 73 if (!(host = gethostbyaddr((char *)b_addr, size, af))) { 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:"); 91 for (cp = (char **)host->h_aliases; *cp; cp++) 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 99 "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"]; 100 101 printf(" %s", inet_ntop(host->h_addrtype, 102 *ap, p, sizeof p)); 103 } 104 printf("\n"); 105 } 106 exit(0); 107 } 108