getservice.c 6.9 KB

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