getservice.c 7.4 KB

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