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(int argc, char **argv) 25 { 26 unsigned char b_addr[IN6ADDRSZ]; 27 struct hostent *host; 28 char **ap, **cp, *arg; 29 const char *prog = "amnesia"; 30 int af = AF_INET; 31 int size = INADDRSZ; 32 int force = 0; 33 34 if (argc < 1) { 35 usage: 36 printf("usage: %s [-d] [-6] [-f] (hostname|ipaddr)\n", prog); 37 exit(1); 38 } 39 prog = *argv++; 40 argc--; 41 #ifdef LOG_USER 42 openlog(prog, LOG_PERROR, LOG_USER); 43 #else 44 openlog(prog, LOG_PERROR); 45 #endif 46 res_init(); 47 48 if (argc >= 1 && !strcmp(*argv, "-d")) { 49 _res.options |= RES_DEBUG; 50 argv++, argc--; 51 } 52 if (argc >= 1 && !strcmp(*argv, "-6")) { 53 af = AF_INET6, size = IN6ADDRSZ; 54 _res.options |= RES_USE_INET6; 55 argv++, argc--; 56 } 57 if (argc >= 1 && !strcmp(*argv, "-f")) { 58 force++; 59 argv++, argc--; 60 } 61 62 if (argc < 1) 63 goto usage; 64 arg = *argv++; 65 argc--; 66 67 if (inet_pton(af, arg, b_addr)) { 68 char p[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"]; 69 70 printf("[%s]\n", inet_ntop(af, b_addr, p, sizeof p)); 71 if (!(host = gethostbyaddr((char *)b_addr, size, af))) { 72 herror("gethostbyaddr"); 73 exit(1); 74 } 75 } else { 76 printf("{%s}\n", arg); 77 if (force) 78 host = gethostbyname2(arg, af); 79 else 80 host = gethostbyname(arg); 81 if (!host) { 82 herror("gethostbyname*"); 83 exit(1); 84 } 85 } 86 printf("name: %s\n", host->h_name); 87 if (host->h_aliases && *host->h_aliases) { 88 printf("aliases:"); 89 for (cp = (char **)host->h_aliases; *cp; cp++) 90 printf(" %s", *cp); 91 printf("\n"); 92 } 93 if (host->h_addr_list && *host->h_addr_list) { 94 printf("addresses:"); 95 for (ap = host->h_addr_list; *ap; ap++) { 96 char p[sizeof 97 "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"]; 98 99 printf(" %s", inet_ntop(host->h_addrtype, 100 *ap, p, sizeof p)); 101 } 102 printf("\n"); 103 } 104 exit(0); 105 } 106