Reputation: 953
I have been trying to figure this out for a while now. Given a Linux socket, how can I
In short I have a machine with multiple network cards and a socket that is only bound to a subset of these cards. I want to programmatically detect and log these interfaces after they have been bound else where in our libraries meaning I don't have access to this infnormation at the time of binding.
I should also point out that I need an IPV4/6 compatible solution preferably for that works with SCTP sockets (though I'm fine with UDP or TCP solutions for now)
Upvotes: 3
Views: 2880
Reputation: 13690
The getsockname function returns socket name i.e. the local socket address. Just call this function for a unbound and a bound socket to determine your socket implementations behavior.
EDIT:
When you have an AF_INET (i.e. IPv4) socket, the member sin_addr.S_un.S_addr
is identical to the appropriate member in the SOCKADDR_IN
struct that you have passed to the bind
function. So if you have bound to INADDR_ANY
you get INADDR_ANY
when the socket isn't bound to a specific network adapter.
I checked this with the socket implementation of my OS (Windows 7). If your implementation returns something else, you might experience an undefined behavior caused by a bug somewhere else. Of course you can verify the implementation of the network stack if you have the source code available.
There is no support for binding to a subset of network adapters. You can only bind to one or all.
Upvotes: 3