summaryrefslogtreecommitdiff
path: root/Source/Core/Common/Network.cpp
diff options
context:
space:
mode:
authorLioncash <mathew1800@gmail.com>2018-06-10 14:32:33 -0400
committerLioncash <mathew1800@gmail.com>2018-06-10 15:43:26 -0400
commitce69201f3398facedd960bb09264e4ef5bf6a672 (patch)
treec1fa63b80ff75473f32eec4a664e4402fea8391a /Source/Core/Common/Network.cpp
parent70417c8d163e165978a913e5d3e4548ccc1e0ce8 (diff)
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.
Diffstat (limited to 'Source/Core/Common/Network.cpp')
-rw-r--r--Source/Core/Common/Network.cpp66
1 files changed, 37 insertions, 29 deletions
diff --git a/Source/Core/Common/Network.cpp b/Source/Core/Common/Network.cpp
index 1d95e0c8a4..acfb60e55c 100644
--- a/Source/Core/Common/Network.cpp
+++ b/Source/Core/Common/Network.cpp
@@ -2,67 +2,75 @@
// Licensed under GPLv2+
// Refer to the license.txt file included.
+#include "Common/Network.h"
+
+#include <algorithm>
#include <cctype>
#include <cstring>
#include <ctime>
-#include "Common/Network.h"
#include "Common/Random.h"
#include "Common/StringUtil.h"
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};
- u8 const oui_ios[] = {0x00, 0x17, 0xab};
+ MACAddress mac{};
switch (type)
{
case MACConsumer::BBA:
- memcpy(mac, oui_bba, 3);
+ std::copy(oui_bba.begin(), oui_bba.end(), mac.begin());
break;
case MACConsumer::IOS:
- memcpy(mac, oui_ios, 3);
+ std::copy(oui_ios.begin(), oui_ios.end(), mac.begin());
break;
}
// 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],
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())
- {
- int x = 0;
- memset(mac, 0, MAC_ADDRESS_SIZE);
+ if (mac_string.empty())
+ return {};
+
+ int x = 0;
+ MACAddress mac{};
- for (size_t i = 0; i < mac_string.size() && x < (MAC_ADDRESS_SIZE * 2); ++i)
+ 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')
{
- 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;
- }
+ 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