Common/Network: Get rid of out parameters for MAC address utilities

Given we have std::array and std::optional, we can use these in
conjunction with one another to avoid the need for out parameters.
This commit is contained in:
Lioncash 2018-06-10 14:32:33 -04:00
parent 70417c8d16
commit ce69201f33
9 changed files with 77 additions and 58 deletions

View file

@ -2,67 +2,75 @@
// Licensed under GPLv2+ // Licensed under GPLv2+
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include "Common/Network.h"
#include <algorithm>
#include <cctype> #include <cctype>
#include <cstring> #include <cstring>
#include <ctime> #include <ctime>
#include "Common/Network.h"
#include "Common/Random.h" #include "Common/Random.h"
#include "Common/StringUtil.h" #include "Common/StringUtil.h"
namespace Common namespace Common
{ {
void GenerateMacAddress(const MACConsumer type, u8* mac) MACAddress GenerateMacAddress(const MACConsumer type)
{ {
memset(mac, 0, MAC_ADDRESS_SIZE); constexpr std::array<u8, 3> oui_bba{{0x00, 0x09, 0xbf}};
constexpr std::array<u8, 3> oui_ios{{0x00, 0x17, 0xab}};
u8 const oui_bba[] = {0x00, 0x09, 0xbf}; MACAddress mac{};
u8 const oui_ios[] = {0x00, 0x17, 0xab};
switch (type) switch (type)
{ {
case MACConsumer::BBA: case MACConsumer::BBA:
memcpy(mac, oui_bba, 3); std::copy(oui_bba.begin(), oui_bba.end(), mac.begin());
break; break;
case MACConsumer::IOS: case MACConsumer::IOS:
memcpy(mac, oui_ios, 3); std::copy(oui_ios.begin(), oui_ios.end(), mac.begin());
break; break;
} }
// Generate the 24-bit NIC-specific portion of the MAC address. // Generate the 24-bit NIC-specific portion of the MAC address.
Common::Random::Generate(&mac[3], 3); Random::Generate(&mac[3], 3);
return mac;
} }
std::string MacAddressToString(const u8* mac) std::string MacAddressToString(const MACAddress& mac)
{ {
return StringFromFormat("%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], return StringFromFormat("%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4],
mac[5]); mac[5]);
} }
bool StringToMacAddress(const std::string& mac_string, u8* mac) std::optional<MACAddress> StringToMacAddress(const std::string& mac_string)
{ {
bool success = false; if (mac_string.empty())
if (!mac_string.empty()) return {};
{
int x = 0;
memset(mac, 0, MAC_ADDRESS_SIZE);
for (size_t i = 0; i < mac_string.size() && x < (MAC_ADDRESS_SIZE * 2); ++i) int x = 0;
MACAddress mac{};
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')
{ {
char c = tolower(mac_string.at(i)); mac[x / 2] |= (c - '0') << ((x & 1) ? 0 : 4);
if (c >= '0' && c <= '9') ++x;
{ }
mac[x / 2] |= (c - '0') << ((x & 1) ? 0 : 4); else if (c >= 'a' && c <= 'f')
++x; {
} mac[x / 2] |= (c - 'a' + 10) << ((x & 1) ? 0 : 4);
else if (c >= 'a' && c <= 'f') ++x;
{
mac[x / 2] |= (c - 'a' + 10) << ((x & 1) ? 0 : 4);
++x;
}
} }
success = x / 2 == MAC_ADDRESS_SIZE;
} }
return success;
// 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);
} }
} // namespace Common } // namespace Common

View file

@ -4,6 +4,8 @@
#pragma once #pragma once
#include <array>
#include <optional>
#include <string> #include <string>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
@ -21,7 +23,9 @@ enum
MAC_ADDRESS_SIZE = 6 MAC_ADDRESS_SIZE = 6
}; };
void GenerateMacAddress(const MACConsumer type, u8* mac); using MACAddress = std::array<u8, MAC_ADDRESS_SIZE>;
std::string MacAddressToString(const u8* mac);
bool StringToMacAddress(const std::string& mac_string, u8* mac); MACAddress GenerateMacAddress(MACConsumer type);
std::string MacAddressToString(const MACAddress& mac);
std::optional<MACAddress> StringToMacAddress(const std::string& mac_string);
} // namespace Common } // namespace Common

View file

@ -5,6 +5,7 @@
#include "Core/HW/EXI/EXI_DeviceEthernet.h" #include "Core/HW/EXI/EXI_DeviceEthernet.h"
#include <memory> #include <memory>
#include <optional>
#include <string> #include <string>
#include "Common/ChunkFile.h" #include "Common/ChunkFile.h"
@ -33,16 +34,17 @@ CEXIETHERNET::CEXIETHERNET()
// Parse MAC address from config, and generate a new one if it doesn't // Parse MAC address from config, and generate a new one if it doesn't
// exist or can't be parsed. // exist or can't be parsed.
std::string& mac_addr_setting = SConfig::GetInstance().m_bba_mac; std::string& mac_addr_setting = SConfig::GetInstance().m_bba_mac;
u8 mac_addr[Common::MAC_ADDRESS_SIZE] = {0}; std::optional<Common::MACAddress> mac_addr = Common::StringToMacAddress(mac_addr_setting);
if (!Common::StringToMacAddress(mac_addr_setting, mac_addr)) if (!mac_addr)
{ {
Common::GenerateMacAddress(Common::MACConsumer::BBA, mac_addr); mac_addr = Common::GenerateMacAddress(Common::MACConsumer::BBA);
mac_addr_setting = Common::MacAddressToString(mac_addr); mac_addr_setting = Common::MacAddressToString(mac_addr.value());
SConfig::GetInstance().SaveSettings(); SConfig::GetInstance().SaveSettings();
} }
memcpy(&mBbaMem[BBA_NAFR_PAR0], mac_addr, Common::MAC_ADDRESS_SIZE); const auto& mac = mac_addr.value();
memcpy(&mBbaMem[BBA_NAFR_PAR0], mac.data(), mac.size());
// HACK: .. fully established 100BASE-T link // HACK: .. fully established 100BASE-T link
mBbaMem[BBA_NWAYS] = NWAYS_LS100 | NWAYS_LPNWAY | NWAYS_100TXF | NWAYS_ANCLPT; mBbaMem[BBA_NWAYS] = NWAYS_LS100 | NWAYS_LPNWAY | NWAYS_100TXF | NWAYS_ANCLPT;

View file

@ -878,10 +878,11 @@ IPCCommandResult NetIPTop::HandleGetInterfaceOptRequest(const IOCtlVRequest& req
break; break;
case 0x1004: // mac address case 0x1004: // mac address
u8 address[Common::MAC_ADDRESS_SIZE]; {
IOS::Net::GetMACAddress(address); const Common::MACAddress address = IOS::Net::GetMACAddress();
Memory::CopyToEmu(request.io_vectors[0].address, address, sizeof(address)); Memory::CopyToEmu(request.io_vectors[0].address, address.data(), address.size());
break; break;
}
case 0x1005: // link state case 0x1005: // link state
Memory::Write_U32(1, request.io_vectors[0].address); Memory::Write_U32(1, request.io_vectors[0].address);

View file

@ -4,6 +4,7 @@
#include "Core/IOS/Network/MACUtils.h" #include "Core/IOS/Network/MACUtils.h"
#include <optional>
#include <string> #include <string>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
@ -15,13 +16,13 @@
namespace IOS::Net namespace IOS::Net
{ {
static void SaveMACAddress(const u8* mac) static void SaveMACAddress(const Common::MACAddress& mac)
{ {
SConfig::GetInstance().m_WirelessMac = Common::MacAddressToString(mac); SConfig::GetInstance().m_WirelessMac = Common::MacAddressToString(mac);
SConfig::GetInstance().SaveSettings(); SConfig::GetInstance().SaveSettings();
} }
void GetMACAddress(u8* mac) Common::MACAddress GetMACAddress()
{ {
// Parse MAC address from config, and generate a new one if it doesn't // Parse MAC address from config, and generate a new one if it doesn't
// exist or can't be parsed. // exist or can't be parsed.
@ -30,19 +31,22 @@ void GetMACAddress(u8* mac)
if (Core::WantsDeterminism()) if (Core::WantsDeterminism())
wireless_mac = "12:34:56:78:9a:bc"; wireless_mac = "12:34:56:78:9a:bc";
if (!Common::StringToMacAddress(wireless_mac, mac)) std::optional<Common::MACAddress> mac = Common::StringToMacAddress(wireless_mac);
if (!mac)
{ {
Common::GenerateMacAddress(Common::MACConsumer::IOS, mac); mac = Common::GenerateMacAddress(Common::MACConsumer::IOS);
SaveMACAddress(mac); SaveMACAddress(mac.value());
if (!wireless_mac.empty()) if (!wireless_mac.empty())
{ {
ERROR_LOG(IOS_NET, ERROR_LOG(IOS_NET,
"The MAC provided (%s) is invalid. We have " "The MAC provided (%s) is invalid. We have "
"generated another one for you.", "generated another one for you.",
Common::MacAddressToString(mac).c_str()); Common::MacAddressToString(mac.value()).c_str());
} }
} }
INFO_LOG(IOS_NET, "Using MAC address: %s", Common::MacAddressToString(mac).c_str()); INFO_LOG(IOS_NET, "Using MAC address: %s", Common::MacAddressToString(mac.value()).c_str());
return mac.value();
} }
} // namespace IOS::Net } // namespace IOS::Net

View file

@ -4,9 +4,9 @@
#pragma once #pragma once
#include "Common/CommonTypes.h" #include "Common/Network.h"
namespace IOS::Net namespace IOS::Net
{ {
void GetMACAddress(u8* mac); Common::MACAddress GetMACAddress();
} // namespace IOS::Net } // namespace IOS::Net

View file

@ -65,12 +65,13 @@ IPCCommandResult NetNCDManage::IOCtlV(const IOCtlVRequest& request)
break; break;
case IOCTLV_NCD_GETWIRELESSMACADDRESS: case IOCTLV_NCD_GETWIRELESSMACADDRESS:
{
INFO_LOG(IOS_NET, "NET_NCD_MANAGE: IOCTLV_NCD_GETWIRELESSMACADDRESS"); INFO_LOG(IOS_NET, "NET_NCD_MANAGE: IOCTLV_NCD_GETWIRELESSMACADDRESS");
u8 address[Common::MAC_ADDRESS_SIZE]; const Common::MACAddress address = IOS::Net::GetMACAddress();
IOS::Net::GetMACAddress(address); Memory::CopyToEmu(request.io_vectors.at(1).address, address.data(), address.size());
Memory::CopyToEmu(request.io_vectors.at(1).address, address, sizeof(address));
break; break;
}
default: default:
INFO_LOG(IOS_NET, "NET_NCD_MANAGE IOCtlV: %#x", request.request); INFO_LOG(IOS_NET, "NET_NCD_MANAGE IOCtlV: %#x", request.request);

View file

@ -4,6 +4,7 @@
#include "Core/IOS/Network/WD/Command.h" #include "Core/IOS/Network/WD/Command.h"
#include <algorithm>
#include <cstring> #include <cstring>
#include <string> #include <string>
@ -65,9 +66,8 @@ IPCCommandResult NetWDCommand::IOCtlV(const IOCtlVRequest& request)
memcpy(info->country, "US", 2); memcpy(info->country, "US", 2);
info->ntr_allowed_channels = Common::swap16(0xfffe); info->ntr_allowed_channels = Common::swap16(0xfffe);
u8 address[Common::MAC_ADDRESS_SIZE]; const Common::MACAddress address = IOS::Net::GetMACAddress();
IOS::Net::GetMACAddress(address); std::copy(address.begin(), address.end(), info->mac);
memcpy(info->mac, address, sizeof(info->mac));
} }
break; break;

View file

@ -514,8 +514,7 @@ void BluetoothReal::LoadLinkKeys()
if (index == std::string::npos) if (index == std::string::npos)
continue; continue;
bdaddr_t address; bdaddr_t address = Common::StringToMacAddress(pair.substr(0, index)).value();
Common::StringToMacAddress(pair.substr(0, index), address.data());
std::reverse(address.begin(), address.end()); std::reverse(address.begin(), address.end());
const std::string& key_string = pair.substr(index + 1); const std::string& key_string = pair.substr(index + 1);
@ -540,7 +539,7 @@ void BluetoothReal::SaveLinkKeys()
bdaddr_t address; bdaddr_t address;
// Reverse the address so that it is stored in the correct order in the config file // Reverse the address so that it is stored in the correct order in the config file
std::reverse_copy(entry.first.begin(), entry.first.end(), address.begin()); std::reverse_copy(entry.first.begin(), entry.first.end(), address.begin());
oss << Common::MacAddressToString(address.data()); oss << Common::MacAddressToString(address);
oss << '='; oss << '=';
oss << std::hex; oss << std::hex;
for (const u16& data : entry.second) for (const u16& data : entry.second)