inet_common.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * stolen from net-tools-1.59 and stripped down for busybox by
  4. * Erik Andersen <andersen@codepoet.org>
  5. *
  6. * Heavily modified by Manuel Novoa III Mar 12, 2001
  7. *
  8. * Licensed under GPLv2, see file LICENSE in this source tree.
  9. */
  10. #include "libbb.h"
  11. #include "inet_common.h"
  12. int FAST_FUNC INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
  13. {
  14. struct hostent *hp;
  15. #if ENABLE_FEATURE_ETC_NETWORKS
  16. struct netent *np;
  17. #endif
  18. /* Grmpf. -FvK */
  19. s_in->sin_family = AF_INET;
  20. s_in->sin_port = 0;
  21. /* Default is special, meaning 0.0.0.0. */
  22. if (strcmp(name, "default") == 0) {
  23. s_in->sin_addr.s_addr = INADDR_ANY;
  24. return 1;
  25. }
  26. /* Look to see if it's a dotted quad. */
  27. if (inet_aton(name, &s_in->sin_addr)) {
  28. return 0;
  29. }
  30. /* If we expect this to be a hostname, try hostname database first */
  31. if (hostfirst) {
  32. #ifdef DEBUG
  33. bb_error_msg("gethostbyname(%s)", name);
  34. #endif
  35. hp = gethostbyname(name);
  36. if (hp) {
  37. memcpy(&s_in->sin_addr, hp->h_addr_list[0],
  38. sizeof(struct in_addr));
  39. return 0;
  40. }
  41. }
  42. #if ENABLE_FEATURE_ETC_NETWORKS
  43. /* Try the NETWORKS database to see if this is a known network. */
  44. #ifdef DEBUG
  45. bb_error_msg("getnetbyname(%s)", name);
  46. #endif
  47. np = getnetbyname(name);
  48. if (np) {
  49. s_in->sin_addr.s_addr = htonl(np->n_net);
  50. return 1;
  51. }
  52. #endif
  53. if (hostfirst) {
  54. /* Don't try again */
  55. return -1;
  56. }
  57. #ifdef DEBUG
  58. res_init();
  59. _res.options |= RES_DEBUG;
  60. bb_error_msg("gethostbyname(%s)", name);
  61. #endif
  62. hp = gethostbyname(name);
  63. if (!hp) {
  64. return -1;
  65. }
  66. memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
  67. return 0;
  68. }
  69. /* numeric: & 0x8000: "default" instead of "*",
  70. * & 0x4000: host instead of net,
  71. * & 0x0fff: don't resolve
  72. */
  73. char* FAST_FUNC INET_rresolve(struct sockaddr_in *s_in, int numeric, uint32_t netmask)
  74. {
  75. /* addr-to-name cache */
  76. struct addr {
  77. struct addr *next;
  78. uint32_t nip;
  79. smallint is_host;
  80. char name[1];
  81. };
  82. static struct addr *cache = NULL;
  83. struct addr *pn;
  84. char *name;
  85. uint32_t nip;
  86. smallint is_host;
  87. if (s_in->sin_family != AF_INET) {
  88. #ifdef DEBUG
  89. bb_error_msg("rresolve: unsupported address family %d!",
  90. s_in->sin_family);
  91. #endif
  92. errno = EAFNOSUPPORT;
  93. return NULL;
  94. }
  95. nip = s_in->sin_addr.s_addr;
  96. #ifdef DEBUG
  97. bb_error_msg("rresolve: %08x mask:%08x num:%08x", (unsigned)nip, netmask, numeric);
  98. #endif
  99. if (numeric & 0x0FFF)
  100. return xmalloc_sockaddr2dotted_noport((void*)s_in);
  101. if (nip == INADDR_ANY) {
  102. if (numeric & 0x8000)
  103. return xstrdup("default");
  104. return xstrdup("*");
  105. }
  106. is_host = ((nip & (~netmask)) != 0 || (numeric & 0x4000));
  107. pn = cache;
  108. while (pn) {
  109. if (pn->nip == nip && pn->is_host == is_host) {
  110. #ifdef DEBUG
  111. bb_error_msg("rresolve: found %s %08x in cache",
  112. (is_host ? "host" : "net"), (unsigned)nip);
  113. #endif
  114. return xstrdup(pn->name);
  115. }
  116. pn = pn->next;
  117. }
  118. name = NULL;
  119. if (is_host) {
  120. #ifdef DEBUG
  121. bb_error_msg("sockaddr2host_noport(%08x)", (unsigned)nip);
  122. #endif
  123. name = xmalloc_sockaddr2host_noport((void*)s_in);
  124. } else if (ENABLE_FEATURE_ETC_NETWORKS) {
  125. struct netent *np;
  126. #ifdef DEBUG
  127. bb_error_msg("getnetbyaddr(%08x)", (unsigned)ntohl(nip));
  128. #endif
  129. np = getnetbyaddr(ntohl(nip), AF_INET);
  130. if (np)
  131. name = xstrdup(np->n_name);
  132. }
  133. if (!name)
  134. name = xmalloc_sockaddr2dotted_noport((void*)s_in);
  135. pn = xmalloc(sizeof(*pn) + strlen(name)); /* no '+ 1', it's already accounted for */
  136. pn->next = cache;
  137. pn->nip = nip;
  138. pn->is_host = is_host;
  139. strcpy(pn->name, name);
  140. cache = pn;
  141. return name;
  142. }
  143. #if ENABLE_FEATURE_IPV6
  144. int FAST_FUNC INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
  145. {
  146. struct addrinfo req, *ai = NULL;
  147. int s;
  148. memset(&req, 0, sizeof(req));
  149. req.ai_family = AF_INET6;
  150. s = getaddrinfo(name, NULL, &req, &ai);
  151. if (s != 0) {
  152. bb_error_msg("getaddrinfo: %s: %d", name, s);
  153. return -1;
  154. }
  155. memcpy(sin6, ai->ai_addr, sizeof(*sin6));
  156. freeaddrinfo(ai);
  157. return 0;
  158. }
  159. #ifndef IN6_IS_ADDR_UNSPECIFIED
  160. # define IN6_IS_ADDR_UNSPECIFIED(a) \
  161. (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
  162. ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
  163. #endif
  164. char* FAST_FUNC INET6_rresolve(struct sockaddr_in6 *sin6, int numeric)
  165. {
  166. if (sin6->sin6_family != AF_INET6) {
  167. #ifdef DEBUG
  168. bb_error_msg("rresolve: unsupported address family %d!",
  169. sin6->sin6_family);
  170. #endif
  171. errno = EAFNOSUPPORT;
  172. return NULL;
  173. }
  174. if (numeric & 0x7FFF) {
  175. return xmalloc_sockaddr2dotted_noport((void*)sin6);
  176. }
  177. if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
  178. if (numeric & 0x8000)
  179. return xstrdup("default");
  180. return xstrdup("*");
  181. }
  182. return xmalloc_sockaddr2host_noport((void*)sin6);
  183. }
  184. #endif /* CONFIG_FEATURE_IPV6 */