/crypto/external/bsd/heimdal/dist/lib/roken/getnameinfo_verified.c

http://www.minix3.org/ · C · 96 lines · 46 code · 10 blank · 40 comment · 12 complexity · e7f55b459e41c56e1f88393fde9f4c52 MD5 · raw file

  1. /* $NetBSD: getnameinfo_verified.c,v 1.1.1.1 2011/04/13 18:15:41 elric Exp $ */
  2. /*
  3. * Copyright (c) 1999 - 2002 Kungliga Tekniska Hรถgskolan
  4. * (Royal Institute of Technology, Stockholm, Sweden).
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * 3. Neither the name of the Institute nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <krb5/roken.h>
  36. /*
  37. * Try to obtain a verified name for the address in `sa, salen' (much
  38. * similar to getnameinfo).
  39. * Verified in this context means that forwards and backwards lookups
  40. * in DNS are consistent. If that fails, return an error if the
  41. * NI_NAMEREQD flag is set or return the numeric address as a string.
  42. */
  43. ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
  44. getnameinfo_verified(const struct sockaddr *sa, socklen_t salen,
  45. char *host, size_t hostlen,
  46. char *serv, size_t servlen,
  47. int flags)
  48. {
  49. int ret;
  50. struct addrinfo *ai, *a;
  51. char servbuf[NI_MAXSERV];
  52. struct addrinfo hints;
  53. void *saaddr;
  54. size_t sasize;
  55. if (host == NULL)
  56. return EAI_NONAME;
  57. if (serv == NULL) {
  58. serv = servbuf;
  59. servlen = sizeof(servbuf);
  60. }
  61. ret = getnameinfo (sa, salen, host, hostlen, serv, servlen,
  62. flags | NI_NUMERICSERV);
  63. if (ret)
  64. goto fail;
  65. memset (&hints, 0, sizeof(hints));
  66. hints.ai_socktype = SOCK_STREAM;
  67. ret = getaddrinfo (host, serv, &hints, &ai);
  68. if (ret)
  69. goto fail;
  70. saaddr = socket_get_address(sa);
  71. sasize = socket_addr_size(sa);
  72. for (a = ai; a != NULL; a = a->ai_next) {
  73. if (sasize == socket_addr_size(a->ai_addr) &&
  74. memcmp(saaddr, socket_get_address(a->ai_addr), sasize) == 0) {
  75. freeaddrinfo (ai);
  76. return 0;
  77. }
  78. }
  79. freeaddrinfo (ai);
  80. fail:
  81. if (flags & NI_NAMEREQD)
  82. return EAI_NONAME;
  83. ret = getnameinfo (sa, salen, host, hostlen, serv, servlen,
  84. flags | NI_NUMERICSERV | NI_NUMERICHOST);
  85. return ret;
  86. }