touch up net init/deinit stuff

This commit is contained in:
Arisotura 2024-06-13 18:36:25 +02:00
parent 31480fea98
commit dfd89fdef4
6 changed files with 28 additions and 40 deletions

View File

@ -49,7 +49,7 @@ bool Init()
bool ret = false; bool ret = false;
if (DirectMode) if (DirectMode)
ret = Net_PCap::Init(true); ret = Net_PCap::Init();
else else
ret = Net_Slirp::Init(); ret = Net_Slirp::Init();

View File

@ -88,10 +88,6 @@ Platform::DynamicLibrary* PCapLib = nullptr;
pcap_t* PCapAdapter = nullptr; pcap_t* PCapAdapter = nullptr;
AdapterData* PCapAdapterData; AdapterData* PCapAdapterData;
u8 PacketBuffer[2048];
int PacketLen;
volatile int RXNum;
#define LOAD_PCAP_FUNC(sym) \ #define LOAD_PCAP_FUNC(sym) \
ptr_##sym = (type_##sym)DynamicLibrary_LoadFunction(lib, #sym); \ ptr_##sym = (type_##sym)DynamicLibrary_LoadFunction(lib, #sym); \
@ -111,18 +107,15 @@ bool TryLoadPCap(Platform::DynamicLibrary *lib)
return true; return true;
} }
bool Init(bool open_adapter) bool InitAdapterList()
{ {
PCapAdapter = nullptr;
PacketLen = 0;
RXNum = 0;
NumAdapters = 0; NumAdapters = 0;
// TODO: how to deal with cases where an adapter is unplugged or changes config?? // TODO: how to deal with cases where an adapter is unplugged or changes config??
if (!PCapLib) if (!PCapLib)
{ {
PCapLib = nullptr; PCapLib = nullptr;
PCapAdapter = nullptr;
for (int i = 0; PCapLibNames[i]; i++) for (int i = 0; PCapLibNames[i]; i++)
{ {
@ -168,18 +161,10 @@ bool Init(bool open_adapter)
dev = alldevs; dev = alldevs;
while (dev) while (dev)
{ {
adata->Internal = dev;
#ifdef __WIN32__
// hax
int len = strlen(dev->name);
len -= 12; if (len > 127) len = 127;
strncpy(adata->DeviceName, &dev->name[12], len);
adata->DeviceName[len] = '\0';
#else
strncpy(adata->DeviceName, dev->name, 127); strncpy(adata->DeviceName, dev->name, 127);
adata->DeviceName[127] = '\0'; adata->DeviceName[127] = '\0';
#ifndef __WIN32__
strncpy(adata->FriendlyName, adata->DeviceName, 127); strncpy(adata->FriendlyName, adata->DeviceName, 127);
adata->FriendlyName[127] = '\0'; adata->FriendlyName[127] = '\0';
#endif // __WIN32__ #endif // __WIN32__
@ -211,7 +196,7 @@ bool Init(bool open_adapter)
IP_ADAPTER_ADDRESSES* addr = buf; IP_ADAPTER_ADDRESSES* addr = buf;
while (addr) while (addr)
{ {
if (strcmp(addr->AdapterName, adata->DeviceName)) if (strcmp(addr->AdapterName, &adata->DeviceName[12]))
{ {
addr = addr->Next; addr = addr->Next;
continue; continue;
@ -283,7 +268,7 @@ bool Init(bool open_adapter)
struct sockaddr_in* sa = (sockaddr_in*)curaddr->ifa_addr; struct sockaddr_in* sa = (sockaddr_in*)curaddr->ifa_addr;
memcpy(adata->IP_v4, &sa->sin_addr, 4); memcpy(adata->IP_v4, &sa->sin_addr, 4);
} }
#ifdef __linux__ #ifdef __linux__
else if (af == AF_PACKET) else if (af == AF_PACKET)
{ {
struct sockaddr_ll* sa = (sockaddr_ll*)curaddr->ifa_addr; struct sockaddr_ll* sa = (sockaddr_ll*)curaddr->ifa_addr;
@ -292,7 +277,7 @@ bool Init(bool open_adapter)
else else
memcpy(adata->MAC, sa->sll_addr, 6); memcpy(adata->MAC, sa->sll_addr, 6);
} }
#else #else
else if (af == AF_LINK) else if (af == AF_LINK)
{ {
struct sockaddr_dl* sa = (sockaddr_dl*)curaddr->ifa_addr; struct sockaddr_dl* sa = (sockaddr_dl*)curaddr->ifa_addr;
@ -301,7 +286,7 @@ bool Init(bool open_adapter)
else else
memcpy(adata->MAC, LLADDR(sa), 6); memcpy(adata->MAC, LLADDR(sa), 6);
} }
#endif #endif
curaddr = curaddr->ifa_next; curaddr = curaddr->ifa_next;
} }
} }
@ -310,9 +295,17 @@ bool Init(bool open_adapter)
#endif // __WIN32__ #endif // __WIN32__
if (!open_adapter) return true; pcap_freealldevs(alldevs);
return true;
}
bool Init()
{
if (!PCapLib) PCapAdapter = nullptr;
if (PCapAdapter) pcap_close(PCapAdapter); if (PCapAdapter) pcap_close(PCapAdapter);
InitAdapterList();
// open pcap device // open pcap device
Config::Table cfg = Config::GetGlobalTable(); Config::Table cfg = Config::GetGlobalTable();
std::string devicename = cfg.GetString("LAN.Device"); std::string devicename = cfg.GetString("LAN.Device");
@ -323,16 +316,14 @@ bool Init(bool open_adapter)
PCapAdapterData = &Adapters[i]; PCapAdapterData = &Adapters[i];
} }
dev = (pcap_if_t*)PCapAdapterData->Internal; char errbuf[PCAP_ERRBUF_SIZE];
PCapAdapter = pcap_open_live(dev->name, 2048, PCAP_OPENFLAG_PROMISCUOUS, 1, errbuf); PCapAdapter = pcap_open_live(PCapAdapterData->DeviceName, 2048, PCAP_OPENFLAG_PROMISCUOUS, 1, errbuf);
if (!PCapAdapter) if (!PCapAdapter)
{ {
Log(LogLevel::Error, "PCap: failed to open adapter %s\n", errbuf); Log(LogLevel::Error, "PCap: failed to open adapter %s\n", errbuf);
return false; return false;
} }
pcap_freealldevs(alldevs);
if (pcap_setnonblock(PCapAdapter, 1, errbuf) < 0) if (pcap_setnonblock(PCapAdapter, 1, errbuf) < 0)
{ {
Log(LogLevel::Error, "PCap: failed to set nonblocking mode\n"); Log(LogLevel::Error, "PCap: failed to set nonblocking mode\n");

View File

@ -33,8 +33,6 @@ struct AdapterData
u8 MAC[6]; u8 MAC[6];
u8 IP_v4[4]; u8 IP_v4[4];
void* Internal;
}; };
@ -42,7 +40,8 @@ extern AdapterData* Adapters;
extern int NumAdapters; extern int NumAdapters;
bool Init(bool open_adapter); bool InitAdapterList();
bool Init();
void DeInit(); void DeInit();
int SendPacket(u8* data, int len); int SendPacket(u8* data, int len);

View File

@ -162,7 +162,7 @@ bool Init()
*(u32*)&cfg.vnameserver = htonl(kDNSIP); *(u32*)&cfg.vnameserver = htonl(kDNSIP);
Ctx = slirp_new(&cfg, &cb, nullptr); Ctx = slirp_new(&cfg, &cb, nullptr);
printf("SLIRP INIT\n");
return true; return true;
} }
@ -373,7 +373,6 @@ void HandleDNSFrame(u8* data, int len)
int SendPacket(u8* data, int len) int SendPacket(u8* data, int len)
{ {
printf("SLIRP SEND PACKET %p %p %d\n", Ctx, data, len);
if (!Ctx) return 0; if (!Ctx) return 0;
if (len > 2048) if (len > 2048)

View File

@ -24,9 +24,7 @@
#include "Config.h" #include "Config.h"
#include "main.h" #include "main.h"
#include "Net_Slirp.h" #include "Net.h"
#include "Net_PCap.h"
#include "Wifi.h"
#include "WifiSettingsDialog.h" #include "WifiSettingsDialog.h"
#include "ui_WifiSettingsDialog.h" #include "ui_WifiSettingsDialog.h"
@ -52,7 +50,8 @@ WifiSettingsDialog::WifiSettingsDialog(QWidget* parent) : QDialog(parent), ui(ne
emuInstance = ((MainWindow*)parent)->getEmuInstance(); emuInstance = ((MainWindow*)parent)->getEmuInstance();
auto& cfg = emuInstance->getGlobalConfig(); auto& cfg = emuInstance->getGlobalConfig();
haspcap = Net_PCap::Init(false); Net::DeInit();
haspcap = Net_PCap::InitAdapterList();
ui->rbDirectMode->setText("Direct mode (requires " PCAP_NAME " and ethernet connection)"); ui->rbDirectMode->setText("Direct mode (requires " PCAP_NAME " and ethernet connection)");
@ -109,6 +108,9 @@ void WifiSettingsDialog::done(int r)
Config::Save(); Config::Save();
} }
Net_PCap::DeInit();
Net::Init();
QDialog::done(r); QDialog::done(r);
closeDlg(); closeDlg();

View File

@ -1853,9 +1853,6 @@ void MainWindow::onOpenWifiSettings()
void MainWindow::onWifiSettingsFinished(int res) void MainWindow::onWifiSettingsFinished(int res)
{ {
//Platform::LAN_DeInit();
//Platform::LAN_Init();
if (WifiSettingsDialog::needsReset) if (WifiSettingsDialog::needsReset)
onReset(); onReset();