allocate-port.c
static int allocate_port(const char *bind_host, uint16_t bind_port, struct sockaddr_in *bound_address, const char *reachable_ipv4)
{
	errno = 0;
    const int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock < 0) 
    {
		perror("socket()");
        return -1;
    }

	errno = 0;
    int enable = 1;
    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)))
    {
		perror("setsockopt()");
        return -1;
    }
	
	errno = 0;
    struct sockaddr_in bind_address;
	if (resolve_host_port(&bind_address, bind_host, bind_port))
    {
		return -1;
    }
	
	errno = 0;
	if (bind(sock, (const struct sockaddr *)&bind_address, sizeof(bind_address)))
	{
		perror("bind()");
		return -1;
	}

    socklen_t bound_address_size = sizeof(*bound_address);
    memset(bound_address, 0, bound_address_size);
    errno = 0;
    if (getsockname(sock, (struct sockaddr *) bound_address, &bound_address_size))
    {
		perror("getsockname()");
		return -1;
    }

    errno = 0;
    if (close(sock))
    {
		perror("close()");
		return -1;
    }

    /** INADDR_ANY (0.0.0.0) ? */
    if (bound_address->sin_addr.s_addr == 0)
    {
        const uint16_t port = bound_address->sin_port;

        if (resolve_primary_address(reachable_ipv4, bound_address, bound_address_size))
        {
            (void) fprintf(stderr, "error: failed to resolve primary address\n");
            return -1;
        }

        bound_address->sin_port = port;
    }

    return 0;
}
/** Allocate a dynamic listen TCP (IPv4) port and immediately close the socket.
 *
 * @return 0 if bound_address has been filled with the outgoing address and an allocated TCP port
 *         or -1 if any error occured
 */
Allocate a dynamic listen TCP (IPv4) port and immediately close the socket.
@return 0 if bound_address has been filled with the outgoing address and an allocated TCP port
or -1 if any error occured
Allocate a dynamic listen TCP ( IPv4 ) port and immediately close the socket . @return 0 if bound_address has been filled with the outgoing address and an allocated TCP port or - 1 if any error occured
static int allocate_port(const char *bind_host, uint16_t bind_port, struct sockaddr_in *bound_address, const char *reachable_ipv4)
{
	errno = 0;
    const int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock < 0) 
    {
		perror("socket()");
        return -1;
    }
	errno = 0;
    int enable = 1;
    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)))
    {
		perror("setsockopt()");
        return -1;
    }
	errno = 0;
    struct sockaddr_in bind_address;
	if (resolve_host_port(&bind_address, bind_host, bind_port))
    {
		return -1;
    }
	errno = 0;
	if (bind(sock, (const struct sockaddr *)&bind_address, sizeof(bind_address)))
	{
		perror("bind()");
		return -1;
	}
    socklen_t bound_address_size = sizeof(*bound_address);
    memset(bound_address, 0, bound_address_size);
    errno = 0;
    if (getsockname(sock, (struct sockaddr *) bound_address, &bound_address_size))
    {
		perror("getsockname()");
		return -1;
    }
    errno = 0;
    if (close(sock))
    {
		perror("close()");
		return -1;
    }
    if (bound_address->sin_addr.s_addr == 0)
    {
        const uint16_t port = bound_address->sin_port;
        if (resolve_primary_address(reachable_ipv4, bound_address, bound_address_size))
        {
            (void) fprintf(stderr, "error: failed to resolve primary address\n");
            return -1;
        }
        bound_address->sin_port = port;
    }
    return 0;
}
static int allocate_port ( const char * bind_host , uint16_t bind_port , struct sockaddr_in * bound_address , const char * reachable_ipv4 ) { errno = 0 ; const int sock = socket ( AF_INET , SOCK_STREAM , IPPROTO_TCP ) ; if ( sock < 0 ) { perror ( " " ) ; return -1 ; } errno = 0 ; int enable = 1 ; if ( setsockopt ( sock , SOL_SOCKET , SO_REUSEADDR , & enable , sizeof ( enable ) ) ) { perror ( " " ) ; return -1 ; } errno = 0 ; struct sockaddr_in bind_address ; if ( resolve_host_port ( & bind_address , bind_host , bind_port ) ) { return -1 ; } errno = 0 ; if ( bind ( sock , ( const struct sockaddr * ) & bind_address , sizeof ( bind_address ) ) ) { perror ( " " ) ; return -1 ; } socklen_t bound_address_size = sizeof ( * bound_address ) ; memset ( bound_address , 0 , bound_address_size ) ; errno = 0 ; if ( getsockname ( sock , ( struct sockaddr * ) bound_address , & bound_address_size ) ) { perror ( " " ) ; return -1 ; } errno = 0 ; if ( close ( sock ) ) { perror ( " " ) ; return -1 ; } if ( bound_address -> sin_addr . s_addr == 0 ) { const uint16_t port = bound_address -> sin_port ; if ( resolve_primary_address ( reachable_ipv4 , bound_address , bound_address_size ) ) { ( void ) fprintf ( stderr , " \n " ) ; return -1 ; } bound_address -> sin_port = port ; } return 0 ; }
Allocate a dynamic listen TCP (IPv4) port and immediately close the socket.
Allocate a dynamic listen TCP ( IPv4 ) port and immediately close the socket .
/** INADDR_ANY (0.0.0.0) ? */