Coverage Report

Created: 2023-01-17 06:15

/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.6k
{
10
17.6k
  FILE *fd;
11
17.6k
  int written = 0;
12
13
17.6k
  fd = fopen("/tmp/fuzz_sample.pcap", "wb");
14
17.6k
  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.6k
  written = fwrite(data, 1, size, fd);
21
17.6k
  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.6k
  fclose(fd);
29
30
17.6k
  return 0;
31
17.6k
}
32
33
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
34
17.6k
{
35
36
17.6k
  if (dumpDataToPcapFile(Data, Size) < 0)
37
0
  {
38
0
    return 1;
39
0
  }
40
41
  // open a pcap file for reading
42
17.6k
  pcpp::PcapFileReaderDevice reader("/tmp/fuzz_sample.pcap");
43
17.6k
  if (!reader.open())
44
932
  {
45
932
    std::cerr << "Error opening the pcap file\n";
46
932
    return 1;
47
932
  }
48
49
  // read the first (and only) packet from the file
50
16.7k
  pcpp::RawPacket rawPacket;
51
16.7k
  if (!reader.getNextPacket(rawPacket))
52
1.15k
  {
53
1.15k
    std::cerr << "Couldn't read the first packet in the file\n";
54
1.15k
    return 1;
55
1.15k
  }
56
57
15.5k
  do
58
997k
  {
59
    // parse the raw packet into a parsed packet
60
997k
    pcpp::Packet parsedPacket(&rawPacket);
61
62
    // verify the packet is IPv4
63
997k
    if (parsedPacket.isPacketOfType(pcpp::IPv4))
64
341k
    {
65
      // extract source and dest IPs
66
341k
      pcpp::IPv4Address srcIP = parsedPacket.getLayerOfType<pcpp::IPv4Layer>()->getSrcIPv4Address();
67
341k
      pcpp::IPv4Address destIP = parsedPacket.getLayerOfType<pcpp::IPv4Layer>()->getDstIPv4Address();
68
69
      // print source and dest IPs
70
341k
      std::cout << "Source IP is '" << srcIP.toString() << "'; Dest IP is '" << destIP.toString() << "'" << std::endl;
71
341k
    }
72
997k
  }
73
997k
  while (reader.getNextPacket(rawPacket));
74
75
  // close the file
76
15.5k
  reader.close();
77
78
15.5k
  return 0;
79
16.7k
}