getproto.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/protocols
  8. # protocol-name number [aliases ...]
  9. ip 0 IP # internet protocol, pseudo protocol number
  10. protocol-name: case sensitive friendly name of the IP protocol
  11. number: decimal protocol number
  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 /* will probably never be more than one */
  27. #define MAXTOKENS (MINTOKENS + MAXALIASES + 1)
  28. #define BUFSZ (255) /* one line */
  29. #define SBUFSIZE (BUFSZ + 1 + (sizeof(char *) * MAXTOKENS))
  30. static parser_t *protop = NULL;
  31. static struct protoent protoe;
  32. static char *protobuf = NULL;
  33. static smallint proto_stayopen;
  34. void setprotoent(int stayopen)
  35. {
  36. __UCLIBC_MUTEX_LOCK(mylock);
  37. if (protop)
  38. config_close(protop);
  39. protop = config_open(_PATH_PROTOCOLS);
  40. if (stayopen)
  41. proto_stayopen = 1;
  42. __UCLIBC_MUTEX_UNLOCK(mylock);
  43. }
  44. libc_hidden_def(setprotoent)
  45. void endprotoent(void)
  46. {
  47. __UCLIBC_MUTEX_LOCK(mylock);
  48. if (protop) {
  49. config_close(protop);
  50. protop = NULL;
  51. }
  52. proto_stayopen = 0;
  53. __UCLIBC_MUTEX_UNLOCK(mylock);
  54. }
  55. libc_hidden_def(endprotoent)
  56. int getprotoent_r(struct protoent *result_buf,
  57. char *buf, size_t buflen, struct protoent **result)
  58. {
  59. char **tok = NULL;
  60. const size_t aliaslen = sizeof(char *) * MAXTOKENS;
  61. int ret = ERANGE;
  62. *result = NULL;
  63. if (buflen < aliaslen
  64. || (buflen - aliaslen) < BUFSZ + 1)
  65. goto DONE_NOUNLOCK;
  66. __UCLIBC_MUTEX_LOCK(mylock);
  67. //tok = (char **) buf;
  68. ret = ENOENT;
  69. if (protop == NULL)
  70. setprotoent(proto_stayopen);
  71. if (protop == NULL)
  72. goto DONE;
  73. protop->data = buf;
  74. protop->data_len = aliaslen;
  75. protop->line_len = buflen - aliaslen;
  76. /* <name>[[:space:]]<protonumber>[[:space:]][<aliases>] */
  77. if (!config_read(protop, &tok, MAXTOKENS - 1, MINTOKENS, "# \t/", PARSE_NORMAL)) {
  78. goto DONE;
  79. }
  80. result_buf->p_name = *(tok++);
  81. result_buf->p_proto = atoi(*(tok++));
  82. result_buf->p_aliases = tok;
  83. *result = result_buf;
  84. ret = 0;
  85. DONE:
  86. __UCLIBC_MUTEX_UNLOCK(mylock);
  87. DONE_NOUNLOCK:
  88. errno = ret;
  89. return errno;
  90. }
  91. libc_hidden_def(getprotoent_r)
  92. static void __initbuf(void)
  93. {
  94. if (!protobuf) {
  95. protobuf = malloc(SBUFSIZE);
  96. if (!protobuf)
  97. abort();
  98. }
  99. }
  100. struct protoent *getprotoent(void)
  101. {
  102. struct protoent *result;
  103. __initbuf();
  104. getprotoent_r(&protoe, protobuf, SBUFSIZE, &result);
  105. return result;
  106. }
  107. int getprotobyname_r(const char *name,
  108. struct protoent *result_buf, char *buf, size_t buflen,
  109. struct protoent **result)
  110. {
  111. register char **cp;
  112. int ret;
  113. __UCLIBC_MUTEX_LOCK(mylock);
  114. setprotoent(proto_stayopen);
  115. while (!(ret = getprotoent_r(result_buf, buf, buflen, result))) {
  116. if (strcmp(name, result_buf->p_name) == 0)
  117. break;
  118. for (cp = result_buf->p_aliases; *cp; cp++)
  119. if (strcmp(name, *cp) == 0)
  120. goto gotname;
  121. }
  122. gotname:
  123. if (!proto_stayopen)
  124. endprotoent();
  125. __UCLIBC_MUTEX_UNLOCK(mylock);
  126. return *result ? 0 : ret;
  127. }
  128. libc_hidden_def(getprotobyname_r)
  129. struct protoent *getprotobyname(const char *name)
  130. {
  131. struct protoent *result;
  132. __initbuf();
  133. getprotobyname_r(name, &protoe, protobuf, SBUFSIZE, &result);
  134. return result;
  135. }
  136. int getprotobynumber_r(int proto,
  137. struct protoent *result_buf, char *buf,
  138. size_t buflen, struct protoent **result)
  139. {
  140. int ret;
  141. __UCLIBC_MUTEX_LOCK(mylock);
  142. setprotoent(proto_stayopen);
  143. while (!(ret = getprotoent_r(result_buf, buf, buflen, result))) {
  144. if (proto == result_buf->p_proto)
  145. break;
  146. }
  147. if (!proto_stayopen)
  148. endprotoent();
  149. __UCLIBC_MUTEX_UNLOCK(mylock);
  150. return *result ? 0 : ret;
  151. }
  152. libc_hidden_def(getprotobynumber_r)
  153. struct protoent *getprotobynumber(int proto)
  154. {
  155. struct protoent *result;
  156. __initbuf();
  157. getprotobynumber_r(proto, &protoe, protobuf, SBUFSIZE, &result);
  158. return result;
  159. }