[TriLUG] C & sockets programming question

Rick DeNatale rick.denatale at gmail.com
Wed Jul 27 18:58:57 EDT 2005


On 7/27/05, Michael Tharp <gxti at partiallystapled.com> wrote:
> Brian Weaver wrote:
> 
> >You can get the remote address (getpeername(2)), but as to which
> >interface (lo, eth0, eth1) the packet was received on... not sure
> >there is any easy way.
> >
> >-Brian
> >
> >
> getsockname() is what you want, assuming you want the local interface name.
> Then maybe do black magic with /sbin/route? There's probably a way to do
> it portably,
> but I don't know it.

I don't claim to be an expert, but I think that there is a basic
misconception about the relationship between sockets and interfaces
here. As I understand it a socket represents an end point. An
interface is a connection to the outside. If there are multiple
interfaces, it's perfectly possible for packets to flow to and from a
given socket through different interfaces.  In otherwords, there is a
little network routing "cloud" between the socket and the interface
inside the network stack within the OS.  While it's possible to bind
certain types of sockets to particular interfaces, it's not normally
done.

If I'm wrong here correct me, I'm here to learn.



More information about the TriLUG mailing list