Search code examples
node.jswindowspowershellipv6

nodejs http request immediately after adding a new address result in EADDRNOTAVAIL


//nodejs code

var ip=<some_ip_address>;
//for me it is 2001:250:401:3611:50c6:6b18:e8f7:f882

exec('powershell new-netipaddress '+ip+' -InterfaceAlias WLAN',(e,so,se)=>{
    http.request({
        host:'2404:6800:4005:805::200e',//just use google as an example
        family:6,
        localAddress:ip
    },(res)=>{
        console.log('reachable');
    }).on('error',(e)=>{
        console.log(e);
    })
}).stdin.end();

then output

{[Error: bind EADDRNOTAVAIL 2001:250:401:3611:50c6:6b18:e8f7:f882]
code: 'EADDRNOTAVAIL',
errno: 'EADDRNOTAVAIL',
syscall: 'bind',
address: '2001:250:401:3611:50c6:6b18:e8f7:f882' }

the 2nd time (actually I am testing address , since our DHCP-stateless is broken)

{ [Error: connect ETIMEDOUT 2404:6800:4005:805::200e:80]
code: 'ETIMEDOUT',
errno: 'ETIMEDOUT',
syscall: 'connect',
address: '2404:6800:4005:805::200e',
port: 80 }

me can draw from the 2nd time output,

new-netipaddress did add the address properly, so the callback runs well(although timeout, which we designed it to be)

but in 1st time the callback failed with EADDRNOTAVAIL

so why it failed with the first run? and how to avoid it?


Solution

  • thanks to @SanderSteffann 's knowledge on tentative address, I think of a solution but may not be very good

    exec('powershell new-netipaddress '+ip+' -InterfaceAlias WLAN',(e,so,se)=>{
        http.request({
        host:'2404:6800:4005:805::200e',//just use google as an example
        family:6,
        localAddress:ip
    },(res)=>{
        console.log('reachable');
    }).on('error',(e)=>{
        console.log(e);
    })
    }).stdin.end();
    
    
    exec('powershell new-netipaddress '+ip+' -InterfaceAlias WLAN',(e,so,se)=>{
    execSync('powershell get-netipaddress '+ip)
        http.request({
        host:'2404:6800:4005:805::200e',//just use google as an example
        family:6,
        localAddress:ip
    },(res)=>{
        console.log('reachable');
    }).on('error',(e)=>{
        console.log(e);
    })
    }).stdin.end();
    

    line 2:++++execSync('powershell get-netipaddress '+ip)