Source IP / INADDR_ANY



Hi,

I'm wondering which IP address is used as the source IP of a TCP
connection.

I came across this issue when I was debugging problems with a box with
several IP aliases that could not connect to another box where it is
identified by its IP. (mysql_connect)

To connect libmysql.c calls socket() and then connect(), without any
bind() inbetween.

In ip(7) I found:
"When listen(2) or connect(2) are called on an unbound socket, it is
automatically bound to a random free port with the local address set
to INADDR_ANY."

So what does this INADDR_ANY mean? I guess, when there are multiple
interfaces but they point to different routes, the one which points to
my destination address is selected. (I did not verify this behaviour
yet.)
But what if there are just a few IP aliases that point all to the same
net. Googling for INADDR_ANY gave me (although unproven):
"When sending, a socket bound with INADDR_ANY binds to the default IP
address, which is that of the lowest-numbered interface."
However this is not the behaviour I see on my box, unless the
"interface numbering" is secretly rotated. ;)

Regards,
André

.



Relevant Pages

  • Re: Programming for two separate Ethernet interfaces
    ... To answer your questions, yes, you can enumerate adapters. ... You can also bind a socket, whether incoming or outgoing, to a given IP ... Ethernet interface in my CE machine. ...
    (microsoft.public.windowsce.app.development)
  • Re: UDP question.
    ... >> My two adapters has two different IP address, and I bind one IP ... >> do you mean that I alloc two socket and bind different IP is not ... > sending a packet *to* 1.2.4.5, it will go out the first interface. ... not real bind the adapter? ...
    (Linux-Kernel)
  • Re: UDP question.
    ... > do you mean that I alloc two socket and bind different IP is not ... it will go out the first interface. ... not real bind the adapter? ...
    (Linux-Kernel)
  • Re: UDP question.
    ... >> bind socket2 network adapter2 ... > I am not really sure, but I think the bind to an adapter under linux only ... To explicitly send things through a specific interface, ... all the packet sent from two socket is go out by one ...
    (Linux-Kernel)
  • Re: [PATCH} Network interface for IPMI
    ... af_ipmi - A network socket interface to IPMI. ... -Note that the KCS-only interface ahs been removed. ... struct sockaddr_ipmi saddr; ...
    (Linux-Kernel)