take it further

This commit is contained in:
Arisotura 2019-03-04 20:58:13 +01:00
parent 1e108ad7fb
commit 8448e6bea0
2 changed files with 15 additions and 70 deletions

View File

@ -16,7 +16,7 @@
with melonDS. If not, see http://www.gnu.org/licenses/. with melonDS. If not, see http://www.gnu.org/licenses/.
*/ */
// LAN interface. Currently powered by libpcap, may change. // direct LAN interface. Currently powered by libpcap, may change.
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -24,7 +24,7 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <pcap/pcap.h> #include <pcap/pcap.h>
#include "Wifi.h" #include "Wifi.h"
#include "LAN.h" #include "LAN_PCap.h"
#include "../Config.h" #include "../Config.h"
#ifdef __WIN32__ #ifdef __WIN32__
@ -55,7 +55,7 @@ DECL_PCAP_FUNC(int, pcap_dispatch, (pcap_t* dev, int num, pcap_handler callback,
DECL_PCAP_FUNC(const u_char*, pcap_next, (pcap_t* dev, struct pcap_pkthdr* hdr), (dev,hdr)) DECL_PCAP_FUNC(const u_char*, pcap_next, (pcap_t* dev, struct pcap_pkthdr* hdr), (dev,hdr))
namespace LAN namespace LAN_PCap
{ {
const char* PCapLibNames[] = const char* PCapLibNames[] =

View File

@ -16,15 +16,14 @@
with melonDS. If not, see http://www.gnu.org/licenses/. with melonDS. If not, see http://www.gnu.org/licenses/.
*/ */
// LAN interface. Currently powered by libpcap, may change. // indirect LAN interface, powered by BSD sockets.
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <pcap/pcap.h>
#include "Wifi.h" #include "Wifi.h"
#include "LAN.h" #include "LAN_Socket.h"
#include "../Config.h" #include "../Config.h"
#ifdef __WIN32__ #ifdef __WIN32__
@ -34,57 +33,21 @@
#endif #endif
// welp namespace LAN_Socket
#ifndef PCAP_OPENFLAG_PROMISCUOUS
#define PCAP_OPENFLAG_PROMISCUOUS 1
#endif
#define DECL_PCAP_FUNC(ret, name, args, args2) \
typedef ret (*type_##name) args; \
type_##name ptr_##name = NULL; \
ret name args { return ptr_##name args2; }
DECL_PCAP_FUNC(int, pcap_findalldevs, (pcap_if_t** alldevs, char* errbuf), (alldevs,errbuf))
DECL_PCAP_FUNC(void, pcap_freealldevs, (pcap_if_t* alldevs), (alldevs))
DECL_PCAP_FUNC(pcap_t*, pcap_open_live, (const char* src, int snaplen, int flags, int readtimeout, char* errbuf), (src,snaplen,flags,readtimeout,errbuf))
DECL_PCAP_FUNC(void, pcap_close, (pcap_t* dev), (dev))
DECL_PCAP_FUNC(int, pcap_setnonblock, (pcap_t* dev, int nonblock, char* errbuf), (dev,nonblock,errbuf))
DECL_PCAP_FUNC(int, pcap_sendpacket, (pcap_t* dev, const u_char* data, int len), (dev,data,len))
DECL_PCAP_FUNC(int, pcap_dispatch, (pcap_t* dev, int num, pcap_handler callback, u_char* data), (dev,num,callback,data))
DECL_PCAP_FUNC(const u_char*, pcap_next, (pcap_t* dev, struct pcap_pkthdr* hdr), (dev,hdr))
namespace LAN
{ {
const char* PCapLibNames[] = u8 PacketBuffer[2048];
{ int PacketLen;
#ifdef __WIN32__ volatile int RXNum;
// TODO: name for npcap in non-WinPCap mode
"wpcap.dll",
#else
// Linux lib names
"libpcap.so.1",
"libpcap.so",
#endif
NULL
};
AdapterData* Adapters = NULL;
int NumAdapters = 0;
void* PCapLib = NULL;
pcap_t* PCapAdapter = NULL;
AdapterData* PCapAdapterData;
u8 PCapPacketBuffer[2048];
int PCapPacketLen;
volatile int PCapRXNum;
u16 IPv4ID; u16 IPv4ID;
// TODO: UDP sockets
// * use FIFO list
// * assign new socket when seeing new IP/port
typedef struct typedef struct
{ {
u8 DestIP[4]; u8 DestIP[4];
@ -96,27 +59,9 @@ typedef struct
} TCPSocket; } TCPSocket;
TCPSocket TCPSocketList[64]; TCPSocket TCPSocketList[16];
#define LOAD_PCAP_FUNC(sym) \
ptr_##sym = (type_##sym)SDL_LoadFunction(lib, #sym); \
if (!ptr_##sym) return false;
bool TryLoadPCap(void* lib)
{
LOAD_PCAP_FUNC(pcap_findalldevs)
LOAD_PCAP_FUNC(pcap_freealldevs)
LOAD_PCAP_FUNC(pcap_open_live)
LOAD_PCAP_FUNC(pcap_close)
LOAD_PCAP_FUNC(pcap_setnonblock)
LOAD_PCAP_FUNC(pcap_sendpacket)
LOAD_PCAP_FUNC(pcap_dispatch)
LOAD_PCAP_FUNC(pcap_next)
return true;
}
bool Init() bool Init()
{ {
// 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??