libtins 4.5
Loading...
Searching...
No Matches
packet_writer.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_PACKET_WRITER_H
31#define TINS_PACKET_WRITER_H
32
33#include <string>
34#include <tins/macros.h>
35#include <tins/cxxstd.h>
36#include <tins/utils/pdu_utils.h>
37
38#ifdef TINS_HAVE_PCAP
39#include <pcap.h>
40#include <tins/data_link_type.h>
41
42struct timeval;
43
44namespace Tins {
45class PDU;
46class Packet;
47
80class TINS_API PacketWriter {
81public:
89 enum LinkType {
90 RADIOTAP = DLT_IEEE802_11_RADIO,
91 DOT11 = DLT_IEEE802_11,
92 ETH2 = DLT_EN10MB,
93 DOT3 = DLT_EN10MB,
94 SLL = DLT_LINUX_SLL
95 };
96
118 template<typename T>
119 PacketWriter(const std::string& file_name, const DataLinkType<T>& lt) {
120 init(file_name, lt.get_type());
121 }
122
133 PacketWriter(const std::string& file_name, LinkType lt);
134
135 #if TINS_IS_CXX11
144 PacketWriter(PacketWriter &&rhs) TINS_NOEXCEPT {
145 *this = std::move(rhs);
146 }
147
156 PacketWriter& operator=(PacketWriter &&rhs) TINS_NOEXCEPT {
157 handle_ = 0;
158 dumper_ = 0;
159 std::swap(handle_, rhs.handle_);
160 std::swap(dumper_, rhs.dumper_);
161 return* this;
162 }
163 #endif
164
170 ~PacketWriter();
171
176 void write(PDU& pdu);
177
186 void write(Packet& packet);
187
195 template<typename T>
196 void write(T& pdu) {
197 write(Utils::dereference_until_pdu(pdu));
198 }
199
207 template<typename ForwardIterator>
208 void write(ForwardIterator start, ForwardIterator end) {
209 while (start != end) {
210 write(Utils::dereference_until_pdu(*start++));
211 }
212 }
213private:
214 // You shall not copy
215 PacketWriter(const PacketWriter&);
216 PacketWriter& operator=(const PacketWriter&);
217
218 void init(const std::string& file_name, int link_type);
219 void write(PDU& pdu, const struct timeval& tv);
220
221 pcap_t* handle_;
222 pcap_dumper_t* dumper_;
223};
224
225} // Tins
226
227#endif // TINS_HAVE_PCAP
228
229#endif // TINS_PACKET_WRITER_H
The Tins namespace.
Definition address_range.h:38