1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include <pcap/pcap.h>
6
7void fuzz_openFile(const char * name){
8    //do nothing
9}
10
11int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
12    pcap_t * pkts;
13    struct bpf_program bpf;
14    char * filter;
15
16    //we need at least 1 byte for linktype
17    if (Size < 1) {
18        return 0;
19    }
20
21    //initialize structure snaplen = 65535
22    pkts = pcap_open_dead(Data[Size-1], 0xFFFF);
23    if (pkts == NULL) {
24        printf("pcap_open_dead failed\n");
25        return 0;
26    }
27    filter = malloc(Size);
28    memcpy(filter, Data, Size);
29    //null terminate string
30    filter[Size-1] = 0;
31
32    if (pcap_compile(pkts, &bpf, filter, 1, PCAP_NETMASK_UNKNOWN) == 0) {
33        pcap_setfilter(pkts, &bpf);
34        pcap_close(pkts);
35        pcap_freecode(&bpf);
36    }
37    else {
38        pcap_close(pkts);
39    }
40    free(filter);
41
42    return 0;
43}
44