home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / c / condor40.zip / CONDOR / src / util_lib / do_connect.c < prev    next >
C/C++ Source or Header  |  1989-05-15  |  4KB  |  136 lines

  1. /* 
  2. ** Copyright 1986, 1987, 1988, 1989 University of Wisconsin
  3. ** 
  4. ** Permission to use, copy, modify, and distribute this software and its
  5. ** documentation for any purpose and without fee is hereby granted,
  6. ** provided that the above copyright notice appear in all copies and that
  7. ** both that copyright notice and this permission notice appear in
  8. ** supporting documentation, and that the name of the University of
  9. ** Wisconsin not be used in advertising or publicity pertaining to
  10. ** distribution of the software without specific, written prior
  11. ** permission.  The University of Wisconsin makes no representations about
  12. ** the suitability of this software for any purpose.  It is provided "as
  13. ** is" without express or implied warranty.
  14. ** 
  15. ** THE UNIVERSITY OF WISCONSIN DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. ** THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. ** FITNESS. IN NO EVENT SHALL THE UNIVERSITY OF WISCONSIN  BE LIABLE FOR
  18. ** ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. ** WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ** ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  21. ** OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22. ** 
  23. ** Authors:  Allan Bricker and Michael J. Litzkow,
  24. **              University of Wisconsin, Computer Sciences Dept.
  25. ** 
  26. */ 
  27.  
  28.  
  29. #include <stdio.h>
  30. #include <sys/types.h>
  31. #include <sys/socket.h>
  32. #include <sys/time.h>
  33. #include <netinet/in.h>
  34. #include <netdb.h>
  35. #include "except.h"
  36. #include "debug.h"
  37. #include "clib.h"
  38.  
  39. static char *_FileName_ = __FILE__;        /* Used by EXCEPT (see except.h)     */
  40.  
  41. extern int    errno;
  42.  
  43. do_connect( host, service, port )
  44. char    *host, *service;
  45. u_short        port;
  46. {
  47.     struct sockaddr_in    sin;
  48.     struct hostent        *hostentp;
  49.     struct servent        *servp;
  50.     int                    status;
  51.     int                    fd;
  52.     int                    true = 1;
  53.  
  54.     hostentp = gethostbyname( host );
  55.     if( hostentp == NULL ) {
  56. #if defined(vax) && !defined(ultrix)
  57.         herror( "gethostbyname" );
  58. #endif
  59.         dprintf( D_ALWAYS, "Can't find host \"%s\" (Nameserver down?)\n",
  60.                             host );
  61.         return( -1 );
  62.     }
  63.  
  64.     if( service ) {
  65.         servp = getservbyname(service, "tcp");
  66.         if( servp != NULL ) {
  67.             port = servp->s_port;
  68.         }
  69.     }
  70.  
  71.     if( (fd=socket(AF_INET,SOCK_STREAM,0)) < 0 ) {
  72.         EXCEPT( "socket" );
  73.     }
  74.  
  75.     if( setsockopt(fd,SOL_SOCKET,SO_KEEPALIVE,(caddr_t *)&true,sizeof(true)) < 0 ) {
  76.         EXCEPT( "setsockopt( SO_KEEPALIVE )" );
  77.     }
  78.  
  79.     bzero( (char *)&sin, sizeof sin );
  80.     bcopy( hostentp->h_addr, (char *)&sin.sin_addr,
  81.                                             (unsigned)hostentp->h_length );
  82.     sin.sin_family = hostentp->h_addrtype;
  83.     sin.sin_port = htons(port);
  84.  
  85.     /*
  86.     dprintf( D_ALWAYS, "Connecting now...\n" );
  87.     */
  88.     if( (status = connect(fd,(struct sockaddr *)&sin,sizeof(sin))) == 0 ) {
  89.         /*
  90.         dprintf( D_ALWAYS, "Done connecting\n" );
  91.         */
  92.         return fd;
  93.     } else {
  94.         dprintf( D_ALWAYS, "connect returns %d, errno = %d\n", status, errno );
  95.         (void)close( fd );
  96.         return -1;
  97.     }
  98. }
  99.  
  100. udp_connect( host, port )
  101. char    *host;
  102. {
  103.     int        sock;
  104.     struct sockaddr_in    sin;
  105.     struct hostent        *hostentp;
  106.  
  107.     hostentp = gethostbyname( host );
  108.     if( hostentp == NULL ) {
  109. #if defined(vax) && !defined(ultrix)
  110.         herror( "gethostbyname" );
  111. #endif
  112.         printf( "Can't find host \"%s\" (Nameserver down?)\n",
  113.                             host );
  114.         return( -1 );
  115.     }
  116.  
  117.     if( (sock=socket(AF_INET,SOCK_DGRAM,0)) < 0 ) {
  118.         perror( "socket" );
  119.         exit( 1 );
  120.     }
  121.  
  122.  
  123.     bzero( (char *)&sin, sizeof sin );
  124.     bcopy( hostentp->h_addr, (char *)&sin.sin_addr,
  125.                                                 (unsigned)hostentp->h_length );
  126.     sin.sin_family = hostentp->h_addrtype;
  127.     sin.sin_port = htons( (u_short)port );
  128.  
  129.     if( connect(sock,(struct sockaddr *)&sin,sizeof(sin)) < 0 ) {
  130.         perror( "connect" );
  131.         exit( 1 );
  132.     }
  133.  
  134.     return sock;
  135. }
  136.