DEV9: Save selected api along with selected ethernet device

Also unified GetNetAdapter()
This commit is contained in:
TheLastRar 2021-01-12 00:52:02 +00:00 committed by refractionpcsx2
parent e883e93f52
commit 4c287191cd
12 changed files with 173 additions and 45 deletions

View File

@ -1095,7 +1095,8 @@ void ApplyConfigIfRunning(Config oldConfig)
if (oldConfig.ethEnable) if (oldConfig.ethEnable)
{ {
//Reload Net if adapter changed //Reload Net if adapter changed
if (strcmp(oldConfig.Eth, config.Eth) != 0) if (strcmp(oldConfig.Eth, config.Eth) != 0 ||
oldConfig.EthApi != config.EthApi)
{ {
TermNet(); TermNet();
InitNet(); InitNet();

View File

@ -59,6 +59,7 @@ bool rx_fifo_can_rx();
typedef struct typedef struct
{ {
char Eth[256]; char Eth[256];
NetApi EthApi;
#ifdef _WIN32 #ifdef _WIN32
wchar_t Hdd[256]; wchar_t Hdd[256];
#else #else

View File

@ -46,6 +46,10 @@ void SaveConf()
xmlNewChild(root_node, NULL, BAD_CAST "Eth", xmlNewChild(root_node, NULL, BAD_CAST "Eth",
BAD_CAST config.Eth); BAD_CAST config.Eth);
sprintf(buff, "%d", (int)config.EthApi);
xmlNewChild(root_node, NULL, BAD_CAST "EthApi",
BAD_CAST buff);
xmlNewChild(root_node, NULL, BAD_CAST "Hdd", xmlNewChild(root_node, NULL, BAD_CAST "Hdd",
BAD_CAST config.Hdd); BAD_CAST config.Hdd);
@ -102,6 +106,7 @@ void LoadConf()
SysMessage("Unable to parse configuration file! Suggest deleting it and starting over."); SysMessage("Unable to parse configuration file! Suggest deleting it and starting over.");
} }
bool foundEthType = false;
for (cur_node = xmlDocGetRootElement(doc)->children; cur_node; cur_node = cur_node->next) for (cur_node = xmlDocGetRootElement(doc)->children; cur_node; cur_node = cur_node->next)
{ {
if (cur_node->type == XML_ELEMENT_NODE) if (cur_node->type == XML_ELEMENT_NODE)
@ -111,6 +116,11 @@ void LoadConf()
{ {
strcpy(config.Eth, (const char*)xmlNodeGetContent(cur_node)); strcpy(config.Eth, (const char*)xmlNodeGetContent(cur_node));
} }
if (0 == strcmp((const char*)cur_node->name, "EthApi"))
{
foundEthType = true;
config.EthApi = (NetApi)atoi((const char*)xmlNodeGetContent(cur_node));
}
if (0 == strcmp((const char*)cur_node->name, "Hdd")) if (0 == strcmp((const char*)cur_node->name, "Hdd"))
{ {
strcpy(config.Hdd, (const char*)xmlNodeGetContent(cur_node)); strcpy(config.Hdd, (const char*)xmlNodeGetContent(cur_node));
@ -130,6 +140,9 @@ void LoadConf()
} }
} }
if (!foundEthType)
config.EthApi = NetApi::PCAP_Switched;
// free(configFile); // free(configFile);
xmlFreeDoc(doc); xmlFreeDoc(doc);
xmlCleanupParser(); xmlCleanupParser();

View File

@ -23,6 +23,8 @@
#include <sys/types.h> #include <sys/types.h>
#include <pwd.h> #include <pwd.h>
#include <string.h> #include <string.h>
#include <vector>
#include "fmt/format.h"
#include <string> #include <string>
#include "ghc/filesystem.h" #include "ghc/filesystem.h"
@ -37,10 +39,10 @@
#include "../ATA/HddCreate.h" #include "../ATA/HddCreate.h"
static GtkBuilder* builder = nullptr; static GtkBuilder* builder = nullptr;
std::vector<AdapterEntry> adapters;
void OnInitDialog() void OnInitDialog()
{ {
char* dev;
gint idx = 0; gint idx = 0;
static int initialized = 0; static int initialized = 0;
@ -51,11 +53,15 @@ void OnInitDialog()
gtk_combo_box_text_append_text((GtkComboBoxText*)gtk_builder_get_object(builder, "IDC_BAYTYPE"), "Expansion"); gtk_combo_box_text_append_text((GtkComboBoxText*)gtk_builder_get_object(builder, "IDC_BAYTYPE"), "Expansion");
gtk_combo_box_text_append_text((GtkComboBoxText*)gtk_builder_get_object(builder, "IDC_BAYTYPE"), "PC Card"); gtk_combo_box_text_append_text((GtkComboBoxText*)gtk_builder_get_object(builder, "IDC_BAYTYPE"), "PC Card");
for (int i = 0; i < pcap_io_get_dev_num(); i++)
adapters = PCAPAdapter::GetAdapters();
for (size_t i = 0; i < adapters.size(); i++)
{ {
dev = pcap_io_get_dev_name(i); std::string dev = fmt::format("{}: {}", (char*)NetApiToString(adapters[i].type), adapters[i].name.c_str());
gtk_combo_box_text_append_text((GtkComboBoxText*)gtk_builder_get_object(builder, "IDC_ETHDEV"), dev);
if (strcmp(dev, config.Eth) == 0) gtk_combo_box_text_append_text((GtkComboBoxText*)gtk_builder_get_object(builder, "IDC_ETHDEV"), dev.c_str());
if (config.EthApi == adapters[i].type && strcmp(adapters[i].guid.c_str(), config.Eth) == 0)
{ {
gtk_combo_box_set_active((GtkComboBox*)gtk_builder_get_object(builder, "IDC_ETHDEV"), idx); gtk_combo_box_set_active((GtkComboBox*)gtk_builder_get_object(builder, "IDC_ETHDEV"), idx);
} }
@ -135,9 +141,12 @@ void OnSlide(GtkRange* range, gpointer usr_data)
void OnOk() void OnOk()
{ {
char* ptr = gtk_combo_box_text_get_active_text((GtkComboBoxText*)gtk_builder_get_object(builder, "IDC_ETHDEV")); int ethIndex = gtk_combo_box_get_active((GtkComboBox*)gtk_builder_get_object(builder, "IDC_ETHDEV"));
if (ptr != nullptr) if (ethIndex != -1)
strcpy(config.Eth, ptr); {
strcpy(config.Eth, adapters[ethIndex].guid.c_str());
config.EthApi = adapters[ethIndex].type;
}
strcpy(config.Hdd, gtk_entry_get_text((GtkEntry*)gtk_builder_get_object(builder, "IDC_HDDFILE"))); strcpy(config.Hdd, gtk_entry_get_text((GtkEntry*)gtk_builder_get_object(builder, "IDC_HDDFILE")));
config.HddSize = gtk_spin_button_get_value((GtkSpinButton*)gtk_builder_get_object(builder, "IDC_HDDSIZE_SPIN")) * 1024; config.HddSize = gtk_spin_button_get_value((GtkSpinButton*)gtk_builder_get_object(builder, "IDC_HDDSIZE_SPIN")) * 1024;

View File

@ -50,6 +50,7 @@ void SaveConf()
wchar_t wEth[sizeof(config.Eth)] = {0}; wchar_t wEth[sizeof(config.Eth)] = {0};
mbstowcs(wEth, config.Eth, sizeof(config.Eth) - 1); mbstowcs(wEth, config.Eth, sizeof(config.Eth) - 1);
WritePrivateProfileString(L"DEV9", L"Eth", wEth, file.c_str()); WritePrivateProfileString(L"DEV9", L"Eth", wEth, file.c_str());
WritePrivateProfileInt(L"DEV9", L"EthApi", (int)config.EthApi, file.c_str());
WritePrivateProfileString(L"DEV9", L"Hdd", config.Hdd, file.c_str()); WritePrivateProfileString(L"DEV9", L"Hdd", config.Hdd, file.c_str());
WritePrivateProfileInt(L"DEV9", L"HddSize", config.HddSize, file.c_str()); WritePrivateProfileInt(L"DEV9", L"HddSize", config.HddSize, file.c_str());
@ -67,6 +68,7 @@ void LoadConf()
mbstowcs(wEth, ETH_DEF, sizeof(config.Eth) - 1); mbstowcs(wEth, ETH_DEF, sizeof(config.Eth) - 1);
GetPrivateProfileString(L"DEV9", L"Eth", wEth, wEth, sizeof(config.Eth), file.c_str()); GetPrivateProfileString(L"DEV9", L"Eth", wEth, wEth, sizeof(config.Eth), file.c_str());
wcstombs(config.Eth, wEth, sizeof(config.Eth) - 1); wcstombs(config.Eth, wEth, sizeof(config.Eth) - 1);
config.EthApi = (NetApi)GetPrivateProfileInt(L"DEV9", L"EthApi", (int)NetApi::TAP, file.c_str());
GetPrivateProfileString(L"DEV9", L"Hdd", HDD_DEF, config.Hdd, sizeof(config.Hdd), file.c_str()); GetPrivateProfileString(L"DEV9", L"Hdd", HDD_DEF, config.Hdd, sizeof(config.Hdd), file.c_str());

View File

@ -19,6 +19,7 @@
#include <string> #include <string>
#include "ghc/filesystem.h" #include "ghc/filesystem.h"
#include "fmt/format.h"
#include "..\Config.h" #include "..\Config.h"
#include "resource.h" #include "resource.h"
@ -33,6 +34,7 @@
extern HINSTANCE hInst; extern HINSTANCE hInst;
//HANDLE handleDEV9Thread = NULL; //HANDLE handleDEV9Thread = NULL;
//DWORD dwThreadId, dwThrdParam; //DWORD dwThreadId, dwThrdParam;
std::vector<AdapterEntry> adapters;
void SysMessage(char* fmt, ...) void SysMessage(char* fmt, ...)
{ {
@ -47,31 +49,29 @@ void SysMessage(char* fmt, ...)
void OnInitDialog(HWND hW) void OnInitDialog(HWND hW)
{ {
char* dev;
//int i; //int i;
LoadConf(); LoadConf();
ComboBox_AddString(GetDlgItem(hW, IDC_BAYTYPE), "Expansion"); ComboBox_AddString(GetDlgItem(hW, IDC_BAYTYPE), "Expansion");
ComboBox_AddString(GetDlgItem(hW, IDC_BAYTYPE), "PC Card"); ComboBox_AddString(GetDlgItem(hW, IDC_BAYTYPE), "PC Card");
for (int i = 0; i < pcap_io_get_dev_num(); i++)
std::vector<AdapterEntry> tapAdapters = TAPAdapter::GetAdapters();
std::vector<AdapterEntry> pcapAdapters = PCAPAdapter::GetAdapters();
adapters.reserve(tapAdapters.size() + pcapAdapters.size());
adapters.insert(adapters.end(), tapAdapters.begin(), tapAdapters.end());
adapters.insert(adapters.end(), pcapAdapters.begin(), pcapAdapters.end());
for (size_t i = 0; i < adapters.size(); i++)
{ {
dev = pcap_io_get_dev_desc(i); std::wstring dev = fmt::format(L"{}: {}", (wchar_t*)NetApiToWstring(adapters[i].type), adapters[i].name.c_str());
int itm = ComboBox_AddString(GetDlgItem(hW, IDC_ETHDEV), dev); int itm = ComboBox_AddString(GetDlgItem(hW, IDC_ETHDEV), dev.c_str());
ComboBox_SetItemData(GetDlgItem(hW, IDC_ETHDEV), itm, _strdup(pcap_io_get_dev_name(i))); ComboBox_SetItemData(GetDlgItem(hW, IDC_ETHDEV), itm, i);
if (strcmp(pcap_io_get_dev_name(i), config.Eth) == 0)
{
ComboBox_SetCurSel(GetDlgItem(hW, IDC_ETHDEV), itm);
}
}
vector<tap_adapter>* al = GetTapAdapters();
for (size_t i = 0; i < al->size(); i++)
{
int itm = ComboBox_AddString(GetDlgItem(hW, IDC_ETHDEV), al[0][i].name);
char guid_char[256]; char guid_char[256];
wcstombs(guid_char, al[0][i].guid, wcslen(al[0][i].guid) + 1); wcstombs(guid_char, adapters[i].guid.c_str(), wcslen(adapters[i].guid.c_str()) + 1);
ComboBox_SetItemData(GetDlgItem(hW, IDC_ETHDEV), itm, _strdup(guid_char)); //TODO, also check type
if (strcmp(guid_char, config.Eth) == 0) if (config.EthApi == adapters[i].type && strcmp(guid_char, config.Eth) == 0)
{ {
ComboBox_SetCurSel(GetDlgItem(hW, IDC_ETHDEV), itm); ComboBox_SetCurSel(GetDlgItem(hW, IDC_ETHDEV), itm);
} }
@ -177,14 +177,17 @@ void OnOk(HWND hW)
{ {
//user not planning on using //user not planning on using
//ethernet anyway //ethernet anyway
config.EthApi = NetApi::Unset;
strcpy(config.Eth, ETH_DEF); strcpy(config.Eth, ETH_DEF);
} }
} }
else else
{ {
//adapter is selected //adapter is selected
char* ptr = (char*)ComboBox_GetItemData(GetDlgItem(hW, IDC_ETHDEV), i); config.EthApi = adapters[i].type;
strcpy(config.Eth, ptr); char guid_char[256];
wcstombs(guid_char, adapters[i].guid.c_str(), wcslen(adapters[i].guid.c_str()) + 1);
strcpy(config.Eth, guid_char);
} }
GetWindowText(GetDlgItem(hW, IDC_HDDFILE), config.Hdd, 256); GetWindowText(GetDlgItem(hW, IDC_HDDFILE), config.Hdd, 256);
@ -229,6 +232,7 @@ void OnOk(HWND hW)
SaveConf(); SaveConf();
adapters.clear();
EndDialog(hW, TRUE); EndDialog(hW, TRUE);
} }
@ -245,6 +249,7 @@ BOOL CALLBACK ConfigureDlgProc(HWND hW, UINT uMsg, WPARAM wParam, LPARAM lParam)
switch (LOWORD(wParam)) switch (LOWORD(wParam))
{ {
case IDCANCEL: case IDCANCEL:
adapters.clear();
EndDialog(hW, FALSE); EndDialog(hW, FALSE);
return TRUE; return TRUE;
case IDOK: case IDOK:

View File

@ -129,9 +129,9 @@ bool IsTAPDevice(const TCHAR* guid)
return false; return false;
} }
vector<tap_adapter>* GetTapAdapters() std::vector<AdapterEntry> TAPAdapter::GetAdapters()
{ {
vector<tap_adapter>* tap_nic = new vector<tap_adapter>(); std::vector<AdapterEntry> tap_nic;
LONG status; LONG status;
HKEY control_net_key; HKEY control_net_key;
DWORD len; DWORD len;
@ -141,13 +141,13 @@ vector<tap_adapter>* GetTapAdapters()
&control_net_key); &control_net_key);
if (status != ERROR_SUCCESS) if (status != ERROR_SUCCESS)
return false; return tap_nic;
status = RegQueryInfoKey(control_net_key, nullptr, nullptr, nullptr, &cSubKeys, nullptr, nullptr, status = RegQueryInfoKey(control_net_key, nullptr, nullptr, nullptr, &cSubKeys, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, nullptr); nullptr, nullptr, nullptr, nullptr, nullptr);
if (status != ERROR_SUCCESS) if (status != ERROR_SUCCESS)
return false; return tap_nic;
for (DWORD i = 0; i < cSubKeys; i++) for (DWORD i = 0; i < cSubKeys; i++)
{ {
@ -183,8 +183,11 @@ vector<tap_adapter>* GetTapAdapters()
{ {
if (IsTAPDevice(enum_name)) if (IsTAPDevice(enum_name))
{ {
tap_adapter t = {_tcsdup(name_data), _tcsdup(enum_name)}; AdapterEntry t;
tap_nic->push_back(t); t.type = NetApi::TAP;
t.name = std::wstring(name_data);
t.guid = std::wstring(enum_name);
tap_nic.push_back(t);
} }
} }

View File

@ -19,12 +19,6 @@
#include "..\net.h" #include "..\net.h"
using namespace std; using namespace std;
struct tap_adapter
{
TCHAR *name;
TCHAR *guid;
};
vector<tap_adapter>* GetTapAdapters();
class TAPAdapter : public NetAdapter class TAPAdapter : public NetAdapter
{ {
HANDLE htap; HANDLE htap;
@ -42,4 +36,5 @@ public:
virtual bool send(NetPacket* pkt); virtual bool send(NetPacket* pkt);
virtual void close(); virtual void close();
virtual ~TAPAdapter(); virtual ~TAPAdapter();
static std::vector<AdapterEntry> GetAdapters();
}; };

View File

@ -22,6 +22,10 @@
#endif #endif
#include "net.h" #include "net.h"
#include "DEV9.h" #include "DEV9.h"
#ifdef _WIN32
#include "Win32/tap.h"
#endif
#include "pcap_io.h"
#ifdef _WIN32 #ifdef _WIN32
#include "Win32\tap.h" #include "Win32\tap.h"
@ -55,16 +59,27 @@ void tx_put(NetPacket* pkt)
NetAdapter* GetNetAdapter() NetAdapter* GetNetAdapter()
{ {
NetAdapter* na = nullptr;
switch (config.EthApi)
{
#ifdef _WIN32 #ifdef _WIN32
NetAdapter* na = static_cast<NetAdapter*>(new TAPAdapter()); case NetApi::TAP:
na = static_cast<NetAdapter*>(new TAPAdapter());
break;
#else #else
NetAdapter* na = static_cast<NetAdapter*>(new PCAPAdapter()); case NetApi::PCAP_Switched:
na = static_cast<NetAdapter*>(new PCAPAdapter());
break;
default:
return 0;
#endif #endif
}
if (!na->isInitialised()) if (!na->isInitialised())
{ {
delete na; delete na;
return nullptr; return 0;
} }
return na; return na;
} }
@ -113,9 +128,40 @@ void TermNet()
} }
} }
const char* NetApiToString(NetApi api)
{
switch (api)
{
case NetApi::PCAP_Bridged:
return "PCAP (Bridged)";
case NetApi::PCAP_Switched:
return "PCAP (Switched)";
case NetApi::TAP:
return "TAP";
default:
return "UNK";
}
}
const wchar_t* NetApiToWstring(NetApi api)
{
switch (api)
{
case NetApi::PCAP_Bridged:
return L"PCAP (Bridged)";
case NetApi::PCAP_Switched:
return L"PCAP (Switched)";
case NetApi::TAP:
return L"TAP";
default:
return L"UNK";
}
}
NetAdapter::NetAdapter() NetAdapter::NetAdapter()
{ {
//Ensure eeprom matches our default //Ensure eeprom matches our default
SetMACAddress(nullptr); SetMACAddress(nullptr);
} }

View File

@ -15,7 +15,7 @@
#pragma once #pragma once
#include <stdlib.h> #include <stdlib.h>
#include <string.h> //uh isnt memcpy @ stdlib ? #include <string>
// first three recognized by Xlink as Sony PS2 // first three recognized by Xlink as Sony PS2
const u8 defaultMAC[6] = {0x00, 0x04, 0x1F, 0x82, 0x30, 0x31}; const u8 defaultMAC[6] = {0x00, 0x04, 0x1F, 0x82, 0x30, 0x31};
@ -37,6 +37,26 @@ extern mtfifo<NetPacket*> rx_fifo;
extern mtfifo<NetPacket*> tx_fifo; extern mtfifo<NetPacket*> tx_fifo;
*/ */
enum struct NetApi : int
{
Unset = 0,
PCAP_Bridged = 1,
PCAP_Switched = 2,
TAP = 3,
};
struct AdapterEntry
{
NetApi type;
#ifdef _WIN32
std::wstring name;
std::wstring guid;
#else
std::string name;
std::string guid;
#endif
};
class NetAdapter class NetAdapter
{ {
protected: protected:
@ -58,3 +78,6 @@ protected:
void tx_put(NetPacket* ptr); void tx_put(NetPacket* ptr);
void InitNet(); void InitNet();
void TermNet(); void TermNet();
const char* NetApiToString(NetApi api);
const wchar_t* NetApiToWstring(NetApi api);

View File

@ -393,3 +393,32 @@ PCAPAdapter::~PCAPAdapter()
{ {
pcap_io_close(); pcap_io_close();
} }
std::vector<AdapterEntry> PCAPAdapter::GetAdapters()
{
std::vector<AdapterEntry> nic;
#ifndef _WIN32
pcap_if_t* alldevs;
pcap_if_t* d;
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
return nic;
}
d = alldevs;
while (d != NULL)
{
AdapterEntry entry;
entry.type = NetApi::PCAP_Switched;
entry.name = std::string(d->name);
entry.guid = std::string(d->name);
nic.push_back(entry);
d = d->next;
}
#endif
return nic;
}

View File

@ -182,4 +182,5 @@ public:
//sends the packet and deletes it when done (if successful).rv :true success //sends the packet and deletes it when done (if successful).rv :true success
virtual bool send(NetPacket* pkt); virtual bool send(NetPacket* pkt);
virtual ~PCAPAdapter(); virtual ~PCAPAdapter();
static std::vector<AdapterEntry> GetAdapters();
}; };