getnet.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (C) 2010 Bernhard Reutner-Fischer <uclibc@uclibc.org>
  3. *
  4. * Licensed under LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
  5. */
  6. /* /etc/networks
  7. # network-name number [aliases ...]
  8. loopback 127.0.0.0 # optional aliases
  9. network-name: symbolic name of the netwkork
  10. number: official number of the network in dotted quad
  11. aliases: case sensitive optional space or tab separated list of other names
  12. */
  13. #include <features.h>
  14. #include <netdb.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <netinet/in.h>
  18. #include <arpa/inet.h>
  19. #include <errno.h>
  20. #include <unistd.h>
  21. #include "internal/parse_config.h"
  22. #include <bits/uClibc_mutex.h>
  23. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  24. #define MINTOKENS 2
  25. #define MAXALIASES 8
  26. #define MAXTOKENS (MINTOKENS + MAXALIASES + 1)
  27. #define BUFSZ (255) /* one line */
  28. #define SBUFSIZE (BUFSZ + 1 + (sizeof(char *) * MAXTOKENS))
  29. static parser_t *netp = NULL;
  30. static struct netent nete;
  31. static char *netbuf = NULL;
  32. static smallint net_stayopen;
  33. void setnetent(int stayopen)
  34. {
  35. __UCLIBC_MUTEX_LOCK(mylock);
  36. if (netp)
  37. config_close(netp);
  38. netp = config_open(_PATH_NETWORKS);
  39. if (stayopen)
  40. net_stayopen = 1;
  41. __UCLIBC_MUTEX_UNLOCK(mylock);
  42. }
  43. libc_hidden_def(setnetent)
  44. void endnetent(void)
  45. {
  46. __UCLIBC_MUTEX_LOCK(mylock);
  47. if (netp) {
  48. config_close(netp);
  49. netp = NULL;
  50. }
  51. net_stayopen = 0;
  52. __UCLIBC_MUTEX_UNLOCK(mylock);
  53. }
  54. libc_hidden_def(endnetent)
  55. int getnetent_r(struct netent *result_buf,
  56. char *buf, size_t buflen, struct netent **result,
  57. int *h_errnop
  58. )
  59. {
  60. char **tok = NULL;
  61. const size_t aliaslen = sizeof(char *) * MAXTOKENS;
  62. int ret = ERANGE;
  63. (void)h_errnop;
  64. *result = NULL;
  65. if (buflen < aliaslen
  66. || (buflen - aliaslen) < BUFSZ + 1)
  67. goto DONE_NOUNLOCK;
  68. __UCLIBC_MUTEX_LOCK(mylock);
  69. ret = ENOENT;
  70. if (netp == NULL)
  71. setnetent(net_stayopen);
  72. if (netp == NULL)
  73. goto DONE;
  74. netp->data = buf;
  75. netp->data_len = aliaslen;
  76. netp->line_len = buflen - aliaslen;
  77. /* <name>[[:space:]]<netnumber>[[:space:]][<aliases>] */
  78. if (!config_read(netp, &tok, MAXTOKENS-1, MINTOKENS, "# \t/", PARSE_NORMAL)) {
  79. goto DONE;
  80. }
  81. result_buf->n_name = *(tok++);
  82. {
  83. struct addrinfo hints, *addri;
  84. # define sa4_to_uint32(sa) \
  85. (ntohl(((struct sockaddr_in*)sa)->sin_addr.s_addr))
  86. #ifdef __UCLIBC_HAS_IPV6__
  87. # define sa6_to_uint8(sa) \
  88. (ntohl(((struct sockaddr_in6*)sa)->sin6_addr.s6_addr))
  89. #endif
  90. memset(&hints, 0, sizeof(struct addrinfo));
  91. hints.ai_family = AF_UNSPEC;
  92. hints.ai_flags = AI_NUMERICHOST;
  93. getaddrinfo(*(tok++), NULL, &hints, &addri);
  94. result_buf->n_addrtype = addri->ai_family;
  95. result_buf->n_net =
  96. #if 0 /*FIXME: implement me! def __UCLIBC_HAS_IPV6__ */
  97. addri->ai_family == AF_INET6 ? sa6_to_uint8(addri->ai_addr) :
  98. #endif
  99. sa4_to_uint32(addri->ai_addr);
  100. freeaddrinfo(addri);
  101. }
  102. result_buf->n_aliases = tok;
  103. *result = result_buf;
  104. ret = 0;
  105. DONE:
  106. __UCLIBC_MUTEX_UNLOCK(mylock);
  107. DONE_NOUNLOCK:
  108. errno = ret;
  109. return errno;
  110. }
  111. libc_hidden_def(getnetent_r)
  112. static void __initbuf(void)
  113. {
  114. if (!netbuf) {
  115. netbuf = malloc(SBUFSIZE);
  116. if (!netbuf)
  117. abort();
  118. }
  119. }
  120. struct netent *getnetent(void)
  121. {
  122. struct netent *result;
  123. int herrnop;
  124. __initbuf();
  125. getnetent_r(&nete, netbuf, SBUFSIZE, &result, &herrnop);
  126. return result;
  127. }
  128. int getnetbyname_r(const char *name,
  129. struct netent *result_buf, char *buf, size_t buflen,
  130. struct netent **result,
  131. int *h_errnop
  132. )
  133. {
  134. register char **cp;
  135. int ret, herrnop;
  136. (void)h_errnop;
  137. __UCLIBC_MUTEX_LOCK(mylock);
  138. setnetent(net_stayopen);
  139. while (!(ret = getnetent_r(result_buf, buf, buflen, result, &herrnop))) {
  140. if (strcmp(name, result_buf->n_name) == 0)
  141. break;
  142. for (cp = result_buf->n_aliases; *cp; cp++)
  143. if (strcmp(name, *cp) == 0)
  144. goto gotname;
  145. }
  146. gotname:
  147. if (!net_stayopen)
  148. endnetent();
  149. __UCLIBC_MUTEX_UNLOCK(mylock);
  150. return *result ? 0 : ret;
  151. }
  152. libc_hidden_def(getnetbyname_r)
  153. struct netent *getnetbyname(const char *name)
  154. {
  155. struct netent *result;
  156. int herrnop;
  157. __initbuf();
  158. getnetbyname_r(name, &nete, netbuf, SBUFSIZE, &result, &herrnop);
  159. return result;
  160. }
  161. int getnetbyaddr_r(uint32_t net, int type,
  162. struct netent *result_buf, char *buf,
  163. size_t buflen, struct netent **result,
  164. int *h_errnop)
  165. {
  166. int ret, herrnop;
  167. (void)h_errnop;
  168. __UCLIBC_MUTEX_LOCK(mylock);
  169. setnetent(net_stayopen);
  170. while (!(ret = getnetent_r(result_buf, buf, buflen, result, &herrnop))) {
  171. if (net == result_buf->n_net && type == result_buf->n_addrtype)
  172. break;
  173. }
  174. if (!net_stayopen)
  175. endnetent();
  176. __UCLIBC_MUTEX_UNLOCK(mylock);
  177. return *result ? 0 : ret;
  178. }
  179. libc_hidden_def(getnetbyaddr_r)
  180. struct netent *getnetbyaddr(uint32_t net, int type)
  181. {
  182. struct netent *result;
  183. int herrnop;
  184. __initbuf();
  185. getnetbyaddr_r(net, type, &nete, netbuf, SBUFSIZE, &result, &herrnop);
  186. return result;
  187. }