18,356
社区成员
发帖
与我相关
我的任务
分享
// 判断MAC地址是否有效
bool peerid::invalid_mac_address(const BYTE* mac_ptr, const std::vector<std::string>& invalid_mac_v)
{
if (invalid_mac_v.empty())
{
return ((mac_ptr[0] == 0x44 && mac_ptr[1] == 0x45 && mac_ptr[2] == 0x53 && mac_ptr[3] == 0x54)
|| (mac_ptr[0] == 0xff && mac_ptr[1] == 0xff && mac_ptr[2] == 0xff && mac_ptr[3] == 0xff && mac_ptr[4] == 0xff && mac_ptr[5] == 0xff )
|| (mac_ptr[0] == 0x00 && mac_ptr[1] == 0x00 && mac_ptr[2] == 0x00 && mac_ptr[3] == 0x00 && mac_ptr[4] == 0x00 && mac_ptr[5] == 0x00 )
|| (mac_ptr[0] == 0x00 && mac_ptr[1] == 0x50 && mac_ptr[2] == 0x56 && mac_ptr[3] == 0xc0)
|| (mac_ptr[0] == 0x00 && mac_ptr[1] == 0xe0 && mac_ptr[2] == 0x06 && mac_ptr[3] == 0x09 && mac_ptr[4] == 0x55 && mac_ptr[5] == 0x66 )
|| (mac_ptr[0] == 0x02 && mac_ptr[1] == 0x00 && mac_ptr[2] == 0x4c && mac_ptr[3] == 0x4f && mac_ptr[4] == 0x4f && mac_ptr[5] == 0x50 )
/*
02004C4F4F50 :Microsoft Loopback Adapter
00e006095566
This was one of those SiS-chipset motherboards with the integrated NIC
whose MAC addresses are all the same -
//someone:
Miles said that if he installs the patch to enable the correct MAC address,
he wouldn't be charged. This is what he/she promised to do.
*/
//filter special mac:0250F23D0001 0600AF090C00
|| (mac_ptr[0] == 0x02 && mac_ptr[1] == 0x50 && mac_ptr[2] == 0xF2 && mac_ptr[3] == 0x3D && mac_ptr[4] == 0x00 && mac_ptr[5] == 0x01 )
|| (mac_ptr[0] == 0x06 && mac_ptr[1] == 0x00 && mac_ptr[2] == 0xAF && mac_ptr[3] == 0x09 && mac_ptr[4] == 0x0C && mac_ptr[5] == 0x00 ));
}
else
{
std::string mac_str;
for (unsigned i = 0; i < 6; i ++)
{
mac_str.append( char_2_hex( mac_ptr[i]) );
}
const char * mac_buffer = mac_str.c_str();
for (unsigned k = 0; k < invalid_mac_v.size(); k++)
{
unsigned j;
for(j = 0; j < 12; j ++)
{
if (mac_buffer[j] != invalid_mac_v[k][j] && invalid_mac_v[k][j] != 'N')
{
break;
}
}
if (j == 12)
{
return true;
}
}
return false;
}
}
// 获取MAC地址
bool peerid::get_mac_address(std::string& result)
{
result = "";
// 读取无效MAC地址列表
std::vector<std::string> filter_peerids;
unsigned filter_num = 0;
// 获取本机MAC地址
IP_ADAPTER_INFO Data[12];
PIP_ADAPTER_INFO pDatas = Data;
ULONG len = sizeof(IP_ADAPTER_INFO) * 12;
if ((GetAdaptersInfo(pDatas, &len)) == ERROR_SUCCESS)
{
for (int i=1; i<12; i++)
{
string desc = pDatas->Description;
if (desc.find("PPP", 0) != desc.npos
|| desc.find("VMare", 0) != desc.npos
|| desc.find("Virtual", 0) != desc.npos
|| desc.find("SLIP", 0) != desc.npos
|| desc.find("P.P.P", 0) != desc.npos
|| invalid_mac_address(pDatas->Address, filter_peerids) )
{
pDatas = pDatas->Next;
if (pDatas == NULL)
{
break;
}
}
else
{
for (int j=5; j>=0; j--)
{
result.append(1, pDatas->Address[j]);
}
return true;
}
}
}
return false;
}
IP_ADAPTER_ADDRESSES info_temp;
ULONG len = 0;
// First get number of networks.
ULONG result = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, &info_temp, &len);
if (result != ERROR_BUFFER_OVERFLOW)
{
// There are 0 networks.
return ;
}
scoped_array<char> buf(new char[len]);
IP_ADAPTER_ADDRESSES *adapters =
reinterpret_cast<IP_ADAPTER_ADDRESSES *>(buf.get());
result = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, adapters, &len);
if (result != NO_ERROR)
{
LOG(ERROR) << "GetAdaptersAddresses failed: " << result;
return;
}
for (IP_ADAPTER_ADDRESSES *adapter = adapters; adapter != NULL;adapter = adapter->Next)
{
if (adapter->IfType == IF_TYPE_PPP) //这里
{
}
}