如何识别哪个MAC地址是WIFI MAC地址,哪个IP是WIFI IP地址?

时间:2011-09-16 19:48:19

标签: c windows

我有以下功能:

此功能将从本地计算机获取每个IP地址

void GetIP()
{
WORD wVersionRequested;
WSADATA wsaData;
char name[ 255 ];
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 1, 1 );
char *ip;

if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
{
    if( gethostname ( name, sizeof( name ) ) == 0 )
    {
        if ( ( hostinfo = gethostbyname( name ) ) != NULL )
        {
            int nCount = 0;
            while ( hostinfo->h_addr_list[ nCount ] )
            {
                ip = inet_ntoa( *(struct in_addr *)hostinfo->h_addr_list[ nCount ] );

                //printf( "IP #%d: %s\n", ++nCount, ip );
                printf( "IP                 : %s\n", ip );
                ++nCount;
            }
        }
    }
}
}//GetIP

这是我的第二个函数,它将从本地机器获取每个MAC地址:

void GetMACaddress()
{
IP_ADAPTER_INFO AdapterInfo[ 16 ];              // Allocate information for up to 16 NICs
DWORD dwBufLen = sizeof( AdapterInfo );         // Save the memory size of buffer

DWORD dwStatus = GetAdaptersInfo(               // Call GetAdapterInfo
                                        AdapterInfo,// [out] buffer to receive data
                                        &dwBufLen   // [in] size of receive data buffer
                                    );  

    assert( dwStatus == ERROR_SUCCESS );            // Verify return value is valid, no buffer overflow

    PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;    // Contains pointer to current adapter info

    do 
    {
        printf( "MAC                : %02X:%02X:%02X:%02X:%02X:%02X\n", pAdapterInfo->Address[ 0 ], 
                                                                    pAdapterInfo->Address[ 1 ], 
                                                                    pAdapterInfo->Address[ 2 ], 
                                                                    pAdapterInfo->Address[ 3 ], 
                                                                    pAdapterInfo->Address[ 4 ], 
                                                                    pAdapterInfo->Address[ 5 ]  );
                    pAdapterInfo = pAdapterInfo->Next;          // Progress through linked list

    }while( pAdapterInfo );                         // Terminate if last adapter
}//GetMACaddress

所以我的问题是: 我如何知道查询的MAC地址和查询的IP地址是否属于WiFi?

谢谢!

2 个答案:

答案 0 :(得分:1)

您查询MAC地址的代码也会提取接口类型。

从Vista开始,for a WiFi interface, the Type field will be IF_TYPE_IEEE80211您还可以识别拨号(MIB_IF_TYPE_PPP)连接和环回(MIB_IF_TYPE_LOOPBACK)虚拟接口。

作为Remy says,您应该从相同的数据结构中提取IP地址(有一个IpAddressList字段)。

答案 1 :(得分:0)

您可以使用GetAdaptersInfo()GetAdaptersAddresses()来获取IP和MAC。我最近发现,尽管常用(甚至是我),使用gethostname()gethostbyname()有时会返回误导性数据,因为gethostbyname()是DNS查找,并且可以返回实际上不是的地址如果主机名配置了其他DNS条目(负载平衡等),则为local。 GetAdaptersInfo()GetAdaptersAddresses()仅返回本地设置。

在任何情况下,API都不会公开有关网络适配器是LAN,WAN,WiFi等的信息。操作系统并不真正需要这些信息,您也不应该这样做。它所关心的是给定的适配器提供对给定网络的访问。它是如何实现私有实现细节和特定于设备的。您可以互换使用适配器,而无需担心它们的类型。