Choose from multiple IP's for outgoing request

1,687 views
Skip to first unread message

jknair

unread,
Aug 19, 2011, 2:26:01 AM8/19/11
to nod...@googlegroups.com
Hey I am trying to use specific ip (not the primary ip) from the multiple ip's bound to the machine for my outgoing requests. any idea how I can do this using node ?

mscdex

unread,
Aug 19, 2011, 3:09:27 AM8/19/11
to nodejs
For node v0.4.x, you can apply this patch to allow binding to specific
addresses for requests: https://gist.github.com/9476d3d2fc4fbf61a878

It adds an extra parameter called 'local' to the request's options
object:

var req = http.request({
host: 'foo',
path: '/',
method: 'GET',
local: '192.168.100.10'
}, function(res) {
// do something fun
});

req.end();

jknair

unread,
Aug 19, 2011, 4:57:48 AM8/19/11
to nod...@googlegroups.com
so can i get all the network addresses that are bound on the machine using this patch ??? 

jknair

unread,
Nov 27, 2011, 3:55:20 AM11/27/11
to nod...@googlegroups.com
hey mscdex,

have you worked out a patch for the v0.6.x ? Cos the source has changed so much from 0.4.12, I am not able to figure out the binding calls to the socket to pass the local interface as an argument

Maarten

unread,
Nov 28, 2011, 2:37:53 AM11/28/11
to nodejs

Thomas Shinnick

unread,
Nov 30, 2011, 8:26:13 PM11/30/11
to nod...@googlegroups.com, jknair, Maarten
This is to help stimulate some discussion on the general problem of binding sockets for outgoing connections.  But I'm also posting a temporary answer to jknair's need.

jknair's desire was to bind an outgoing HTTP connection to a particular source address.  This is reasonable and necessary at times when a machine is multi-homed, having more than one interface and/or network address.  For instance, sometimes firewall rules can be quite picky about source/destination address pairs.  If you aren't coming from the correct interface your traffic will be rejected.

node currently only handles binding _server_ addresses.  A server must be bound to a particular address(es) in order to listen for incoming connections from that address(es).  That there are dropout rules which allow one to just say "any address on this machine" does not change that a bind must be done, it just may not be obvious what addresses those are.

Maarten and I had a need for binding other outgoing sockets besides HTTP client requests. 
(see https://groups.google.com/d/topic/nodejs/PwY1uiPUSoM/discussion)

Again, there is no provision by node for generally binding sockets.  The net.js/http.js code has not been 'shaped' by the need, yet.

But Maarten threw out a wild idea (hack) for getting around the difficulty that binding is only available for servers.  He suggested using part of the internal API for server, looking inside net.Server.listen to find the call to net._createServerHandle() that does the actual calls to bind()/bind6(). 

I've used that idea plus pawing through http.js to come up with a temporary answer for jknair.  I hope it is forgivable.

Please check gist "Example of binding socket for nodejs HTTP client request (for discussion only)"
    https://gist.github.com/1412297

I noticed that http.js Agent() had a hook for socket creation, self.createConnection.  (Thank you @bnoordhuis)  Together with Maarten's idea I created a derived Agent that uses its own createConnection() to force binding of new client sockets.  This is found in lines 39-60 of the gist, and then enabled in line 69.  See the third part of the gist for output showing the bind working on the client socket.

This idea would 'work' for jknair, at least until something changes within net.js to disable Maarten's workaround.  But the acrobatics required are rather unsatisfying....  Any reasons not to work towards a better implementation for binding outgoing sockets?

Ben Noordhuis

unread,
Dec 1, 2011, 7:26:40 AM12/1/11
to nod...@googlegroups.com

jknair

unread,
Dec 5, 2011, 6:06:33 AM12/5/11
to nod...@googlegroups.com, jknair, Maarten
hey Maarten,

sorry for the late reply. You workaround helps a lot , I agree with the solution being extremely hacky !!!  . I am trying to get this work for the URL's and also it helped me in understanding the source code better. Will post back again if i have any success in figuring out a proper method to deal with this.

Also prolly will be trying to write a rewrite of the request library to enable this same feature.

Lucas Jans

unread,
Sep 10, 2013, 9:37:32 AM9/10/13
to nod...@googlegroups.com, jknair, Maarten
I realize this is an old thread. I'm wondering if there's a better way to do this with the current API?

Jérémy Lal

unread,
Sep 10, 2013, 10:20:20 AM9/10/13
to nod...@googlegroups.com, Lucas Jans, jknair, Maarten
yes now there is a straightforward way to do this.
read the documentation :)

On 10/09/2013 15:37, Lucas Jans wrote:
> I realize this is an old thread. I'm wondering if there's a better way to do this with the current API?
>
> On Monday, December 5, 2011 3:06:33 AM UTC-8, jknair wrote:
>
> hey Maarten,
>
> sorry for the late reply. You workaround helps a lot , I agree with the solution being extremely hacky !!! . I am trying to get this work for the URL's and also it helped me in understanding the source code better. Will post back again if i have any success in figuring out a proper method to deal with this.
>
> Also prolly will be trying to write a rewrite of the request library to enable this same feature.
>
>
> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> *Agency Revolution <http://www.agencyrevolution.com/>* | Knowledge Base Articles <https://support.agencyrevolution.com/home> | Navigator <http://www.agencyrevolution.com/members/quantum-club-2.0> | Support Forum <https://support.agencyrevolution.com/categories/20036027-Forums> | Support Ticket <https://support.agencyrevolution.com/requests/anonymous/new> | 541 330-2300 |
>
> --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

mscdex

unread,
Sep 10, 2013, 12:08:48 PM9/10/13
to nod...@googlegroups.com, jknair, Maarten
On Tuesday, September 10, 2013 9:37:32 AM UTC-4, Lucas Jans wrote:
I realize this is an old thread. I'm wondering if there's a better way to do this with the current API?

Reply all
Reply to author
Forward
0 new messages