getservice.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #define MAXALIASES 35
  65. static FILE *servf = NULL;
  66. static struct servent serv;
  67. static char buf[BUFSIZ+1 + sizeof(char *)*MAXALIASES];
  68. static int serv_stayopen;
  69. void setservent(int f)
  70. {
  71. if (servf == NULL)
  72. servf = fopen(_PATH_SERVICES, "r" );
  73. else
  74. rewind(servf);
  75. serv_stayopen |= f;
  76. }
  77. void endservent(void)
  78. {
  79. if (servf) {
  80. fclose(servf);
  81. servf = NULL;
  82. }
  83. serv_stayopen = 0;
  84. }
  85. struct servent * getservent(void)
  86. {
  87. struct servent *result;
  88. getservent_r(&serv, buf, sizeof(buf), &result);
  89. return result;
  90. }
  91. struct servent *getservbyname(const char *name, const char *proto)
  92. {
  93. struct servent *result;
  94. getservbyname_r(name, proto, &serv, buf, sizeof(buf), &result);
  95. return result;
  96. }
  97. struct servent * getservbyport(int port, const char *proto)
  98. {
  99. struct servent *result;
  100. getservbyport_r(port, proto, &serv, buf, sizeof(buf), &result);
  101. return result;
  102. }
  103. int getservent_r(struct servent * result_buf,
  104. char * buf, size_t buflen,
  105. struct servent ** result)
  106. {
  107. char *p;
  108. register char *cp, **q;
  109. char **serv_aliases;
  110. char *line;
  111. *result=NULL;
  112. if (buflen < sizeof(*serv_aliases)*MAXALIASES) {
  113. errno=ERANGE;
  114. return errno;
  115. }
  116. serv_aliases=(char **)buf;
  117. buf+=sizeof(*serv_aliases)*MAXALIASES;
  118. buflen-=sizeof(*serv_aliases)*MAXALIASES;
  119. if (buflen < BUFSIZ+1) {
  120. errno=ERANGE;
  121. return errno;
  122. }
  123. line=buf;
  124. buf+=BUFSIZ+1;
  125. buflen-=BUFSIZ+1;
  126. if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL)
  127. return errno;
  128. again:
  129. if ((p = fgets(line, BUFSIZ, servf)) == NULL)
  130. return TRY_AGAIN;
  131. if (*p == '#')
  132. goto again;
  133. cp = strpbrk(p, "#\n");
  134. if (cp == NULL)
  135. goto again;
  136. *cp = '\0';
  137. result_buf->s_name = p;
  138. p = strpbrk(p, " \t");
  139. if (p == NULL)
  140. goto again;
  141. *p++ = '\0';
  142. while (*p == ' ' || *p == '\t')
  143. p++;
  144. cp = strpbrk(p, ",/");
  145. if (cp == NULL)
  146. goto again;
  147. *cp++ = '\0';
  148. result_buf->s_port = htons((u_short)atoi(p));
  149. result_buf->s_proto = cp;
  150. q = result_buf->s_aliases = serv_aliases;
  151. cp = strpbrk(cp, " \t");
  152. if (cp != NULL)
  153. *cp++ = '\0';
  154. while (cp && *cp) {
  155. if (*cp == ' ' || *cp == '\t') {
  156. cp++;
  157. continue;
  158. }
  159. if (q < &serv_aliases[MAXALIASES - 1])
  160. *q++ = cp;
  161. cp = strpbrk(cp, " \t");
  162. if (cp != NULL)
  163. *cp++ = '\0';
  164. }
  165. *q = NULL;
  166. *result=result_buf;
  167. return 0;
  168. }
  169. int getservbyname_r(const char *name, const char *proto,
  170. struct servent * result_buf,
  171. char * buf, size_t buflen,
  172. struct servent ** result)
  173. {
  174. register char **cp;
  175. int ret;
  176. setservent(serv_stayopen);
  177. while (!(ret=getservent_r(result_buf, buf, buflen, result))) {
  178. if (strcmp(name, result_buf->s_name) == 0)
  179. goto gotname;
  180. for (cp = result_buf->s_aliases; *cp; cp++)
  181. if (strcmp(name, *cp) == 0)
  182. goto gotname;
  183. continue;
  184. gotname:
  185. if (proto == 0 || strcmp(result_buf->s_proto, proto) == 0)
  186. break;
  187. }
  188. if (!serv_stayopen)
  189. endservent();
  190. return *result?0:ret;
  191. }
  192. int getservbyport_r(int port, const char *proto,
  193. struct servent * result_buf,
  194. char * buf, size_t buflen,
  195. struct servent ** result)
  196. {
  197. int ret;
  198. setservent(serv_stayopen);
  199. while (!(ret=getservent_r(result_buf, buf, buflen, result))) {
  200. if (result_buf->s_port != port)
  201. continue;
  202. if (proto == 0 || strcmp(result_buf->s_proto, proto) == 0)
  203. break;
  204. }
  205. if (!serv_stayopen)
  206. endservent();
  207. return *result?0:ret;
  208. }