]>
The Tcpdump Group git mirrors - libpcap/blob - testprogs/fuzz/fuzz_pcap.c
10 static int bufferToFile(const char * name
, const uint8_t *Data
, size_t Size
) {
12 if (remove(name
) != 0) {
13 if (errno
!= ENOENT
) {
14 printf("failed remove, errno=%d\n", errno
);
18 fd
= fopen(name
, "wb");
20 printf("failed open, errno=%d\n", errno
);
23 if (fwrite (Data
, 1, Size
, fd
) != Size
) {
31 void fuzz_openFile(const char * name
) {
32 if (outfile
!= NULL
) {
35 outfile
= fopen(name
, "w");
38 int LLVMFuzzerTestOneInput(const uint8_t *Data
, size_t Size
) {
40 char errbuf
[PCAP_ERRBUF_SIZE
];
42 struct pcap_pkthdr
*header
;
43 struct pcap_stat stats
;
46 //initialize output file
47 if (outfile
== NULL
) {
48 outfile
= fopen("/dev/null", "w");
49 if (outfile
== NULL
) {
54 //rewrite buffer to a file as libpcap does not have buffer inputs
55 if (bufferToFile("/tmp/fuzz.pcap", Data
, Size
) < 0) {
59 //initialize structure
60 pkts
= pcap_open_offline("/tmp/fuzz.pcap", errbuf
);
62 fprintf(outfile
, "Couldn't open pcap file %s\n", errbuf
);
67 r
= pcap_next_ex(pkts
, &header
, &pkt
);
69 //TODO pcap_offline_filter
70 fprintf(outfile
, "packet length=%d/%d\n",header
->caplen
, header
->len
);
71 r
= pcap_next_ex(pkts
, &header
, &pkt
);
73 if (pcap_stats(pkts
, &stats
) == 0) {
74 fprintf(outfile
, "number of packets=%d\n", stats
.ps_recv
);