Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / dns / dns_sa_to_rr.c
blobbfeaf2063f694daa9974912d7a1d7fdb9af8772e
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* dns_sa_to_rr 3
6 /* SUMMARY
7 /* socket address to resource record
8 /* SYNOPSIS
9 /* #include <dns.h>
11 /* DNS_RR *dns_sa_to_rr(hostname, pref, sa)
12 /* const char *hostname;
13 /* unsigned pref;
14 /* struct sockaddr *sa;
15 /* DESCRIPTION
16 /* dns_sa_to_rr() converts a socket address into a DNS resource record.
18 /* Arguments:
19 /* .IP hostname
20 /* The resource record host name.
21 /* .IP pref
22 /* The resource record MX host preference, if applicable.
23 /* .IP sa
24 /* Binary address.
25 /* DIAGNOSTICS
26 /* The result is a null pointer in case of problems, with the
27 /* errno variable set to indicate the problem type.
28 /* LICENSE
29 /* .ad
30 /* .fi
31 /* The Secure Mailer license must be distributed with this software.
32 /* AUTHOR(S)
33 /* Wietse Venema
34 /* IBM T.J. Watson Research
35 /* P.O. Box 704
36 /* Yorktown Heights, NY 10598, USA
37 /*--*/
39 /* System libraries. */
41 #include <sys_defs.h>
42 #include <errno.h>
44 /* Utility library. */
46 #include <msg.h>
48 /* DNS library. */
50 #include <dns.h>
52 /* dns_sa_to_rr - socket address to resource record */
54 DNS_RR *dns_sa_to_rr(const char *hostname, unsigned pref, struct sockaddr * sa)
56 #define DUMMY_TTL 0
58 if (sa->sa_family == AF_INET) {
59 return (dns_rr_create(hostname, hostname, T_A, C_IN, DUMMY_TTL, pref,
60 (char *) &SOCK_ADDR_IN_ADDR(sa),
61 sizeof(SOCK_ADDR_IN_ADDR(sa))));
62 #ifdef HAS_IPV6
63 } else if (sa->sa_family == AF_INET6) {
64 return (dns_rr_create(hostname, hostname, T_AAAA, C_IN, DUMMY_TTL, pref,
65 (char *) &SOCK_ADDR_IN6_ADDR(sa),
66 sizeof(SOCK_ADDR_IN6_ADDR(sa))));
67 #endif
68 } else {
69 errno = EAFNOSUPPORT;
70 return (0);
75 * Stand-alone test program.
77 #ifdef TEST
78 #include <vstream.h>
79 #include <myaddrinfo.h>
80 #include <inet_proto.h>
82 static const char *myname;
84 static NORETURN usage(void)
86 msg_fatal("usage: %s hostname", myname);
89 int main(int argc, char **argv)
91 MAI_HOSTADDR_STR hostaddr;
92 struct addrinfo *res0;
93 struct addrinfo *res;
94 DNS_RR *rr;
95 int aierr;
97 myname = argv[0];
98 if (argc < 2)
99 usage();
101 inet_proto_init(argv[0], INET_PROTO_NAME_ALL);
103 while (*++argv) {
104 if ((aierr = hostname_to_sockaddr(argv[0], (char *) 0, 0, &res0)) != 0)
105 msg_fatal("%s: %s", argv[0], MAI_STRERROR(aierr));
106 for (res = res0; res != 0; res = res->ai_next) {
107 if ((rr = dns_sa_to_rr(argv[0], 0, res->ai_addr)) == 0)
108 msg_fatal("dns_sa_to_rr: %m");
109 if (dns_rr_to_pa(rr, &hostaddr) == 0)
110 msg_fatal("dns_rr_to_pa: %m");
111 vstream_printf("%s -> %s\n", argv[0], hostaddr.buf);
112 vstream_fflush(VSTREAM_OUT);
113 dns_rr_free(rr);
115 freeaddrinfo(res0);
117 return (0);
120 #endif