/contrib/bind9/lib/lwres/getnameinfo.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 347 lines · 194 code · 28 blank · 125 comment · 73 complexity · e7541b8fe6ff2cd258d36a18bc34bb66 MD5 · raw file

  1. /*
  2. * Portions Copyright (C) 2004, 2005, 2007, 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
  3. * Portions Copyright (C) 1999-2001, 2003 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id$ */
  18. /*! \file */
  19. /*
  20. * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  21. * All rights reserved.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the above copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. Neither the name of the project nor the names of its contributors
  32. * may be used to endorse or promote products derived from this software
  33. * without specific prior written permission.
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  36. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  38. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  39. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  40. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  41. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  42. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  43. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  44. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  45. * SUCH DAMAGE.
  46. */
  47. /*
  48. * XXX
  49. * Issues to be discussed:
  50. * - Return values. There seems to be no standard for return value (RFC2553)
  51. * but INRIA implementation returns EAI_xxx defined for getaddrinfo().
  52. */
  53. /**
  54. * This function is equivalent to the getnameinfo(3) function defined in
  55. * RFC2133. lwres_getnameinfo() returns the hostname for the struct
  56. * sockaddr sa which is salen bytes long. The hostname is of length
  57. * hostlen and is returned via *host. The maximum length of the hostname
  58. * is 1025 bytes: #NI_MAXHOST.
  59. *
  60. * The name of the service associated with the port number in sa is
  61. * returned in *serv. It is servlen bytes long. The maximum length of the
  62. * service name is #NI_MAXSERV - 32 bytes.
  63. *
  64. * The flags argument sets the following bits:
  65. *
  66. * \li #NI_NOFQDN:
  67. * A fully qualified domain name is not required for local hosts.
  68. * The local part of the fully qualified domain name is returned
  69. * instead.
  70. *
  71. * \li #NI_NUMERICHOST
  72. * Return the address in numeric form, as if calling inet_ntop(),
  73. * instead of a host name.
  74. *
  75. * \li #NI_NAMEREQD
  76. * A name is required. If the hostname cannot be found in the DNS
  77. * and this flag is set, a non-zero error code is returned. If the
  78. * hostname is not found and the flag is not set, the address is
  79. * returned in numeric form.
  80. *
  81. * \li #NI_NUMERICSERV
  82. * The service name is returned as a digit string representing the
  83. * port number.
  84. *
  85. * \li #NI_DGRAM
  86. * Specifies that the service being looked up is a datagram
  87. * service, and causes getservbyport() to be called with a second
  88. * argument of "udp" instead of its default of "tcp". This is
  89. * required for the few ports (512-514) that have different
  90. * services for UDP and TCP.
  91. *
  92. * \section getnameinfo_return Return Values
  93. *
  94. * lwres_getnameinfo() returns 0 on success or a non-zero error code if
  95. * an error occurs.
  96. *
  97. * \section getname_see See Also
  98. *
  99. * RFC2133, getservbyport(),
  100. * lwres_getnamebyaddr(). lwres_net_ntop().
  101. *
  102. * \section getnameinfo_bugs Bugs
  103. *
  104. * RFC2133 fails to define what the nonzero return values of
  105. * getnameinfo() are.
  106. */
  107. #include <config.h>
  108. #include <stdio.h>
  109. #include <string.h>
  110. #include <lwres/lwres.h>
  111. #include <lwres/net.h>
  112. #include <lwres/netdb.h>
  113. #include "print_p.h"
  114. #include "assert_p.h"
  115. #define SUCCESS 0
  116. /*% afd structure definition */
  117. static struct afd {
  118. int a_af;
  119. size_t a_addrlen;
  120. size_t a_socklen;
  121. } afdl [] = {
  122. /*!
  123. * First entry is linked last...
  124. */
  125. { AF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in) },
  126. { AF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6) },
  127. {0, 0, 0},
  128. };
  129. #define ENI_NOSERVNAME 1
  130. #define ENI_NOHOSTNAME 2
  131. #define ENI_MEMORY 3
  132. #define ENI_SYSTEM 4
  133. #define ENI_FAMILY 5
  134. #define ENI_SALEN 6
  135. #define ENI_NOSOCKET 7
  136. /*!
  137. * The test against 0 is there to keep the Solaris compiler
  138. * from complaining about "end-of-loop code not reached".
  139. */
  140. #define ERR(code) \
  141. do { result = (code); \
  142. if (result != 0) goto cleanup; \
  143. } while (0)
  144. /*% lightweight resolver socket address structure to hostname and service name */
  145. int
  146. lwres_getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
  147. size_t hostlen, char *serv, size_t servlen, int flags)
  148. {
  149. struct afd *afd;
  150. struct servent *sp;
  151. unsigned short port;
  152. #ifdef LWRES_PLATFORM_HAVESALEN
  153. size_t len;
  154. #endif
  155. int family, i;
  156. const void *addr;
  157. char *p;
  158. #if 0
  159. unsigned long v4a;
  160. unsigned char pfx;
  161. #endif
  162. char numserv[sizeof("65000")];
  163. char numaddr[sizeof("abcd:abcd:abcd:abcd:abcd:abcd:255.255.255.255")
  164. + 1 + sizeof("4294967295")];
  165. const char *proto;
  166. lwres_uint32_t lwf = 0;
  167. lwres_context_t *lwrctx = NULL;
  168. lwres_gnbaresponse_t *by = NULL;
  169. int result = SUCCESS;
  170. int n;
  171. if (sa == NULL)
  172. ERR(ENI_NOSOCKET);
  173. #ifdef LWRES_PLATFORM_HAVESALEN
  174. len = sa->sa_len;
  175. if (len != salen)
  176. ERR(ENI_SALEN);
  177. #endif
  178. family = sa->sa_family;
  179. for (i = 0; afdl[i].a_af; i++)
  180. if (afdl[i].a_af == family) {
  181. afd = &afdl[i];
  182. goto found;
  183. }
  184. ERR(ENI_FAMILY);
  185. found:
  186. if (salen != afd->a_socklen)
  187. ERR(ENI_SALEN);
  188. switch (family) {
  189. case AF_INET:
  190. port = ((const struct sockaddr_in *)sa)->sin_port;
  191. addr = &((const struct sockaddr_in *)sa)->sin_addr.s_addr;
  192. break;
  193. case AF_INET6:
  194. port = ((const struct sockaddr_in6 *)sa)->sin6_port;
  195. addr = ((const struct sockaddr_in6 *)sa)->sin6_addr.s6_addr;
  196. break;
  197. default:
  198. port = 0;
  199. addr = NULL;
  200. POST(port); POST(addr);
  201. INSIST(0);
  202. }
  203. proto = (flags & NI_DGRAM) ? "udp" : "tcp";
  204. if (serv == NULL || servlen == 0U) {
  205. /*
  206. * Caller does not want service.
  207. */
  208. } else if ((flags & NI_NUMERICSERV) != 0 ||
  209. (sp = getservbyport(port, proto)) == NULL) {
  210. snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
  211. if ((strlen(numserv) + 1) > servlen)
  212. ERR(ENI_MEMORY);
  213. strcpy(serv, numserv);
  214. } else {
  215. if ((strlen(sp->s_name) + 1) > servlen)
  216. ERR(ENI_MEMORY);
  217. strcpy(serv, sp->s_name);
  218. }
  219. #if 0
  220. switch (sa->sa_family) {
  221. case AF_INET:
  222. v4a = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
  223. if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
  224. flags |= NI_NUMERICHOST;
  225. v4a >>= IN_CLASSA_NSHIFT;
  226. if (v4a == 0 || v4a == IN_LOOPBACKNET)
  227. flags |= NI_NUMERICHOST;
  228. break;
  229. case AF_INET6:
  230. pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[0];
  231. if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
  232. flags |= NI_NUMERICHOST;
  233. break;
  234. }
  235. #endif
  236. if (host == NULL || hostlen == 0U) {
  237. /*
  238. * What should we do?
  239. */
  240. } else if (flags & NI_NUMERICHOST) {
  241. if (lwres_net_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
  242. == NULL)
  243. ERR(ENI_SYSTEM);
  244. #if defined(LWRES_HAVE_SIN6_SCOPE_ID)
  245. if (afd->a_af == AF_INET6 &&
  246. ((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
  247. char *p = numaddr + strlen(numaddr);
  248. const char *stringscope = NULL;
  249. #if 0
  250. if ((flags & NI_NUMERICSCOPE) == 0) {
  251. /*
  252. * Vendors may want to add support for
  253. * non-numeric scope identifier.
  254. */
  255. stringscope = foo;
  256. }
  257. #endif
  258. if (stringscope == NULL) {
  259. snprintf(p, sizeof(numaddr) - (p - numaddr),
  260. "%%%u",
  261. ((const struct sockaddr_in6 *)sa)->sin6_scope_id);
  262. } else {
  263. snprintf(p, sizeof(numaddr) - (p - numaddr),
  264. "%%%s", stringscope);
  265. }
  266. }
  267. #endif
  268. if (strlen(numaddr) + 1 > hostlen)
  269. ERR(ENI_MEMORY);
  270. strcpy(host, numaddr);
  271. } else {
  272. switch (family) {
  273. case AF_INET:
  274. lwf = LWRES_ADDRTYPE_V4;
  275. break;
  276. case AF_INET6:
  277. lwf = LWRES_ADDRTYPE_V6;
  278. break;
  279. default:
  280. INSIST(0);
  281. }
  282. n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
  283. if (n == 0)
  284. (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
  285. if (n == 0)
  286. n = lwres_getnamebyaddr(lwrctx, lwf,
  287. (lwres_uint16_t)afd->a_addrlen,
  288. addr, &by);
  289. if (n == 0) {
  290. if (flags & NI_NOFQDN) {
  291. p = strchr(by->realname, '.');
  292. if (p)
  293. *p = '\0';
  294. }
  295. if ((strlen(by->realname) + 1) > hostlen)
  296. ERR(ENI_MEMORY);
  297. strcpy(host, by->realname);
  298. } else {
  299. if (flags & NI_NAMEREQD)
  300. ERR(ENI_NOHOSTNAME);
  301. if (lwres_net_ntop(afd->a_af, addr, numaddr,
  302. sizeof(numaddr))
  303. == NULL)
  304. ERR(ENI_NOHOSTNAME);
  305. if ((strlen(numaddr) + 1) > hostlen)
  306. ERR(ENI_MEMORY);
  307. strcpy(host, numaddr);
  308. }
  309. }
  310. result = SUCCESS;
  311. cleanup:
  312. if (by != NULL)
  313. lwres_gnbaresponse_free(lwrctx, &by);
  314. if (lwrctx != NULL) {
  315. lwres_conf_clear(lwrctx);
  316. lwres_context_destroy(&lwrctx);
  317. }
  318. return (result);
  319. }