libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
offline_packet_filter.h
1 /*
2  * Copyright (c) 2016, 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 "data_link_type.h"
36 #include "macros.h"
37 
38 namespace Tins {
39 
40 class PDU;
41 
79 class TINS_API OfflinePacketFilter {
80 public:
88  template<typename T>
89  OfflinePacketFilter(const std::string& filter,
90  const DataLinkType<T>& lt,
91  unsigned int snap_len = 65535)
92  : string_filter_(filter) {
93  init(filter, lt.get_type(), snap_len);
94  }
95 
106 
114  OfflinePacketFilter& operator=(const OfflinePacketFilter& other);
115 
120 
132  bool matches_filter(const uint8_t* buffer, uint32_t total_sz) const;
133 
147  bool matches_filter(PDU& pdu) const;
148 private:
149  void init(const std::string& pcap_filter, int link_type,
150  unsigned int snap_len);
151 
152 
153  pcap_t* handle_;
154  mutable bpf_program filter_;
155  std::string string_filter_;
156 };
157 } // Tins
158 
159 #endif // TINS_OFFLINE_PACKET_FILTER_H
Maps a libtins link layer PDU to a libpcap data link identifier.
Definition: data_link_type.h:58
Wraps a pcap filter and matches it against a packet or buffer.
Definition: offline_packet_filter.h:79
OfflinePacketFilter(const std::string &filter, const DataLinkType< T > &lt, unsigned int snap_len=65535)
Definition: offline_packet_filter.h:89
Base class for protocol data units.
Definition: pdu.h:108