1/*++
2/* NAME
3/*	dns_sa_to_rr 3
4/* SUMMARY
5/*	socket address to resource record
6/* SYNOPSIS
7/*	#include <dns.h>
8/*
9/*	DNS_RR	*dns_sa_to_rr(hostname, pref, sa)
10/*	const char *hostname;
11/*	unsigned pref;
12/*	struct sockaddr *sa;
13/* DESCRIPTION
14/*	dns_sa_to_rr() converts a socket address into a DNS resource record.
15/*
16/*	Arguments:
17/* .IP hostname
18/*	The resource record host name.
19/* .IP pref
20/*	The resource record MX host preference, if applicable.
21/* .IP sa
22/*	Binary address.
23/* DIAGNOSTICS
24/*	The result is a null pointer in case of problems, with the
25/*	errno variable set to indicate the problem type.
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/*--*/
36
37/* System libraries. */
38
39#include <sys_defs.h>
40#include <errno.h>
41
42/* Utility library. */
43
44#include <msg.h>
45
46/* DNS library. */
47
48#include <dns.h>
49
50/* dns_sa_to_rr - socket address to resource record */
51
52DNS_RR *dns_sa_to_rr(const char *hostname, unsigned pref, struct sockaddr * sa)
53{
54#define DUMMY_TTL	0
55
56    if (sa->sa_family == AF_INET) {
57	return (dns_rr_create(hostname, hostname, T_A, C_IN, DUMMY_TTL, pref,
58			      (char *) &SOCK_ADDR_IN_ADDR(sa),
59			      sizeof(SOCK_ADDR_IN_ADDR(sa))));
60#ifdef HAS_IPV6
61    } else if (sa->sa_family == AF_INET6) {
62	return (dns_rr_create(hostname, hostname, T_AAAA, C_IN, DUMMY_TTL, pref,
63			      (char *) &SOCK_ADDR_IN6_ADDR(sa),
64			      sizeof(SOCK_ADDR_IN6_ADDR(sa))));
65#endif
66    } else {
67	errno = EAFNOSUPPORT;
68	return (0);
69    }
70}
71
72 /*
73  * Stand-alone test program.
74  */
75#ifdef TEST
76#include <vstream.h>
77#include <myaddrinfo.h>
78#include <inet_proto.h>
79
80static const char *myname;
81
82static NORETURN usage(void)
83{
84    msg_fatal("usage: %s hostname", myname);
85}
86
87int     main(int argc, char **argv)
88{
89    MAI_HOSTADDR_STR hostaddr;
90    struct addrinfo *res0;
91    struct addrinfo *res;
92    DNS_RR *rr;
93    int     aierr;
94
95    myname = argv[0];
96    if (argc < 2)
97	usage();
98
99    inet_proto_init(argv[0], INET_PROTO_NAME_ALL);
100
101    while (*++argv) {
102	if ((aierr = hostname_to_sockaddr(argv[0], (char *) 0, 0, &res0)) != 0)
103	    msg_fatal("%s: %s", argv[0], MAI_STRERROR(aierr));
104	for (res = res0; res != 0; res = res->ai_next) {
105	    if ((rr = dns_sa_to_rr(argv[0], 0, res->ai_addr)) == 0)
106		msg_fatal("dns_sa_to_rr: %m");
107	    if (dns_rr_to_pa(rr, &hostaddr) == 0)
108		msg_fatal("dns_rr_to_pa: %m");
109	    vstream_printf("%s -> %s\n", argv[0], hostaddr.buf);
110	    vstream_fflush(VSTREAM_OUT);
111	    dns_rr_free(rr);
112	}
113	freeaddrinfo(res0);
114    }
115    return (0);
116}
117
118#endif
119