how to determine socket interface type

女生的网名这么多〃 提交于 2019-12-13 03:48:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!