简体   繁体   English

查找当前连接的网络接口/适配器 Windows

[英]Finding current connected network interface/adaptor Windows

I'm thinking there must be a way to ask windows for information about the network adaptor of the current connected network (available unicast/multicast, is it Wi-Fi, the name, etc)我在想一定有办法向 windows 询问有关当前连接网络的网络适配器的信息(可用的单播/多播,是 Wi-Fi,名称等)

When I say connected, I mean like the current Wi-Fi connection like windows shows you in the Wi-Fi options - the definition of connected is probably different in the networking world当我说连接时,我的意思是像 windows 这样的当前 Wi-Fi 连接在 Wi-Fi 选项中显示 - 连接的定义在网络世界中可能不同

Even if it's just possible the interface index, because It's easy to look up most other things using GetAdaptersAddresses() etc即使它只是可能的接口索引,因为使用GetAdaptersAddresses()等查找大多数其他内容很容易

In case this is an x/y problem: I'm trying to do this as part of writing an mdns client (for academic purposes, I know windows has an mdns api).如果这是一个 x/y 问题:我正在尝试将其作为编写 mdns 客户端的一部分(出于学术目的,我知道 windows 有一个 mdns api)。 I'd like to only broadcast and receive on the current Wi-Fi network (for which I think you need to set the IP_ADD_SOURCE_MEMBERSHIP flag in setsockopt ) and I also need to then know which IP address to return to the mdns response我只想在当前的 Wi-Fi 网络上广播和接收(我认为您需要在setsockopt中设置IP_ADD_SOURCE_MEMBERSHIP标志),然后我还需要知道返回 mdns 响应的 IP 地址

I could set IP_ADD_MEMBERSHIP but then I would still need to find out which IP to return and everything just becomes conceptually easier if things work on a single network (or so I thought)我可以设置IP_ADD_MEMBERSHIP但是我仍然需要找出返回哪个 IP 如果事情在单个网络上工作(或者我认为),那么一切在概念上都会变得更容易

The GetAdaptersAddresses will give you the list of network interfaces on the system and will tell you what type of interface each of them is. GetAdaptersAddresses将为您提供系统上的网络接口列表,并告诉您每个接口的类型。

In the returned IP_ADAPTER_ADDRESSES list, the IfType field tells you the type of the interface, which for wireless will be IF_TYPE_IEEE80211 .在返回的IP_ADAPTER_ADDRESSES列表中, IfType字段告诉您接口的类型,无线接口的类型为IF_TYPE_IEEE80211 Then when you find an interface of this type, you can iterate through the list of assigned addresses via the FirstUnicastAddress member to join the relevant multicast groups.然后,当您找到该类型的接口时,您可以通过FirstUnicastAddress成员遍历分配的地址列表以加入相关的多播组。

IP_ADAPTER_ADDRESSES *head, *curr;
IP_ADAPTER_UNICAST_ADDRESS *uni;
int buflen, err, i;

buflen = sizeof(IP_ADAPTER_UNICAST_ADDRESS) * 500;  //  enough for 500 interfaces
head = malloc(buflen);
if (!head) exit(1);
if ((err = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, head,
                                &buflen)) != ERROR_SUCCESS) {
    char errbuf[300];
    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
                  0, errbuf, sizeof(errbuf), NULL);
    printf("GetAdaptersAddresses failed: (%d) %s", err, errbuf);
    free(head);
    exit(1);
}
for (curr = head; curr; curr = curr->Next) {
    if (curr->IfType != IF_TYPE_IEEE80211) continue;
    for (uni = curr->FirstUnicastAddress; uni; uni = uni->Next) {
        if (curr->OperStatus == IfOperStatusUp) {
            char addrstr[INET6_ADDRSTRLEN];

            inet_ntop(uni->Address.lpSockaddr->sa_family, uni->Address.lpSockaddr, 
                      addrstr, uni->Address.iSockaddrLength);
            printf("interface name: %s\n", curr->AdapterName);
            printf("interface address: %s\n", addrstr);
        }
    }
}
free(head);

An important note here is that there may be more that one wireless interface active.这里的一个重要注意事项是,可能有不止一个无线接口处于活动状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM