如何确定套接字接口类型 [英] how to determine socket interface type

查看:111
本文介绍了如何确定套接字接口类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题的一些背景:

我最近在使用Microsoft和套接字绑定方面经历了近乎死亡的经历.我正在测试生产代码,其中一台测试PC一直失败.我最终建议bind()函数中存在错误,这是其中最古老的函数之一.

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.

由于我无法调试bind(),并且Microsoft拒绝提供帮助,因此我在开发工作2周后终于发现,所涉及的测试计算机启用了Hyper-V,并且我的界面解析代码正在吸收这种虚假信息AF_INET地址,然后在其上解析,而不是在实际IP地址上.

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.

我不能期望我的客户仅仅为了运行我的软件而关闭不必要的Hyper-V功能,因此我需要使我的解析代码更精细.

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.

我的问题:

我需要做的是能够获得特定地址的详细名称,例如ipconfig /all.我可以看到其他人问了这个问题,例如Ivan的如何确定接口名称和tez的Linux获取所有网络接口名称. Ivan的答案是使用getsockname(),而tez的答案是使用if_nameindex().两者都不返回如ipconfig中所示的详细接口名称.

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?

推荐答案

Remy Lebeau的答案是使用GetAdaptersAddresses.该函数的示例代码显示如何遍历套接字适配器接口并获取大量接口数据.要回答这个问题(即获得ipconfig般的冗长性),但是,您需要单独的IP地址.这些在LPSOCKADDR lpSockaddr成员中可用,这些成员是GetAdaptersAddresses调用的输出,其中一个示例是pUnicast->Address.lpSockaddr.

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.

这篇关于如何确定套接字接口类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆