Postfix3.3.1
dns_rr_to_pa.c
[詳解]
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 
53 const 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 
76 static const char *myname;
77 
78 static NORETURN usage(void)
79 {
80  msg_fatal("usage: %s dnsaddrtype hostname", myname);
81 }
82 
83 int 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);
106  argv += 1;
107  dns_rr_free(rr);
108  }
109  vstring_free(why);
110  return (0);
111 }
112 
113 #endif
const char * dns_rr_to_pa(DNS_RR *rr, MAI_HOSTADDR_STR *hostaddr)
Definition: dns_rr_to_pa.c:53
#define NORETURN
Definition: sys_defs.h:1583
#define vstring_str(vp)
Definition: vstring.h:71
#define VSTREAM_OUT
Definition: vstream.h:67
int main(int argc, char **argv)
Definition: anvil.c:1010
char data[1]
Definition: dns.h:149
char buf[MAI_HOSTADDR_STRSIZE]
Definition: myaddrinfo.h:146
const char * inet_ntop(int af, const void *src, char *dst, SOCKADDR_SIZE size)
Definition: sys_compat.c:325
VSTREAM * vstream_printf(const char *fmt,...)
Definition: vstream.c:1335
#define DNS_OK
Definition: dns.h:284
VSTRING * vstring_alloc(ssize_t len)
Definition: vstring.c:353
NORETURN msg_fatal(const char *fmt,...)
Definition: msg.c:249
int vstream_fflush(VSTREAM *stream)
Definition: vstream.c:1257
unsigned dns_type(const char *)
Definition: dns_strtype.c:203
VSTRING * vstring_free(VSTRING *vp)
Definition: vstring.c:380
unsigned short type
Definition: dns.h:142
void dns_rr_free(DNS_RR *)
Definition: dns_rr.c:137
Definition: dns.h:139
#define dns_lookup(name, type, rflags, list, fqdn, why)
Definition: dns.h:229