Networking Forums

Networking Forums > Computer Networking > Linux Networking > Source IP / INADDR_ANY

Reply
Thread Tools Display Modes

Source IP / INADDR_ANY

 
 
André Hänsel
Guest
Posts: n/a

 
      03-06-2007, 01:18 AM
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é

 
Reply With Quote
 
 
 
 
Tauno Voipio
Guest
Posts: n/a

 
      03-06-2007, 03:47 PM
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
 
Reply With Quote
 
André Hänsel
Guest
Posts: n/a

 
      03-06-2007, 04:33 PM
On 6 Mrz., 17:47, Tauno Voipio <tauno.voi...@INVALIDiki.fi> wrote:
> 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 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.


As I wrote:
But what if there are just a few IP aliases that point all to the
same
net.

I have for example:
eth0 192.168.0.1
eth0:1 192.168.0.2
eth0:2 192.168.0.3

So my routing table says:
Destination Gateway Genmask Flags Metric Ref
Use Iface
gateway * 255.255.255.255 U 0 0 0 eth0
default gateway 0.0.0.0 UG 0 0 0 eth0
default gateway 0.0.0.0 UG 0 0 0 eth0
default gateway 0.0.0.0 UG 0 0 0 eth0

I don't know whether the three routing table entries are because of
the three interfaces but I suppose so.

Regards,
André

 
Reply With Quote
 
Tauno Voipio
Guest
Posts: n/a

 
      03-06-2007, 04:59 PM
André Hänsel wrote:
> On 6 Mrz., 17:47, Tauno Voipio <tauno.voi...@INVALIDiki.fi> wrote:
>
>>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 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.

>
>
> As I wrote:
> But what if there are just a few IP aliases that point all to the
> same
> net.
>
> I have for example:
> eth0 192.168.0.1
> eth0:1 192.168.0.2
> eth0:2 192.168.0.3


You must not have several addresses in one host
and the same subnet.

What are you trying to achieve with the aliases?

The system picks the address which it first meets
in the routing table.

> So my routing table says:
> Destination Gateway Genmask Flags Metric Ref
> Use Iface
> gateway * 255.255.255.255 U 0 0 0 eth0
> default gateway 0.0.0.0 UG 0 0 0 eth0
> default gateway 0.0.0.0 UG 0 0 0 eth0
> default gateway 0.0.0.0 UG 0 0 0 eth0
>
> I don't know whether the three routing table entries are because of
> the three interfaces but I suppose so.


This is a mess. Please post the results of

route -n

and

ifconfig -a

There should be only one default route, and it has to point
to the next hop router towards the Internet.

--

Tauno Voipio
tauno voipio (at) iki fi
 
Reply With Quote
 
André Hänsel
Guest
Posts: n/a

 
      03-07-2007, 04:21 AM
On 6 Mrz., 18:59, Tauno Voipio <tauno.voi...@INVALIDiki.fi> wrote:
> André Hänsel wrote:
> > On 6 Mrz., 17:47, Tauno Voipio <tauno.voi...@INVALIDiki.fi> wrote:

>
> >>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 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.

>
> > As I wrote:
> > But what if there are just a few IP aliases that point all to the
> > same
> > net.

>
> > I have for example:
> > eth0 192.168.0.1
> > eth0:1 192.168.0.2
> > eth0:2 192.168.0.3

>
> You must not have several addresses in one host
> and the same subnet.
>
> What are you trying to achieve with the aliases?


They are for different services, so the services can move from one
host to another without changing their ip. I really wasn't aware of
the fact that this is prohibited by some specification.

So if this is so, how can I achieve quite common tasks as:

- Have different IPs for different SSL certificates on a web server
- Put several VServers with different IPs on one host
- Combine two services with different IPs on one host

Regards,
André

 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Discover Source IP wilson_eldritch Linux Networking 1 05-14-2008 09:09 PM
INADDR_ANY Wenfei Linux Networking 1 02-15-2006 04:36 PM
Pros and Cons of using INADDR_ANY vs the ip addr of the machine mshetty@mail.com Linux Networking 1 12-08-2005 03:20 AM
packets with source ip 0.0.0.0 linux.lover2004@gmail.com Linux Networking 7 05-05-2005 06:45 AM
Source of microfilters? Tim \(Remove NOSPAM. Home Networking 9 01-27-2005 03:51 PM



1 2 3 4 5 6 7 8 9 10 11