1 /*	$NetBSD: dns_sa_to_rr.c,v 1.1.1.1 2009/06/23 10:08:43 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	dns_sa_to_rr 3
6 /* SUMMARY
7 /*	socket address to resource record
8 /* SYNOPSIS
9 /*	#include <dns.h>
10 /*
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.
17 /*
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 /*--*/
38 
39 /* System libraries. */
40 
41 #include <sys_defs.h>
42 #include <errno.h>
43 
44 /* Utility library. */
45 
46 #include <msg.h>
47 
48 /* DNS library. */
49 
50 #include <dns.h>
51 
52 /* dns_sa_to_rr - socket address to resource record */
53 
54 DNS_RR *dns_sa_to_rr(const char *hostname, unsigned pref, struct sockaddr * sa)
55 {
56 #define DUMMY_TTL	0
57 
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);
71     }
72 }
73 
74  /*
75   * Stand-alone test program.
76   */
77 #ifdef TEST
78 #include <vstream.h>
79 #include <myaddrinfo.h>
80 #include <inet_proto.h>
81 
82 static const char *myname;
83 
84 static NORETURN usage(void)
85 {
86     msg_fatal("usage: %s hostname", myname);
87 }
88 
89 int     main(int argc, char **argv)
90 {
91     MAI_HOSTADDR_STR hostaddr;
92     struct addrinfo *res0;
93     struct addrinfo *res;
94     DNS_RR *rr;
95     int     aierr;
96 
97     myname = argv[0];
98     if (argc < 2)
99 	usage();
100 
101     inet_proto_init(argv[0], INET_PROTO_NAME_ALL);
102 
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);
114 	}
115 	freeaddrinfo(res0);
116     }
117     return (0);
118 }
119 
120 #endif
121