libtins  4.0
offline_packet_filter.h
1 /*
2  * Copyright (c) 2017, Matias Fontanini
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef TINS_OFFLINE_PACKET_FILTER_H
31 #define TINS_OFFLINE_PACKET_FILTER_H
32 
33 #include <string>
34 #include <stdint.h>
35 #include <tins/macros.h>
36 
37 #ifdef TINS_HAVE_PCAP
38 
39 #include <tins/data_link_type.h>
40 
41 namespace Tins {
42 
43 class PDU;
44 
82 class TINS_API OfflinePacketFilter {
83 public:
91  template<typename T>
92  OfflinePacketFilter(const std::string& filter,
93  const DataLinkType<T>& lt,
94  unsigned int snap_len = 65535)
95  : string_filter_(filter) {
96  init(filter, lt.get_type(), snap_len);
97  }
98 
108  OfflinePacketFilter(const OfflinePacketFilter& other);
109 
117  OfflinePacketFilter& operator=(const OfflinePacketFilter& other);
118 
122  ~OfflinePacketFilter();
123 
135  bool matches_filter(const uint8_t* buffer, uint32_t total_sz) const;
136 
150  bool matches_filter(PDU& pdu) const;
151 private:
152  void init(const std::string& pcap_filter, int link_type,
153  unsigned int snap_len);
154 
155 
156  pcap_t* handle_;
157  mutable bpf_program filter_;
158  std::string string_filter_;
159 };
160 
161 } // Tins
162 
163 #endif // TINS_HAVE_PCAP
164 
165 #endif // TINS_OFFLINE_PACKET_FILTER_H
The Tins namespace.
Definition: address_range.h:38