home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume21 / coda / part01 / libbsd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-08  |  4.2 KB  |  213 lines

  1. /*
  2. **  Copyright 1989 BBN Systems and Technologies Corporation.
  3. **  All Rights Reserved.
  4. **  This is free software, and may be distributed under the terms of the
  5. **  GNU Public License; see the file COPYING for more details.
  6. **
  7. **  Client library routines for BSD-derived systems.
  8. */
  9. #include "client.h"
  10. #include <sys/types.h>
  11. #include <sgtty.h>
  12. #include <netdb.h>
  13. #include <sys/socket.h>
  14. #include <netinet/in.h>
  15. #ifdef    RCSID
  16. static char     RCS[] =
  17.     "$Header: libbsd.c,v 2.0 90/03/23 14:41:41 rsalz Exp $";
  18. #endif    /* RCSID */
  19.  
  20.  
  21. STATIC FILE    *WriteStream;        /* Something to write to    */
  22. STATIC FILE    *ReadStream;        /* Something to read from    */
  23.  
  24.  
  25. /*
  26. **  Do the grundge work of getting us a socket.  Thanks to the NNTP folks
  27. **  for the trick of using h_addr to see if we're running with a 4.3
  28. **  multiple address netdb, or a 4.2 single address netdb.
  29. */
  30. STATIC int
  31. GetSocket(machine, port)
  32.     char         *machine;
  33.     int              port;
  34. {
  35.     struct hostent     *gethostbyname();
  36. #ifdef    h_addr
  37.     REGISTER char    **addr;
  38. #endif    /* h_addr */
  39.     REGISTER int      s;
  40.     struct hostent     *hp;
  41.     struct sockaddr_in      sin;
  42.  
  43.     /* Get the address of the server. */
  44.     if ((hp = gethostbyname(machine)) == NULL) {
  45.     (void)fprintf(stderr, "%s: Unknown host.\n", machine);
  46.     return -1;
  47.     }
  48.  
  49.     /* Set up the socket. */
  50.     bzero((char *)&sin, sizeof sin);
  51.     sin.sin_family = AF_INET;
  52.     sin.sin_port = htons((unsigned short)port);
  53.  
  54. #ifdef    h_addr
  55.     /* get a socket and initiate connection -- use multiple addresses */
  56.     for (addr = hp->h_addr_list; addr && *addr; addr++) {
  57.     if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
  58.         perror("socket");
  59.         return -1;
  60.     }
  61.         
  62.     bcopy(*addr, (char *)&sin.sin_addr, hp->h_length);
  63.     if (connect(s, (struct sockaddr *)&sin, sizeof sin) == 0)
  64.         break;
  65.     (void)close(s);
  66.     }
  67.     if (addr == NULL || *addr == '\0') {
  68.     perror("no connection possible");
  69.     return -1;
  70.     }
  71. #else
  72.     if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  73.     perror("socket");
  74.     return -1;
  75.     }
  76.  
  77.     bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
  78.     if (connect(s, (struct sockaddr *)&sin, sizeof sin) < 0) {
  79.     perror("connect");
  80.     (void)close(s);
  81.     return -1;
  82.     }
  83. #endif    /* h_addr */
  84.  
  85.     return s;
  86. }
  87.  
  88.  
  89. /*
  90. **  Open connection to server, return FALSE on error.
  91. */
  92. int
  93. SRVopen(machine, port)
  94.     char    *machine;
  95.     int         port;
  96. {
  97.     int         i;
  98.  
  99.     if ((i = GetSocket(machine, port)) < 0)
  100.     return FALSE;
  101.  
  102.     if ((ReadStream = fdopen(i, "r")) == NULL) {
  103.     perror("OpenServerChannel: fdopen #1");
  104.     return FALSE;
  105.     }
  106.  
  107.     if ((i = dup(i)) < 0) {
  108.     perror("OpenServerChannel: dup");
  109.     return FALSE;
  110.     }
  111.     if ((WriteStream = fdopen(i, "w")) == NULL) {
  112.     perror("OpenServerChannel: fdopen #2");
  113.     (void)fclose(ReadStream);
  114.     return FALSE;
  115.     }
  116.     return TRUE;
  117. }
  118.  
  119.  
  120. /*
  121. **  Send a QUIT and shut down.
  122. */
  123. void
  124. SRVclose()
  125. {
  126.     if (WriteStream == NULL || ReadStream == NULL)
  127.     return;
  128.  
  129.     SRVput("QUIT");
  130.     (void)fclose(WriteStream);
  131.     (void)fclose(ReadStream);
  132. }
  133.  
  134.  
  135. /*
  136. **  Send a line to the server.
  137. */
  138. void
  139. SRVput(p)
  140.     char    *p;
  141. {
  142.     if (SRVtrace)
  143.     (void)printf(">>>%s\n", p);
  144.     (void)fprintf(WriteStream, "%s\r\n", p);
  145.     (void)fflush(WriteStream);
  146. }
  147.  
  148.  
  149. /*
  150. **  Get a line of text from the server.  Strip end-of-line characters.
  151. */
  152. int
  153. SRVget(buff, size)
  154.     char        *buff;
  155.     int             size;
  156. {
  157.     REGISTER char    *p;
  158.  
  159.     while (fgets(buff, size, ReadStream)) {
  160.     if ((p = strchr(buff, '\r')) || (p = strchr(buff, '\n')))
  161.         *p = '\0';
  162.     if (SRVtrace)
  163.         (void)printf("<<<%s\n", buff);
  164.     if (strncmp(buff, "INF ", 4))
  165.         return TRUE;
  166.     (void)printf("Server message:\n\t%s\n", &buff[4]);
  167.     (void)fflush(stdout);
  168.     }
  169.     return FALSE;
  170. }
  171.  
  172.  
  173. /*
  174. **  Get a character from the server.
  175. */
  176. int
  177. SRVcget()
  178. {
  179.     return getc(ReadStream);
  180. }
  181.  
  182.  
  183. /*
  184. **  Get a password, without echoing it.
  185. */
  186. void
  187. GetPassword(buff, size)
  188.     char        *buff;
  189.     int             size;
  190. {
  191.     struct sgttyb     Modes;
  192.     char        *p;
  193.     int             ok;
  194.     int             flags;
  195.  
  196.     if (ok = ioctl(fileno(stdin), TIOCGETP, &Modes) >= 0) {
  197.     flags = Modes.sg_flags;
  198.     Modes.sg_flags &= ~ECHO;
  199.     (void)ioctl(fileno(stdin), TIOCSETP, &Modes);
  200.     }
  201.  
  202.     if (fgets(buff, size, stdin) == NULL)
  203.     Fatal("No password!?");
  204.     if (p = strchr(buff, '\n'))
  205.     *p = '\0';
  206.  
  207.     if (ok) {
  208.     Modes.sg_flags = flags;
  209.     (void)ioctl(fileno(stdin), TIOCSETP, &Modes);
  210.     (void)printf("\n");
  211.     }
  212. }
  213.