Coverage Report

Created: 2023-01-25 06:41

/src/PcapPlusPlus/Tests/Fuzzers/FuzzTarget.cpp
Line
Count
Source (jump to first uncovered line)
1
#include <iostream>
2
#include <IPv4Layer.h>
3
#include <Packet.h>
4
#include <PcapFileDevice.h>
5
6
// This function is created as PcapPlusPlus doesn't seem to offer a way of
7
// parsing Pcap files directly from memory
8
int dumpDataToPcapFile(const uint8_t *data, size_t size)
9
17.8k
{
10
17.8k
  FILE *fd;
11
17.8k
  int written = 0;
12
13
17.8k
  fd = fopen("/tmp/fuzz_sample.pcap", "wb");
14
17.8k
  if (fd == NULL)
15
0
  {
16
0
    std::cerr << "Error opening pcap file for writing\n";
17
0
    return -1;
18
0
  }
19
20
17.8k
  written = fwrite(data, 1, size, fd);
21
17.8k
  if (written != size)
22
0
  {
23
0
    std::cerr << "Error writing pcap file\n";
24
0
    fclose(fd);
25
0
    return -1;
26
0
  }
27
28
17.8k
  fclose(fd);
29
30
17.8k
  return 0;
31
17.8k
}
32
33
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
34
17.8k
{
35
36
17.8k
  if (dumpDataToPcapFile(Data, Size) < 0)
37
0
  {
38
0
    return 1;
39
0
  }
40
41
  // open a pcap file for reading
42
17.8k
  pcpp::PcapFileReaderDevice reader("/tmp/fuzz_sample.pcap");
43
17.8k
  if (!reader.open())
44
928
  {
45
928
    std::cerr << "Error opening the pcap file\n";
46
928
    return 1;
47
928
  }
48
49
  // read the first (and only) packet from the file
50
16.8k
  pcpp::RawPacket rawPacket;
51
16.8k
  if (!reader.getNextPacket(rawPacket))
52
1.11k
  {
53
1.11k
    std::cerr << "Couldn't read the first packet in the file\n";
54
1.11k
    return 1;
55
1.11k
  }
56
57
15.7k
  do
58
898k
  {
59
    // parse the raw packet into a parsed packet
60
898k
    pcpp::Packet parsedPacket(&rawPacket);
61
62
    // verify the packet is IPv4
63
898k
    if (parsedPacket.isPacketOfType(pcpp::IPv4))
64
321k
    {
65
      // extract source and dest IPs
66
321k
      pcpp::IPv4Address srcIP = parsedPacket.getLayerOfType<pcpp::IPv4Layer>()->getSrcIPv4Address();
67
321k
      pcpp::IPv4Address destIP = parsedPacket.getLayerOfType<pcpp::IPv4Layer>()->getDstIPv4Address();
68
69
      // print source and dest IPs
70
321k
      std::cout << "Source IP is '" << srcIP.toString() << "'; Dest IP is '" << destIP.toString() << "'" << std::endl;
71
321k
    }
72
898k
  }
73
898k
  while (reader.getNextPacket(rawPacket));
74
75
  // close the file
76
15.7k
  reader.close();
77
78
15.7k
  return 0;
79
16.8k
}