allocate-port.c
static int resolve_primary_address(const char *reachable_ipv4, struct sockaddr_in *primary_address, socklen_t primary_address_size)
{
    errno = 0;
    const int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (sock < 0)
    {
        perror("resolve_primary_address: socket()");
        return -1;
    }

    /* use TEST-NET-1 IP (reserved by IANA for documentation purposes) as a fallback */
    const char* test_ip = "192.0.2.1";
    if (reachable_ipv4 != NULL && strlen(reachable_ipv4) > 0)
    {
        test_ip = reachable_ipv4;
    }

    const uint16_t dns_port = 53;  /* whatever */

    struct sockaddr_in test_address;
    memset(&test_address, 0, sizeof(test_address));
    test_address.sin_family = AF_INET;
    test_address.sin_port = htons(dns_port);

    if (!inet_aton(test_ip, &test_address.sin_addr))
    {
        (void) fprintf(stderr, "error: invalid IPv4 address: %s\n", test_ip);
        return -1;
    }

    errno = 0;
    if (connect(sock, (const struct sockaddr*) &test_address, sizeof(test_address)))
    {
        perror("resolve_primary_address: connect()");
        return -1;
    }

    errno = 0;
    if (getsockname(sock, (struct sockaddr*) primary_address, &primary_address_size))
    {
        perror("resolve_primary_address: getsockname()");
        return -1;
    }

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

    return 0;
}
/**
 * Create an outgoing UDP socket (to a test address) and return its local IP.
 */
Create an outgoing UDP socket (to a test address) and return its local IP.
Create an outgoing UDP socket ( to a test address ) and return its local IP .
static int resolve_primary_address(const char *reachable_ipv4, struct sockaddr_in *primary_address, socklen_t primary_address_size)
{
    errno = 0;
    const int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (sock < 0)
    {
        perror("resolve_primary_address: socket()");
        return -1;
    }
    const char* test_ip = "192.0.2.1";
    if (reachable_ipv4 != NULL && strlen(reachable_ipv4) > 0)
    {
        test_ip = reachable_ipv4;
    }
    const uint16_t dns_port = 53;  
    struct sockaddr_in test_address;
    memset(&test_address, 0, sizeof(test_address));
    test_address.sin_family = AF_INET;
    test_address.sin_port = htons(dns_port);
    if (!inet_aton(test_ip, &test_address.sin_addr))
    {
        (void) fprintf(stderr, "error: invalid IPv4 address: %s\n", test_ip);
        return -1;
    }
    errno = 0;
    if (connect(sock, (const struct sockaddr*) &test_address, sizeof(test_address)))
    {
        perror("resolve_primary_address: connect()");
        return -1;
    }
    errno = 0;
    if (getsockname(sock, (struct sockaddr*) primary_address, &primary_address_size))
    {
        perror("resolve_primary_address: getsockname()");
        return -1;
    }
    errno = 0;
    if (close(sock))
    {
        perror("resolve_primary_address: close()");
        return -1;
    }
    return 0;
}
static int resolve_primary_address ( const char * reachable_ipv4 , struct sockaddr_in * primary_address , socklen_t primary_address_size ) { errno = 0 ; const int sock = socket ( AF_INET , SOCK_DGRAM , IPPROTO_UDP ) ; if ( sock < 0 ) { perror ( " " ) ; return -1 ; } const char * test_ip = " " ; if ( reachable_ipv4 != NULL && strlen ( reachable_ipv4 ) > 0 ) { test_ip = reachable_ipv4 ; } const uint16_t dns_port = 53 ; struct sockaddr_in test_address ; memset ( & test_address , 0 , sizeof ( test_address ) ) ; test_address . sin_family = AF_INET ; test_address . sin_port = htons ( dns_port ) ; if ( ! inet_aton ( test_ip , & test_address . sin_addr ) ) { ( void ) fprintf ( stderr , " \n " , test_ip ) ; return -1 ; } errno = 0 ; if ( connect ( sock , ( const struct sockaddr * ) & test_address , sizeof ( test_address ) ) ) { perror ( " " ) ; return -1 ; } errno = 0 ; if ( getsockname ( sock , ( struct sockaddr * ) primary_address , & primary_address_size ) ) { perror ( " " ) ; return -1 ; } errno = 0 ; if ( close ( sock ) ) { perror ( " " ) ; return -1 ; } return 0 ; }
Create an outgoing UDP socket (to a test address) and return its local IP.
Create an outgoing UDP socket ( to a test address ) and return its local IP .
/* use TEST-NET-1 IP (reserved by IANA for documentation purposes) as a fallback */ /* whatever */