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;
if (DirectMode)
ret = Net_PCap::Init(true);
ret = Net_PCap::Init();
else
ret = Net_Slirp::Init();

View File

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

View File

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

View File

@ -162,7 +162,7 @@ bool Init()
*(u32*)&cfg.vnameserver = htonl(kDNSIP);
Ctx = slirp_new(&cfg, &cb, nullptr);
printf("SLIRP INIT\n");
return true;
}
@ -373,7 +373,6 @@ void HandleDNSFrame(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 (len > 2048)

View File

@ -24,9 +24,7 @@
#include "Config.h"
#include "main.h"
#include "Net_Slirp.h"
#include "Net_PCap.h"
#include "Wifi.h"
#include "Net.h"
#include "WifiSettingsDialog.h"
#include "ui_WifiSettingsDialog.h"
@ -52,7 +50,8 @@ WifiSettingsDialog::WifiSettingsDialog(QWidget* parent) : QDialog(parent), ui(ne
emuInstance = ((MainWindow*)parent)->getEmuInstance();
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)");
@ -109,6 +108,9 @@ void WifiSettingsDialog::done(int r)
Config::Save();
}
Net_PCap::DeInit();
Net::Init();
QDialog::done(r);
closeDlg();

View File

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