DEV9: Move reconfiguration code into net.cpp

Also make the internal server aware of the reconfiguration
This commit is contained in:
TheLastRar 2021-03-18 23:05:35 +00:00 committed by lightningterror
parent 36406e2fd9
commit 221a8dc530
8 changed files with 82 additions and 21 deletions

View File

@ -1090,26 +1090,10 @@ void ApplyConfigIfRunning(Config oldConfig)
return; return;
//Eth //Eth
if (config.ethEnable) ReconfigureLiveNet(&oldConfig);
{
if (oldConfig.ethEnable)
{
//Reload Net if adapter changed
if (strcmp(oldConfig.Eth, config.Eth) != 0 ||
oldConfig.EthApi != config.EthApi)
{
TermNet();
InitNet();
}
}
else
InitNet();
}
else if (oldConfig.ethEnable)
TermNet();
//Hdd //Hdd
//Hdd Validate Path //Hdd Validate Path
#ifdef _WIN32 #ifdef _WIN32
ghc::filesystem::path hddPath(std::wstring(config.Hdd)); ghc::filesystem::path hddPath(std::wstring(config.Hdd));
#else #else

View File

@ -56,7 +56,7 @@ bool rx_fifo_can_rx();
#define HDD_MIN_GB 8 #define HDD_MIN_GB 8
#define HDD_MAX_GB 120 #define HDD_MAX_GB 120
typedef struct struct Config
{ {
char Eth[256]; char Eth[256];
NetApi EthApi; NetApi EthApi;
@ -69,7 +69,7 @@ typedef struct
int hddEnable; int hddEnable;
int ethEnable; int ethEnable;
} Config; };
EXTERN Config config; EXTERN Config config;

View File

@ -665,6 +665,17 @@ bool TAPAdapter::send(NetPacket* pkt)
else else
return false; return false;
} }
void TAPAdapter::reloadSettings()
{
IP_ADAPTER_ADDRESSES adapter;
std::unique_ptr<IP_ADAPTER_ADDRESSES[]> buffer;
if (TAPGetWin32Adapter(config.Eth, &adapter, &buffer))
ReloadInternalServer(&adapter);
else
ReloadInternalServer(nullptr);
}
void TAPAdapter::close() void TAPAdapter::close()
{ {
SetEvent(cancel); SetEvent(cancel);

View File

@ -34,6 +34,7 @@ public:
virtual bool recv(NetPacket* pkt); virtual bool recv(NetPacket* pkt);
//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 void reloadSettings();
virtual void close(); virtual void close();
virtual ~TAPAdapter(); virtual ~TAPAdapter();
static std::vector<AdapterEntry> GetAdapters(); static std::vector<AdapterEntry> GetAdapters();

View File

@ -121,6 +121,31 @@ void InitNet()
#endif #endif
} }
void ReconfigureLiveNet(Config* oldConfig)
{
//Eth
if (config.ethEnable)
{
if (oldConfig->ethEnable)
{
//Reload Net if adapter changed
if (strcmp(oldConfig->Eth, config.Eth) != 0 ||
oldConfig->EthApi != config.EthApi)
{
TermNet();
InitNet();
return;
}
else
nif->reloadSettings();
}
else
InitNet();
}
else if (oldConfig->ethEnable)
TermNet();
}
void TermNet() void TermNet()
{ {
if (RxRunning) if (RxRunning)
@ -257,6 +282,16 @@ void NetAdapter::InitInternalServer(ifaddrs* adapter)
} }
} }
#ifdef _WIN32
void NetAdapter::ReloadInternalServer(PIP_ADAPTER_ADDRESSES adapter)
#elif defined(__POSIX__)
void NetAdapter::ReloadInternalServer(ifaddrs* adapter)
#endif
{
if (adapter == nullptr)
Console.Error("DEV9: ReloadInternalServer() got nullptr for adapter");
}
bool NetAdapter::InternalServerRecv(NetPacket* pkt) bool NetAdapter::InternalServerRecv(NetPacket* pkt)
{ {
return false; return false;

View File

@ -33,6 +33,8 @@
#include "PacketReader/IP/IP_Address.h" #include "PacketReader/IP/IP_Address.h"
struct Config;
// 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};
@ -97,6 +99,7 @@ public:
virtual bool isInitialised() = 0; virtual bool isInitialised() = 0;
virtual bool recv(NetPacket* pkt); //gets a packet virtual bool recv(NetPacket* pkt); //gets a packet
virtual bool send(NetPacket* pkt); //sends the packet and deletes it when done virtual bool send(NetPacket* pkt); //sends the packet and deletes it when done
virtual void reloadSettings() = 0;
virtual void close(){}; virtual void close(){};
virtual ~NetAdapter(); virtual ~NetAdapter();
@ -106,8 +109,10 @@ protected:
#ifdef _WIN32 #ifdef _WIN32
void InitInternalServer(PIP_ADAPTER_ADDRESSES adapter); void InitInternalServer(PIP_ADAPTER_ADDRESSES adapter);
void ReloadInternalServer(PIP_ADAPTER_ADDRESSES adapter);
#elif defined(__POSIX__) #elif defined(__POSIX__)
void InitInternalServer(ifaddrs* adapter); void InitInternalServer(ifaddrs* adapter);
void ReloadInternalServer(ifaddrs* adapter);
#endif #endif
private: private:
@ -120,6 +125,7 @@ private:
void tx_put(NetPacket* ptr); void tx_put(NetPacket* ptr);
void InitNet(); void InitNet();
void ReconfigureLiveNet(Config* oldConfig);
void TermNet(); void TermNet();
const char* NetApiToString(NetApi api); const char* NetApiToString(NetApi api);

View File

@ -427,6 +427,29 @@ bool PCAPAdapter::send(NetPacket* pkt)
return true; return true;
} }
} }
void PCAPAdapter::reloadSettings()
{
#ifdef _WIN32
IP_ADAPTER_ADDRESSES adapter;
std::unique_ptr<IP_ADAPTER_ADDRESSES[]> buffer;
if (PCAPGetWin32Adapter(config.Eth, &adapter, &buffer))
ReloadInternalServer(&adapter);
else
ReloadInternalServer(nullptr);
#elif defined(__POSIX__)
ifaddrs adapter;
ifaddrs* buffer;
if (PCAPGetIfAdapter(config.Eth, &adapter, &buffer))
{
ReloadInternalServer(&adapter);
freeifaddrs(buffer);
}
else
ReloadInternalServer(nullptr);
#endif
}
PCAPAdapter::~PCAPAdapter() PCAPAdapter::~PCAPAdapter()
{ {
pcap_io_close(); pcap_io_close();

View File

@ -181,6 +181,7 @@ public:
virtual bool recv(NetPacket* pkt); virtual bool recv(NetPacket* pkt);
//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 void reloadSettings();
virtual ~PCAPAdapter(); virtual ~PCAPAdapter();
static std::vector<AdapterEntry> GetAdapters(); static std::vector<AdapterEntry> GetAdapters();
}; };