nodepp icon indicating copy to clipboard operation
nodepp copied to clipboard

IPv6 is not handled properly by client code and impossible to use

Open lsergiy opened this issue 8 months ago • 3 comments

The nodepp::tcp_t::connect() API initializes socket_t as following:

        socket_t sk; obj->state= STATE::TCP_STATE_USED;
        sk.SOCK    = SOCK_STREAM ;
        sk.IPPROTO = IPPROTO_TCP ;

As result, sk.AF is always AF_INET and never set to AF_INET6 based on const string_t& host parameter of nodepp::tcp_t::connect() API call. This means, code cannot connect to IPv6 server.

Suggested solution is to add address family as parameter of void connect( const string_t& host, int port, NODE_CLB cb=nullptr )

lsergiy avatar Mar 30 '26 10:03 lsergiy

Additionally, may be good idea to treat default AF_UNSPECIFIED value of address family as auto-detection request. For example, we can use address in [ ] braces as the only IPv6 format. I.e. IPv6 version of local is [::1] - not ::1.

lsergiy avatar Mar 30 '26 11:03 lsergiy

RE: Issue - IPv6 Support and Socket Family Initialization

You are correct; the static initialization of AF_INET was a blind spot for IPv6 connectivity. However, I cannot simply change the signature to connect( host, port, af, cb ) because that would break the entire Nodepp net-stack and create a ripple effect requiring manual AF management in higher-level protocols like HTTP and WebSockets.

To resolve this without breaking the API or compromising performance, I have implemented two strategic solutions:

1. Explicit Control via agent_t

I’ve added a socket_family variable to the agent_t structure. This allows the socket to be told exactly which address family to look for and return.

    fetch_t args; ssl_t ssl;
    args.method  = "GET";
    args.url     = "https://www.google.com/";
    args.headers = header_t({ { "Host", url::host(args.url) } });
                
    agent_t agent; 
    agent.socket_family = AF_INET6; // Default is AF_INET

    https::fetch( args, &ssl, &agent );

This approach preserves the existing stack and allows HTTP/WS to inherit IPv6 support transparently.

2. The dns_t Context Injection

I’ve introduced an overload for connect() and listen() that accepts a dns_t structure. This structure is more than just an IP; it contains the necessary metadata for modern networking.

struct dns_t { 
    string_t address;  // Resolved IP address (v4 or v6)
    string_t hostname; // Original hostname (e.g., www.google.com) - important for SNI
    int family;        // The address family (AF_INET6 / AF_INET)
};

Why include the hostname? Including the hostname is critical for TLS/SSL. Without it, SNI (Server Name Indication) fails because the client wouldn't know which domain to request a certificate for when multiple domains share the same IP.

By using dns::lookup(), you can resolve the host once at the beginning of your process, store the dns_t object, and reuse it. This skips the DNS resolution overhead in subsequent calls while maintaining full support for SNI and IPv6.

// Detect everything once
auto list = dns::lookup( "www.google.com", AF_UNSPEC ); 
if( list.empty() ){ console::error( "Nothing found" ); }

// Use the pre-resolved context later
dns_t my_server = list[0]; ssl_t ssl;
tls::client( &ssl ).connect( my_server, 443, []( socket_t /*unused*/ ){
    console::log( "client connected" );    
});

This prevents an O(n) bottleneck during every handshake (common in "auto-detect" strategies) and keeps Nodepp deterministic, lightweight, and fast.

EDBCREPO avatar Mar 30 '26 17:03 EDBCREPO

Thanks for explanation. Both workarounds are good to know and I will use them)

lsergiy avatar Mar 30 '26 19:03 lsergiy