How to identify which MAC address is a WIFI MAC address, and which IP is a WIFI IP Address?
I have the following the functions:
This function will get every IP Addresses from the local machine
void GetIP()
{
WORD wVersionRequested;
WSADATA wsaData;
char name[ 255 ];
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 1, 1 );
char *ip;
if ( WSAStartup( wVersionRequested, &开发者_Go百科;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
And here is my second function, which will get every MAC Address from the local machine:
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
So my question is: How do i know if the queried MAC Address and the queried IP Address belongs to a WiFi?
Thanks!
That code you have for querying MAC addresses, also fetches the interface type.
Starting with Vista, for a WiFi interface, the Type
field will be IF_TYPE_IEEE80211
You can also identify dial-up (MIB_IF_TYPE_PPP
) connections and loopback (MIB_IF_TYPE_LOOPBACK
) virtual interfaces.
As Remy says, you should pull the IP addresses out of that same data structure (there's an IpAddressList
field).
You can use GetAdaptersInfo()
and GetAdaptersAddresses()
to get the IPs as well as MACs. I recently discovered that, although commonly used (even my me), using gethostname()
and gethostbyname()
can return misleading data at times since gethostbyname()
is a DNS lookup and can return addresses that are not actually local if the hostname has additional DNS entries configured (load balancing, etc). GetAdaptersInfo()
and GetAdaptersAddresses()
only return local settings instead.
In any case, the APIs do not expose info about whether a network adapter is LAN, WAN, WiFi, etc. The OS doesn't really need that info, and neither should you. All it cares about is that a given adapter provides access to a given network. How it does that is a private implementation detail and device-specific. You can interchangeably use adapters without worrying about what types they are.
精彩评论