netdev
[Top] [All Lists]

Re: How to determine source ip address

To: Padraig Brady <padraig@xxxxxxxxxxxxx>
Subject: Re: How to determine source ip address
From: "Christopher Friesen" <cfriesen@xxxxxxxxxxxxxxxxxx>
Date: Mon, 10 Sep 2001 16:42:44 -0400
Cc: netdev@xxxxxxxxxxx
References: <3B9D1325.8030304@antefacto.com>
Sender: owner-netdev@xxxxxxxxxxx
Padraig Brady wrote:

> I'm writing a network app (currently on linux 2.2.18,
> but soon moving to 2.4) that needs to build up ip &
> tcp packets manually. So my question is how do I determine
> the source ip address to use for a given destination
> ip address. I.E. take account of multiple interfaces/routes/
> aliases/...

How low in the protocol stack are you going?  If you use an IP raw socket with
IP_HDRINCL and just leave the sending address filled with zeros, then it will be
filled in with the default address for the link used to send to the destination
address.  See the man page on raw(4) for more details.

> If a port number is not allocated with the above connect(),
> how do I manually allocate a source port, bind()?

Yes, bind() works.

Why do you care about source address?  I would suspect that most of the time you
want to either a) use the default, in which case you let the IP stack fill it
in, or b) use some wierd source address, in which case you're usually better off
using a config file or commandline parm.

Chris


-- 
Chris Friesen                    | MailStop: 043/33/F10  
Nortel Networks                  | work: (613) 765-0557
3500 Carling Avenue              | fax:  (613) 765-2986
Nepean, ON K2H 8E9 Canada        | email: cfriesen@xxxxxxxxxxxxxxxxxx

<Prev in Thread] Current Thread [Next in Thread>