Coverage Report

Created: 2024-02-25 06:29

/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
5.49k
{
12
5.49k
  if (tmpName.empty())
13
3
    tmpName = tmpnam (NULL);
14
15
5.49k
  if (tmpFile.empty())
16
3
    tmpFile = tmpName + FILE_EXT;
17
18
5.49k
  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
5.49k
  pcpp::Logger::getInstance().suppressLogs();
25
26
5.49k
  std::unique_ptr<pcpp::IFileReaderDevice> reader(pcpp::IFileReaderDevice::getReader(tmpFile));
27
5.49k
  if (!reader->open())
28
141
  {
29
141
    std::cerr << "Error opening the '" << tmpFile << "' file\n";
30
141
    return -1;
31
141
  }
32
33
5.35k
  pcpp::IPcapDevice::PcapStats stats;
34
5.35k
  reader->getStatistics(stats);
35
5.35k
  std::cout << "Read " << stats.packetsRecv << " packets successfully and "
36
5.35k
                       << stats.packetsDrop << " packets could not be read" << std::endl;
37
38
5.35k
  if (auto ngReader = dynamic_cast<pcpp::PcapNgFileReaderDevice*>(reader.get()))
39
4.01k
  {
40
4.01k
    std::cout << "OS is '" << ngReader->getOS() << "'; Hardware is '" << ngReader->getHardware() << "'"
41
4.01k
          << "'; CaptureApplication is '" << ngReader->getCaptureApplication()
42
4.01k
          << "'; CaptureFileComment is '" << ngReader->getCaptureFileComment()
43
4.01k
          << "'" << std::endl;
44
4.01k
  }
45
46
5.35k
  pcpp::RawPacketVector packets;
47
5.35k
  if (reader->getNextPackets(packets, 1) != 1)
48
546
  {
49
546
    std::cerr << "Couldn't read the first packet in the file\n";
50
546
    return 0;
51
546
  }
52
53
4.80k
  pcpp::RawPacket& rawPacket = *packets.front();
54
4.80k
  do
55
315k
  {
56
    // go deeper only for .pcap and .pcapng format
57
    // for .snoop we are only fuzzing the reader
58
315k
    if (0 == strcmp(FILE_EXT, ".pcap") || 0 == strcmp(FILE_EXT, ".pcapng"))
59
314k
    {
60
314k
      pcpp::Packet parsedPacket(&rawPacket);
61
314k
      parsedPacket.toString();
62
314k
      auto layer = parsedPacket.getFirstLayer();
63
1.20M
      while (layer != NULL)
64
893k
      {
65
893k
        std::cout << layer->toString() << std::endl;
66
893k
        layer->getHeaderLen();
67
893k
        readParsedPacket(parsedPacket, layer);
68
893k
        layer = layer->getNextLayer();
69
893k
      }
70
314k
      parsedPacket.computeCalculateFields();
71
314k
    }
72
315k
  } while (reader->getNextPacket(rawPacket));
73
74
4.80k
  reader->close();
75
4.80k
  return 0;
76
5.35k
}