how to tell a socket to route thru a specific interface...

Philip George invest at juun.com
Fri May 20 20:10:47 UTC 2005


How can I specify that a socket use a specific interface?

If I have a wi-fi connection to one network and a cat5 connection to 
another, how can i tell my socket to route through a specific one (and 
not even try the other)?

To be clear, I'm talking about an outgoing connection.  For example, I 
use send() a few lines after what is shown here to send a web request 
and recv() to get the requested page.  The catch being that I need to 
specify that the request be routed through a specific interface, not 
just any old interface (assuming that at runtime there are 2 or more 
available connections from the computer out to the internet).

I thought it was bind() that did this, but it doesn't seem like that's 
what it's doing at all.  I have a book that led me to believe bind() 
would tell a socket to route through a specific interface (thus BINDing 
it to the socket), but I think it's more likely that it's a dns-related 
call.  unfortunately, the man page is strangely ambiguous about what it 
actually does, using the word 'name' a lot, but never actually saying 
'hostname'.

In any case, here's the code i've got that isn't working (although I'm 
pretty sure the setsockopt() line is okay) :

// setup local address stuff...

struct sockaddr_in LOCaddr;
LOCaddr.sin_family = AF_INET;
LOCaddr.sin_addr.s_addr = inet_addr(local_ip);


// set to true for SO_DONTROUTE...

int val = 1;


// bind to specific interface and then set SO_DONTROUTE...

if (bind(sockethandle, (struct sockaddr *)&LOCaddr, 
sizeof(LOCaddr))==0) {
	setsockopt(sockethandle, SOL_SOCKET, SO_DONTROUTE, &val, sizeof(val));
}



What do I need to do here instead of bind()?

Thanks.

- Philip






More information about the fedora-devel-list mailing list