inet_common.c 4.5 KB

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