home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / ONLINE / IJB20OS2 / SOURCE / CONN.C < prev    next >
C/C++ Source or Header  |  1997-09-16  |  3KB  |  159 lines

  1. char *conn_rcs = "$Id: conn.c,v 2.9 1997/09/06 18:55:39 ACJC Exp $";
  2. /* Written and copyright by the Anonymous Coders and Junkbusters Corporation.
  3.  * Will be made available under the GNU General Public License.
  4.  * This software comes with NO WARRANTY.
  5.  */
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. #include <fcntl.h>
  12. #include <sys/types.h>
  13.  
  14. #ifdef _WIN32
  15.  
  16. #include <windows.h>
  17. #include <sys/timeb.h>
  18. #include <io.h>
  19.  
  20. #else
  21.  
  22. #include <unistd.h>
  23. #include <sys/time.h>
  24. #include <netinet/in.h>
  25. #include <sys/ioctl.h>
  26. #include <netdb.h> 
  27. #include <sys/socket.h>
  28. #include <netinet/tcp.h>
  29. #include <arpa/inet.h>
  30.  
  31. #endif
  32.  
  33. #ifdef REGEX
  34. #include "gnu_regex.h"
  35. #endif
  36.  
  37. #include "jcc.h"
  38.  
  39. extern int    errno;
  40.  
  41. int
  42. direct_connect(struct gateway *gw, struct http_request *http)
  43. {
  44.     if(gw->forward_host) {
  45.         return(connect_to(gw->forward_host, gw->forward_port));
  46.     } else {
  47.         return(connect_to(http->host, http->port));
  48.     }
  49. }
  50.  
  51. int
  52. atoip(char *host)
  53. {
  54.     struct sockaddr_in inaddr;
  55.     struct hostent *hostp;
  56.  
  57.     if ((host == NULL) || (*host == '\0')) 
  58.         return(INADDR_ANY);
  59.  
  60.     memset ((char * ) &inaddr, 0, sizeof inaddr);
  61.     if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1) {
  62.         if (((hostp = gethostbyname(host)) == NULL)
  63.          && ((hostp = gethostbyname(host)) == NULL)) {
  64.             errno = EINVAL;
  65.             return(-1);
  66.         }
  67.         if (hostp->h_addrtype != AF_INET) {
  68. #ifdef _WIN32
  69.             errno = WSAEPROTOTYPE;
  70. #else
  71.             errno = EPROTOTYPE;
  72. #endif
  73.             return(-1);
  74.         }
  75.         memcpy((char * ) &inaddr.sin_addr, (char * ) hostp->h_addr,
  76.             sizeof(inaddr.sin_addr));
  77.     }
  78.     return(inaddr.sin_addr.s_addr);
  79. }
  80.  
  81.  
  82. int
  83. connect_to(char *host, int portnum)
  84. {
  85.     struct sockaddr_in inaddr;
  86.     int    fd, addr;
  87.     fd_set wfds;
  88.     struct timeval tv[1];
  89.     int    flags;
  90.  
  91.     memset ((char * ) &inaddr, 0, sizeof inaddr);
  92.  
  93.     if((addr = atoip(host)) == -1) return(-1);
  94.  
  95.     inaddr.sin_addr.s_addr = atoip(host);
  96.     inaddr.sin_family      = AF_INET;
  97.  
  98.     if (sizeof(inaddr.sin_port) == sizeof(short)) {
  99.         inaddr.sin_port = htons(portnum);
  100.     } else {
  101.         inaddr.sin_port = htonl(portnum);
  102.     }
  103.  
  104.     if((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0) {
  105.         return(-1);
  106.     }
  107.  
  108. #ifdef TCP_NODELAY
  109. {    /* turn off TCP coalescence */
  110.     int    mi = 1;
  111.     setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (char * ) &mi, sizeof (int));
  112. }
  113. #endif
  114.  
  115. #ifndef _WIN32
  116.     if ((flags = fcntl(fd, F_GETFL, 0)) != -1) {
  117.         flags |= O_NDELAY;
  118.         fcntl(fd, F_SETFL, flags);
  119.     }
  120. #endif
  121.  
  122.     while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == -1) {
  123.  
  124. #ifdef _WIN32
  125.         if (errno == WSAEINPROGRESS)
  126. #else
  127.         if (errno == EINPROGRESS)
  128. #endif
  129.         {
  130.             break;
  131.         }
  132.  
  133.         if (errno != EINTR) {
  134.             (void) close (fd);
  135.             return(-1);
  136.         }
  137.     }
  138.  
  139. #ifndef _WIN32
  140.     if (flags != -1) {
  141.         flags &= ~O_NDELAY;
  142.         fcntl(fd, F_SETFL, flags);
  143.     }
  144. #endif
  145.  
  146.     /* wait for connection to complete */
  147.     FD_ZERO(&wfds);
  148.     FD_SET(fd, &wfds);
  149.  
  150.     tv->tv_sec  = 30;
  151.     tv->tv_usec = 0;
  152.  
  153.     if (select(fd + 1, NULL, &wfds, NULL, tv) <= 0) {
  154.         (void) close(fd);
  155.         return(-1);
  156.     }
  157.     return(fd);
  158. }
  159.