diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Platform.h | 6 | ||||
-rw-r--r-- | src/Wifi.cpp | 1 | ||||
-rw-r--r-- | src/libui_sdl/Platform.cpp | 122 |
3 files changed, 111 insertions, 18 deletions
diff --git a/src/Platform.h b/src/Platform.h index eeb6f9a..7fb6793 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -36,13 +36,19 @@ void Semaphore_Reset(void* sema); void Semaphore_Wait(void* sema); void Semaphore_Post(void* sema); +// local multiplayer comm interface +// packet type: DS-style TX header (12 bytes) + original 802.11 frame bool MP_Init(); void MP_DeInit(); int MP_SendPacket(u8* data, int len); int MP_RecvPacket(u8* data, bool block); +// LAN comm interface +// packet type: Ethernet (802.3) bool LAN_Init(); void LAN_DeInit(); +int LAN_SendPacket(u8* data, int len); +int LAN_RecvPacket(u8* data); } diff --git a/src/Wifi.cpp b/src/Wifi.cpp index eac32d2..c5039d1 100644 --- a/src/Wifi.cpp +++ b/src/Wifi.cpp @@ -767,6 +767,7 @@ bool CheckRX(bool block) for (;;) { int rxlen = Platform::MP_RecvPacket(RXBuffer, block); + //if (rxlen == 0) rxlen = Platform::LAN_RecvPacket(RXBuffer); if (rxlen == 0) return false; if (rxlen < 12+24) continue; diff --git a/src/libui_sdl/Platform.cpp b/src/libui_sdl/Platform.cpp index 22bdbb6..d835af1 100644 --- a/src/libui_sdl/Platform.cpp +++ b/src/libui_sdl/Platform.cpp @@ -29,7 +29,6 @@ #include <ws2tcpip.h> #define socket_t SOCKET #define sockaddr_t SOCKADDR - #define pcap_dev_name description #else #include <unistd.h> #include <arpa/inet.h> @@ -39,7 +38,6 @@ #define socket_t int #define sockaddr_t struct sockaddr #define closesocket close - #define pcap_dev_name name #endif #ifndef INVALID_SOCKET @@ -47,6 +45,20 @@ #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)) + + void Stop(bool internal); @@ -86,20 +98,12 @@ const char* PCapLibNames[] = NULL }; -void* PCapLib; +void* PCapLib = NULL; +pcap_t* PCapAdapter = NULL; -#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)) +u8 PCapPacketBuffer[2048]; +int PCapPacketLen; +int PCapRXNum; void StopEmu() @@ -164,7 +168,7 @@ bool MP_Init() { return false; } -#endif // __WXMSW__ +#endif // __WIN32__ MPSocket = socket(AF_INET, SOCK_DGRAM, 0); if (MPSocket < 0) @@ -214,7 +218,7 @@ void MP_DeInit() #ifdef __WIN32__ WSACleanup(); -#endif // __WXMSW__ +#endif // __WIN32__ } int MP_SendPacket(u8* data, int len) @@ -308,6 +312,9 @@ bool TryLoadPCap(void* lib) bool LAN_Init() { PCapLib = NULL; + PCapAdapter = NULL; + PCapPacketLen = 0; + PCapRXNum = 0; for (int i = 0; PCapLibNames[i]; i++) { @@ -331,7 +338,38 @@ bool LAN_Init() return false; } - // + char errbuf[PCAP_ERRBUF_SIZE]; + int ret; + + pcap_if_t* alldevs; + ret = pcap_findalldevs(&alldevs, errbuf); + if (ret < 0 || alldevs == NULL) + { + printf("PCap: no devices available\n"); + return false; + } +/*while (alldevs){ + printf("picking dev %08X %s | %s\n", alldevs->flags, alldevs->name, alldevs->description); + alldevs = alldevs->next;}*/ + // temp hack + // TODO: ADAPTER SELECTOR!! + pcap_if_t* dev = alldevs->next; + + PCapAdapter = pcap_open_live(dev->name, 2048, PCAP_OPENFLAG_PROMISCUOUS, 1, errbuf); + if (!PCapAdapter) + { + printf("PCap: failed to open adapter\n"); + return false; + } + + pcap_freealldevs(alldevs); + + if (pcap_setnonblock(PCapAdapter, 1, errbuf) < 0) + { + printf("PCap: failed to set nonblocking mode\n"); + pcap_close(PCapAdapter); PCapAdapter = NULL; + return false; + } return true; } @@ -340,10 +378,58 @@ void LAN_DeInit() { if (PCapLib) { + if (PCapAdapter) + { + pcap_close(PCapAdapter); + PCapAdapter = NULL; + } + SDL_UnloadObject(PCapLib); PCapLib = NULL; } } +int LAN_SendPacket(u8* data, int len) +{ + if (PCapAdapter == NULL) + return 0; + + if (len > 2048) + { + printf("LAN_SendPacket: error: packet too long (%d)\n", len); + return 0; + } + + pcap_sendpacket(PCapAdapter, data, len); + // TODO: check success + return len; +} + +void LAN_RXCallback(u_char* blarg, const struct pcap_pkthdr* header, const u_char* data) +{ + PCapPacketLen = header->len; + memcpy(PCapPacketBuffer, data, PCapPacketLen); + PCapRXNum = 1; + + printf("received shit\n"); +} + +int LAN_RecvPacket(u8* data) +{ + if (PCapAdapter == NULL) + return 0; + + int ret = 0; + if (PCapRXNum > 0) + { + memcpy(data, PCapPacketBuffer, PCapPacketLen); + ret = PCapPacketLen; + PCapRXNum = 0; + } + + pcap_dispatch(PCapAdapter, 1, LAN_RXCallback, NULL); + return ret; +} + } |