DEV9: Re-enable windows support

This commit is contained in:
TheLastRar 2021-01-12 08:33:41 +00:00 committed by refractionpcsx2
parent df6c0b610b
commit 1144cc9ad9
3 changed files with 33 additions and 31 deletions

View File

@ -67,13 +67,12 @@ NetAdapter* GetNetAdapter()
case NetApi::TAP: case NetApi::TAP:
na = static_cast<NetAdapter*>(new TAPAdapter()); na = static_cast<NetAdapter*>(new TAPAdapter());
break; break;
#else #endif
case NetApi::PCAP_Switched: case NetApi::PCAP_Switched:
na = static_cast<NetAdapter*>(new PCAPAdapter()); na = static_cast<NetAdapter*>(new PCAPAdapter());
break; break;
default: default:
return 0; return 0;
#endif
} }
if (!na->isInitialised()) if (!na->isInitialised())

View File

@ -35,14 +35,11 @@
#define PCAP_NETMASK_UNKNOWN 0xffffffff #define PCAP_NETMASK_UNKNOWN 0xffffffff
#endif #endif
#ifdef _WIN32
#define mac_address char*
#else
pcap_t* adhandle; pcap_t* adhandle;
pcap_dumper_t* dump_pcap; pcap_dumper_t* dump_pcap;
char errbuf[PCAP_ERRBUF_SIZE]; char errbuf[PCAP_ERRBUF_SIZE];
mac_address broadcast_mac = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; mac_address broadcast_mac = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
#endif
int pcap_io_running = 0; int pcap_io_running = 0;
extern u8 eeprom[]; extern u8 eeprom[];
@ -114,7 +111,6 @@ int GetMACAddress(char* adapter, mac_address* addr)
int pcap_io_init(char* adapter, mac_address virtual_mac) int pcap_io_init(char* adapter, mac_address virtual_mac)
{ {
#ifndef _WIN32
struct bpf_program fp; struct bpf_program fp;
char filter[1024] = "ether broadcast or ether dst "; char filter[1024] = "ether broadcast or ether dst ";
int dlt; int dlt;
@ -172,7 +168,6 @@ int pcap_io_init(char* adapter, mac_address virtual_mac)
pcap_io_running = 1; pcap_io_running = 1;
Console.WriteLn("Adapter Ok."); Console.WriteLn("Adapter Ok.");
#endif
return 0; return 0;
} }
@ -190,7 +185,6 @@ int gettimeofday(struct timeval* tv, void* tz)
int pcap_io_send(void* packet, int plen) int pcap_io_send(void* packet, int plen)
{ {
#ifndef _WIN32
if (pcap_io_running <= 0) if (pcap_io_running <= 0)
return -1; return -1;
@ -204,13 +198,10 @@ int pcap_io_send(void* packet, int plen)
} }
return pcap_sendpacket(adhandle, (u_char*)packet, plen); return pcap_sendpacket(adhandle, (u_char*)packet, plen);
#endif
return 0;
} }
int pcap_io_recv(void* packet, int max_len) int pcap_io_recv(void* packet, int max_len)
{ {
#ifndef _WIN32
static struct pcap_pkthdr* header; static struct pcap_pkthdr* header;
static const u_char* pkt_data1; static const u_char* pkt_data1;
@ -226,26 +217,22 @@ int pcap_io_recv(void* packet, int max_len)
return header->len; return header->len;
} }
#endif
return -1; return -1;
} }
void pcap_io_close() void pcap_io_close()
{ {
#ifndef _WIN32
if (dump_pcap) if (dump_pcap)
pcap_dump_close(dump_pcap); pcap_dump_close(dump_pcap);
if (adhandle) if (adhandle)
pcap_close(adhandle); pcap_close(adhandle);
pcap_io_running = 0; pcap_io_running = 0;
#endif
} }
int pcap_io_get_dev_num() int pcap_io_get_dev_num()
{ {
int i = 0; int i = 0;
#ifndef _WIN32
pcap_if_t* alldevs; pcap_if_t* alldevs;
pcap_if_t* d; pcap_if_t* d;
@ -263,13 +250,11 @@ int pcap_io_get_dev_num()
pcap_freealldevs(alldevs); pcap_freealldevs(alldevs);
#endif
return i; return i;
} }
char* pcap_io_get_dev_name(int num) char* pcap_io_get_dev_name(int num)
{ {
#ifndef _WIN32
pcap_if_t* alldevs; pcap_if_t* alldevs;
pcap_if_t* d; pcap_if_t* d;
int i = 0; int i = 0;
@ -293,13 +278,11 @@ char* pcap_io_get_dev_name(int num)
} }
pcap_freealldevs(alldevs); pcap_freealldevs(alldevs);
#endif
return NULL; return NULL;
} }
char* pcap_io_get_dev_desc(int num) char* pcap_io_get_dev_desc(int num)
{ {
#ifndef _WIN32
pcap_if_t* alldevs; pcap_if_t* alldevs;
pcap_if_t* d; pcap_if_t* d;
int i = 0; int i = 0;
@ -323,7 +306,6 @@ char* pcap_io_get_dev_desc(int num)
} }
pcap_freealldevs(alldevs); pcap_freealldevs(alldevs);
#endif
return NULL; return NULL;
} }
@ -331,10 +313,14 @@ char* pcap_io_get_dev_desc(int num)
PCAPAdapter::PCAPAdapter() PCAPAdapter::PCAPAdapter()
: NetAdapter() : NetAdapter()
{ {
#ifndef _WIN32
if (config.ethEnable == 0) if (config.ethEnable == 0)
return; return;
#ifdef _WIN32
if (!load_pcap())
return;
#endif
mac_address hostMAC; mac_address hostMAC;
mac_address newMAC; mac_address newMAC;
@ -351,9 +337,6 @@ PCAPAdapter::PCAPAdapter()
{ {
SysMessage("Can't open Device '%s'\n", config.Eth); SysMessage("Can't open Device '%s'\n", config.Eth);
} }
#else
Console.Error("pcap not supported on windows\n");
#endif
} }
bool PCAPAdapter::blocks() bool PCAPAdapter::blocks()
{ {
@ -398,7 +381,11 @@ std::vector<AdapterEntry> PCAPAdapter::GetAdapters()
{ {
std::vector<AdapterEntry> nic; std::vector<AdapterEntry> nic;
#ifndef _WIN32 #ifdef _WIN32
if (!load_pcap())
return nic;
#endif
pcap_if_t* alldevs; pcap_if_t* alldevs;
pcap_if_t* d; pcap_if_t* d;
@ -412,13 +399,31 @@ std::vector<AdapterEntry> PCAPAdapter::GetAdapters()
{ {
AdapterEntry entry; AdapterEntry entry;
entry.type = NetApi::PCAP_Switched; entry.type = NetApi::PCAP_Switched;
#ifdef _WIN32
//guid
wchar_t wEth[sizeof(config.Eth)] = {0};
mbstowcs(wEth, d->name, sizeof(config.Eth) - 1);
entry.guid = std::wstring(wEth);
//NPCAP 1.10 is using an version of pcap that dosn't
//allow us to set it to use UTF8
//see https://github.com/nmap/npcap/issues/276
//But use MultiByteToWideChar so we can later switch to UTF8
int len_desc = strlen(d->description) + 1;
int len_buf = MultiByteToWideChar(CP_ACP, 0, d->description, len_desc, nullptr, 0);
wchar_t* buf = new wchar_t[len_buf];
MultiByteToWideChar(CP_ACP, 0, d->description, len_desc, buf, len_buf);
entry.name = std::wstring(buf);
delete[] buf;
#else
entry.name = std::string(d->name); entry.name = std::string(d->name);
entry.guid = std::string(d->name); entry.guid = std::string(d->name);
#endif
nic.push_back(entry); nic.push_back(entry);
d = d->next; d = d->next;
} }
#endif
return nic; return nic;
} }

View File

@ -14,9 +14,7 @@
*/ */
#pragma once #pragma once
#if !defined(_WIN32)
#include "pcap.h" #include "pcap.h"
#endif
#include "net.h" #include "net.h"
#ifdef __cplusplus #ifdef __cplusplus
@ -24,7 +22,7 @@ extern "C" {
#endif #endif
#ifndef _WIN32 //#ifndef _WIN32
#pragma pack(push, 1) #pragma pack(push, 1)
typedef struct _ip_address typedef struct _ip_address
{ {
@ -156,7 +154,7 @@ extern mac_address broadcast_mac;
#define mac_compare(a, b) (memcmp(&(a), &(b), 6)) #define mac_compare(a, b) (memcmp(&(a), &(b), 6))
#define ip_compare(a, b) (memcmp(&(a), &(b), 4)) #define ip_compare(a, b) (memcmp(&(a), &(b), 4))
#endif //#endif
/* /*
int pcap_io_init(char *adapter); int pcap_io_init(char *adapter);
int pcap_io_send(void* packet, int plen); int pcap_io_send(void* packet, int plen);