You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello! The binding to Local interface option is not working at all.
The case: Arch Linux system with two different ethernet interfaces.
eth0: 192.168.0.173/24
eth1: 192.168.0.182/24
routing table is
default via 192.168.0.1 dev eth0 metric 280
default via 192.168.0.1 dev eth1 metric 281
Every interface has a listening http server on other end with the address: 192.168.0.1
So I'm trying to send a request to each of them with nodejs.
Sample code:
varhttp=require('http');varoptions={};options.port=80;options.host="192.168.0.1";options.path='/index.html';options.headers={'Referer': 'http://'+options.host+':'+options.port+'/index.html'};options.localAddress='192.168.0.182';//NOT WORKING!! - REQUEST GOING FROM 192.168.0.173!varcreq=http.get(options);creq.on('response',function(res){res.on('data',function(chunk){console.log(chunk);});});
I've even tried creating a socket for that request with new net.Socket({ handle: net._createServerHandle( '192.168.0.182')}); and requests kept going from the wrong address.
There's workaround with 'curl --interface eth1 ...' that works, but it's not a good solution.
IIRC routing on Linux uses the route with the lowest metric value when multiple routes match, which in this case would agree with what you're experiencing.
Perhaps it's stating the obvious but the .localAddress option binds to an address, not an interface. There is no direct support for SO_BINDTODEVICE in node.js but you can jury-rig it with socat(1):
Activity
mscdex commentedon Nov 2, 2015
IIRC routing on Linux uses the route with the lowest metric value when multiple routes match, which in this case would agree with what you're experiencing.
k1tzu commentedon Nov 2, 2015
Of cause it uses the route with the lowest metric! But that's the question!
I need to send packets to both interfaces with nodejs.
In C language it is a simplest question to resolve with SO_BINDTODEVICE socket option. And I thought nodejs do this also in some intellectual way like
No problemo
bnoordhuis commentedon Nov 2, 2015
Perhaps it's stating the obvious but the
.localAddressoption binds to an address, not an interface. There is no direct support for SO_BINDTODEVICE in node.js but you can jury-rig it with socat(1):With script.js looking something like this:
k1tzu commentedon Nov 2, 2015
I've found another workaround that works. Using ffi and call to setsockopt. But I wish I could do that with nodejs only.