From f16ee630eee5abc967deb11d471173e57014f201 Mon Sep 17 00:00:00 2001 From: Paul Hollinsky Date: Wed, 6 Nov 2019 10:24:55 -0500 Subject: [PATCH] Checksum failure logging to stderr for debugging --- communication/packetizer.cpp | 36 +++++++++++++++++++++++ include/icsneo/communication/packetizer.h | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/communication/packetizer.cpp b/communication/packetizer.cpp index ba0519b..8096b82 100644 --- a/communication/packetizer.cpp +++ b/communication/packetizer.cpp @@ -41,6 +41,7 @@ bool Packetizer::input(const std::vector& inputBytes) { state = ReadState::ParseHeader; currentIndex = 1; } else { + std::cerr << "Discarding byte " << std::hex << std::setw(2) << std::setfill('0') << int(bytes.front()) << std::endl; bytes.pop_front(); // Discard } break; @@ -81,6 +82,21 @@ bool Packetizer::input(const std::vector& inputBytes) { * payload, and not the header or checksum. */ if(packetLength < 4 || packetLength > 4000) { + std::cerr << "====================================\n"; + std::cerr << "An improper packet has occurred!\n"; + std::cerr << "Packet: (" << std::dec << packetLength << ")\n"; + std::cerr << '\t'; + for(auto i = 0; i < std::min(packetLength, 16); i++) + std::cerr << std::hex << std::setw(2) << std::setfill('0') << int(bytes[i]) << ' '; + std::cerr << std::endl; + std::cerr << "Previous: (" << std::dec << previousPacket.data.size() << ") " << previousPacket.network << '\n'; + for(auto i = 0; i < previousPacket.data.size(); i += 16) { + std::cerr << '\t'; + for(auto j = 0; j < 16 && (i + j) < previousPacket.data.size(); j++) + std::cerr << std::hex << std::setw(2) << std::setfill('0') << int(previousPacket.data[i+j]) << ' '; + std::cerr << std::endl; + } + std::cerr << "====================================\n"; bytes.pop_front(); EventManager::GetInstance().add(APIEvent::Type::FailedToRead, APIEvent::Severity::Error); state = ReadState::SearchForHeader; @@ -112,8 +128,28 @@ bool Packetizer::input(const std::vector& inputBytes) { } else { if(gotGoodPackets) // Don't complain unless we've already gotten a good packet, in case we started in the middle of a stream report(APIEvent::Type::PacketChecksumError, APIEvent::Severity::Error); + + std::cerr << "====================================\n"; + std::cerr << "A checksum error has occurred!\n"; + std::cerr << "Packet: (" << std::dec << packet.data.size() << ") " << packet.network << '\n'; + for(auto i = 0; i < packet.data.size(); i += 16) { + std::cerr << '\t'; + for(auto j = 0; j < 16 && (i + j) < packet.data.size(); j++) + std::cerr << std::hex << std::setw(2) << std::setfill('0') << int(packet.data[i+j]) << ' '; + std::cerr << std::endl; + } + std::cerr << "Previous: (" << std::dec << previousPacket.data.size() << ") " << previousPacket.network << '\n'; + for(auto i = 0; i < previousPacket.data.size(); i += 16) { + std::cerr << '\t'; + for(auto j = 0; j < 16 && (i + j) < previousPacket.data.size(); j++) + std::cerr << std::hex << std::setw(2) << std::setfill('0') << int(previousPacket.data[i+j]) << ' '; + std::cerr << std::endl; + } + std::cerr << "====================================\n"; + bytes.pop_front(); // Drop the first byte so it doesn't get picked up again } + previousPacket = packet; // Reset for the next packet currentIndex = 0; diff --git a/include/icsneo/communication/packetizer.h b/include/icsneo/communication/packetizer.h index 9cea2a9..4e5718b 100644 --- a/include/icsneo/communication/packetizer.h +++ b/include/icsneo/communication/packetizer.h @@ -37,7 +37,7 @@ private: int headerSize = 0; bool checksum = false; bool gotGoodPackets = false; // Tracks whether we've ever gotten a good packet - Packet packet; + Packet packet, previousPacket; std::deque bytes; std::vector> processedPackets;