FreeBSD/contrib/netcat/netcat.c
/*
 * local_listen()
 * Returns a socket listening on a local port, binds to specified source
 * address. Returns -1 on failure.
 */
local_listen()
Returns a socket listening on a local port, binds to specified source
address. Returns -1 on failure.
local_listen () Returns a socket listening on a local port binds to specified source address . Returns - 1 on failure .
int
local_listen(char *host, char *port, struct addrinfo hints)
{
	struct addrinfo *res, *res0;
	int s, ret, x = 1;
	int error;
	hints.ai_flags |= AI_PASSIVE;
	if (host == NULL && hints.ai_family == AF_UNSPEC)
		hints.ai_family = AF_INET;
	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;
		if (rtableid >= 0 && (setsockopt(s, SOL_SOCKET, SO_SETFIB,
		    &rtableid, sizeof(rtableid)) == -1))
			err(1, "setsockopt SO_SETFIB");
		ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x));
		if (ret == -1)
			err(1, NULL);
#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 (FreeBSD_Oflag) {
			if (setsockopt(s, IPPROTO_TCP, TCP_NOOPT,
			    &FreeBSD_Oflag, sizeof(FreeBSD_Oflag)) == -1)
				err(1, "disable TCP options");
		}
		set_common_sockopts(s, res0->ai_family);
		if (bind(s, (struct sockaddr *)res0->ai_addr,
		    res0->ai_addrlen) == 0)
			break;
		close(s);
		s = -1;
	} while ((res0 = res0->ai_next) != NULL);
	if (!uflag && s != -1) {
		if (listen(s, 1) < 0)
			err(1, "listen");
	}
	freeaddrinfo(res);
	return (s);
}
int local_listen ( char * host , char * port , struct addrinfo hints ) { struct addrinfo * res , * res0 ; int s , ret , x = 1 ; int error ; hints . ai_flags |= AI_PASSIVE ; if ( host == NULL && hints . ai_family == AF_UNSPEC ) hints . ai_family = AF_INET ; 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 ; if ( rtableid >= 0 && ( setsockopt ( s , SOL_SOCKET , SO_SETFIB , & rtableid , sizeof ( rtableid ) ) == -1 ) ) err ( 1 , " " ) ; ret = setsockopt ( s , SOL_SOCKET , SO_REUSEPORT , & x , sizeof ( x ) ) ; if ( ret == -1 ) err ( 1 , NULL ) ; #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 ( FreeBSD_Oflag ) { if ( setsockopt ( s , IPPROTO_TCP , TCP_NOOPT , & FreeBSD_Oflag , sizeof ( FreeBSD_Oflag ) ) == -1 ) err ( 1 , " " ) ; } set_common_sockopts ( s , res0 -> ai_family ) ; if ( bind ( s , ( struct sockaddr * ) res0 -> ai_addr , res0 -> ai_addrlen ) == 0 ) break ; close ( s ) ; s = -1 ; } while ( ( res0 = res0 -> ai_next ) != NULL ) ; if ( ! uflag && s != -1 ) { if ( listen ( s , 1 ) < 0 ) err ( 1 , " " ) ; } freeaddrinfo ( res ) ; return ( s ) ; }
local_listen()
 Returns a socket listening on a local port, binds to specified source
 address.
local_listen () Returns a socket listening on a local port binds to specified source address .
/* Allow nodename to be null. */ /*
	 * In the case of binding to a wildcard address
	 * default to binding to an ipv4 address.
	 */