home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume21 / coda / part01 / libatt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-08  |  3.9 KB  |  208 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 an ATT3B2 with Wollongong TCP.
  8. **  Client library routines for an ATT6386 with Interlan V.3 TCP.
  9. */
  10. #include "client.h"
  11. #ifdef    ATT3B2
  12. #include <sys/types.h>
  13. #include <sys/socket.h>
  14. #include <sys/in.h>
  15. #endif    /* ATT3B2 */
  16. #ifdef    ATTI386
  17. #include <sys/types.h>
  18. #include <sys/in.h>
  19. #include <sys/socket.h>
  20. #endif    /* ATTI386 */
  21. #include <netdb.h>
  22. #include <termio.h>
  23. #ifdef    RCSID
  24. static char     RCS[] =
  25.     "$Header: libatt.c,v 2.0 90/03/23 14:41:38 rsalz Exp $";
  26. #endif    /* RCSID */
  27.  
  28.  
  29. STATIC int     Channel;        /* Something to talk with    */
  30.  
  31.  
  32. #ifdef    ATTI386
  33. /*
  34. **  Stupid Interlan software.  Why does their library need this routine,
  35. **  yet not provide it?
  36. */
  37. #include "alloca.inc"
  38. #endif    /* ATTI386 */
  39.  
  40. /*
  41. **  Do the grundge work of getting us a socket.
  42. */
  43. STATIC int
  44. GetSocket(machine, port)
  45.     char         *machine;
  46.     int              port;
  47. {
  48.     struct hostent     *gethostbyname();
  49.     REGISTER int      s;
  50.     struct hostent     *hp;
  51.     struct sockaddr_in      sin;
  52.  
  53.     /* Get the address of the server. */
  54.     if ((hp = gethostbyname(machine)) == NULL) {
  55.     (void)fprintf(stderr, "%s: Unknown host.\n", machine);
  56.     return -1;
  57.     }
  58.  
  59.     /* Set up the socket. */
  60.     (void)memset((char *)&sin, '\0', sizeof sin);
  61.     sin.sin_family = AF_INET;
  62.     sin.sin_port = htons(port);
  63.  
  64.     if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  65.     perror("socket");
  66.     return -1;
  67.     }
  68.  
  69.     (void)memcpy((char *)&sin.sin_addr, hp->h_addr, hp->h_length);
  70.     if (connect(s, (struct sockaddr *)&sin, sizeof sin) < 0) {
  71.     perror("connect");
  72.     (void)close(s);
  73.     return -1;
  74.     }
  75.  
  76.     return s;
  77. }
  78.  
  79.  
  80. /*
  81. **  Open connection to server, return FALSE on error.
  82. */
  83. int
  84. SRVopen(machine, port)
  85.     char    *machine;
  86.     int         port;
  87. {
  88. #ifdef    ATT3B2
  89. #ifdef    lint
  90.     /* Stupid SCCS strings in TWG library... */
  91.     S_netdb = S_netdb;
  92.     S_socket = S_socket;
  93.     S_in = S_in;
  94. #endif    /* lint */
  95. #endif    /* ATT3B2 */
  96.     return (Channel = GetSocket(machine, port)) >= 0;
  97. }
  98.  
  99.  
  100. /*
  101. **  Send a QUIT and shut down.
  102. */
  103. void
  104. SRVclose()
  105. {
  106.     SRVput("QUIT");
  107.     (void)close(Channel);
  108. }
  109.  
  110.  
  111. /*
  112. **  Send a line to the server.
  113. */
  114. void
  115. SRVput(p)
  116.     char    *p;
  117. {
  118.     if (SRVtrace)
  119.     (void)printf(">>>%s\n", p);
  120.     (void)write(Channel, p, (unsigned int)strlen(p));
  121.     (void)write(Channel, "\r\n", 2);
  122. }
  123.  
  124.  
  125. /*
  126. **  Get a line of text from the server.  Strip end-of-line characters.
  127. */
  128. int
  129. SRVget(buff, size)
  130.     char        *buff;
  131.     int             size;
  132. {
  133.     REGISTER char    *p;
  134.     REGISTER char    *bend;
  135.     REGISTER int     c;
  136.  
  137.     for (bend = &buff[size - 1]; ; ) {
  138.     for (p = buff; ((c = SRVcget()) != '\n'); ) {
  139.         if (c == EOF)
  140.         return FALSE;
  141.         if (c != '\r' && p < bend)
  142.         *p++ = c;
  143.     }
  144.     *p = '\0';
  145.     if (SRVtrace)
  146.         (void)printf("<<<%s\n", buff);
  147.     if (strncmp(buff, "INF ", 4))
  148.         return TRUE;
  149.     (void)printf("Server message:\n\t%s\n", &buff[4]);
  150.     (void)fflush(stdout);
  151.     }
  152. }
  153.  
  154.  
  155. /*
  156. **  Get a character from the server.
  157. */
  158. int
  159. SRVcget()
  160. {
  161.     static char     buff[1024];
  162.     static int     count;
  163.     static int     max;
  164.  
  165.     if (count == max) {
  166.     while ((max = read(Channel, buff, sizeof buff)) == 0)
  167.         ;
  168.     if (max < 0)
  169.         return EOF;
  170.     if (max > sizeof buff)
  171.         (void)abort();
  172.     count = 0;
  173.     }
  174.     return buff[count++];
  175. }
  176.  
  177.  
  178. /*
  179. **  Get a password, without echoing it.
  180. */
  181. void
  182. GetPassword(buff, size)
  183.     char        *buff;
  184.     int             size;
  185. {
  186.     struct termio     Modes;
  187.     char        *p;
  188.     int             ok;
  189.     int             flags;
  190.  
  191.     if (ok = ioctl(fileno(stdin), TCGETA, &Modes) >= 0) {
  192.     flags = Modes.c_lflag;
  193.     Modes.c_lflag &= ~(ECHO | ECHOE | ECHONL);
  194.     (void)ioctl(fileno(stdin), TCSETA, &Modes);
  195.     }
  196.  
  197.     if (fgets(buff, size, stdin) == NULL)
  198.     Fatal("No password!?");
  199.     if (p = strchr(buff, '\n'))
  200.     *p = '\0';
  201.  
  202.     if (ok) {
  203.     Modes.c_lflag = flags;
  204.     (void)ioctl(fileno(stdin), TCSETA, &Modes);
  205.     (void)printf("\n");
  206.     }
  207. }
  208.