home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / POPCLIEN.000 / POPCLIEN / pop / socket.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-01  |  3.8 KB  |  176 lines

  1. /***********************************************************************
  2.   module:       socket.c
  3.   program:      popclient
  4.   SCCS ID:      @(#)socket.c    1.5  4/1/94
  5.   programmer:   Virginia Tech Computing Center
  6.   compiler:     DEC RISC C compiler (Ultrix 4.1)
  7.   environment:  DEC Ultrix 4.3 
  8.   description:  UNIX sockets code.
  9.  ***********************************************************************/
  10.  
  11. #include <sys/types.h>
  12. #include <sys/socket.h>
  13. #include <fcntl.h>
  14. #include <netinet/in.h>
  15. #include <arpa/inet.h>
  16. #include <netdb.h>
  17. #include <sys/time.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <varargs.h>
  22.  
  23. #include "socket.h"
  24.  
  25. /* requisite, venerable SCCS ID */
  26. static char sccs_id [] = "@(#)socket.c    1.4\t3/29/94";
  27.  
  28. int Socket(host, clientPort)
  29. char *host;
  30. int clientPort;
  31. {
  32.     int sock;
  33. #ifndef USE_TERM  /*  USE_TERM *IS NOT* defined  */
  34.     unsigned long inaddr;
  35.     struct sockaddr_in ad;
  36.     struct hostent *hp;
  37.     
  38.     memset(&ad, 0, sizeof(ad));
  39.     ad.sin_family = AF_INET;
  40.  
  41.     inaddr = inet_addr(host);
  42.     if (inaddr != INADDR_NONE)
  43.         memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
  44.     else
  45.     {
  46.         hp = gethostbyname(host);
  47.         if (hp == NULL)
  48.             return -1;
  49.         memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
  50.     }
  51.     ad.sin_port = htons(clientPort);
  52.     
  53.     sock = socket(AF_INET, SOCK_STREAM, 0);
  54.     if (sock < 0)
  55.         return sock;
  56.     if (connect(sock, &ad, sizeof(ad)) < 0)
  57.         return -1;
  58.     return sock;
  59. #else  /*  USE_TERM *IS* defined  */
  60. #include <client.h>
  61.     if ((sock = connect_server(0)) < 0) {  /*  try to get an fd from term  */
  62.     /*  couldn't get an fd because the term server apparently wasn't
  63.         running  */
  64.     return(-1);
  65.     }
  66.     if (send_command(sock, C_PORT, 0, "%s:%d", host, clientPort) >= 0) {
  67.     /*  tell the term server how to handle this particular connection  */
  68.     send_command(sock, C_PRIORITY, 1, TERM_PRIORITY);
  69.         /*  how much priority the term server should give this client  */
  70.     send_command(sock, C_COMPRESS, 1, (TERM_COMPRESS) ? "y" : "n");
  71.         /*  whether or not term should compress data for this client  */
  72.     send_command(sock, C_DUMB, 1, 0);
  73.         /*  send term server into dumb mode (ie it won't accept any
  74.         further commands  */
  75.     return(sock);
  76.     }
  77.     else  /*  term couldn't connect to host:clientPort;  report error(?)  */
  78.     return(-1);
  79. #endif
  80. }
  81.  
  82. int SockGets(socket,buf,len)
  83. int socket;
  84. char *buf;
  85. int len;
  86. {
  87.     while (--len)
  88.     {
  89.         if (read(socket, buf, 1) != 1)
  90.             return -1;
  91.         if (*buf == '\n')
  92.             break;
  93.         if (*buf != '\r') /* remove all CRs */
  94.             buf++;
  95.     }
  96.     *buf = 0;
  97.     return 0;
  98. }
  99.  
  100. int SockPuts(socket,buf)
  101. int socket;
  102. char *buf;
  103. {
  104.     int rc;
  105.     
  106.     if (rc = SockWrite(socket, buf, strlen(buf)))
  107.         return rc;
  108.     return SockWrite(socket, "\r\n", 2);
  109. }
  110.  
  111. int SockWrite(socket,buf,len)
  112. int socket;
  113. char *buf;
  114. int len;
  115. {
  116.     int n;
  117.     
  118.     while (len)
  119.     {
  120.         n = write(socket, buf, len);
  121.         if (n <= 0)
  122.             return -1;
  123.         len -= n;
  124.         buf += n;
  125.     }
  126.     return 0;
  127. }
  128.  
  129. int SockRead(socket,buf,len)
  130. int socket;
  131. char *buf;
  132. int len;
  133. {
  134.     int n;
  135.     
  136.     while (len)
  137.     {
  138.         n = read(socket, buf, len);
  139.         if (n <= 0)
  140.             return -1;
  141.         len -= n;
  142.         buf += n;
  143.     }
  144.     return 0;
  145. }
  146.  
  147. int SockPrintf(socket,format,va_alist)
  148. int socket;
  149. char *format;
  150. va_dcl {
  151.  
  152.     va_list ap;
  153.     char buf[8192];
  154.     
  155.     va_start(ap);
  156.     vsprintf(buf, format, ap);
  157.     va_end(ap);
  158.     return SockWrite(socket, buf, strlen(buf));
  159. }
  160.  
  161. int SockStatus(socket,seconds)
  162. int socket;
  163. int seconds;
  164. {
  165.     int fds = 0;
  166.     struct timeval timeout;
  167.     
  168.     timeout.tv_sec = seconds;
  169.     timeout.tv_usec = 0;
  170.  
  171.     fds |= 1 << socket;
  172.     if (select(socket+1, &fds, NULL, NULL, &timeout) <= 0)
  173.         return 0;
  174.     return 1;
  175. }
  176.