PageRenderTime 31ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/avm2_env/usr/src/lib/libutil/realhostname.c

https://gitlab.com/kidaa/crossbridge
C | 200 lines | 138 code | 24 blank | 38 comment | 35 complexity | b9da9474271756cdd6bd12bfc34e0a6b MD5 | raw file
Possible License(s): GPL-3.0, Apache-2.0, CC-BY-SA-3.0, LGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0, LGPL-2.1, AGPL-1.0, BSD-3-Clause, LGPL-2.0
  1. /*-
  2. * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. */
  26. #include <sys/cdefs.h>
  27. __FBSDID("$FreeBSD: src/lib/libutil/realhostname.c,v 1.21.2.1.6.1 2010/12/21 17:09:25 kensmith Exp $");
  28. #include <sys/param.h>
  29. #include <sys/socket.h>
  30. #include <netdb.h>
  31. #include <netinet/in.h>
  32. #include <arpa/inet.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include "libutil.h"
  36. struct sockinet {
  37. u_char si_len;
  38. u_char si_family;
  39. u_short si_port;
  40. };
  41. int
  42. realhostname(char *host, size_t hsize, const struct in_addr *ip)
  43. {
  44. char trimmed[MAXHOSTNAMELEN];
  45. int result;
  46. struct hostent *hp;
  47. result = HOSTNAME_INVALIDADDR;
  48. hp = gethostbyaddr((const char *)ip, sizeof(*ip), AF_INET);
  49. if (hp != NULL) {
  50. strlcpy(trimmed, hp->h_name, sizeof(trimmed));
  51. trimdomain(trimmed, strlen(trimmed));
  52. if (strlen(trimmed) <= hsize) {
  53. char lookup[MAXHOSTNAMELEN];
  54. strlcpy(lookup, hp->h_name, sizeof(lookup));
  55. hp = gethostbyname(lookup);
  56. if (hp == NULL)
  57. result = HOSTNAME_INVALIDNAME;
  58. else for (; ; hp->h_addr_list++) {
  59. if (*hp->h_addr_list == NULL) {
  60. result = HOSTNAME_INCORRECTNAME;
  61. break;
  62. }
  63. if (!memcmp(*hp->h_addr_list, ip, sizeof(*ip))) {
  64. strncpy(host, trimmed, hsize);
  65. return HOSTNAME_FOUND;
  66. }
  67. }
  68. }
  69. }
  70. strncpy(host, inet_ntoa(*ip), hsize);
  71. return result;
  72. }
  73. /*
  74. * struct sockaddr has very lax alignment requirements, since all its
  75. * members are char or equivalent. This is a problem when trying to
  76. * dereference a struct sockaddr_in6 * that was passed in as a struct
  77. * sockaddr *. Although we know (or trust) that the passed-in struct was
  78. * properly aligned, the compiler doesn't, and (rightly) complains. These
  79. * macros perform the cast in a way that the compiler will accept.
  80. */
  81. #define SOCKADDR_IN6(p) ((struct sockaddr_in6 *)(void *)(p))
  82. #define SOCKADDR_IN(p) ((struct sockaddr_in *)(void *)(p))
  83. #define SOCKINET(p) ((struct sockinet *)(void *)(p))
  84. int
  85. realhostname_sa(char *host, size_t hsize, struct sockaddr *addr, int addrlen)
  86. {
  87. int result, error;
  88. char buf[NI_MAXHOST];
  89. #ifdef INET6
  90. struct sockaddr_in lsin;
  91. #endif
  92. result = HOSTNAME_INVALIDADDR;
  93. #ifdef INET6
  94. /* IPv4 mapped IPv6 addr consideraton, specified in rfc2373. */
  95. if (addr->sa_family == AF_INET6 &&
  96. addrlen == sizeof(struct sockaddr_in6) &&
  97. IN6_IS_ADDR_V4MAPPED(&SOCKADDR_IN6(addr)->sin6_addr)) {
  98. struct sockaddr_in6 *sin6;
  99. sin6 = SOCKADDR_IN6(addr);
  100. memset(&lsin, 0, sizeof(lsin));
  101. lsin.sin_len = sizeof(struct sockaddr_in);
  102. lsin.sin_family = AF_INET;
  103. lsin.sin_port = sin6->sin6_port;
  104. memcpy(&lsin.sin_addr, &sin6->sin6_addr.s6_addr[12],
  105. sizeof(struct in_addr));
  106. addr = (struct sockaddr *)&lsin;
  107. addrlen = lsin.sin_len;
  108. }
  109. #endif
  110. error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
  111. NI_NAMEREQD);
  112. if (error == 0) {
  113. struct addrinfo hints, *res, *ores;
  114. struct sockaddr *sa;
  115. memset(&hints, 0, sizeof(struct addrinfo));
  116. hints.ai_family = addr->sa_family;
  117. hints.ai_flags = AI_CANONNAME | AI_PASSIVE;
  118. hints.ai_socktype = SOCK_STREAM;
  119. error = getaddrinfo(buf, NULL, &hints, &res);
  120. if (error) {
  121. result = HOSTNAME_INVALIDNAME;
  122. goto numeric;
  123. }
  124. for (ores = res; ; res = res->ai_next) {
  125. if (res == NULL) {
  126. freeaddrinfo(ores);
  127. result = HOSTNAME_INCORRECTNAME;
  128. goto numeric;
  129. }
  130. sa = res->ai_addr;
  131. if (sa == NULL) {
  132. freeaddrinfo(ores);
  133. result = HOSTNAME_INCORRECTNAME;
  134. goto numeric;
  135. }
  136. if (sa->sa_len == addrlen &&
  137. sa->sa_family == addr->sa_family) {
  138. SOCKINET(sa)->si_port = SOCKINET(addr)->si_port;
  139. #ifdef INET6
  140. /*
  141. * XXX: sin6_socpe_id may not been
  142. * filled by DNS
  143. */
  144. if (sa->sa_family == AF_INET6 &&
  145. SOCKADDR_IN6(sa)->sin6_scope_id == 0)
  146. SOCKADDR_IN6(sa)->sin6_scope_id =
  147. SOCKADDR_IN6(addr)->sin6_scope_id;
  148. #endif
  149. if (!memcmp(sa, addr, sa->sa_len)) {
  150. result = HOSTNAME_FOUND;
  151. if (ores->ai_canonname == NULL) {
  152. freeaddrinfo(ores);
  153. goto numeric;
  154. }
  155. strlcpy(buf, ores->ai_canonname,
  156. sizeof(buf));
  157. trimdomain(buf, hsize);
  158. if (strlen(buf) > hsize &&
  159. addr->sa_family == AF_INET) {
  160. freeaddrinfo(ores);
  161. goto numeric;
  162. }
  163. strncpy(host, buf, hsize);
  164. break;
  165. }
  166. }
  167. }
  168. freeaddrinfo(ores);
  169. } else {
  170. numeric:
  171. if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
  172. NI_NUMERICHOST) == 0)
  173. strncpy(host, buf, hsize);
  174. }
  175. return result;
  176. }