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 >
Wrap
C/C++ Source or Header
|
1989-05-15
|
4KB
|
136 lines
/*
** Copyright 1986, 1987, 1988, 1989 University of Wisconsin
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted,
** provided that the above copyright notice appear in all copies and that
** both that copyright notice and this permission notice appear in
** supporting documentation, and that the name of the University of
** Wisconsin not be used in advertising or publicity pertaining to
** distribution of the software without specific, written prior
** permission. The University of Wisconsin makes no representations about
** the suitability of this software for any purpose. It is provided "as
** is" without express or implied warranty.
**
** THE UNIVERSITY OF WISCONSIN DISCLAIMS ALL WARRANTIES WITH REGARD TO
** THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
** FITNESS. IN NO EVENT SHALL THE UNIVERSITY OF WISCONSIN BE LIABLE FOR
** ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
** WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
** ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
** OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**
** Authors: Allan Bricker and Michael J. Litzkow,
** University of Wisconsin, Computer Sciences Dept.
**
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h>
#include "except.h"
#include "debug.h"
#include "clib.h"
static char *_FileName_ = __FILE__; /* Used by EXCEPT (see except.h) */
extern int errno;
do_connect( host, service, port )
char *host, *service;
u_short port;
{
struct sockaddr_in sin;
struct hostent *hostentp;
struct servent *servp;
int status;
int fd;
int true = 1;
hostentp = gethostbyname( host );
if( hostentp == NULL ) {
#if defined(vax) && !defined(ultrix)
herror( "gethostbyname" );
#endif
dprintf( D_ALWAYS, "Can't find host \"%s\" (Nameserver down?)\n",
host );
return( -1 );
}
if( service ) {
servp = getservbyname(service, "tcp");
if( servp != NULL ) {
port = servp->s_port;
}
}
if( (fd=socket(AF_INET,SOCK_STREAM,0)) < 0 ) {
EXCEPT( "socket" );
}
if( setsockopt(fd,SOL_SOCKET,SO_KEEPALIVE,(caddr_t *)&true,sizeof(true)) < 0 ) {
EXCEPT( "setsockopt( SO_KEEPALIVE )" );
}
bzero( (char *)&sin, sizeof sin );
bcopy( hostentp->h_addr, (char *)&sin.sin_addr,
(unsigned)hostentp->h_length );
sin.sin_family = hostentp->h_addrtype;
sin.sin_port = htons(port);
/*
dprintf( D_ALWAYS, "Connecting now...\n" );
*/
if( (status = connect(fd,(struct sockaddr *)&sin,sizeof(sin))) == 0 ) {
/*
dprintf( D_ALWAYS, "Done connecting\n" );
*/
return fd;
} else {
dprintf( D_ALWAYS, "connect returns %d, errno = %d\n", status, errno );
(void)close( fd );
return -1;
}
}
udp_connect( host, port )
char *host;
{
int sock;
struct sockaddr_in sin;
struct hostent *hostentp;
hostentp = gethostbyname( host );
if( hostentp == NULL ) {
#if defined(vax) && !defined(ultrix)
herror( "gethostbyname" );
#endif
printf( "Can't find host \"%s\" (Nameserver down?)\n",
host );
return( -1 );
}
if( (sock=socket(AF_INET,SOCK_DGRAM,0)) < 0 ) {
perror( "socket" );
exit( 1 );
}
bzero( (char *)&sin, sizeof sin );
bcopy( hostentp->h_addr, (char *)&sin.sin_addr,
(unsigned)hostentp->h_length );
sin.sin_family = hostentp->h_addrtype;
sin.sin_port = htons( (u_short)port );
if( connect(sock,(struct sockaddr *)&sin,sizeof(sin)) < 0 ) {
perror( "connect" );
exit( 1 );
}
return sock;
}