问题
A little background for the question:
I recently had a near-death experience with Microsoft and socket binding. I was testing production code and one of the test PCs kept failing. I eventually suggested that there was a bug in the bind()
function, one of the oldest functions out there.
I submitted the error (see CAsyncSocket Create's bind implementation has a bug in Visual Studio's Developer Community feedback forum), but got handed off to the Windows team (see "__imp_bind implementation has a long-standing situational bug in it" in their Feedback Hub), who never did anything about it.
Since I couldn't debug bind()
, and Microsoft refused to assist, I eventually discovered after 2 weeks of dev work, that the test machine in question had Hyper-V enabled and my interface resolution code was picking up this spurious AF_INET
address and resolving on it instead of on the real IP address.
I can't expect my customers to turn off the unwanted Hyper-V feature just to run my software, so I need to make my resolution code more granular.
My question:
What I need to do is to be able to get a particular address's verbose name, like for ipconfig /all
. I can see that others have asked this question, like Ivan's how to determine interface name and tez's Linux getting all network interface names. Ivan's answer was to use getsockname()
and tez's answer was to use if_nameindex()
. Neither one of which returns the verbose interface name as seen in ipconfig
.
So, does someone know what function call that would be?
回答1:
Remy Lebeau
's answer was to use GetAdaptersAddresses
. The sample code for that function shows how to iterate through socket adapter interfaces and get copious amounts of interface data. To answer the question (ie, get ipconfig
-like verbosity), however, you need the individual ip addresses. These are available in the LPSOCKADDR lpSockaddr
members, which are outputs of the GetAdaptersAddresses
call, an example of which is pUnicast->Address.lpSockaddr
.
来源:https://stackoverflow.com/questions/57732163/how-to-determine-socket-interface-type