1 /* $NetBSD: dns_sa_to_rr.c,v 1.1.1.2 2014/07/06 19:27:50 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. This will be both the qname 21 /* and the rname in the synthetic DNS resource record. 22 /* .IP pref 23 /* The resource record MX host preference, if applicable. 24 /* .IP sa 25 /* Binary address. 26 /* DIAGNOSTICS 27 /* The result is a null pointer in case of problems, with the 28 /* errno variable set to indicate the problem type. 29 /* LICENSE 30 /* .ad 31 /* .fi 32 /* The Secure Mailer license must be distributed with this software. 33 /* AUTHOR(S) 34 /* Wietse Venema 35 /* IBM T.J. Watson Research 36 /* P.O. Box 704 37 /* Yorktown Heights, NY 10598, USA 38 /*--*/ 39 40 /* System libraries. */ 41 42 #include <sys_defs.h> 43 #include <errno.h> 44 45 /* Utility library. */ 46 47 #include <msg.h> 48 49 /* DNS library. */ 50 51 #include <dns.h> 52 53 /* dns_sa_to_rr - socket address to resource record */ 54 55 DNS_RR *dns_sa_to_rr(const char *hostname, unsigned pref, struct sockaddr * sa) 56 { 57 #define DUMMY_TTL 0 58 59 if (sa->sa_family == AF_INET) { 60 return (dns_rr_create(hostname, hostname, T_A, C_IN, DUMMY_TTL, pref, 61 (char *) &SOCK_ADDR_IN_ADDR(sa), 62 sizeof(SOCK_ADDR_IN_ADDR(sa)))); 63 #ifdef HAS_IPV6 64 } else if (sa->sa_family == AF_INET6) { 65 return (dns_rr_create(hostname, hostname, T_AAAA, C_IN, DUMMY_TTL, pref, 66 (char *) &SOCK_ADDR_IN6_ADDR(sa), 67 sizeof(SOCK_ADDR_IN6_ADDR(sa)))); 68 #endif 69 } else { 70 errno = EAFNOSUPPORT; 71 return (0); 72 } 73 } 74 75 /* 76 * Stand-alone test program. 77 */ 78 #ifdef TEST 79 #include <vstream.h> 80 #include <myaddrinfo.h> 81 #include <inet_proto.h> 82 83 static const char *myname; 84 85 static NORETURN usage(void) 86 { 87 msg_fatal("usage: %s hostname", myname); 88 } 89 90 int main(int argc, char **argv) 91 { 92 MAI_HOSTADDR_STR hostaddr; 93 struct addrinfo *res0; 94 struct addrinfo *res; 95 DNS_RR *rr; 96 int aierr; 97 98 myname = argv[0]; 99 if (argc < 2) 100 usage(); 101 102 inet_proto_init(argv[0], INET_PROTO_NAME_ALL); 103 104 while (*++argv) { 105 if ((aierr = hostname_to_sockaddr(argv[0], (char *) 0, 0, &res0)) != 0) 106 msg_fatal("%s: %s", argv[0], MAI_STRERROR(aierr)); 107 for (res = res0; res != 0; res = res->ai_next) { 108 if ((rr = dns_sa_to_rr(argv[0], 0, res->ai_addr)) == 0) 109 msg_fatal("dns_sa_to_rr: %m"); 110 if (dns_rr_to_pa(rr, &hostaddr) == 0) 111 msg_fatal("dns_rr_to_pa: %m"); 112 vstream_printf("%s -> %s\n", argv[0], hostaddr.buf); 113 vstream_fflush(VSTREAM_OUT); 114 dns_rr_free(rr); 115 } 116 freeaddrinfo(res0); 117 } 118 return (0); 119 } 120 121 #endif 122