diff options
| author | Jules Blok <jules.blok@gmail.com> | 2015-06-10 00:00:12 +0200 |
|---|---|---|
| committer | Jules Blok <jules.blok@gmail.com> | 2015-06-10 00:00:12 +0200 |
| commit | 7dfced21a2e5aac7195e0e1ad76468e30306766b (patch) | |
| tree | bd671b639d3d911460b767caabad8fc8a759a6bf /Source/Core/Common/Network.cpp | |
| parent | c25be031fc1031d795157d8344b87b7841145197 (diff) | |
| parent | 7b0a65e295c784f9d573f2ef52d4e2a7b9bb2889 (diff) | |
Merge branch 'master' into stable
Diffstat (limited to 'Source/Core/Common/Network.cpp')
| -rw-r--r-- | Source/Core/Common/Network.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/Source/Core/Common/Network.cpp b/Source/Core/Common/Network.cpp new file mode 100644 index 0000000000..b0e0f5a624 --- /dev/null +++ b/Source/Core/Common/Network.cpp @@ -0,0 +1,70 @@ +// Copyright 2014 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include <cctype> +#include <ctime> + +#include "Common/Network.h" +#include "Common/StringUtil.h" + +void GenerateMacAddress(const MACConsumer type, u8* mac) +{ + memset(mac, 0, MAC_ADDRESS_SIZE); + + u8 const oui_bba[] = { 0x00, 0x09, 0xbf }; + u8 const oui_ios[] = { 0x00, 0x17, 0xab }; + + switch (type) + { + case BBA: + memcpy(mac, oui_bba, 3); + break; + case IOS: + memcpy(mac, oui_ios, 3); + break; + } + + srand((unsigned int)time(nullptr)); + + u8 id[3] = + { + (u8)rand(), + (u8)rand(), + (u8)rand() + }; + + memcpy(&mac[3], id, 3); +} + +std::string MacAddressToString(const u8* 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) +{ + bool success = false; + if (!mac_string.empty()) + { + int x = 0; + memset(mac, 0, MAC_ADDRESS_SIZE); + + 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; +} |
