From 562964df547e9cdbb3ee32edae5592ab5a513b25 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Tue, 30 Apr 2024 16:30:47 +0200 Subject: tweak packet dumper --- src/frontend/qt_sdl/LocalMP.cpp | 60 +++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/src/frontend/qt_sdl/LocalMP.cpp b/src/frontend/qt_sdl/LocalMP.cpp index b90ff05..cc42706 100644 --- a/src/frontend/qt_sdl/LocalMP.cpp +++ b/src/frontend/qt_sdl/LocalMP.cpp @@ -41,8 +41,11 @@ #include #include +#include +#include pcap_dumper_t* dumper; +unsigned long test_start = time(NULL); using namespace melonDS; using namespace melonDS::Platform; @@ -76,15 +79,6 @@ struct MPPacketHeader u64 Timestamp; }; -struct MPSync -{ - u32 Magic; - u32 SenderID; - u16 ClientMask; - u16 Type; - u64 Timestamp; -}; - QSharedMemory* MPQueue; int InstanceID; u32 PacketReadOffset; @@ -399,11 +393,13 @@ void FIFORead(int fifo, void* buf, int len) if (fifo == 0) PacketReadOffset = offset; else ReplyReadOffset = offset; - struct pcap_pkthdr p; - p.len = len; - p.caplen = len; - gettimeofday(&p.ts, NULL); - pcap_dump((u_char*) dumper, &p, (u_char*) buf); + // WIP: temporarily capture sent packets only, capture is done on both + // sides anyways, so who cares + // struct pcap_pkthdr p; + // p.len = len; + // p.caplen = len; + // gettimeofday(&p.ts, NULL); + // pcap_dump((u_char*) dumper, &p, (u_char*) buf); } void FIFOWrite(int fifo, void* buf, int len) @@ -440,12 +436,6 @@ void FIFOWrite(int fifo, void* buf, int len) if (fifo == 0) header->PacketWriteOffset = offset; else header->ReplyWriteOffset = offset; - - struct pcap_pkthdr p; - p.len = len; - p.caplen = len; - gettimeofday(&p.ts, NULL); - pcap_dump((u_char*) dumper, &p, (u_char*) buf); } int SendPacketGeneric(u32 type, u8* packet, int len, u64 timestamp) @@ -457,6 +447,17 @@ int SendPacketGeneric(u32 type, u8* packet, int len, u64 timestamp) u16 mask = header->ConnectedBitmask; + // // packet dropping test + // unsigned long now = time(NULL); + // static bool first = true; + // bool dropping = false; + // if (now - test_start > 20 && InstanceID == 0) { + // if (first == true) + // printf("[INSTANCE %d] started dropping packets!\n", InstanceID); + // first = false; + // dropping = true; + // } + // TODO: check if the FIFO is full! MPPacketHeader pktheader; @@ -466,11 +467,30 @@ int SendPacketGeneric(u32 type, u8* packet, int len, u64 timestamp) pktheader.Length = len; pktheader.Timestamp = timestamp; + std::vector buf; + type &= 0xFFFF; int nfifo = (type == 2) ? 1 : 0; + // NOTE: this is the ni-fi packet header, altering this header in any way + // seems to consistently trigger PACKET FIFO OVERFLOW errors FIFOWrite(nfifo, &pktheader, sizeof(pktheader)); + buf.insert(buf.end(), (uint8_t*) &pktheader, (uint8_t*) &pktheader + sizeof(pktheader)); if (len) + { + // // replace packet content with random noise while dropping + // if (dropping) + // for (size_t i = 0; i < len; i++) + // packet[i] = rand(); + + buf.insert(buf.end(), (uint8_t*) packet, (uint8_t*) packet + len); FIFOWrite(nfifo, packet, len); + } + + struct pcap_pkthdr p; + p.len = buf.size(); + p.caplen = buf.size(); + gettimeofday(&p.ts, NULL); + pcap_dump((u_char*) dumper, &p, buf.data()); if (type == 1) { -- cgit v1.2.3