FreeBSD/contrib/netcat/netcat.c
/*
 * remote_connect()
 * Returns a socket connected to a remote host. Properly binds to a local
 * port or source address if needed. Returns -1 on failure.
 */
remote_connect()
Returns a socket connected to a remote host. Properly binds to a local
port or source address if needed. Returns -1 on failure.
remote_connect () Returns a socket connected to a remote host . Properly binds to a local port or source address if needed . Returns - 1 on failure .
int
remote_connect(const char *host, const char *port, struct addrinfo hints)
{
	struct addrinfo *res, *res0;
	int s, error, on = 1;
	if ((error = getaddrinfo(host, port, &hints, &res)))
		errx(1, "getaddrinfo: %s", gai_strerror(error));
	res0 = res;
	do {
		if ((s = socket(res0->ai_family, res0->ai_socktype,
		    res0->ai_protocol)) < 0)
			continue;
#ifdef IPSEC
		if (ipsec_policy[0] != NULL)
			add_ipsec_policy(s, ipsec_policy[0]);
		if (ipsec_policy[1] != NULL)
			add_ipsec_policy(s, ipsec_policy[1]);
#endif
		if (rtableid >= 0 && (setsockopt(s, SOL_SOCKET, SO_SETFIB,
		    &rtableid, sizeof(rtableid)) == -1))
			err(1, "setsockopt SO_SETFIB");
		if (sflag || pflag) {
			struct addrinfo ahints, *ares;
			setsockopt(s, IPPROTO_IP, IP_BINDANY, &on, sizeof(on));
			memset(&ahints, 0, sizeof(struct addrinfo));
			ahints.ai_family = res0->ai_family;
			ahints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
			ahints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
			ahints.ai_flags = AI_PASSIVE;
			if ((error = getaddrinfo(sflag, pflag, &ahints, &ares)))
				errx(1, "getaddrinfo: %s", gai_strerror(error));
			if (bind(s, (struct sockaddr *)ares->ai_addr,
			    ares->ai_addrlen) < 0)
				err(1, "bind failed");
			freeaddrinfo(ares);
		}
		set_common_sockopts(s, res0->ai_family);
		if (timeout_connect(s, res0->ai_addr, res0->ai_addrlen) == 0)
			break;
		else if (vflag)
			warn("connect to %s port %s (%s) failed", host, port,
			    uflag ? "udp" : "tcp");
		close(s);
		s = -1;
	} while ((res0 = res0->ai_next) != NULL);
	freeaddrinfo(res);
	return (s);
}
int remote_connect ( const char * host , const char * port , struct addrinfo hints ) { struct addrinfo * res , * res0 ; int s , error , on = 1 ; if ( ( error = getaddrinfo ( host , port , & hints , & res ) ) ) errx ( 1 , " " , gai_strerror ( error ) ) ; res0 = res ; do { if ( ( s = socket ( res0 -> ai_family , res0 -> ai_socktype , res0 -> ai_protocol ) ) < 0 ) continue ; #ifdef IPSEC if ( ipsec_policy [ 0 ] != NULL ) add_ipsec_policy ( s , ipsec_policy [ 0 ] ) ; if ( ipsec_policy [ 1 ] != NULL ) add_ipsec_policy ( s , ipsec_policy [ 1 ] ) ; #endif if ( rtableid >= 0 && ( setsockopt ( s , SOL_SOCKET , SO_SETFIB , & rtableid , sizeof ( rtableid ) ) == -1 ) ) err ( 1 , " " ) ; if ( sflag || pflag ) { struct addrinfo ahints , * ares ; setsockopt ( s , IPPROTO_IP , IP_BINDANY , & on , sizeof ( on ) ) ; memset ( & ahints , 0 , sizeof ( struct addrinfo ) ) ; ahints . ai_family = res0 -> ai_family ; ahints . ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM ; ahints . ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP ; ahints . ai_flags = AI_PASSIVE ; if ( ( error = getaddrinfo ( sflag , pflag , & ahints , & ares ) ) ) errx ( 1 , " " , gai_strerror ( error ) ) ; if ( bind ( s , ( struct sockaddr * ) ares -> ai_addr , ares -> ai_addrlen ) < 0 ) err ( 1 , " " ) ; freeaddrinfo ( ares ) ; } set_common_sockopts ( s , res0 -> ai_family ) ; if ( timeout_connect ( s , res0 -> ai_addr , res0 -> ai_addrlen ) == 0 ) break ; else if ( vflag ) warn ( " " , host , port , uflag ? " " : " " ) ; close ( s ) ; s = -1 ; } while ( ( res0 = res0 -> ai_next ) != NULL ) ; freeaddrinfo ( res ) ; return ( s ) ; }
remote_connect()
 Returns a socket connected to a remote host.
remote_connect () Returns a socket connected to a remote host .
/* Bind to a local port or source address if specified. */ /* try IP_BINDANY, but don't insist */