]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/bpf-filter.hh
Merge pull request #8223 from PowerDNS/omoerbeek-patch-1
[thirdparty/pdns.git] / pdns / bpf-filter.hh
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 #pragma once
23 #include "config.h"
24
25 #include <mutex>
26
27 #include "iputils.hh"
28
29 #ifdef HAVE_EBPF
30
31 class BPFFilter
32 {
33 public:
34 BPFFilter(uint32_t maxV4Addresses, uint32_t maxV6Addresses, uint32_t maxQNames);
35 void addSocket(int sock);
36 void removeSocket(int sock);
37 void block(const ComboAddress& addr);
38 void block(const DNSName& qname, uint16_t qtype=255);
39 void unblock(const ComboAddress& addr);
40 void unblock(const DNSName& qname, uint16_t qtype=255);
41 std::vector<std::pair<ComboAddress, uint64_t> > getAddrStats();
42 std::vector<std::tuple<DNSName, uint16_t, uint64_t> > getQNameStats();
43 private:
44 struct FDWrapper
45 {
46 ~FDWrapper()
47 {
48 if (fd != -1) {
49 close(fd);
50 }
51 }
52 int fd{-1};
53 };
54 std::mutex d_mutex;
55 uint32_t d_maxV4;
56 uint32_t d_maxV6;
57 uint32_t d_maxQNames;
58 uint32_t d_v4Count{0};
59 uint32_t d_v6Count{0};
60 uint32_t d_qNamesCount{0};
61 FDWrapper d_v4map;
62 FDWrapper d_v6map;
63 FDWrapper d_qnamemap;
64 FDWrapper d_filtermap;
65 FDWrapper d_mainfilter;
66 FDWrapper d_qnamefilter;
67 };
68
69 #endif /* HAVE_EBPF */