/src/PcapPlusPlus/Tests/Fuzzers/FuzzTarget.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include <PcapFileDevice.h> |
2 | | #include <Packet.h> |
3 | | #include <Logger.h> |
4 | | #include "DumpToFile.h" |
5 | | #include "ReadParsedPacket.h" |
6 | | |
7 | | static std::string tmpName; |
8 | | static std::string tmpFile; |
9 | | |
10 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) |
11 | 6.30k | { |
12 | 6.30k | if (tmpName.empty()) |
13 | 2 | tmpName = tmpnam(nullptr); |
14 | | |
15 | 6.30k | if (tmpFile.empty()) |
16 | 2 | tmpFile = tmpName + FILE_EXT; |
17 | | |
18 | 6.30k | if (dumpDataToPcapFile(data, size, tmpFile.c_str()) != 0) |
19 | 0 | { |
20 | 0 | std::cerr << "Can't Dump buffer to the '" << tmpFile << "' file!!!!\n"; |
21 | 0 | return -1; |
22 | 0 | } |
23 | | |
24 | 6.30k | pcpp::Logger::getInstance().suppressLogs(); |
25 | | |
26 | 6.30k | std::unique_ptr<pcpp::IFileReaderDevice> reader(pcpp::IFileReaderDevice::getReader(tmpFile)); |
27 | 6.30k | if (!reader->open()) |
28 | 55 | { |
29 | 55 | std::cerr << "Error opening the '" << tmpFile << "' file\n"; |
30 | 55 | return -1; |
31 | 55 | } |
32 | | |
33 | 6.24k | pcpp::IPcapDevice::PcapStats stats; |
34 | 6.24k | reader->getStatistics(stats); |
35 | 6.24k | std::cout << "Read " << stats.packetsRecv << " packets successfully and " << stats.packetsDrop |
36 | 6.24k | << " packets could not be read" << std::endl; |
37 | | |
38 | 6.24k | if (auto ngReader = dynamic_cast<pcpp::PcapNgFileReaderDevice*>(reader.get())) |
39 | 6.08k | { |
40 | 6.08k | std::cout << "OS is '" << ngReader->getOS() << "'; Hardware is '" << ngReader->getHardware() << "'" |
41 | 6.08k | << "'; CaptureApplication is '" << ngReader->getCaptureApplication() << "'; CaptureFileComment is '" |
42 | 6.08k | << ngReader->getCaptureFileComment() << "'" << std::endl; |
43 | 6.08k | } |
44 | | |
45 | 6.24k | pcpp::RawPacketVector packets; |
46 | 6.24k | if (reader->getNextPackets(packets, 1) != 1) |
47 | 226 | { |
48 | 226 | std::cerr << "Couldn't read the first packet in the file\n"; |
49 | 226 | return 0; |
50 | 226 | } |
51 | | |
52 | 6.02k | pcpp::RawPacket& rawPacket = *packets.front(); |
53 | 6.02k | do |
54 | 134k | { |
55 | | // go deeper only for .pcap and .pcapng format |
56 | | // for .snoop we are only fuzzing the reader |
57 | 134k | if (0 == strcmp(FILE_EXT, ".pcap") || 0 == strcmp(FILE_EXT, ".pcapng")) |
58 | 133k | { |
59 | 133k | pcpp::Packet parsedPacket(&rawPacket); |
60 | 133k | parsedPacket.toString(); |
61 | 133k | auto layer = parsedPacket.getFirstLayer(); |
62 | 707k | while (layer != nullptr) |
63 | 573k | { |
64 | 573k | std::cout << layer->toString() << std::endl; |
65 | 573k | layer->getHeaderLen(); |
66 | 573k | readParsedPacket(parsedPacket, layer); |
67 | 573k | layer = layer->getNextLayer(); |
68 | 573k | } |
69 | 133k | parsedPacket.computeCalculateFields(); |
70 | 133k | } |
71 | 134k | } while (reader->getNextPacket(rawPacket)); |
72 | | |
73 | 6.02k | reader->close(); |
74 | 6.02k | return 0; |
75 | 6.24k | } |