• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #include <errno.h>
5 
6 #include <pcap/pcap.h>
7 
8 FILE * outfile = NULL;
9 struct pcap_rmtauth auth;
10 
fuzz_openFile(const char * name)11 void fuzz_openFile(const char * name) {
12     if (outfile != NULL) {
13         fclose(outfile);
14     }
15     outfile = fopen(name, "w");
16     auth.type = RPCAP_RMTAUTH_PWD;
17     auth.username = "user";
18     auth.password = "pass";
19 }
20 
21 void sock_initfuzz(const uint8_t *Data, size_t Size);
LLVMFuzzerTestOneInput(const uint8_t * Data,size_t Size)22 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
23     pcap_t * pkts;
24     char errbuf[PCAP_ERRBUF_SIZE];
25     const u_char *pkt;
26     struct pcap_pkthdr *header;
27     struct pcap_stat stats;
28     int r;
29 
30     //initialization
31     if (outfile == NULL) {
32         fuzz_openFile("/dev/null");
33     }
34 
35     sock_initfuzz(Data, Size);
36     //initialize structure
37     pkts = pcap_open("rpcap://127.0.0.1/fuzz.pcap", 0, 0, 1000, &auth, errbuf);
38     if (pkts == NULL) {
39         fprintf(outfile, "Couldn't open pcap file %s\n", errbuf);
40         return 0;
41     }
42 
43     //loop over packets
44     r = pcap_next_ex(pkts, &header, &pkt);
45     while (r > 0) {
46         fprintf(outfile, "packet length=%d/%d\n",header->caplen, header->len);
47         r = pcap_next_ex(pkts, &header, &pkt);
48     }
49     if (pcap_stats(pkts, &stats) == 0) {
50         fprintf(outfile, "number of packets=%d\n", stats.ps_recv);
51     }
52     //close structure
53     pcap_close(pkts);
54 
55     return 0;
56 }
57