From a84a315473b67e40863bd7e53b77b63931cd3c58 Mon Sep 17 00:00:00 2001 From: Daniel Nowak <13685818+lowlyocean@users.noreply.github.com> Date: Tue, 9 Oct 2018 14:37:41 -0400 Subject: [PATCH] Dev9ghzdrk: Get host mac correctly on Windows. (#2619) Follow up from #2586 which ensures that MAC address is unique on Xlink Kai when using Windows. Previous build was returning last two bytes as 00:00 rather than last two bytes of host adapter's MAC. --- plugins/dev9ghzdrk/Linux/Linux.cpp | 5 -- plugins/dev9ghzdrk/Linux/dev9ghzdrk.ui | 7 +- plugins/dev9ghzdrk/Win32/Config.cpp | 95 ++------------------------ plugins/dev9ghzdrk/Win32/Win32.cpp | 25 ++++--- plugins/dev9ghzdrk/pcap_io.cpp | 51 +++++++++----- plugins/dev9ghzdrk/smap.cpp | 26 +++---- 6 files changed, 70 insertions(+), 139 deletions(-) diff --git a/plugins/dev9ghzdrk/Linux/Linux.cpp b/plugins/dev9ghzdrk/Linux/Linux.cpp index d0e3730405..c8bfe5c9ba 100644 --- a/plugins/dev9ghzdrk/Linux/Linux.cpp +++ b/plugins/dev9ghzdrk/Linux/Linux.cpp @@ -143,11 +143,6 @@ DEV9configure() { } -void __attribute__((constructor)) DllMain() { - //gtk_builder_add_from_file(builder, "dev9ghzdrk.ui", NULL); - //builder = gtk_build_new_from_resource( "/net/pcsx2/dev9ghzdrk/dev9ghzdrk.ui" ); -} - NetAdapter* GetNetAdapter() { NetAdapter* na; diff --git a/plugins/dev9ghzdrk/Linux/dev9ghzdrk.ui b/plugins/dev9ghzdrk/Linux/dev9ghzdrk.ui index 8e32a0235d..214b19cf1a 100644 --- a/plugins/dev9ghzdrk/Linux/dev9ghzdrk.ui +++ b/plugins/dev9ghzdrk/Linux/dev9ghzdrk.ui @@ -69,6 +69,7 @@ + False True False True @@ -129,7 +130,7 @@ True False - True + False 0 @@ -172,6 +173,7 @@ ENABLED + False True True False @@ -201,6 +203,7 @@ + False True False @@ -223,7 +226,7 @@ True False - Hard Disk Drive (not yet properly implemented + Hard Disk Drive (not yet properly implemented) diff --git a/plugins/dev9ghzdrk/Win32/Config.cpp b/plugins/dev9ghzdrk/Win32/Config.cpp index 23b843a167..09ed7c7b93 100644 --- a/plugins/dev9ghzdrk/Win32/Config.cpp +++ b/plugins/dev9ghzdrk/Win32/Config.cpp @@ -19,19 +19,6 @@ #include #include "..\DEV9.h" -#define GetKeyV(name, var, s, t) \ - size = s; type = t; \ - RegQueryValueEx(myKey, name, 0, &type, (LPBYTE) var, &size); - -#define GetKeyVdw(name, var) \ - GetKeyV(name, var, 4, REG_DWORD); - -//#define SetKeyV(name, var, s, t) \ -// RegSetValueEx(myKey, name, 0, t, (LPBYTE) var, s); - -//#define SetKeyVdw(name, var) \ -// SetKeyV(name, var, 4, REG_DWORD); - BOOL WritePrivateProfileInt(LPCSTR lpAppName, LPCSTR lpKeyName, int intvar, LPCSTR lpFileName) { return WritePrivateProfileString(lpAppName, lpKeyName, std::to_string(intvar).c_str(), lpFileName); @@ -44,7 +31,7 @@ bool FileExists(std::string szPath) } void SaveConf() { - const std::string file(s_strIniPath + "/dev9ghz.ini"); + const std::string file(s_strIniPath + "dev9ghz.ini"); DeleteFile(file.c_str()); WritePrivateProfileString("DEV9", "Eth", config.Eth, file.c_str()); @@ -54,84 +41,14 @@ void SaveConf() { WritePrivateProfileInt("DEV9", "hddEnable", config.hddEnable, file.c_str()); } -void DeleteRegConf() { - HKEY myKey; - //DWORD type, size; +void LoadConf() { + const std::string file(s_strIniPath + "dev9ghz.ini"); + if (FileExists(file.c_str()) == false) + return; - if (RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\PS2Eplugin\\DEV9\\DEV9linuz", 0, KEY_ALL_ACCESS, &myKey) != ERROR_SUCCESS) { - return; - } - RegDeleteKey(myKey, "Eth"); - RegDeleteKey(myKey, "Hdd"); - RegDeleteKey(myKey, "HddSize"); - RegDeleteKey(myKey, "ethEnable"); - RegDeleteKey(myKey, "hddEnable"); - RegCloseKey(myKey); - //Delete Key Software\PS2Eplugin\DEV9\DEV9linuz - if (RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\PS2Eplugin\\DEV9", 0, KEY_ALL_ACCESS, &myKey) != ERROR_SUCCESS) { - emu_printf("Error Opening Key DEV9\n"); - return; - } - if (RegDeleteKey(myKey, "DEV9linuz") != ERROR_SUCCESS) { - emu_printf("Error Removing Key DEV9linuz\n"); - RegCloseKey(myKey); - return; - } - RegCloseKey(myKey); - //Delete Key Software\PS2Eplugin\DEV9 - if (RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\PS2Eplugin", 0, KEY_ALL_ACCESS, &myKey) != ERROR_SUCCESS) { - emu_printf("Error Opening Key PS2Eplugin\n"); - return; - } - if (RegDeleteKey(myKey, "DEV9") != ERROR_SUCCESS) { - emu_printf("Error Removing Key DEV9\n"); - RegCloseKey(myKey); - return; - } - RegCloseKey(myKey); -} - -void LoadIniConf() { - //memset(&config, 0, sizeof(config)); - //strcpy(config.Hdd, HDD_DEF); - //config.HddSize = 8 * 1024; - //strcpy(config.Eth, ETH_DEF); - - const std::string file(s_strIniPath + "/dev9ghz.ini"); - if (FileExists(file.c_str()) == false) { - SaveConf(); - return; - } GetPrivateProfileString("DEV9", "Eth", ETH_DEF, config.Eth, sizeof(config.Eth), file.c_str()); GetPrivateProfileString("DEV9", "Hdd", HDD_DEF, config.Hdd, sizeof(config.Hdd), file.c_str()); config.HddSize = GetPrivateProfileInt("DEV9", "HddSize", config.HddSize, file.c_str()); config.ethEnable = GetPrivateProfileInt("DEV9", "ethEnable", config.ethEnable, file.c_str()); config.hddEnable = GetPrivateProfileInt("DEV9", "hddEnable", config.hddEnable, file.c_str()); -} - -void LoadConf() { - HKEY myKey; - DWORD type, size; - - memset(&config, 0, sizeof(config)); - strcpy(config.Hdd, HDD_DEF); - config.HddSize = 8 * 1024; - strcpy(config.Eth, ETH_DEF); - - if (RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\PS2Eplugin\\DEV9\\DEV9linuz", 0, KEY_ALL_ACCESS, &myKey)!=ERROR_SUCCESS) { - LoadIniConf(); - return; - } - printf("Importing Settings\n"); - //Import old settings if user has upgraded this plugin - GetKeyV("Eth", config.Eth, sizeof(config.Eth), REG_SZ); - GetKeyV("Hdd", config.Hdd, sizeof(config.Hdd), REG_SZ); - GetKeyVdw("HddSize", &config.HddSize); - GetKeyVdw("ethEnable", &config.ethEnable); - GetKeyVdw("hddEnable", &config.hddEnable); - - RegCloseKey(myKey); - SaveConf(); - DeleteRegConf(); -} - +} \ No newline at end of file diff --git a/plugins/dev9ghzdrk/Win32/Win32.cpp b/plugins/dev9ghzdrk/Win32/Win32.cpp index 36525d59ec..4b83349af2 100644 --- a/plugins/dev9ghzdrk/Win32/Win32.cpp +++ b/plugins/dev9ghzdrk/Win32/Win32.cpp @@ -51,16 +51,15 @@ void OnInitDialog(HWND hW) { for (int i=0; i * al=GetTapAdapters(); for (size_t i=0; isize(); i++) { - int itm=ComboBox_AddString(GetDlgItem(hW, IDC_ETHDEV), al[0][i].name.c_str()); - ComboBox_SetItemData(GetDlgItem(hW, IDC_ETHDEV),itm,strdup( al[0][i].guid.c_str())); + ComboBox_SetItemData(GetDlgItem(hW, IDC_ETHDEV),itm,_strdup( al[0][i].guid.c_str())); if (strcmp(al[0][i].guid.c_str(), config.Eth) == 0) { ComboBox_SetCurSel(GetDlgItem(hW, IDC_ETHDEV), itm); } @@ -77,27 +76,27 @@ void OnOk(HWND hW) { if (i == -1) { //adapter not selected - if (Button_GetCheck(GetDlgItem(hW, IDC_ETHENABLED))) + if ( Button_GetCheck(GetDlgItem(hW, IDC_ETHENABLED))) { //Trying to use an ethernet without //selected adapter, we can't have that SysMessage("Please select an ethernet adapter"); return; } - else + else { //user not planning on using //ethernet anyway strcpy(config.Eth, ETH_DEF); } } - else + else { //adapter is selected char* ptr = (char*)ComboBox_GetItemData(GetDlgItem(hW, IDC_ETHDEV), i); strcpy(config.Eth, ptr); } - + Edit_GetText(GetDlgItem(hW, IDC_HDDFILE), config.Hdd, 256); config.ethEnable = Button_GetCheck(GetDlgItem(hW, IDC_ETHENABLED)); @@ -109,7 +108,7 @@ void OnOk(HWND hW) { } BOOL CALLBACK ConfigureDlgProc(HWND hW, UINT uMsg, WPARAM wParam, LPARAM lParam) { - + switch(uMsg) { case WM_INITDIALOG: OnInitDialog(hW); @@ -147,8 +146,8 @@ EXPORT_C_(void) DEV9configure() { DialogBox(hInst, MAKEINTRESOURCE(IDD_CONFIG), - GetActiveWindow(), - (DLGPROC)ConfigureDlgProc); + GetActiveWindow(), + (DLGPROC)ConfigureDlgProc); //SysMessage("Nothing to Configure"); } @@ -156,12 +155,12 @@ EXPORT_C_(void) DEV9about() { DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUT), - GetActiveWindow(), + GetActiveWindow(), (DLGPROC)AboutDlgProc); } BOOL APIENTRY DllMain(HANDLE hModule, // DLL INIT - DWORD dwReason, + DWORD dwReason, LPVOID lpReserved) { hInst = (HINSTANCE)hModule; return TRUE; // very quick :) @@ -184,7 +183,7 @@ NetAdapter* GetNetAdapter() } return na; } -s32 _DEV9open() +s32 _DEV9open() { //handleDEV9Thread = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) DEV9ThreadProc, &dwThrdParam, CREATE_SUSPENDED, &dwThreadId); //SetThreadPriority(handleDEV9Thread,THREAD_PRIORITY_HIGHEST); diff --git a/plugins/dev9ghzdrk/pcap_io.cpp b/plugins/dev9ghzdrk/pcap_io.cpp index e507a85dbc..d327586481 100644 --- a/plugins/dev9ghzdrk/pcap_io.cpp +++ b/plugins/dev9ghzdrk/pcap_io.cpp @@ -51,29 +51,46 @@ int GetMACAddress(char *adapter, mac_address* addr) { int retval = 0; #ifdef _WIN32 - static IP_ADAPTER_INFO AdapterInfo[128]; // Allocate information - // for up to 128 NICs - static PIP_ADAPTER_INFO pAdapterInfo; - ULONG dwBufLen = sizeof(AdapterInfo); // Save memory size of buffer + static IP_ADAPTER_ADDRESSES AdapterInfo[128]; - DWORD dwStatus = GetAdaptersInfo( // Call GetAdapterInfo - AdapterInfo, // [out] buffer to receive data - &dwBufLen); // [in] size of receive data buffer - if(dwStatus != ERROR_SUCCESS) // Verify return value is - return 0; // valid, no buffer overflow + static PIP_ADAPTER_ADDRESSES pAdapterInfo; + ULONG dwBufLen = sizeof(AdapterInfo); - pAdapterInfo = AdapterInfo; // Contains pointer to - // current adapter info - do { - if(strcmp(pAdapterInfo->AdapterName,adapter)==0) + DWORD dwStatus = GetAdaptersAddresses( + AF_UNSPEC, + GAA_FLAG_INCLUDE_PREFIX, + NULL, + AdapterInfo, + &dwBufLen + ); + if(dwStatus != ERROR_SUCCESS) + return 0; + + pAdapterInfo = AdapterInfo; + + char adapter_desc[128] = ""; + + // Must get friendly description from the cryptic adapter name + for (int ii = 0; ii < pcap_io_get_dev_num(); ii++) + if (0 == strcmp(pcap_io_get_dev_name(ii), adapter)) { - memcpy(addr,pAdapterInfo->Address,6); + strcpy(adapter_desc, pcap_io_get_dev_desc(ii)); + break; + } + + wchar_t wadapter[128]; + std::mbstowcs(wadapter, adapter_desc, 128); + + do { + if ( 0 == wcscmp(pAdapterInfo->Description, wadapter ) ) + { + memcpy(addr,pAdapterInfo->PhysicalAddress,6); return 1; } - pAdapterInfo = pAdapterInfo->Next; // Progress through + pAdapterInfo = pAdapterInfo->Next; } - while(pAdapterInfo); // Terminate if last adapter + while(pAdapterInfo); #elif defined(__linux__) struct ifreq ifr; int fd = socket(AF_INET, SOCK_DGRAM, 0); @@ -304,7 +321,7 @@ char* pcap_io_get_dev_desc(int num) PCAPAdapter::PCAPAdapter() { - //if (config.ethEnable == 0) return; //whut? nada! + if (config.ethEnable == 0) return; if (pcap_io_init(config.Eth) == -1) { SysMessage("Can't open Device '%s'\n", config.Eth); } diff --git a/plugins/dev9ghzdrk/smap.cpp b/plugins/dev9ghzdrk/smap.cpp index 6853c5b813..eadcb02550 100644 --- a/plugins/dev9ghzdrk/smap.cpp +++ b/plugins/dev9ghzdrk/smap.cpp @@ -344,14 +344,14 @@ smap_read8(u32 addr) EXPORT_C_(u16) smap_read16(u32 addr) { - if (addr >= SMAP_BD_TX_BASE && addr < (SMAP_BD_TX_BASE + SMAP_BD_SIZE)) + int rv = dev9Ru16(addr); + if (addr >= SMAP_BD_TX_BASE && addr < (SMAP_BD_TX_BASE + SMAP_BD_SIZE)) { - int rv = dev9Ru16(addr); - if(dev9.bd_swap) - return (rv<<8)|(rv>>8); + if (dev9.bd_swap) + return (rv << 8) | (rv >> 8); return rv; /* - switch (addr & 0x7) + switch (addr & 0x7) { case 0: // ctrl_stat hard = dev9Ru16(addr); @@ -380,14 +380,13 @@ smap_read16(u32 addr) } */ } - else if (addr >= SMAP_BD_RX_BASE && addr < (SMAP_BD_RX_BASE + SMAP_BD_SIZE)) + else if (addr >= SMAP_BD_RX_BASE && addr < (SMAP_BD_RX_BASE + SMAP_BD_SIZE)) { - int rv = dev9Ru16(addr); - if(dev9.bd_swap) - return (rv<<8)|(rv>>8); + if (dev9.bd_swap) + return (rv << 8) | (rv >> 8); return rv; /* - switch (addr & 0x7) + switch (addr & 0x7) { case 0: // ctrl_stat hard = dev9Ru16(addr); @@ -416,10 +415,9 @@ smap_read16(u32 addr) } */ } - +#ifdef DEV9_LOG_ENABLE switch(addr) { -#ifdef DEV9_LOG_ENABLE case SMAP_R_TXFIFO_FRAME_CNT: printf("SMAP_R_TXFIFO_FRAME_CNT read 16\n"); return dev9Ru16(addr); @@ -489,12 +487,14 @@ smap_read16(u32 addr) case SMAP_R_EMAC3_STA_CTRL_H: DEV9_LOG("SMAP_R_EMAC3_STA_CTRL_H 16bit read %x\n", dev9Ru16(addr)); return dev9Ru16(addr); -#endif default: DEV9_LOG("SMAP : Unknown 16 bit read @ %X,v=%X\n",addr,dev9Ru16(addr)); return dev9Ru16(addr); } +#endif + return rv; } + EXPORT_C_(u32) smap_read32(u32 addr) {