[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Selecting source ip

On Wed, May 28, 2003 at 11:02:27PM +0200, Stefan Neufeind wrote:
> when I request a file from a linux box using lynx the source ip of my 
> request is the ip of eth0. But how can I change the source ip to 
> other ips which are also bound to eth0 (via eth0:0 etc.)? Do I need 
> to change the default gate from eth0 to eth0:0 for example? Isn't 
> there a way to just change the source ip for all programs I will 
> execute in my current environment / bash without affecting other 
> programs / tasks?

Not without rewriting the programs.

In geekese, the programs open their socket with code like this:

    my_addr = sa_zero;
    my_addr.sin_family = AF_INET;
    my_addr.sin_addr.s_addr = INADDR_ANY;
    if ( ( bind( fd, (struct sockaddr *) &my_addr, 
				       sizeof(struct sockaddr_in))) < 0 ) {
	fprintf(stderr,"bind failed\n");

(Pretty standard socket code...)

The catch is the 'INADDR_ANY', which tells the kernel "hey, just grab
any handy IP and use that!".  On most systems (not just Linux, most 
Unix systems and probably Windows as well), that means "use the IP
nearest to the destination", or for single-homed machines, the IP
of eth0 or whatever your default gateway sits on.

This value is filled in by the kernel, in other words.

You could change how it behaves be swapping eth0 and eth0:1, assuming
they are both on the same subnet.

                          | Scratch out the pin holes, open up the sores
brian moore <bem@rom.org> | Don't look out the window, white hatred's
                          |     at the door.
                          |   -- the residents

Reply to: