Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / dns / dns_rr_to_pa.c
blobfcaf4e19d24d1983eefc6a7003218908c4f63834
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* dns_rr_to_pa 3
6 /* SUMMARY
7 /* resource record to printable address
8 /* SYNOPSIS
9 /* #include <dns.h>
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.
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 /*--*/
37 /* System libraries. */
39 #include <sys_defs.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 #include <errno.h>
45 /* Utility library. */
47 #include <msg.h>
49 /* DNS library. */
51 #include <dns.h>
53 /* dns_rr_to_pa - resource record to printable address */
55 const char *dns_rr_to_pa(DNS_RR *rr, MAI_HOSTADDR_STR *hostaddr)
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);
72 * Stand-alone test program.
74 #ifdef TEST
75 #include <vstream.h>
76 #include <myaddrinfo.h>
78 static const char *myname;
80 static NORETURN usage(void)
82 msg_fatal("usage: %s dnsaddrtype hostname", myname);
85 int main(int argc, char **argv)
87 DNS_RR *rr;
88 MAI_HOSTADDR_STR hostaddr;
89 VSTRING *why;
90 int type;
92 myname = argv[0];
93 if (argc < 3)
94 usage();
95 why = vstring_alloc(1);
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);
111 vstring_free(why);
112 return (0);
115 #endif