I’ve got an interface with multiple IP addresses and I’m looking to
make HTTP requests from the non-primary IP.How would I pull that off?
In principle, you ‘bind’ the socket to the correct local IP address
before
you ‘connect’
Unfortunately, TCPSocket#open (or #new) does the connect before it gives
you
a chance to bind. So you’ll need to replace TCPSocket.open with
something
messy along the lines of
s = Socket.new(Socket::PF_INET, Socket::SOCK_STREAM,
Socket::IPPROTO_TCP)
s.bind(Socket.pack_sockaddr_in(0, “1.2.3.4”)) # chosen local IP
s.connect(Socket.pack_sockaddr_in(80, “pragprog.com”))
Test this and check that it works at the TCP layer, e.g. by adding
s.write("GET / HTTP/1.0\r\n\r\n")
puts s.read
If that works, then unfortunately net/http.rb doesn’t seem to have a
suitable hook, so copy the source and look for TCPSocket.open to see
where
you need to modify it.
Good luck,
Brian.