Re: how to tell a socket to use a specific interface...
Re: how to tell a socket to use a specific interface...
- Subject: Re: how to tell a socket to use a specific interface...
- From: "Peter Lovell" <email@hidden>
- Date: Fri, 20 May 2005 17:14:02 -0400
>How can I specify that a socket use a specific interface?
>
>I thought it was bind() that did this, but it doesn't seem like that's
>what it's doing at all.
>
>If I have an airport connection to one network and a cat5 connection to
>another, how can i tell my socket to use a specific one.
>
>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 drval = 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));
>}
>
>
>Now that I'm looking at this isolated code, it seems more and more
>obvious that bind() isn't what i want. Haha. Now that I think about
>it, I guess it's probably more likely related to the dns server called
>'bind'.
>
>In any case... what is it that I need to do here?
If you want to use a specific interface then you need to bind your socket
to that interface's IP address, rather than having the routing mechanism
pick one for you.
That means of course that the address + interface needs to have a route
somehow to your destination, but that's an exercise for a different day.
Note that the "bind" referred to in DNS terms is NOT the "bind" used for
sockets. So your code should first open a socket, then use the "bind"
call to specify a sending IP address (and therefore a sending interface),
and then start to send.
Cheers.....Peter
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden