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