2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2014-03-06 18:09:12 +00:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <cctype>
|
2015-09-28 15:57:16 +00:00
|
|
|
#include <cstring>
|
2014-03-06 18:09:12 +00:00
|
|
|
#include <ctime>
|
2016-01-31 13:03:55 +00:00
|
|
|
#include <random>
|
2014-03-06 18:09:12 +00:00
|
|
|
|
|
|
|
#include "Common/Network.h"
|
|
|
|
#include "Common/StringUtil.h"
|
2016-01-31 13:03:55 +00:00
|
|
|
#include "Common/Timer.h"
|
2014-03-06 18:09:12 +00:00
|
|
|
|
2017-01-18 00:19:20 +00:00
|
|
|
namespace Common
|
|
|
|
{
|
2014-03-06 18:09:12 +00:00
|
|
|
void GenerateMacAddress(const MACConsumer type, u8* mac)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
memset(mac, 0, MAC_ADDRESS_SIZE);
|
2014-03-06 18:09:12 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
u8 const oui_bba[] = {0x00, 0x09, 0xbf};
|
|
|
|
u8 const oui_ios[] = {0x00, 0x17, 0xab};
|
2014-03-06 18:09:12 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
switch (type)
|
|
|
|
{
|
2017-01-18 00:19:20 +00:00
|
|
|
case MACConsumer::BBA:
|
2016-06-24 08:43:46 +00:00
|
|
|
memcpy(mac, oui_bba, 3);
|
|
|
|
break;
|
2017-01-18 00:19:20 +00:00
|
|
|
case MACConsumer::IOS:
|
2016-06-24 08:43:46 +00:00
|
|
|
memcpy(mac, oui_ios, 3);
|
|
|
|
break;
|
|
|
|
}
|
2014-03-06 18:09:12 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Generate the 24-bit NIC-specific portion of the MAC address.
|
|
|
|
std::default_random_engine generator(Common::Timer::GetTimeMs());
|
|
|
|
std::uniform_int_distribution<int> distribution(0x00, 0xFF);
|
|
|
|
mac[3] = static_cast<u8>(distribution(generator));
|
|
|
|
mac[4] = static_cast<u8>(distribution(generator));
|
|
|
|
mac[5] = static_cast<u8>(distribution(generator));
|
2014-03-06 18:09:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string MacAddressToString(const u8* mac)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return StringFromFormat("%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4],
|
|
|
|
mac[5]);
|
2014-03-06 18:09:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StringToMacAddress(const std::string& mac_string, u8* mac)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
bool success = false;
|
|
|
|
if (!mac_string.empty())
|
|
|
|
{
|
|
|
|
int x = 0;
|
|
|
|
memset(mac, 0, MAC_ADDRESS_SIZE);
|
2014-03-06 18:09:12 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
for (size_t i = 0; i < mac_string.size() && x < (MAC_ADDRESS_SIZE * 2); ++i)
|
|
|
|
{
|
|
|
|
char c = tolower(mac_string.at(i));
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
{
|
|
|
|
mac[x / 2] |= (c - '0') << ((x & 1) ? 0 : 4);
|
|
|
|
++x;
|
|
|
|
}
|
|
|
|
else if (c >= 'a' && c <= 'f')
|
|
|
|
{
|
|
|
|
mac[x / 2] |= (c - 'a' + 10) << ((x & 1) ? 0 : 4);
|
|
|
|
++x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
success = x / 2 == MAC_ADDRESS_SIZE;
|
|
|
|
}
|
|
|
|
return success;
|
2014-03-06 18:09:12 +00:00
|
|
|
}
|
2017-01-18 00:19:20 +00:00
|
|
|
} // namespace Common
|