From c83a7e002980deacdfa977f6578838cbf383575d Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Thu, 28 Nov 2013 12:38:45 +0100 Subject: [PATCH] Parse mac addresses in one place. --- Core/Config.cpp | 4 ++-- Core/Config.h | 4 ++-- Core/HLE/proAdhoc.cpp | 43 ++++++++++++++++++++++++------------------- Core/HLE/proAdhoc.h | 32 ++++++++++++-------------------- Core/HLE/sceNet.cpp | 22 ++++++++++++---------- native | 2 +- unittest/UnitTest.cpp | 15 +++++++++++++++ 7 files changed, 68 insertions(+), 54 deletions(-) diff --git a/Core/Config.cpp b/Core/Config.cpp index 1d77a6e6be..2cf4ece238 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -295,8 +295,8 @@ void Config::Load(const char *iniFileName, const char *controllerIniFilename) { IniFile::Section *pspConfig = iniFile.GetOrCreateSection("SystemParam"); pspConfig->Get("NickName", &sNickName, "PPSSPP"); - pspConfig->Get("proAdhocServer", &proAdhocServer, "localhost"); - pspConfig->Get("MacAddress", &localMacAddress, "01:02:03:04:05:06"); + pspConfig->Get("proAdhocServer", &proAdhocServer, "localhost"); + pspConfig->Get("MacAddress", &localMacAddress, "01:02:03:04:05:06"); pspConfig->Get("Language", &iLanguage, PSP_SYSTEMPARAM_LANGUAGE_ENGLISH); pspConfig->Get("TimeFormat", &iTimeFormat, PSP_SYSTEMPARAM_TIME_FORMAT_24HR); pspConfig->Get("DateFormat", &iDateFormat, PSP_SYSTEMPARAM_DATE_FORMAT_YYYYMMDD); diff --git a/Core/Config.h b/Core/Config.h index c4d19708c2..c2803bd71b 100644 --- a/Core/Config.h +++ b/Core/Config.h @@ -201,8 +201,8 @@ public: // SystemParam std::string sNickName; - std::string proAdhocServer; - std::string localMacAddress; + std::string proAdhocServer; + std::string localMacAddress; int iLanguage; int iTimeFormat; int iDateFormat; diff --git a/Core/HLE/proAdhoc.cpp b/Core/HLE/proAdhoc.cpp index 7ee938343f..a8fd0e5d3e 100644 --- a/Core/HLE/proAdhoc.cpp +++ b/Core/HLE/proAdhoc.cpp @@ -1,3 +1,6 @@ +// TODO: Add license + +#include "util/text/parsers.h" #include "proAdhoc.h" uint32_t fakePoolSize = 0; @@ -478,31 +481,33 @@ int getActivePeerCount(void) { int getLocalIp(sockaddr_in * SocketAddress){ #ifdef _MSC_VER - // Get local host name - char szHostName[128] = ""; + // Get local host name + char szHostName[128] = ""; - if(::gethostname(szHostName, sizeof(szHostName))) { - // Error handling - } - // Get local IP addresses - struct hostent *pHost = 0; - pHost = ::gethostbyname(szHostName); - if(pHost) { - memcpy(&SocketAddress->sin_addr, pHost->h_addr_list[0], pHost->h_length); - return 0; - } - return -1; + if(::gethostname(szHostName, sizeof(szHostName))) { + // Error handling + } + // Get local IP addresses + struct hostent *pHost = 0; + pHost = ::gethostbyname(szHostName); + if(pHost) { + memcpy(&SocketAddress->sin_addr, pHost->h_addr_list[0], pHost->h_length); + return 0; + } + return -1; #else - SocketAddress->sin_addr.s_addr = inet_addr("192.168.12.1"); - return 0; + SocketAddress->sin_addr.s_addr = inet_addr("192.168.12.1"); + return 0; #endif } void getLocalMac(SceNetEtherAddr * addr){ - //MAC Adress from config - uint8_t mac[ETHER_ADDR_LEN]; - sscanf(g_Config.localMacAddress.c_str(), "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",&mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]); - memcpy(addr,mac,ETHER_ADDR_LEN); + // Read MAC Address from config + uint8_t mac[ETHER_ADDR_LEN] = {0}; + if (!ParseMacAddress(g_Config.localMacAddress.c_str(), mac)) { + ERROR_LOG(SCENET, "Error parsing mac address %s", g_Config.localMacAddress.c_str()); + } + memcpy(addr, mac, ETHER_ADDR_LEN); } int getPTPSocketCount(void) { diff --git a/Core/HLE/proAdhoc.h b/Core/HLE/proAdhoc.h index 385c219c6d..818472ae67 100644 --- a/Core/HLE/proAdhoc.h +++ b/Core/HLE/proAdhoc.h @@ -1,27 +1,19 @@ #pragma once -#include "Common/ChunkFile.h" -#include "../Config.h" -#include "Core/HLE/HLE.h" -#include "../CoreTiming.h" -#include "Core/HLE/sceNetAdhoc.h" -#include "native/base/timeutil.h" -#include "native/base/mutex.h" -#include "native/thread/thread.h" - -#include "sceKernel.h" -#include "sceKernelThread.h" -#include "sceKernelMutex.h" -#include "sceUtility.h" +#include "base/timeutil.h" +#include "base/mutex.h" +#include "thread/thread.h" #include "net/resolve.h" -/* -#ifdef _MSC_VER -#include -#else -#include -#endif -*/ +#include "Common/ChunkFile.h" +#include "Core/Config.h" +#include "Core/CoreTiming.h" +#include "Core/HLE/HLE.h" +#include "Core/HLE/sceNetAdhoc.h" +#include "Core/HLE/sceKernel.h" +#include "Core/HLE/sceKernelThread.h" +#include "Core/HLE/sceKernelMutex.h" +#include "Core/HLE/sceUtility.h" // Net stuff #ifdef _MSC_VER diff --git a/Core/HLE/sceNet.cpp b/Core/HLE/sceNet.cpp index 8212f025eb..a0aa6f4d62 100644 --- a/Core/HLE/sceNet.cpp +++ b/Core/HLE/sceNet.cpp @@ -15,18 +15,19 @@ // Official git repository and contact information can be found at // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. +#include "net/resolve.h" +#include "util/text/parsers.h" + #include "Common/ChunkFile.h" -#include "HLE.h" -#include "../MIPS/MIPS.h" -#include "../Config.h" +#include "Core/HLE/HLE.h" +#include "Core/MIPS/MIPS.h" +#include "Core/Config.h" #include "sceKernel.h" #include "sceKernelThread.h" #include "sceKernelMutex.h" #include "sceUtility.h" -#include "net/resolve.h" - static bool netInited; static bool netInetInited; static bool netApctlInited; @@ -131,13 +132,14 @@ u32 sceNetTerm() { } u32 sceWlanGetEtherAddr(u32 addrAddr) { - //MAC Adress from config - uint8_t mac[6]; - sscanf(g_Config.localMacAddress.c_str(), "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",&mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]); + // Read MAC Address from config + uint8_t mac[6] = {0}; + if (!ParseMacAddress(g_Config.localMacAddress.c_str(), mac)) { + ERROR_LOG(SCENET, "Error parsing mac address %s", g_Config.localMacAddress.c_str()); + } DEBUG_LOG(SCENET, "sceWlanGetEtherAddr(%08x)", addrAddr); for (int i = 0; i < 6; i++) - Memory::Write_U8(mac[i], addrAddr + i); - + Memory::Write_U8(mac[i], addrAddr + i); return 0; } diff --git a/native b/native index 26821c5f9f..26eaba2157 160000 --- a/native +++ b/native @@ -1 +1 @@ -Subproject commit 26821c5f9f30d4afd381e4a78a013b611707e740 +Subproject commit 26eaba2157a44338e6c085065ca0471b2b4dea7d diff --git a/unittest/UnitTest.cpp b/unittest/UnitTest.cpp index 66ed868772..4a7cd7f442 100644 --- a/unittest/UnitTest.cpp +++ b/unittest/UnitTest.cpp @@ -34,6 +34,7 @@ #include "Common/ArmEmitter.h" #include "ext/disarm.h" #include "math/math_util.h" +#include "util/text/parsers.h" #define EXPECT_TRUE(a) if (!(a)) { printf(__FUNCTION__ ":%i: Test Fail\n", __LINE__); return false; } #define EXPECT_FALSE(a) if ((a)) { printf(__FUNCTION__ ":%i: Test Fail\n", __LINE__); return false; } @@ -107,9 +108,23 @@ bool TestMathUtil() { return true; } +bool TestParsers() { + const char *macstr = "01:02:03:ff:fe:fd"; + uint8_t mac[6]; + ParseMacAddress(macstr, mac); + EXPECT_TRUE(mac[0] == 1); + EXPECT_TRUE(mac[1] == 2); + EXPECT_TRUE(mac[2] == 3); + EXPECT_TRUE(mac[3] == 255); + EXPECT_TRUE(mac[4] == 254); + EXPECT_TRUE(mac[5] == 253); + return true; +} + int main(int argc, const char *argv[]) { TestArmEmitter(); TestMathUtil(); + TestParsers(); return 0; } \ No newline at end of file