1/* 2 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC") 3 * Copyright (C) 2000, 2001 Internet Software Consortium. 4 * 5 * Permission to use, copy, modify, and/or distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15 * PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18/* $Id: gxba_test.c,v 1.13 2007/06/19 23:46:59 tbox Exp $ */ 19 20/*! \file */ 21#include <config.h> 22 23#include <stdio.h> 24 25#include <isc/net.h> 26 27#include <lwres/netdb.h> 28 29static void 30print_he(struct hostent *he, int error, const char *fun, const char *name) { 31 char **c; 32 int i; 33 34 if (he != NULL) { 35 printf("%s(%s):\n", fun, name); 36 printf("\tname = %s\n", he->h_name); 37 printf("\taddrtype = %d\n", he->h_addrtype); 38 printf("\tlength = %d\n", he->h_length); 39 c = he->h_aliases; 40 i = 1; 41 while (*c != NULL) { 42 printf("\talias[%d] = %s\n", i, *c); 43 i++; 44 c++; 45 } 46 c = he->h_addr_list; 47 i = 1; 48 while (*c != NULL) { 49 char buf[128]; 50 inet_ntop(he->h_addrtype, *c, buf, sizeof(buf)); 51 printf("\taddress[%d] = %s\n", i, buf); 52 c++; 53 i++; 54 } 55 } else { 56 printf("%s(%s): error = %d (%s)\n", fun, name, error, 57 hstrerror(error)); 58 } 59} 60 61int 62main(int argc, char **argv) { 63 struct hostent *he; 64 int error; 65 struct in_addr in_addr; 66 struct in6_addr in6_addr; 67 void *addr; 68 int af; 69 size_t len; 70 71 (void)argc; 72 73 while (argv[1] != NULL) { 74 if (inet_pton(AF_INET, argv[1], &in_addr) == 1) { 75 af = AF_INET; 76 addr = &in_addr; 77 len = sizeof(in_addr); 78 } else if (inet_pton(AF_INET6, argv[1], &in6_addr) == 1) { 79 af = AF_INET6; 80 addr = &in6_addr; 81 len = sizeof(in6_addr); 82 } else { 83 printf("unable to convert \"%s\" to an address\n", 84 argv[1]); 85 argv++; 86 continue; 87 } 88 he = gethostbyaddr(addr, len, af); 89 print_he(he, h_errno, "gethostbyaddr", argv[1]); 90 91 he = getipnodebyaddr(addr, len, af, &error); 92 print_he(he, error, "getipnodebyaddr", argv[1]); 93 if (he != NULL) 94 freehostent(he); 95 argv++; 96 } 97 return (0); 98} 99