getproto.c 7.1 KB

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