inet_common.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 tarball for details.
  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, bb_str_default)) {
  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. #ifdef DEBUG
  32. if (hostfirst) {
  33. bb_error_msg("gethostbyname(%s)", name);
  34. }
  35. #endif
  36. if (hostfirst) {
  37. hp = gethostbyname(name);
  38. if (hp != NULL) {
  39. memcpy(&s_in->sin_addr, hp->h_addr_list[0],
  40. sizeof(struct in_addr));
  41. return 0;
  42. }
  43. }
  44. #if ENABLE_FEATURE_ETC_NETWORKS
  45. /* Try the NETWORKS database to see if this is a known network. */
  46. #ifdef DEBUG
  47. bb_error_msg("getnetbyname(%s)", name);
  48. #endif
  49. np = getnetbyname(name);
  50. if (np != NULL) {
  51. s_in->sin_addr.s_addr = htonl(np->n_net);
  52. return 1;
  53. }
  54. #endif
  55. if (hostfirst) {
  56. /* Don't try again */
  57. return -1;
  58. }
  59. #ifdef DEBUG
  60. res_init();
  61. _res.options |= RES_DEBUG;
  62. bb_error_msg("gethostbyname(%s)", name);
  63. #endif
  64. hp = gethostbyname(name);
  65. if (hp == NULL) {
  66. return -1;
  67. }
  68. memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
  69. return 0;
  70. }
  71. /* numeric: & 0x8000: default instead of *,
  72. * & 0x4000: host instead of net,
  73. * & 0x0fff: don't resolve
  74. */
  75. char* FAST_FUNC INET_rresolve(struct sockaddr_in *s_in, int numeric, uint32_t netmask)
  76. {
  77. /* addr-to-name cache */
  78. struct addr {
  79. struct addr *next;
  80. struct sockaddr_in addr;
  81. int host;
  82. char name[1];
  83. };
  84. static struct addr *cache = NULL;
  85. struct addr *pn;
  86. char *name;
  87. uint32_t ad, host_ad;
  88. int host = 0;
  89. if (s_in->sin_family != AF_INET) {
  90. #ifdef DEBUG
  91. bb_error_msg("rresolve: unsupported address family %d!",
  92. s_in->sin_family);
  93. #endif
  94. errno = EAFNOSUPPORT;
  95. return NULL;
  96. }
  97. ad = s_in->sin_addr.s_addr;
  98. #ifdef DEBUG
  99. bb_error_msg("rresolve: %08x, mask %08x, num %08x", (unsigned)ad, netmask, numeric);
  100. #endif
  101. if (ad == INADDR_ANY) {
  102. if ((numeric & 0x0FFF) == 0) {
  103. if (numeric & 0x8000)
  104. return xstrdup(bb_str_default);
  105. return xstrdup("*");
  106. }
  107. }
  108. if (numeric & 0x0FFF)
  109. return xstrdup(inet_ntoa(s_in->sin_addr));
  110. if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
  111. host = 1;
  112. pn = cache;
  113. while (pn) {
  114. if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
  115. #ifdef DEBUG
  116. bb_error_msg("rresolve: found %s %08x in cache",
  117. (host ? "host" : "net"), (unsigned)ad);
  118. #endif
  119. return xstrdup(pn->name);
  120. }
  121. pn = pn->next;
  122. }
  123. host_ad = ntohl(ad);
  124. name = NULL;
  125. if (host) {
  126. struct hostent *ent;
  127. #ifdef DEBUG
  128. bb_error_msg("gethostbyaddr (%08x)", (unsigned)ad);
  129. #endif
  130. ent = gethostbyaddr((char *) &ad, 4, AF_INET);
  131. if (ent)
  132. name = xstrdup(ent->h_name);
  133. } else if (ENABLE_FEATURE_ETC_NETWORKS) {
  134. struct netent *np;
  135. #ifdef DEBUG
  136. bb_error_msg("getnetbyaddr (%08x)", (unsigned)host_ad);
  137. #endif
  138. np = getnetbyaddr(host_ad, AF_INET);
  139. if (np)
  140. name = xstrdup(np->n_name);
  141. }
  142. if (!name)
  143. name = xstrdup(inet_ntoa(s_in->sin_addr));
  144. pn = xmalloc(sizeof(*pn) + strlen(name)); /* no '+ 1', it's already accounted for */
  145. pn->next = cache;
  146. pn->addr = *s_in;
  147. pn->host = host;
  148. strcpy(pn->name, name);
  149. cache = pn;
  150. return name;
  151. }
  152. #if ENABLE_FEATURE_IPV6
  153. int FAST_FUNC INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
  154. {
  155. struct addrinfo req, *ai;
  156. int s;
  157. memset(&req, '\0', sizeof req);
  158. req.ai_family = AF_INET6;
  159. s = getaddrinfo(name, NULL, &req, &ai);
  160. if (s) {
  161. bb_error_msg("getaddrinfo: %s: %d", name, s);
  162. return -1;
  163. }
  164. memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
  165. freeaddrinfo(ai);
  166. return 0;
  167. }
  168. #ifndef IN6_IS_ADDR_UNSPECIFIED
  169. # define IN6_IS_ADDR_UNSPECIFIED(a) \
  170. (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
  171. ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
  172. #endif
  173. char* FAST_FUNC INET6_rresolve(struct sockaddr_in6 *sin6, int numeric)
  174. {
  175. char name[128];
  176. int s;
  177. if (sin6->sin6_family != AF_INET6) {
  178. #ifdef DEBUG
  179. bb_error_msg("rresolve: unsupported address family %d!",
  180. sin6->sin6_family);
  181. #endif
  182. errno = EAFNOSUPPORT;
  183. return NULL;
  184. }
  185. if (numeric & 0x7FFF) {
  186. inet_ntop(AF_INET6, &sin6->sin6_addr, name, sizeof(name));
  187. return xstrdup(name);
  188. }
  189. if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
  190. if (numeric & 0x8000)
  191. return xstrdup(bb_str_default);
  192. return xstrdup("*");
  193. }
  194. s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6),
  195. name, sizeof(name), NULL, 0, 0);
  196. if (s) {
  197. bb_error_msg("getnameinfo failed");
  198. return NULL;
  199. }
  200. return xstrdup(name);
  201. }
  202. #endif /* CONFIG_FEATURE_IPV6 */