From 0550d228335571220705c5dfbecb44595232058a Mon Sep 17 00:00:00 2001 From: Arisotura Date: Thu, 21 Feb 2019 03:44:22 +0100 Subject: [PATCH] hook LAN shito. open proper pcap device. etc --- src/libui_sdl/LAN.cpp | 79 ++++++++++++++++++++++++++++++++------ src/libui_sdl/LAN.h | 3 ++ src/libui_sdl/Platform.cpp | 45 +++------------------- 3 files changed, 75 insertions(+), 52 deletions(-) diff --git a/src/libui_sdl/LAN.cpp b/src/libui_sdl/LAN.cpp index 2021b3d4..da352df9 100644 --- a/src/libui_sdl/LAN.cpp +++ b/src/libui_sdl/LAN.cpp @@ -24,6 +24,7 @@ #include #include #include "LAN.h" +#include "../Config.h" #ifdef __WIN32__ #include @@ -243,23 +244,28 @@ bool Init() #endif // __WIN32__ - printf("devices: %d\n", NumAdapters); + // open pcap device + dev = (pcap_if_t*)Adapters[0].Internal; for (int i = 0; i < NumAdapters; i++) { - AdapterData* zog = &Adapters[i]; + if (!strncmp(Adapters[i].DeviceName, Config::LANDevice, 128)) + dev = (pcap_if_t*)Adapters[i].Internal; + } - printf("%s:\n", ((pcap_if_t*)zog->Internal)->name); + PCapAdapter = pcap_open_live(dev->name, 2048, PCAP_OPENFLAG_PROMISCUOUS, 1, errbuf); + if (!PCapAdapter) + { + printf("PCap: failed to open adapter\n"); + return false; + } - printf("* %s\n", zog->FriendlyName); - printf("* %s\n", zog->Description); + pcap_freealldevs(alldevs); - printf("* "); for (int j = 0; j < 6; j++) printf("%02X:", zog->MAC[j]); printf("\n"); - printf("* "); for (int j = 0; j < 4; j++) printf("%d.", zog->IP_v4[j]); printf("\n"); - - for (int k = 0; k < 8; k++) - { - printf("* "); for (int j = 0; j < 4; j++) printf("%d.", zog->DNS[k][j]); printf("\n"); - } + if (pcap_setnonblock(PCapAdapter, 1, errbuf) < 0) + { + printf("PCap: failed to set nonblocking mode\n"); + pcap_close(PCapAdapter); PCapAdapter = NULL; + return false; } return true; @@ -280,4 +286,53 @@ void DeInit() } } +void RXCallback(u_char* blarg, const struct pcap_pkthdr* header, const u_char* data) +{ + while (PCapRXNum > 0); + + if (header->len > 2048-64) return; + + PCapPacketLen = header->len; + memcpy(PCapPacketBuffer, data, PCapPacketLen); + PCapRXNum = 1; +} + +int 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; + } + + if (!Config::DirectLAN) + { + // TODO! + } + + pcap_sendpacket(PCapAdapter, data, len); + // TODO: check success + return len; +} + +int 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, RXCallback, NULL); + return ret; +} + } diff --git a/src/libui_sdl/LAN.h b/src/libui_sdl/LAN.h index 47e59c91..8021b312 100644 --- a/src/libui_sdl/LAN.h +++ b/src/libui_sdl/LAN.h @@ -45,6 +45,9 @@ extern int NumAdapters; bool Init(); void DeInit(); +int SendPacket(u8* data, int len); +int RecvPacket(u8* data); + } #endif // LAN_H diff --git a/src/libui_sdl/Platform.cpp b/src/libui_sdl/Platform.cpp index 4400357c..5cbb1425 100644 --- a/src/libui_sdl/Platform.cpp +++ b/src/libui_sdl/Platform.cpp @@ -22,6 +22,7 @@ #include #include "../Platform.h" #include "../Config.h" +#include "LAN.h" #ifdef __WIN32__ #include @@ -260,59 +261,23 @@ int MP_RecvPacket(u8* data, bool block) bool LAN_Init() { - // welp + if (!LAN::Init()) return false; return true; } void LAN_DeInit() { - // + LAN::DeInit(); } 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;*/ - return len; + return LAN::SendPacket(data, len); } -/*void LAN_RXCallback(u_char* blarg, const struct pcap_pkthdr* header, const u_char* data) -{ - while (PCapRXNum > 0); - - if (header->len > 2048-64) return; - - PCapPacketLen = header->len; - memcpy(PCapPacketBuffer, data, PCapPacketLen); - PCapRXNum = 1; -}*/ - 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;*/ - return 0; + return LAN::RecvPacket(data); }