]>
The Tcpdump Group git mirrors - libpcap/blob - testprogs/fuzz/fuzz_both.c
11 static int bufferToFile(const char * name
, const uint8_t *Data
, size_t Size
) {
13 if (remove(name
) != 0) {
14 if (errno
!= ENOENT
) {
15 printf("failed remove, errno=%d\n", errno
);
19 fd
= fopen(name
, "wb");
21 printf("failed open, errno=%d\n", errno
);
24 if (fwrite (Data
, 1, Size
, fd
) != Size
) {
32 void fuzz_openFile(const char * name
) {
33 if (outfile
!= NULL
) {
36 outfile
= fopen(name
, "w");
39 int LLVMFuzzerTestOneInput(const uint8_t *Data
, size_t Size
) {
41 char errbuf
[PCAP_ERRBUF_SIZE
];
43 struct pcap_pkthdr
*header
;
47 struct bpf_program bpf
;
50 //initialize output file
51 if (outfile
== NULL
) {
52 outfile
= fopen("/dev/null", "w");
53 if (outfile
== NULL
) {
62 if (Size
< 1+filterSize
|| filterSize
== 0) {
66 //rewrite buffer to a file as libpcap does not have buffer inputs
67 if (bufferToFile("/tmp/fuzz.pcap", Data
+1+filterSize
, Size
-(1+filterSize
)) < 0) {
71 //initialize structure
72 pkts
= pcap_open_offline("/tmp/fuzz.pcap", errbuf
);
74 fprintf(outfile
, "Couldn't open pcap file %s\n", errbuf
);
78 filter
= malloc(filterSize
);
79 memcpy(filter
, Data
+1, filterSize
);
80 //null terminate string
81 filter
[filterSize
-1] = 0;
83 if (pcap_compile(pkts
, &bpf
, filter
, 1, PCAP_NETMASK_UNKNOWN
) == 0) {
85 r
= pcap_next_ex(pkts
, &header
, &pkt
);
88 fprintf(outfile
, "packet length=%d/%d filter=%d\n",header
->caplen
, header
->len
, pcap_offline_filter(&bpf
, header
, pkt
));
89 r
= pcap_next_ex(pkts
, &header
, &pkt
);