summaryrefslogtreecommitdiffstats
path: root/dnsdist-dynbpf.hh
blob: afe77965275583fb1357a777a159f2fbf0a1606d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * This file is part of PowerDNS or dnsdist.
 * Copyright -- PowerDNS.COM B.V. and its contributors
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * In addition, for the avoidance of any doubt, permission is granted to
 * link this program with OpenSSL and to (re)distribute the binaries
 * produced as the result of such linking.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#pragma once
#include "config.h"

#include "bpf-filter.hh"
#include "iputils.hh"

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>

class DynBPFFilter
{
public:
  DynBPFFilter(std::shared_ptr<BPFFilter>& bpf)
  {
    d_data.lock()->d_bpf = bpf;
  }
  ~DynBPFFilter()
  {
  }
  void excludeRange(const Netmask& range)
  {
    d_data.lock()->d_excludedSubnets.addMask(range);
  }
  void includeRange(const Netmask& range)
  {
    d_data.lock()->d_excludedSubnets.addMask(range, false);
  }
  /* returns true if the addr wasn't already blocked, false otherwise */
  bool block(const ComboAddress& addr, const struct timespec& until);
  void purgeExpired(const struct timespec& now);
  std::vector<std::tuple<ComboAddress, uint64_t, struct timespec> > getAddrStats();
private:
  struct BlockEntry
  {
    BlockEntry(const ComboAddress& addr, const struct timespec until): d_addr(addr), d_until(until)
    {
    }
    ComboAddress d_addr;
    struct timespec d_until;
  };
  typedef multi_index_container<BlockEntry,
                                indexed_by <
                                  ordered_unique< member<BlockEntry,ComboAddress,&BlockEntry::d_addr>, ComboAddress::addressOnlyLessThan >,
                                  ordered_non_unique< member<BlockEntry,struct timespec,&BlockEntry::d_until> >
                                  >
                                > container_t;
  struct Data {
    container_t d_entries;
    std::shared_ptr<BPFFilter> d_bpf{nullptr};
    NetmaskGroup d_excludedSubnets;
  };
  LockGuarded<Data> d_data;
};