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)
{
//Reload Net if adapter changed
if (strcmp(oldConfig.Eth, config.Eth) != 0)
if (strcmp(oldConfig.Eth, config.Eth) != 0 ||
oldConfig.EthApi != config.EthApi)
{
TermNet();
InitNet();

View File

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

View File

@ -46,6 +46,10 @@ void SaveConf()
xmlNewChild(root_node, NULL, BAD_CAST "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",
BAD_CAST config.Hdd);
@ -102,6 +106,7 @@ void LoadConf()
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)
{
if (cur_node->type == XML_ELEMENT_NODE)
@ -111,6 +116,11 @@ void LoadConf()
{
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"))
{
strcpy(config.Hdd, (const char*)xmlNodeGetContent(cur_node));
@ -130,6 +140,9 @@ void LoadConf()
}
}
if (!foundEthType)
config.EthApi = NetApi::PCAP_Switched;
// free(configFile);
xmlFreeDoc(doc);
xmlCleanupParser();

View File

@ -23,6 +23,8 @@
#include <sys/types.h>
#include <pwd.h>
#include <string.h>
#include <vector>
#include "fmt/format.h"
#include <string>
#include "ghc/filesystem.h"
@ -37,10 +39,10 @@
#include "../ATA/HddCreate.h"
static GtkBuilder* builder = nullptr;
std::vector<AdapterEntry> adapters;
void OnInitDialog()
{
char* dev;
gint idx = 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"), "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);
gtk_combo_box_text_append_text((GtkComboBoxText*)gtk_builder_get_object(builder, "IDC_ETHDEV"), dev);
if (strcmp(dev, config.Eth) == 0)
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.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);
}
@ -135,9 +141,12 @@ void OnSlide(GtkRange* range, gpointer usr_data)
void OnOk()
{
char* ptr = gtk_combo_box_text_get_active_text((GtkComboBoxText*)gtk_builder_get_object(builder, "IDC_ETHDEV"));
if (ptr != nullptr)
strcpy(config.Eth, ptr);
int ethIndex = gtk_combo_box_get_active((GtkComboBox*)gtk_builder_get_object(builder, "IDC_ETHDEV"));
if (ethIndex != -1)
{
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")));
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};
mbstowcs(wEth, config.Eth, sizeof(config.Eth) - 1);
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());
WritePrivateProfileInt(L"DEV9", L"HddSize", config.HddSize, file.c_str());
@ -67,6 +68,7 @@ void LoadConf()
mbstowcs(wEth, ETH_DEF, sizeof(config.Eth) - 1);
GetPrivateProfileString(L"DEV9", L"Eth", wEth, wEth, sizeof(config.Eth), file.c_str());
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());

View File

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

View File

@ -129,9 +129,9 @@ bool IsTAPDevice(const TCHAR* guid)
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;
HKEY control_net_key;
DWORD len;
@ -141,13 +141,13 @@ vector<tap_adapter>* GetTapAdapters()
&control_net_key);
if (status != ERROR_SUCCESS)
return false;
return tap_nic;
status = RegQueryInfoKey(control_net_key, nullptr, nullptr, nullptr, &cSubKeys, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, nullptr);
if (status != ERROR_SUCCESS)
return false;
return tap_nic;
for (DWORD i = 0; i < cSubKeys; i++)
{
@ -183,8 +183,11 @@ vector<tap_adapter>* GetTapAdapters()
{
if (IsTAPDevice(enum_name))
{
tap_adapter t = {_tcsdup(name_data), _tcsdup(enum_name)};
tap_nic->push_back(t);
AdapterEntry 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"
using namespace std;
struct tap_adapter
{
TCHAR *name;
TCHAR *guid;
};
vector<tap_adapter>* GetTapAdapters();
class TAPAdapter : public NetAdapter
{
HANDLE htap;
@ -42,4 +36,5 @@ public:
virtual bool send(NetPacket* pkt);
virtual void close();
virtual ~TAPAdapter();
static std::vector<AdapterEntry> GetAdapters();
};

View File

@ -22,6 +22,10 @@
#endif
#include "net.h"
#include "DEV9.h"
#ifdef _WIN32
#include "Win32/tap.h"
#endif
#include "pcap_io.h"
#ifdef _WIN32
#include "Win32\tap.h"
@ -55,16 +59,27 @@ void tx_put(NetPacket* pkt)
NetAdapter* GetNetAdapter()
{
NetAdapter* na = nullptr;
switch (config.EthApi)
{
#ifdef _WIN32
NetAdapter* na = static_cast<NetAdapter*>(new TAPAdapter());
case NetApi::TAP:
na = static_cast<NetAdapter*>(new TAPAdapter());
break;
#else
NetAdapter* na = static_cast<NetAdapter*>(new PCAPAdapter());
case NetApi::PCAP_Switched:
na = static_cast<NetAdapter*>(new PCAPAdapter());
break;
default:
return 0;
#endif
}
if (!na->isInitialised())
{
delete na;
return nullptr;
return 0;
}
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()
{
//Ensure eeprom matches our default
//Ensure eeprom matches our default
SetMACAddress(nullptr);
}

View File

@ -15,7 +15,7 @@
#pragma once
#include <stdlib.h>
#include <string.h> //uh isnt memcpy @ stdlib ?
#include <string>
// first three recognized by Xlink as Sony PS2
const u8 defaultMAC[6] = {0x00, 0x04, 0x1F, 0x82, 0x30, 0x31};
@ -37,6 +37,26 @@ extern mtfifo<NetPacket*> rx_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
{
protected:
@ -58,3 +78,6 @@ protected:
void tx_put(NetPacket* ptr);
void InitNet();
void TermNet();
const char* NetApiToString(NetApi api);
const wchar_t* NetApiToWstring(NetApi api);

View File

@ -393,3 +393,32 @@ PCAPAdapter::~PCAPAdapter()
{
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
virtual bool send(NetPacket* pkt);
virtual ~PCAPAdapter();
static std::vector<AdapterEntry> GetAdapters();
};