Re: Source IP / INADDR_ANY



André Hänsel wrote:
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. ;)


The operating system will find a free port and use it.

The IP used is the IP address associated with the interface
which handles the connection.

So, the local in a multi-homed host depends on the target
address and the routing tables.

--

Tauno Voipio
tauno voipio (at) iki fi
.



Relevant Pages

  • Re: IP address aliases
    ... |> One fix we need is the ability to bind a subnet to an interface _without_ ... |> binding an address (to that interface). ... |> If a process has a socket already bound, connected or listen, to an IP ... | to wait forever for something to happen on this connection. ...
    (comp.os.linux.development.system)
  • Re: Detecting virtual IP addresses
    ... since the only "error" is a connection ... A network interface as seen by the application - virtual or otherwise ... binding is an inherent part of every Socket connection (even though ... Have a look at your routing table, and try using ethereal to see what ...
    (comp.lang.java.programmer)
  • Re: LAN and GPRS
    ... To solve this problem IP Helpers will do one thing. ... > to indicate which interface is the preferred interface. ... > and you will not want to change sending sockets until the connection is no ... Sounds to me like you need a socket pool. ...
    (microsoft.public.win32.programmer.networks)
  • Re: Connection Manager Questions
    ... You can have only one active connection request per process. ... all sockets will be automatically bound to this interface. ... before you tried to connect a socket. ... It looks like Connection Manager is the api i need to use. ...
    (microsoft.public.pocketpc.developer)
  • [PATCH 0/5] [RFC] AF_RXRPC socket family implementation [try #3]
    ... These patches together supply secure client-side RxRPC connectivity as a Linux ... kernel socket family. ... presentation side is left to the client. ... Each connection goes to a particular "service". ...
    (Linux-Kernel)