Re: how to tell a socket to use a specific interface...
Re: how to tell a socket to use a specific interface...
- Subject: Re: how to tell a socket to use a specific interface...
- From: Justin Walker <email@hidden>
- Date: Sun, 22 May 2005 11:51:21 -0700
On May 22, 2005, at 11:22, Igor Garnov wrote:
Hello everybody
Perhaps you need to spend some time with a book like W. Richard
Stevens' "Unix Network Programming, v1, 2ed (or 3ed)". First, what
do you mean "code ... gets autorouted"? Second, bind() has *no*
effect on routing. Routing is dictated by the destination address of
the packet you want to be sent.
The comment may be old, but just in case: you *can't* bind to an
interface.
But then could somebody imagine a situation when calling 'bind' before
'connect' makes some sense?
You *must* call bind() before connect() if you call it at all.
Remember that bind() assigns address info to *your* end of the
'connection' you want to establish. Once you call connect(), the
kernel will fill in the missing parts, and if you call bind() then, you
will get an error (EINVAL).
I'm asking because I thought that if I call 'bind' before
'connect'ing, the outgoing packets will have the IP address in their
headers, to which I have 'bound' the socket. If I don't 'bind' the
socket that I'll use to connect to a server, the kernel will
automatically 'bind' this socket for me.
That is correct;
So, in my opinion 'bind'ing a socket before 'connect'ing it to a
remote server is only worth it when the remote server has some kind of
restriction on the address of incoming connections. Then if I don't
'bind' the socket to the IP address which I know is trustworthy from
the server's point of view, my kernel may bind this socket to another
IP, and the remote server will refuse the connection.
There's relatively little that a bind() can do to make a remote server
happy. Unless you have something in mind that I don't understand
(Nah...), the only real reason to call bind() on a socket that you will
call connect() on is to deal with the possibility that routing is not
enabled on your system, and you have multiple interfaces to choose
from.
In this one case, you would want to bind to an address associated to an
interface that is "in the direction" of your destination. Given the
complex routing setups that could occur (adding a lot of host routes,
for example :-}), this could be necessary to overcome bad decisions in
the routing engine.
Other opinions solicited...
Cheers,
Justin
--
Justin C. Walker, Curmudgeon-At-Large
Institute for General Semantics
--------
"Weaseling out of things is what separates us from the animals.
Well, except the weasel."
- Homer J Simpson
--------
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden