getnet.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2010 Bernhard Reutner-Fischer <uclibc@uclibc.org>
  4. *
  5. * Licensed under LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
  6. */
  7. /* /etc/networks
  8. # network-name number [aliases ...]
  9. loopback 127.0.0.0 # optional aliases
  10. network-name: symbolic name of the netwkork
  11. number: official number of the network in dotted quad
  12. aliases: case sensitive optional space or tab separated list of other names
  13. */
  14. #include <features.h>
  15. #include <netdb.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <netinet/in.h>
  19. #include <arpa/inet.h>
  20. #include <errno.h>
  21. #include <unistd.h>
  22. #include "internal/parse_config.h"
  23. #include <bits/uClibc_mutex.h>
  24. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  25. #define MINTOKENS 2
  26. #define MAXALIASES 8
  27. #define MAXTOKENS (MINTOKENS + MAXALIASES + 1)
  28. #define BUFSZ (255) /* one line */
  29. #define SBUFSIZE (BUFSZ + 1 + (sizeof(char *) * MAXTOKENS))
  30. static parser_t *netp = NULL;
  31. static struct netent nete;
  32. static char *netbuf = NULL;
  33. static smallint net_stayopen;
  34. void setnetent(int stayopen)
  35. {
  36. __UCLIBC_MUTEX_LOCK(mylock);
  37. if (netp)
  38. config_close(netp);
  39. netp = config_open(_PATH_NETWORKS);
  40. if (stayopen)
  41. net_stayopen = 1;
  42. __UCLIBC_MUTEX_UNLOCK(mylock);
  43. }
  44. libc_hidden_def(setnetent)
  45. void endnetent(void)
  46. {
  47. __UCLIBC_MUTEX_LOCK(mylock);
  48. if (netp) {
  49. config_close(netp);
  50. netp = NULL;
  51. }
  52. net_stayopen = 0;
  53. __UCLIBC_MUTEX_UNLOCK(mylock);
  54. }
  55. libc_hidden_def(endnetent)
  56. int getnetent_r(struct netent *result_buf,
  57. char *buf, size_t buflen, struct netent **result,
  58. int *h_errnop
  59. )
  60. {
  61. char **tok = NULL;
  62. const size_t aliaslen = sizeof(char *) * MAXTOKENS;
  63. int ret = ERANGE;
  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. __UCLIBC_MUTEX_LOCK(mylock);
  137. setnetent(net_stayopen);
  138. while (!(ret = getnetent_r(result_buf, buf, buflen, result, &herrnop))) {
  139. if (strcmp(name, result_buf->n_name) == 0)
  140. break;
  141. for (cp = result_buf->n_aliases; *cp; cp++)
  142. if (strcmp(name, *cp) == 0)
  143. goto gotname;
  144. }
  145. gotname:
  146. if (!net_stayopen)
  147. endnetent();
  148. __UCLIBC_MUTEX_UNLOCK(mylock);
  149. return *result ? 0 : ret;
  150. }
  151. libc_hidden_def(getnetbyname_r)
  152. struct netent *getnetbyname(const char *name)
  153. {
  154. struct netent *result;
  155. int herrnop;
  156. __initbuf();
  157. getnetbyname_r(name, &nete, netbuf, SBUFSIZE, &result, &herrnop);
  158. return result;
  159. }
  160. int getnetbyaddr_r(uint32_t net, int type,
  161. struct netent *result_buf, char *buf,
  162. size_t buflen, struct netent **result,
  163. int *h_errnop)
  164. {
  165. int ret, herrnop;
  166. __UCLIBC_MUTEX_LOCK(mylock);
  167. setnetent(net_stayopen);
  168. while (!(ret = getnetent_r(result_buf, buf, buflen, result, &herrnop))) {
  169. if (net == result_buf->n_net && type == result_buf->n_addrtype)
  170. break;
  171. }
  172. if (!net_stayopen)
  173. endnetent();
  174. __UCLIBC_MUTEX_UNLOCK(mylock);
  175. return *result ? 0 : ret;
  176. }
  177. libc_hidden_def(getnetbyaddr_r)
  178. struct netent *getnetbyaddr(uint32_t net, int type)
  179. {
  180. struct netent *result;
  181. int herrnop;
  182. __initbuf();
  183. getnetbyaddr_r(net, type, &nete, netbuf, SBUFSIZE, &result, &herrnop);
  184. return result;
  185. }