libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
packet_writer.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_PACKET_WRITER_H
31 #define TINS_PACKET_WRITER_H
32 
33 #include "utils.h"
34 #include <string>
35 #include <iterator>
36 #include <pcap.h>
37 #include "data_link_type.h"
38 #include "macros.h"
39 #include "cxxstd.h"
40 
41 struct timeval;
42 
43 namespace Tins {
44 class PDU;
45 class Packet;
46 
79 class TINS_API PacketWriter {
80 public:
88  enum LinkType {
89  RADIOTAP = DLT_IEEE802_11_RADIO,
90  DOT11 = DLT_IEEE802_11,
91  ETH2 = DLT_EN10MB,
92  DOT3 = DLT_EN10MB,
93  SLL = DLT_LINUX_SLL
94  };
95 
117  template<typename T>
118  PacketWriter(const std::string& file_name, const DataLinkType<T>& lt) {
119  init(file_name, lt.get_type());
120  }
121 
132  PacketWriter(const std::string& file_name, LinkType lt);
133 
134  #if TINS_IS_CXX11
135 
143  PacketWriter(PacketWriter &&rhs) TINS_NOEXCEPT {
144  *this = std::move(rhs);
145  }
146 
155  PacketWriter& operator=(PacketWriter &&rhs) TINS_NOEXCEPT {
156  handle_ = 0;
157  dumper_ = 0;
158  std::swap(handle_, rhs.handle_);
159  std::swap(dumper_, rhs.dumper_);
160  return* this;
161  }
162  #endif
163 
169  ~PacketWriter();
170 
175  void write(PDU& pdu);
176 
185  void write(Packet& packet);
186 
194  template<typename T>
195  void write(T& pdu) {
196  write(Utils::dereference_until_pdu(pdu));
197  }
198 
206  template<typename ForwardIterator>
207  void write(ForwardIterator start, ForwardIterator end) {
208  while (start != end) {
209  write(Utils::dereference_until_pdu(*start++));
210  }
211  }
212 private:
213  // You shall not copy
214  PacketWriter(const PacketWriter&);
215  PacketWriter& operator=(const PacketWriter&);
216 
217  void init(const std::string& file_name, int link_type);
218  void write(PDU& pdu, const struct timeval& tv);
219 
220  pcap_t* handle_;
221  pcap_dumper_t* dumper_;
222 };
223 }
224 
225 #endif // TINS_PACKET_WRITER_H
Maps a libtins link layer PDU to a libpcap data link identifier.
Definition: data_link_type.h:58
Definition: packet.h:131
PacketWriter(const std::string &file_name, const DataLinkType< T > &lt)
Constructs a PacketWriter.
Definition: packet_writer.h:118
Represents a Linux cooked-mode capture (SLL) PDU.
Definition: sll.h:45
PacketWriter(PacketWriter &&rhs) TINS_NOEXCEPT
Move constructor.
Definition: packet_writer.h:143
Writes PDUs to a pcap format file.
Definition: packet_writer.h:79
PDU & dereference_until_pdu(PDU &pdu)
Definition: utils.h:309
void write(T &pdu)
Writes a PDU to this file.
Definition: packet_writer.h:195
void write(ForwardIterator start, ForwardIterator end)
Writes all the PDUs in the range [start, end)
Definition: packet_writer.h:207
PacketWriter & operator=(PacketWriter &&rhs) TINS_NOEXCEPT
Move assignment operator.
Definition: packet_writer.h:155
LinkType
The type of PDUs that will be written to this file (deprecated).
Definition: packet_writer.h:88
Base class for protocol data units.
Definition: pdu.h:108