getproto.c 6.5 KB

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