From 9d2a60ef43d0ba0db80b08ce00a1ccad124dc6af Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Sat, 30 Aug 2014 14:28:29 -0300 Subject: [PATCH] Added DataLinkType class. --- include/data_link_type.h | 72 ++++++++++++++++++++++++++++++++++++++++ include/packet_writer.h | 30 +++++++++++++++++ src/packet_writer.cpp | 23 ++++++++----- 3 files changed, 116 insertions(+), 9 deletions(-) create mode 100644 include/data_link_type.h diff --git a/include/data_link_type.h b/include/data_link_type.h new file mode 100644 index 0000000..6d05377 --- /dev/null +++ b/include/data_link_type.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2014, Matias Fontanini + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef TINS_DATA_LINK_TYPE_H +#define TINS_DATA_LINK_TYPE_H + +#include + +namespace Tins { + +class EthernetII; +class RadioTap; +class Dot11; +class Dot3; +class SLL; +class Loopback; +class PPI; + +/** + * \brief Maps a libtins link layer PDU to a libpcap data link identifier. + */ +template +struct DataLinkType; + +#define TINS_MAKE_DATA_LINK_TYPE(tins_type, pcap_type) \ +template<> \ +struct DataLinkType { \ + static const int type = pcap_type; \ + int get_type() const { \ + return type; \ + } \ +}; + +TINS_MAKE_DATA_LINK_TYPE(EthernetII, DLT_EN10MB) +TINS_MAKE_DATA_LINK_TYPE(Dot3, DLT_EN10MB) +TINS_MAKE_DATA_LINK_TYPE(SLL, DLT_LINUX_SLL) +TINS_MAKE_DATA_LINK_TYPE(Loopback, DLT_LOOP) +TINS_MAKE_DATA_LINK_TYPE(PPI, DLT_PPI) +TINS_MAKE_DATA_LINK_TYPE(Dot11, DLT_IEEE802_11) +TINS_MAKE_DATA_LINK_TYPE(RadioTap, DLT_IEEE802_11_RADIO) + +#undef TINS_MAKE_DATA_LINK_TYPE + +} // Tins + +#endif // TINS_DATA_LINK_TYPE_H diff --git a/include/packet_writer.h b/include/packet_writer.h index 186bd42..d56d6b4 100644 --- a/include/packet_writer.h +++ b/include/packet_writer.h @@ -33,6 +33,7 @@ #include #include #include +#include "data_link_type.h" #include "utils.h" #include "cxxstd.h" @@ -66,6 +67,33 @@ public: * \sa LinkType. */ PacketWriter(const std::string &file_name, LinkType lt); + + /** + * \brief Constructs a PacketWriter. + * + * This method takes a DataLinkType, which indicates the link + * layer protocol that will be used on the packets to write. + * + * For example, you can write packets that contain an + * EthernetII link layer type by doing: + * + * \code + * // Construct a PacketWriter + * PacketWriter writer("/tmp/test.pcap", DataLinkType()); + * // Write some packet + * writer.write(packet); + * \endcode + * + * \param file_name The file in which to store the written PDUs. + * \param lt A DataLinkType that represents the link layer + * protocol to use. + * \sa PcapIdentifier. + */ + template + PacketWriter(const std::string &file_name, const DataLinkType& lt) + { + init(file_name, lt.get_type()); + } #if TINS_IS_CXX11 /** @@ -136,6 +164,8 @@ private: PacketWriter(const PacketWriter&); PacketWriter& operator=(const PacketWriter&); + void init(const std::string& file_name, int link_type); + pcap_t *handle; pcap_dumper_t *dumper; }; diff --git a/src/packet_writer.cpp b/src/packet_writer.cpp index b0b6d32..19df0cd 100644 --- a/src/packet_writer.cpp +++ b/src/packet_writer.cpp @@ -36,15 +36,7 @@ namespace Tins { PacketWriter::PacketWriter(const std::string &file_name, LinkType lt) { - handle = pcap_open_dead(lt, 65535); - if(!handle) - throw std::runtime_error("Error creating pcap handle"); - dumper = pcap_dump_open(handle, file_name.c_str()); - if(!dumper) { - // RAII plx - pcap_close(handle); - throw std::runtime_error(pcap_geterr(handle)); - } + init(file_name, lt); } PacketWriter::~PacketWriter() { @@ -70,4 +62,17 @@ void PacketWriter::write(PDU &pdu) { }; pcap_dump((u_char*)dumper, &header, &buffer[0]); } + +void PacketWriter::init(const std::string& file_name, int link_type) { + handle = pcap_open_dead(link_type, 65535); + if(!handle) + throw std::runtime_error("Error creating pcap handle"); + dumper = pcap_dump_open(handle, file_name.c_str()); + if(!dumper) { + // RAII plx + pcap_close(handle); + throw std::runtime_error(pcap_geterr(handle)); + } +} + }