Home | History | Annotate | Line # | Download | only in dns
      1 /*	$NetBSD: dns_rr_to_pa.c,v 1.1.1.1 2009/06/23 10:08:43 tron Exp $	*/
      2 
      3 /*++
      4 /* NAME
      5 /*	dns_rr_to_pa 3
      6 /* SUMMARY
      7 /*	resource record to printable address
      8 /* SYNOPSIS
      9 /*	#include <dns.h>
     10 /*
     11 /*	const char *dns_rr_to_pa(rr, hostaddr)
     12 /*	DNS_RR	*rr;
     13 /*	MAI_HOSTADDR_STR *hostaddr;
     14 /* DESCRIPTION
     15 /*	dns_rr_to_pa() converts the address in a DNS resource record
     16 /*	into printable form and returns a pointer to the result.
     17 /*
     18 /*	Arguments:
     19 /* .IP rr
     20 /*	The DNS resource record.
     21 /* .IP hostaddr
     22 /*	Storage for the printable address.
     23 /* DIAGNOSTICS
     24 /*	The result is null in case of problems, with errno set
     25 /*	to indicate the nature of the problem.
     26 /* LICENSE
     27 /* .ad
     28 /* .fi
     29 /*	The Secure Mailer license must be distributed with this software.
     30 /* AUTHOR(S)
     31 /*	Wietse Venema
     32 /*	IBM T.J. Watson Research
     33 /*	P.O. Box 704
     34 /*	Yorktown Heights, NY 10598, USA
     35 /*--*/
     36 
     37 /* System libraries. */
     38 
     39 #include <sys_defs.h>
     40 #include <sys/socket.h>
     41 #include <netinet/in.h>
     42 #include <arpa/inet.h>
     43 #include <errno.h>
     44 
     45 /* Utility library. */
     46 
     47 #include <msg.h>
     48 
     49 /* DNS library. */
     50 
     51 #include <dns.h>
     52 
     53 /* dns_rr_to_pa - resource record to printable address */
     54 
     55 const char *dns_rr_to_pa(DNS_RR *rr, MAI_HOSTADDR_STR *hostaddr)
     56 {
     57     if (rr->type == T_A) {
     58 	return (inet_ntop(AF_INET, rr->data, hostaddr->buf,
     59 			  sizeof(hostaddr->buf)));
     60 #ifdef HAS_IPV6
     61     } else if (rr->type == T_AAAA) {
     62 	return (inet_ntop(AF_INET6, rr->data, hostaddr->buf,
     63 			  sizeof(hostaddr->buf)));
     64 #endif
     65     } else {
     66 	errno = EAFNOSUPPORT;
     67 	return (0);
     68     }
     69 }
     70 
     71  /*
     72   * Stand-alone test program.
     73   */
     74 #ifdef TEST
     75 #include <vstream.h>
     76 #include <myaddrinfo.h>
     77 
     78 static const char *myname;
     79 
     80 static NORETURN usage(void)
     81 {
     82     msg_fatal("usage: %s dnsaddrtype hostname", myname);
     83 }
     84 
     85 int     main(int argc, char **argv)
     86 {
     87     DNS_RR *rr;
     88     MAI_HOSTADDR_STR hostaddr;
     89     VSTRING *why;
     90     int     type;
     91 
     92     myname = argv[0];
     93     if (argc < 3)
     94 	usage();
     95     why = vstring_alloc(1);
     96 
     97     while (*++argv) {
     98 	if (argv[1] == 0)
     99 	    usage();
    100 	if ((type = dns_type(argv[0])) == 0)
    101 	    usage();
    102 	if (dns_lookup(argv[1], type, 0, &rr, (VSTRING *) 0, why) != DNS_OK)
    103 	    msg_fatal("%s: %s", argv[1], vstring_str(why));
    104 	if (dns_rr_to_pa(rr, &hostaddr) == 0)
    105 	    msg_fatal("dns_rr_to_sa: %m");
    106 	vstream_printf("%s -> %s\n", argv[1], hostaddr.buf);
    107 	vstream_fflush(VSTREAM_OUT);
    108 	argv += 1;
    109 	dns_rr_free(rr);
    110     }
    111     vstring_free(why);
    112     return (0);
    113 }
    114 
    115 #endif
    116