1/* $NetBSD$ */ 2 3/* 4 * Copyright (C) 2009, 2011, 2012 Internet Systems Consortium, Inc. ("ISC") 5 * 6 * Permission to use, copy, modify, and/or distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 16 * PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19/* Id */ 20 21/*! \file */ 22 23/* 24 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 25 * All rights reserved. 26 * 27 * Redistribution and use in source and binary forms, with or without 28 * modification, are permitted provided that the following conditions 29 * are met: 30 * 1. Redistributions of source code must retain the above copyright 31 * notice, this list of conditions and the following disclaimer. 32 * 2. Redistributions in binary form must reproduce the above copyright 33 * notice, this list of conditions and the following disclaimer in the 34 * documentation and/or other materials provided with the distribution. 35 * 3. Neither the name of the project nor the names of its contributors 36 * may be used to endorse or promote products derived from this software 37 * without specific prior written permission. 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 40 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 42 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 43 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 44 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 45 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 47 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 48 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 49 * SUCH DAMAGE. 50 */ 51 52/** 53 * getnameinfo() returns the hostname for the struct sockaddr sa which is 54 * salen bytes long. The hostname is of length hostlen and is returned via 55 * *host. The maximum length of the hostname is 1025 bytes: #NI_MAXHOST. 56 * 57 * The name of the service associated with the port number in sa is 58 * returned in *serv. It is servlen bytes long. The maximum length of the 59 * service name is #NI_MAXSERV - 32 bytes. 60 * 61 * The flags argument sets the following bits: 62 * 63 * \li #NI_NOFQDN: 64 * A fully qualified domain name is not required for local hosts. 65 * The local part of the fully qualified domain name is returned 66 * instead. 67 * 68 * \li #NI_NUMERICHOST 69 * Return the address in numeric form, as if calling inet_ntop(), 70 * instead of a host name. 71 * 72 * \li #NI_NAMEREQD 73 * A name is required. If the hostname cannot be found in the DNS 74 * and this flag is set, a non-zero error code is returned. If the 75 * hostname is not found and the flag is not set, the address is 76 * returned in numeric form. 77 * 78 * \li #NI_NUMERICSERV 79 * The service name is returned as a digit string representing the 80 * port number. 81 * 82 * \li #NI_DGRAM 83 * Specifies that the service being looked up is a datagram 84 * service, and causes getservbyport() to be called with a second 85 * argument of "udp" instead of its default of "tcp". This is 86 * required for the few ports (512-514) that have different 87 * services for UDP and TCP. 88 * 89 * \section getnameinfo_return Return Values 90 * 91 * getnameinfo() returns 0 on success or a non-zero error code if 92 * an error occurs. 93 * 94 * \section getname_see See Also 95 * 96 * RFC3493, getservbyport(), 97 * getnamebyaddr(). inet_ntop(). 98 */ 99 100#include <config.h> 101 102#include <stdio.h> 103#include <string.h> 104 105#include <isc/netaddr.h> 106#include <isc/print.h> 107#include <isc/sockaddr.h> 108#include <isc/util.h> 109 110#include <dns/byaddr.h> 111#include <dns/client.h> 112#include <dns/fixedname.h> 113#include <dns/name.h> 114#include <dns/rdata.h> 115#include <dns/rdataset.h> 116#include <dns/rdatastruct.h> 117#include <dns/result.h> 118 119#include <irs/context.h> 120#include <irs/netdb.h> 121 122#define SUCCESS 0 123 124/*% afd structure definition */ 125static struct afd { 126 int a_af; 127 size_t a_addrlen; 128 size_t a_socklen; 129} afdl [] = { 130 /*! 131 * First entry is linked last... 132 */ 133 { AF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in) }, 134 { AF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6) }, 135 {0, 0, 0}, 136}; 137 138/*! 139 * The test against 0 is there to keep the Solaris compiler 140 * from complaining about "end-of-loop code not reached". 141 */ 142#define ERR(code) \ 143 do { result = (code); \ 144 if (result != 0) goto cleanup; \ 145 } while (/*CONSTCOND*/0) 146 147int 148getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, 149 IRS_GETNAMEINFO_BUFLEN_T hostlen, char *serv, 150 IRS_GETNAMEINFO_BUFLEN_T servlen, IRS_GETNAMEINFO_FLAGS_T flags) 151{ 152 struct afd *afd; 153 struct servent *sp; 154 unsigned short port = 0; 155#ifdef IRS_PLATFORM_HAVESALEN 156 size_t len; 157#endif 158 int family, i; 159 const void *addr = NULL; 160 char *p; 161#if 0 162 unsigned long v4a; 163 unsigned char pfx; 164#endif 165 char numserv[sizeof("65000")]; 166 char numaddr[sizeof("abcd:abcd:abcd:abcd:abcd:abcd:255.255.255.255") 167 + 1 + sizeof("4294967295")]; 168 const char *proto; 169 int result = SUCCESS; 170 171 if (sa == NULL) 172 ERR(EAI_FAIL); 173 174#ifdef IRS_PLATFORM_HAVESALEN 175 len = sa->sa_len; 176 if (len != salen) 177 ERR(EAI_FAIL); 178#endif 179 180 family = sa->sa_family; 181 for (i = 0; afdl[i].a_af; i++) 182 if (afdl[i].a_af == family) { 183 afd = &afdl[i]; 184 goto found; 185 } 186 ERR(EAI_FAMILY); 187 188 found: 189 if (salen != afd->a_socklen) 190 ERR(EAI_FAIL); 191 192 switch (family) { 193 case AF_INET: 194 port = ((const struct sockaddr_in *)sa)->sin_port; 195 addr = &((const struct sockaddr_in *)sa)->sin_addr.s_addr; 196 break; 197 198 case AF_INET6: 199 port = ((const struct sockaddr_in6 *)sa)->sin6_port; 200 addr = ((const struct sockaddr_in6 *)sa)->sin6_addr.s6_addr; 201 break; 202 203 default: 204 INSIST(0); 205 } 206 proto = (flags & NI_DGRAM) ? "udp" : "tcp"; 207 208 if (serv == NULL || servlen == 0U) { 209 /* 210 * Caller does not want service. 211 */ 212 } else if ((flags & NI_NUMERICSERV) != 0 || 213 (sp = getservbyport(port, proto)) == NULL) { 214 snprintf(numserv, sizeof(numserv), "%d", ntohs(port)); 215 if ((strlen(numserv) + 1) > servlen) 216 ERR(EAI_OVERFLOW); 217 strcpy(serv, numserv); 218 } else { 219 if ((strlen(sp->s_name) + 1) > servlen) 220 ERR(EAI_OVERFLOW); 221 strcpy(serv, sp->s_name); 222 } 223 224#if 0 225 switch (sa->sa_family) { 226 case AF_INET: 227 v4a = ((struct sockaddr_in *)sa)->sin_addr.s_addr; 228 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a)) 229 flags |= NI_NUMERICHOST; 230 v4a >>= IN_CLASSA_NSHIFT; 231 if (v4a == 0 || v4a == IN_LOOPBACKNET) 232 flags |= NI_NUMERICHOST; 233 break; 234 235 case AF_INET6: 236 pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[0]; 237 if (pfx == 0 || pfx == 0xfe || pfx == 0xff) 238 flags |= NI_NUMERICHOST; 239 break; 240 } 241#endif 242 243 if (host == NULL || hostlen == 0U) { 244 /* 245 * do nothing in this case. 246 * in case you are wondering if "&&" is more correct than 247 * "||" here: RFC3493 says that host == NULL or hostlen == 0 248 * means that the caller does not want the result. 249 */ 250 } else if ((flags & NI_NUMERICHOST) != 0) { 251 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr)) 252 == NULL) 253 ERR(EAI_SYSTEM); 254#if defined(IRS_HAVE_SIN6_SCOPE_ID) 255 if (afd->a_af == AF_INET6 && 256 ((const struct sockaddr_in6 *)sa)->sin6_scope_id) { 257 char *p = numaddr + strlen(numaddr); 258 const char *stringscope = NULL; 259#ifdef VENDOR_SPECIFIC 260 /* 261 * Vendors may want to add support for 262 * non-numeric scope identifier. 263 */ 264 stringscope = foo; 265#endif 266 if (stringscope == NULL) { 267 snprintf(p, sizeof(numaddr) - (p - numaddr), 268 "%%%u", 269 ((const struct sockaddr_in6 *)sa)->sin6_scope_id); 270 } else { 271 snprintf(p, sizeof(numaddr) - (p - numaddr), 272 "%%%s", stringscope); 273 } 274 } 275#endif 276 if (strlen(numaddr) + 1 > hostlen) 277 ERR(EAI_OVERFLOW); 278 strcpy(host, numaddr); 279 } else { 280 isc_netaddr_t netaddr; 281 dns_fixedname_t ptrfname; 282 dns_name_t *ptrname; 283 irs_context_t *irsctx = NULL; 284 dns_client_t *client; 285 isc_boolean_t found = ISC_FALSE; 286 dns_namelist_t answerlist; 287 dns_rdataset_t *rdataset; 288 isc_region_t hostregion; 289 char hoststr[1024]; /* is this enough? */ 290 isc_result_t iresult; 291 292 /* Get IRS context and the associated DNS client object */ 293 iresult = irs_context_get(&irsctx); 294 if (iresult != ISC_R_SUCCESS) 295 ERR(EAI_FAIL); 296 client = irs_context_getdnsclient(irsctx); 297 298 /* Make query name */ 299 isc_netaddr_fromsockaddr(&netaddr, (const isc_sockaddr_t *)sa); 300 dns_fixedname_init(&ptrfname); 301 ptrname = dns_fixedname_name(&ptrfname); 302 iresult = dns_byaddr_createptrname2(&netaddr, 0, ptrname); 303 if (iresult != ISC_R_SUCCESS) 304 ERR(EAI_FAIL); 305 306 /* Get the PTR RRset */ 307 ISC_LIST_INIT(answerlist); 308 iresult = dns_client_resolve(client, ptrname, 309 dns_rdataclass_in, 310 dns_rdatatype_ptr, 311 DNS_CLIENTRESOPT_ALLOWRUN, 312 &answerlist); 313 switch (iresult) { 314 case ISC_R_SUCCESS: 315 /* 316 * a 'non-existent' error is not necessarily fatal for 317 * getnameinfo(). 318 */ 319 case DNS_R_NCACHENXDOMAIN: 320 case DNS_R_NCACHENXRRSET: 321 break; 322 case DNS_R_SIGINVALID: 323 case DNS_R_SIGEXPIRED: 324 case DNS_R_SIGFUTURE: 325 case DNS_R_KEYUNAUTHORIZED: 326 case DNS_R_MUSTBESECURE: 327 case DNS_R_COVERINGNSEC: 328 case DNS_R_NOTAUTHORITATIVE: 329 case DNS_R_NOVALIDKEY: 330 case DNS_R_NOVALIDDS: 331 case DNS_R_NOVALIDSIG: 332 ERR(EAI_INSECUREDATA); 333 default: 334 ERR(EAI_FAIL); 335 } 336 337 /* Parse the answer for the hostname */ 338 for (ptrname = ISC_LIST_HEAD(answerlist); ptrname != NULL; 339 ptrname = ISC_LIST_NEXT(ptrname, link)) { 340 for (rdataset = ISC_LIST_HEAD(ptrname->list); 341 rdataset != NULL; 342 rdataset = ISC_LIST_NEXT(rdataset, link)) { 343 if (!dns_rdataset_isassociated(rdataset)) 344 continue; 345 if (rdataset->type != dns_rdatatype_ptr) 346 continue; 347 348 for (iresult = dns_rdataset_first(rdataset); 349 iresult == ISC_R_SUCCESS; 350 iresult = dns_rdataset_next(rdataset)) { 351 dns_rdata_t rdata; 352 dns_rdata_ptr_t rdata_ptr; 353 isc_buffer_t b; 354 355 dns_rdata_init(&rdata); 356 dns_rdataset_current(rdataset, &rdata); 357 dns_rdata_tostruct(&rdata, &rdata_ptr, 358 NULL); 359 360 isc_buffer_init(&b, hoststr, 361 sizeof(hoststr)); 362 iresult = 363 dns_name_totext(&rdata_ptr.ptr, 364 ISC_TRUE, &b); 365 dns_rdata_freestruct(&rdata_ptr); 366 if (iresult == ISC_R_SUCCESS) { 367 /* 368 * We ignore the rest of the 369 * answer. After all, 370 * getnameinfo() can return 371 * at most one hostname. 372 */ 373 found = ISC_TRUE; 374 isc_buffer_usedregion( 375 &b, &hostregion); 376 goto ptrfound; 377 } 378 379 } 380 } 381 } 382 ptrfound: 383 dns_client_freeresanswer(client, &answerlist); 384 if (found) { 385 if ((flags & NI_NOFQDN) != 0) { 386 p = strchr(hoststr, '.'); 387 if (p) 388 *p = '\0'; 389 } 390 if (hostregion.length + 1 > hostlen) 391 ERR(EAI_OVERFLOW); 392 snprintf(host, hostlen, "%.*s", 393 (int)hostregion.length, 394 (char *)hostregion.base); 395 } else { 396 if ((flags & NI_NAMEREQD) != 0) 397 ERR(EAI_NONAME); 398 if (inet_ntop(afd->a_af, addr, numaddr, 399 sizeof(numaddr)) == NULL) 400 ERR(EAI_SYSTEM); 401 if ((strlen(numaddr) + 1) > hostlen) 402 ERR(EAI_OVERFLOW); 403 strcpy(host, numaddr); 404 } 405 } 406 result = SUCCESS; 407 408 cleanup: 409 return (result); 410} 411