]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dnsdist-dynbpf.cc
Merge pull request #4306 from Habbie/mysqllongtext
[thirdparty/pdns.git] / pdns / dnsdist-dynbpf.cc
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 #include "dnsdist-dynbpf.hh"
23
24 #ifdef HAVE_EBPF
25
26 void DynBPFFilter::block(const ComboAddress& addr, const struct timespec& until)
27 {
28 std::unique_lock<std::mutex> lock(d_mutex);
29
30 const container_t::iterator it = d_entries.find(addr);
31 if (it != d_entries.end()) {
32 if (it->d_until < until) {
33 d_entries.replace(it, BlockEntry(addr, until));
34 }
35 }
36 else {
37 d_bpf->block(addr);
38 d_entries.insert(BlockEntry(addr, until));
39 }
40 }
41
42 void DynBPFFilter::purgeExpired(const struct timespec& now)
43 {
44 std::unique_lock<std::mutex> lock(d_mutex);
45
46 typedef nth_index<container_t,1>::type ordered_until;
47 ordered_until& ou = get<1>(d_entries);
48
49 for (ordered_until::iterator it=ou.begin(); it != ou.end(); ) {
50 if (it->d_until < now) {
51 ComboAddress addr = it->d_addr;
52 it = ou.erase(it);
53 d_bpf->unblock(addr);
54 }
55 else {
56 break;
57 }
58 }
59 }
60
61 std::vector<std::tuple<ComboAddress, uint64_t, struct timespec> > DynBPFFilter::getAddrStats()
62 {
63 std::vector<std::tuple<ComboAddress, uint64_t, struct timespec> > result;
64 if (!d_bpf) {
65 return result;
66 }
67
68 const auto& stats = d_bpf->getAddrStats();
69 for (const auto& stat : stats) {
70 const container_t::iterator it = d_entries.find(stat.first);
71 if (it != d_entries.end()) {
72 result.push_back(std::make_tuple(stat.first, stat.second, it->d_until));
73 }
74 }
75 return result;
76 }
77
78 #endif /* HAVE_EBPF */