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)
The getsockname function returns socket name ie 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 (ie 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.