]>
The Tcpdump Group git mirrors - libpcap/blob - testprogs/fuzz/fuzz_pcap.c
10 FILE * outfile
= NULL
;
12 static int bufferToFile(const char * name
, const uint8_t *Data
, size_t Size
) {
14 if (remove(name
) != 0) {
15 if (errno
!= ENOENT
) {
16 printf("failed remove, errno=%d\n", errno
);
20 fd
= fopen(name
, "wb");
22 printf("failed open, errno=%d\n", errno
);
25 if (fwrite (Data
, 1, Size
, fd
) != Size
) {
33 void fuzz_openFile(const char * name
) {
34 if (outfile
!= NULL
) {
37 outfile
= fopen(name
, "w");
40 int LLVMFuzzerTestOneInput(const uint8_t *Data
, size_t Size
) {
42 char errbuf
[PCAP_ERRBUF_SIZE
];
43 char filename
[FILENAME_MAX
] = { 0 };
45 struct pcap_pkthdr
*header
;
46 struct pcap_stat stats
;
49 //initialize output file
50 if (outfile
== NULL
) {
51 outfile
= fopen("/dev/null", "w");
52 if (outfile
== NULL
) {
57 //generate temporary file name
58 snprintf(filename
, FILENAME_MAX
, "/tmp/libpcap_fuzz_pcap.XXXXXX");
59 if ((fd
= mkstemp(filename
)) < 0) {
64 //rewrite buffer to a file as libpcap does not have buffer inputs
65 if (bufferToFile(filename
, Data
, Size
) < 0) {
70 //initialize structure
71 pkts
= pcap_open_offline(filename
, errbuf
);
73 fprintf(outfile
, "Couldn't open pcap file %s\n", errbuf
);
79 r
= pcap_next_ex(pkts
, &header
, &pkt
);
81 //TODO pcap_offline_filter
82 fprintf(outfile
, "packet length=%d/%d\n",header
->caplen
, header
->len
);
83 r
= pcap_next_ex(pkts
, &header
, &pkt
);
85 if (pcap_stats(pkts
, &stats
) == 0) {
86 fprintf(outfile
, "number of packets=%d\n", stats
.ps_recv
);