3
0

inet_common.c 4.9 KB

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