getproto.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. ** protocols.c /etc/protocols access functions
  3. **
  4. ** This file is part of the NYS Library.
  5. **
  6. ** The NYS Library is free software; you can redistribute it and/or
  7. ** modify it under the terms of the GNU Library General Public License as
  8. ** published by the Free Software Foundation; either version 2 of the
  9. ** License, or (at your option) any later version.
  10. **
  11. ** The NYS Library is distributed in the hope that it will be useful,
  12. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ** Library General Public License for more details.
  15. **
  16. ** You should have received a copy of the GNU Library General Public
  17. ** License along with the NYS Library; see the file COPYING.LIB. If
  18. ** not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  19. ** Cambridge, MA 02139, USA.
  20. **
  21. **
  22. ** Copyright (c) 1983 Regents of the University of California.
  23. ** All rights reserved.
  24. **
  25. ** Redistribution and use in source and binary forms, with or without
  26. ** modification, are permitted provided that the following conditions
  27. ** are met:
  28. ** 1. Redistributions of source code must retain the above copyright
  29. ** notice, this list of conditions and the following disclaimer.
  30. ** 2. Redistributions in binary form must reproduce the above copyright
  31. ** notice, this list of conditions and the following disclaimer in the
  32. ** documentation and/or other materials provided with the distribution.
  33. ** 3. All advertising materials mentioning features or use of this software
  34. ** must display the following acknowledgement:
  35. ** This product includes software developed by the University of
  36. ** California, Berkeley and its contributors.
  37. ** 4. Neither the name of the University nor the names of its contributors
  38. ** may be used to endorse or promote products derived from this software
  39. ** without specific prior written permission.
  40. **
  41. ** THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  42. ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. ** ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  45. ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. ** SUCH DAMAGE.
  52. */
  53. #define __FORCE_GLIBC
  54. #include <features.h>
  55. #include <sys/types.h>
  56. #include <sys/socket.h>
  57. #include <netdb.h>
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include <errno.h>
  62. #include <unistd.h>
  63. #include <bits/uClibc_mutex.h>
  64. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  65. #define MAXALIASES 35
  66. #define SBUFSIZE (BUFSIZ + 1 + (sizeof(char *) * MAXALIASES))
  67. static FILE *protof = NULL;
  68. static struct protoent proto;
  69. static char *static_aliases = NULL;
  70. static smallint proto_stayopen;
  71. static void __initbuf(void)
  72. {
  73. if (!static_aliases) {
  74. static_aliases = malloc(SBUFSIZE);
  75. if (!static_aliases)
  76. abort();
  77. }
  78. }
  79. void setprotoent(int f)
  80. {
  81. __UCLIBC_MUTEX_LOCK(mylock);
  82. if (protof == NULL)
  83. protof = fopen(_PATH_PROTOCOLS, "r" );
  84. else
  85. rewind(protof);
  86. if (f) proto_stayopen = 1;
  87. __UCLIBC_MUTEX_UNLOCK(mylock);
  88. }
  89. libc_hidden_def(setprotoent)
  90. void endprotoent(void)
  91. {
  92. __UCLIBC_MUTEX_LOCK(mylock);
  93. if (protof) {
  94. fclose(protof);
  95. protof = NULL;
  96. }
  97. proto_stayopen = 0;
  98. __UCLIBC_MUTEX_UNLOCK(mylock);
  99. }
  100. libc_hidden_def(endprotoent)
  101. int getprotoent_r(struct protoent *result_buf,
  102. char *buf, size_t buflen,
  103. struct protoent **result)
  104. {
  105. char *p;
  106. register char *cp, **q;
  107. char **proto_aliases;
  108. char *line;
  109. int rv;
  110. *result = NULL;
  111. if (buflen < sizeof(*proto_aliases)*MAXALIASES) {
  112. errno=ERANGE;
  113. return errno;
  114. }
  115. __UCLIBC_MUTEX_LOCK(mylock);
  116. proto_aliases=(char **)buf;
  117. buf+=sizeof(*proto_aliases)*MAXALIASES;
  118. buflen-=sizeof(*proto_aliases)*MAXALIASES;
  119. if (buflen < BUFSIZ+1) {
  120. errno=rv=ERANGE;
  121. goto DONE;
  122. }
  123. line=buf;
  124. buf+=BUFSIZ+1;
  125. buflen-=BUFSIZ+1;
  126. if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL) {
  127. rv=errno;
  128. goto DONE;
  129. }
  130. again:
  131. if ((p = fgets(line, BUFSIZ, protof)) == NULL) {
  132. rv=ENOENT;
  133. goto DONE;
  134. }
  135. if (*p == '#')
  136. goto again;
  137. cp = strpbrk(p, "#\n");
  138. if (cp == NULL)
  139. goto again;
  140. *cp = '\0';
  141. result_buf->p_name = p;
  142. cp = strpbrk(p, " \t");
  143. if (cp == NULL)
  144. goto again;
  145. *cp++ = '\0';
  146. while (*cp == ' ' || *cp == '\t')
  147. cp++;
  148. p = strpbrk(cp, " \t");
  149. if (p != NULL)
  150. *p++ = '\0';
  151. result_buf->p_proto = atoi(cp);
  152. q = result_buf->p_aliases = proto_aliases;
  153. if (p != NULL) {
  154. cp = p;
  155. while (cp && *cp) {
  156. if (*cp == ' ' || *cp == '\t') {
  157. cp++;
  158. continue;
  159. }
  160. if (q < &proto_aliases[MAXALIASES - 1])
  161. *q++ = cp;
  162. cp = strpbrk(cp, " \t");
  163. if (cp != NULL)
  164. *cp++ = '\0';
  165. }
  166. }
  167. *q = NULL;
  168. *result=result_buf;
  169. rv = 0;
  170. DONE:
  171. __UCLIBC_MUTEX_UNLOCK(mylock);
  172. return rv;
  173. }
  174. libc_hidden_def(getprotoent_r)
  175. struct protoent * getprotoent(void)
  176. {
  177. struct protoent *result;
  178. __initbuf();
  179. getprotoent_r(&proto, static_aliases, SBUFSIZE, &result);
  180. return result;
  181. }
  182. int getprotobyname_r(const char *name,
  183. struct protoent *result_buf,
  184. char *buf, size_t buflen,
  185. struct protoent **result)
  186. {
  187. register char **cp;
  188. int ret;
  189. __UCLIBC_MUTEX_LOCK(mylock);
  190. setprotoent(proto_stayopen);
  191. while (!(ret=getprotoent_r(result_buf, buf, buflen, result))) {
  192. if (strcmp(result_buf->p_name, name) == 0)
  193. break;
  194. for (cp = result_buf->p_aliases; *cp != 0; cp++)
  195. if (strcmp(*cp, name) == 0)
  196. goto found;
  197. }
  198. found:
  199. if (!proto_stayopen)
  200. endprotoent();
  201. __UCLIBC_MUTEX_UNLOCK(mylock);
  202. return *result?0:ret;
  203. }
  204. libc_hidden_def(getprotobyname_r)
  205. struct protoent * getprotobyname(const char *name)
  206. {
  207. struct protoent *result;
  208. __initbuf();
  209. getprotobyname_r(name, &proto, static_aliases, SBUFSIZE, &result);
  210. return result;
  211. }
  212. int getprotobynumber_r (int proto_num,
  213. struct protoent *result_buf,
  214. char *buf, size_t buflen,
  215. struct protoent **result)
  216. {
  217. int ret;
  218. __UCLIBC_MUTEX_LOCK(mylock);
  219. setprotoent(proto_stayopen);
  220. while (!(ret=getprotoent_r(result_buf, buf, buflen, result)))
  221. if (result_buf->p_proto == proto_num)
  222. break;
  223. if (!proto_stayopen)
  224. endprotoent();
  225. __UCLIBC_MUTEX_UNLOCK(mylock);
  226. return *result?0:ret;
  227. }
  228. libc_hidden_def(getprotobynumber_r)
  229. struct protoent * getprotobynumber(int proto_num)
  230. {
  231. struct protoent *result;
  232. __initbuf();
  233. getprotobynumber_r(proto_num, &proto, static_aliases,
  234. SBUFSIZE, &result);
  235. return result;
  236. }