inet_common.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. * Version: $Id: inet_common.c,v 1.8 2004/03/10 07:42:38 mjn3 Exp $
  9. *
  10. */
  11. #include "libbb.h"
  12. #include "inet_common.h"
  13. int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
  14. {
  15. struct hostent *hp;
  16. struct netent *np;
  17. /* Grmpf. -FvK */
  18. s_in->sin_family = AF_INET;
  19. s_in->sin_port = 0;
  20. /* Default is special, meaning 0.0.0.0. */
  21. if (!strcmp(name, bb_str_default)) {
  22. s_in->sin_addr.s_addr = INADDR_ANY;
  23. return 1;
  24. }
  25. /* Look to see if it's a dotted quad. */
  26. if (inet_aton(name, &s_in->sin_addr)) {
  27. return 0;
  28. }
  29. /* If we expect this to be a hostname, try hostname database first */
  30. #ifdef DEBUG
  31. if (hostfirst) {
  32. bb_error_msg("gethostbyname (%s)", name);
  33. }
  34. #endif
  35. if (hostfirst && (hp = gethostbyname(name)) != (struct hostent *) NULL) {
  36. memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
  37. sizeof(struct in_addr));
  38. return 0;
  39. }
  40. /* Try the NETWORKS database to see if this is a known network. */
  41. #ifdef DEBUG
  42. bb_error_msg("getnetbyname (%s)", name);
  43. #endif
  44. if ((np = getnetbyname(name)) != (struct netent *) NULL) {
  45. s_in->sin_addr.s_addr = htonl(np->n_net);
  46. return 1;
  47. }
  48. if (hostfirst) {
  49. /* Don't try again */
  50. return -1;
  51. }
  52. #ifdef DEBUG
  53. res_init();
  54. _res.options |= RES_DEBUG;
  55. #endif
  56. #ifdef DEBUG
  57. bb_error_msg("gethostbyname (%s)", name);
  58. #endif
  59. if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
  60. return -1;
  61. }
  62. memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
  63. sizeof(struct in_addr));
  64. return 0;
  65. }
  66. /* cache */
  67. struct addr {
  68. struct sockaddr_in addr;
  69. char *name;
  70. int host;
  71. struct addr *next;
  72. };
  73. static struct addr *INET_nn = NULL; /* addr-to-name cache */
  74. /* numeric: & 0x8000: default instead of *,
  75. * & 0x4000: host instead of net,
  76. * & 0x0fff: don't resolve
  77. */
  78. int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
  79. int numeric, unsigned int netmask)
  80. {
  81. struct hostent *ent;
  82. struct netent *np;
  83. struct addr *pn;
  84. unsigned long ad, host_ad;
  85. int host = 0;
  86. /* Grmpf. -FvK */
  87. if (s_in->sin_family != AF_INET) {
  88. #ifdef DEBUG
  89. bb_error_msg("rresolve: unsupport address family %d !",
  90. s_in->sin_family);
  91. #endif
  92. errno = EAFNOSUPPORT;
  93. return -1;
  94. }
  95. ad = (unsigned long) s_in->sin_addr.s_addr;
  96. #ifdef DEBUG
  97. bb_error_msg("rresolve: %08lx, mask %08x, num %08x", ad, netmask, numeric);
  98. #endif
  99. if (ad == INADDR_ANY) {
  100. if ((numeric & 0x0FFF) == 0) {
  101. if (numeric & 0x8000)
  102. safe_strncpy(name, bb_str_default, len);
  103. else
  104. safe_strncpy(name, "*", len);
  105. return 0;
  106. }
  107. }
  108. if (numeric & 0x0FFF) {
  109. safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
  110. return 0;
  111. }
  112. if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
  113. host = 1;
  114. pn = INET_nn;
  115. while (pn != NULL) {
  116. if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
  117. safe_strncpy(name, pn->name, len);
  118. #ifdef DEBUG
  119. bb_error_msg("rresolve: found %s %08lx in cache",
  120. (host ? "host" : "net"), ad);
  121. #endif
  122. return 0;
  123. }
  124. pn = pn->next;
  125. }
  126. host_ad = ntohl(ad);
  127. np = NULL;
  128. ent = NULL;
  129. if (host) {
  130. #ifdef DEBUG
  131. bb_error_msg("gethostbyaddr (%08lx)", ad);
  132. #endif
  133. ent = gethostbyaddr((char *) &ad, 4, AF_INET);
  134. if (ent != NULL) {
  135. safe_strncpy(name, ent->h_name, len);
  136. }
  137. } else {
  138. #ifdef DEBUG
  139. bb_error_msg("getnetbyaddr (%08lx)", host_ad);
  140. #endif
  141. np = getnetbyaddr(host_ad, AF_INET);
  142. if (np != NULL) {
  143. safe_strncpy(name, np->n_name, len);
  144. }
  145. }
  146. if ((ent == NULL) && (np == NULL)) {
  147. safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
  148. }
  149. pn = (struct addr *) xmalloc(sizeof(struct addr));
  150. pn->addr = *s_in;
  151. pn->next = INET_nn;
  152. pn->host = host;
  153. pn->name = xstrdup(name);
  154. INET_nn = pn;
  155. return 0;
  156. }
  157. #ifdef CONFIG_FEATURE_IPV6
  158. int INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
  159. {
  160. struct addrinfo req, *ai;
  161. int s;
  162. memset(&req, '\0', sizeof req);
  163. req.ai_family = AF_INET6;
  164. s = getaddrinfo(name, NULL, &req, &ai);
  165. if (s) {
  166. bb_error_msg("getaddrinfo: %s: %d", name, s);
  167. return -1;
  168. }
  169. memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
  170. freeaddrinfo(ai);
  171. return 0;
  172. }
  173. #ifndef IN6_IS_ADDR_UNSPECIFIED
  174. # define IN6_IS_ADDR_UNSPECIFIED(a) \
  175. (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
  176. ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
  177. #endif
  178. int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
  179. int numeric)
  180. {
  181. int s;
  182. /* Grmpf. -FvK */
  183. if (sin6->sin6_family != AF_INET6) {
  184. #ifdef DEBUG
  185. bb_error_msg("rresolve: unsupport address family %d!",
  186. sin6->sin6_family);
  187. #endif
  188. errno = EAFNOSUPPORT;
  189. return -1;
  190. }
  191. if (numeric & 0x7FFF) {
  192. inet_ntop(AF_INET6, &sin6->sin6_addr, name, len);
  193. return 0;
  194. }
  195. if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
  196. if (numeric & 0x8000) {
  197. strcpy(name, bb_str_default);
  198. } else {
  199. name[0] = '*';
  200. name[1] = '\0';
  201. }
  202. return 0;
  203. }
  204. s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), name, len, NULL, 0, 0);
  205. if (s) {
  206. bb_error_msg("getnameinfo failed");
  207. return -1;
  208. }
  209. return 0;
  210. }
  211. #endif /* CONFIG_FEATURE_IPV6 */