getservice.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #include <sys/types.h>
  54. #include <sys/socket.h>
  55. #include <netdb.h>
  56. #include <stdio.h>
  57. #include <string.h>
  58. #include <stdlib.h>
  59. #include <netinet/in.h>
  60. #include <arpa/inet.h>
  61. #define MAXALIASES 35
  62. static FILE *servf = NULL;
  63. static char line[BUFSIZ+1];
  64. static struct servent serv;
  65. static char *serv_aliases[MAXALIASES];
  66. static int serv_stayopen;
  67. void setservent(int f)
  68. {
  69. if (servf == NULL)
  70. servf = fopen(_PATH_SERVICES, "r" );
  71. else
  72. rewind(servf);
  73. serv_stayopen |= f;
  74. }
  75. void endservent(void)
  76. {
  77. if (servf) {
  78. fclose(servf);
  79. servf = NULL;
  80. }
  81. serv_stayopen = 0;
  82. }
  83. struct servent * getservent(void)
  84. {
  85. char *p;
  86. register char *cp, **q;
  87. if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL)
  88. return (NULL);
  89. again:
  90. if ((p = fgets(line, BUFSIZ, servf)) == NULL)
  91. return (NULL);
  92. if (*p == '#')
  93. goto again;
  94. cp = strpbrk(p, "#\n");
  95. if (cp == NULL)
  96. goto again;
  97. *cp = '\0';
  98. serv.s_name = p;
  99. p = strpbrk(p, " \t");
  100. if (p == NULL)
  101. goto again;
  102. *p++ = '\0';
  103. while (*p == ' ' || *p == '\t')
  104. p++;
  105. cp = strpbrk(p, ",/");
  106. if (cp == NULL)
  107. goto again;
  108. *cp++ = '\0';
  109. serv.s_port = htons((u_short)atoi(p));
  110. serv.s_proto = cp;
  111. q = serv.s_aliases = serv_aliases;
  112. cp = strpbrk(cp, " \t");
  113. if (cp != NULL)
  114. *cp++ = '\0';
  115. while (cp && *cp) {
  116. if (*cp == ' ' || *cp == '\t') {
  117. cp++;
  118. continue;
  119. }
  120. if (q < &serv_aliases[MAXALIASES - 1])
  121. *q++ = cp;
  122. cp = strpbrk(cp, " \t");
  123. if (cp != NULL)
  124. *cp++ = '\0';
  125. }
  126. *q = NULL;
  127. return (&serv);
  128. }
  129. struct servent *getservbyname(const char *name, const char *proto)
  130. {
  131. register struct servent *p;
  132. register char **cp;
  133. setservent(serv_stayopen);
  134. while ((p = getservent()) != NULL) {
  135. if (strcmp(name, p->s_name) == 0)
  136. goto gotname;
  137. for (cp = p->s_aliases; *cp; cp++)
  138. if (strcmp(name, *cp) == 0)
  139. goto gotname;
  140. continue;
  141. gotname:
  142. if (proto == 0 || strcmp(p->s_proto, proto) == 0)
  143. break;
  144. }
  145. if (!serv_stayopen)
  146. endservent();
  147. return (p);
  148. }
  149. struct servent * getservbyport(int port, const char *proto)
  150. {
  151. register struct servent *p;
  152. setservent(serv_stayopen);
  153. while ((p = getservent()) != NULL) {
  154. if (p->s_port != port)
  155. continue;
  156. if (proto == 0 || strcmp(p->s_proto, proto) == 0)
  157. break;
  158. }
  159. if (!serv_stayopen)
  160. endservent();
  161. return (p);
  162. }