getproto.c 7.1 KB

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