inet_common.c 5.4 KB

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