]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dnsdist-dynbpf.cc
pkcs11signers: Use emplace_back for attributes
[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 bool DynBPFFilter::block(const ComboAddress& addr, const struct timespec& until)
25 {
26 bool inserted = false;
27 auto data = d_data.lock();
28
29 if (data->d_excludedSubnets.match(addr)) {
30 /* do not add a block for excluded subnets */
31 return inserted;
32 }
33
34 const container_t::iterator it = data->d_entries.find(addr);
35 if (it != data->d_entries.end()) {
36 if (it->d_until < until) {
37 data->d_entries.replace(it, BlockEntry(addr, until));
38 }
39 }
40 else {
41 data->d_bpf->block(addr, BPFFilter::MatchAction::Drop);
42 data->d_entries.insert(BlockEntry(addr, until));
43 inserted = true;
44 }
45 return inserted;
46 }
47
48 void DynBPFFilter::purgeExpired(const struct timespec& now)
49 {
50 auto data = d_data.lock();
51
52 typedef boost::multi_index::nth_index<container_t,1>::type ordered_until;
53 ordered_until& ou = boost::multi_index::get<1>(data->d_entries);
54
55 for (ordered_until::iterator it = ou.begin(); it != ou.end(); ) {
56 if (it->d_until < now) {
57 ComboAddress addr = it->d_addr;
58 it = ou.erase(it);
59 data->d_bpf->unblock(addr);
60 }
61 else {
62 break;
63 }
64 }
65 }
66
67 std::vector<std::tuple<ComboAddress, uint64_t, struct timespec> > DynBPFFilter::getAddrStats()
68 {
69 std::vector<std::tuple<ComboAddress, uint64_t, struct timespec> > result;
70 auto data = d_data.lock();
71
72 if (!data->d_bpf) {
73 return result;
74 }
75
76 const auto& stats = data->d_bpf->getAddrStats();
77 result.reserve(stats.size());
78 for (const auto& stat : stats) {
79 const container_t::iterator it = data->d_entries.find(stat.first);
80 if (it != data->d_entries.end()) {
81 result.push_back(std::make_tuple(stat.first, stat.second, it->d_until));
82 }
83 }
84 return result;
85 }