getproto.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 strpbrk __strpbrk
  54. #define atoi __atoi
  55. #define rewind __rewind
  56. #define __FORCE_GLIBC
  57. #define _GNU_SOURCE
  58. #include <features.h>
  59. #include <sys/types.h>
  60. #include <sys/socket.h>
  61. #include <netdb.h>
  62. #include <stdio.h>
  63. #include <stdlib.h>
  64. #include <string.h>
  65. #include <errno.h>
  66. #ifdef __UCLIBC_HAS_THREADS__
  67. # include <pthread.h>
  68. static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  69. #endif
  70. #define LOCK __pthread_mutex_lock(&mylock)
  71. #define UNLOCK __pthread_mutex_unlock(&mylock)
  72. #define MAXALIASES 35
  73. #define SBUFSIZE (BUFSIZ + 1 + (sizeof(char *) * MAXALIASES))
  74. static FILE *protof = NULL;
  75. static struct protoent proto;
  76. static char *static_aliases = NULL;
  77. static int proto_stayopen;
  78. static void __initbuf(void)
  79. {
  80. if (!static_aliases) {
  81. static_aliases = malloc(SBUFSIZE);
  82. if (!static_aliases)
  83. abort();
  84. }
  85. }
  86. void attribute_hidden __setprotoent(int f)
  87. {
  88. LOCK;
  89. if (protof == NULL)
  90. protof = fopen(_PATH_PROTOCOLS, "r" );
  91. else
  92. rewind(protof);
  93. proto_stayopen |= f;
  94. UNLOCK;
  95. }
  96. strong_alias(__setprotoent,setprotoent)
  97. void attribute_hidden __endprotoent(void)
  98. {
  99. LOCK;
  100. if (protof) {
  101. fclose(protof);
  102. protof = NULL;
  103. }
  104. proto_stayopen = 0;
  105. UNLOCK;
  106. }
  107. strong_alias(__endprotoent,endprotoent)
  108. int attribute_hidden __getprotoent_r(struct protoent *result_buf,
  109. char *buf, size_t buflen,
  110. struct protoent **result)
  111. {
  112. char *p;
  113. register char *cp, **q;
  114. char **proto_aliases;
  115. char *line;
  116. *result = NULL;
  117. if (buflen < sizeof(*proto_aliases)*MAXALIASES) {
  118. errno=ERANGE;
  119. return errno;
  120. }
  121. LOCK;
  122. proto_aliases=(char **)buf;
  123. buf+=sizeof(*proto_aliases)*MAXALIASES;
  124. buflen-=sizeof(*proto_aliases)*MAXALIASES;
  125. if (buflen < BUFSIZ+1) {
  126. UNLOCK;
  127. errno=ERANGE;
  128. return errno;
  129. }
  130. line=buf;
  131. buf+=BUFSIZ+1;
  132. buflen-=BUFSIZ+1;
  133. if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL) {
  134. UNLOCK;
  135. return errno;
  136. }
  137. again:
  138. if ((p = fgets(line, BUFSIZ, protof)) == NULL) {
  139. UNLOCK;
  140. return TRY_AGAIN;
  141. }
  142. if (*p == '#')
  143. goto again;
  144. cp = strpbrk(p, "#\n");
  145. if (cp == NULL)
  146. goto again;
  147. *cp = '\0';
  148. result_buf->p_name = p;
  149. cp = strpbrk(p, " \t");
  150. if (cp == NULL)
  151. goto again;
  152. *cp++ = '\0';
  153. while (*cp == ' ' || *cp == '\t')
  154. cp++;
  155. p = strpbrk(cp, " \t");
  156. if (p != NULL)
  157. *p++ = '\0';
  158. result_buf->p_proto = atoi(cp);
  159. q = result_buf->p_aliases = proto_aliases;
  160. if (p != NULL) {
  161. cp = p;
  162. while (cp && *cp) {
  163. if (*cp == ' ' || *cp == '\t') {
  164. cp++;
  165. continue;
  166. }
  167. if (q < &proto_aliases[MAXALIASES - 1])
  168. *q++ = cp;
  169. cp = strpbrk(cp, " \t");
  170. if (cp != NULL)
  171. *cp++ = '\0';
  172. }
  173. }
  174. *q = NULL;
  175. *result=result_buf;
  176. UNLOCK;
  177. return 0;
  178. }
  179. strong_alias(__getprotoent_r,getprotoent_r)
  180. struct protoent * getprotoent(void)
  181. {
  182. struct protoent *result;
  183. __initbuf();
  184. __getprotoent_r(&proto, static_aliases, SBUFSIZE, &result);
  185. return result;
  186. }
  187. int attribute_hidden __getprotobyname_r(const char *name,
  188. struct protoent *result_buf,
  189. char *buf, size_t buflen,
  190. struct protoent **result)
  191. {
  192. register char **cp;
  193. int ret;
  194. LOCK;
  195. __setprotoent(proto_stayopen);
  196. while (!(ret=__getprotoent_r(result_buf, buf, buflen, result))) {
  197. if (__strcmp(result_buf->p_name, name) == 0)
  198. break;
  199. for (cp = result_buf->p_aliases; *cp != 0; cp++)
  200. if (__strcmp(*cp, name) == 0)
  201. goto found;
  202. }
  203. found:
  204. if (!proto_stayopen)
  205. __endprotoent();
  206. UNLOCK;
  207. return *result?0:ret;
  208. }
  209. strong_alias(__getprotobyname_r,getprotobyname_r)
  210. struct protoent * getprotobyname(const char *name)
  211. {
  212. struct protoent *result;
  213. __initbuf();
  214. __getprotobyname_r(name, &proto, static_aliases, SBUFSIZE, &result);
  215. return result;
  216. }
  217. int attribute_hidden __getprotobynumber_r (int proto_num,
  218. struct protoent *result_buf,
  219. char *buf, size_t buflen,
  220. struct protoent **result)
  221. {
  222. int ret;
  223. LOCK;
  224. __setprotoent(proto_stayopen);
  225. while (!(ret=__getprotoent_r(result_buf, buf, buflen, result)))
  226. if (result_buf->p_proto == proto_num)
  227. break;
  228. if (!proto_stayopen)
  229. __endprotoent();
  230. UNLOCK;
  231. return *result?0:ret;
  232. }
  233. strong_alias(__getprotobynumber_r,getprotobynumber_r)
  234. struct protoent * getprotobynumber(int proto_num)
  235. {
  236. struct protoent *result;
  237. __initbuf();
  238. __getprotobynumber_r(proto_num, &proto, static_aliases,
  239. SBUFSIZE, &result);
  240. return result;
  241. }