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.
|
|
|
|
|
2018-06-10 18:32:33 +00:00
|
|
|
#include "Common/Network.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2014-03-06 18:09:12 +00:00
|
|
|
#include <cctype>
|
2015-09-28 15:57:16 +00:00
|
|
|
#include <cstring>
|
2014-03-06 18:09:12 +00:00
|
|
|
#include <ctime>
|
|
|
|
|
2019-06-14 14:53:46 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2018-05-21 13:48:17 +00:00
|
|
|
#include "Common/Random.h"
|
2014-03-06 18:09:12 +00:00
|
|
|
|
2017-01-18 00:19:20 +00:00
|
|
|
namespace Common
|
|
|
|
{
|
2018-06-10 18:32:33 +00:00
|
|
|
MACAddress GenerateMacAddress(const MACConsumer type)
|
2014-03-06 18:09:12 +00:00
|
|
|
{
|
2018-06-10 18:32:33 +00:00
|
|
|
constexpr std::array<u8, 3> oui_bba{{0x00, 0x09, 0xbf}};
|
|
|
|
constexpr std::array<u8, 3> oui_ios{{0x00, 0x17, 0xab}};
|
2014-03-06 18:09:12 +00:00
|
|
|
|
2018-06-10 18:32:33 +00:00
|
|
|
MACAddress mac{};
|
2014-03-06 18:09:12 +00:00
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
2017-01-18 00:19:20 +00:00
|
|
|
case MACConsumer::BBA:
|
2018-06-10 18:32:33 +00:00
|
|
|
std::copy(oui_bba.begin(), oui_bba.end(), mac.begin());
|
2014-03-06 18:09:12 +00:00
|
|
|
break;
|
2017-01-18 00:19:20 +00:00
|
|
|
case MACConsumer::IOS:
|
2018-06-10 18:32:33 +00:00
|
|
|
std::copy(oui_ios.begin(), oui_ios.end(), mac.begin());
|
2014-03-06 18:09:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-01-31 13:03:55 +00:00
|
|
|
// Generate the 24-bit NIC-specific portion of the MAC address.
|
2018-06-10 18:32:33 +00:00
|
|
|
Random::Generate(&mac[3], 3);
|
|
|
|
return mac;
|
2014-03-06 18:09:12 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 18:32:33 +00:00
|
|
|
std::string MacAddressToString(const MACAddress& mac)
|
2014-03-06 18:09:12 +00:00
|
|
|
{
|
2019-06-14 14:53:46 +00:00
|
|
|
return fmt::format("{: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
|
|
|
}
|
|
|
|
|
2018-06-10 18:32:33 +00:00
|
|
|
std::optional<MACAddress> StringToMacAddress(const std::string& mac_string)
|
2014-03-06 18:09:12 +00:00
|
|
|
{
|
2018-06-10 18:32:33 +00:00
|
|
|
if (mac_string.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
int x = 0;
|
|
|
|
MACAddress mac{};
|
2014-03-06 18:09:12 +00:00
|
|
|
|
2018-06-10 18:32:33 +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')
|
2014-03-06 18:09:12 +00:00
|
|
|
{
|
2018-06-10 18:32:33 +00:00
|
|
|
mac[x / 2] |= (c - 'a' + 10) << ((x & 1) ? 0 : 4);
|
|
|
|
++x;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2014-03-06 18:09:12 +00:00
|
|
|
}
|
2018-06-10 18:32:33 +00:00
|
|
|
|
|
|
|
// A valid 48-bit MAC address consists of 6 octets, where each
|
|
|
|
// nibble is a character in the MAC address, making 12 characters
|
|
|
|
// in total.
|
|
|
|
if (x / 2 != MAC_ADDRESS_SIZE)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return std::make_optional(mac);
|
2014-03-06 18:09:12 +00:00
|
|
|
}
|
2017-01-18 00:19:20 +00:00
|
|
|
} // namespace Common
|