mirror of
https://github.com/mfontanini/libtins
synced 2026-01-30 13:34:27 +01:00
Added DataLinkType class.
This commit is contained in:
72
include/data_link_type.h
Normal file
72
include/data_link_type.h
Normal file
@@ -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 <pcap.h>
|
||||||
|
|
||||||
|
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<typename T>
|
||||||
|
struct DataLinkType;
|
||||||
|
|
||||||
|
#define TINS_MAKE_DATA_LINK_TYPE(tins_type, pcap_type) \
|
||||||
|
template<> \
|
||||||
|
struct DataLinkType<tins_type> { \
|
||||||
|
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
|
||||||
@@ -33,6 +33,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <pcap.h>
|
#include <pcap.h>
|
||||||
|
#include "data_link_type.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "cxxstd.h"
|
#include "cxxstd.h"
|
||||||
|
|
||||||
@@ -66,6 +67,33 @@ public:
|
|||||||
* \sa LinkType.
|
* \sa LinkType.
|
||||||
*/
|
*/
|
||||||
PacketWriter(const std::string &file_name, LinkType lt);
|
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<EthernetII>());
|
||||||
|
* // 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<typename T>
|
||||||
|
PacketWriter(const std::string &file_name, const DataLinkType<T>& lt)
|
||||||
|
{
|
||||||
|
init(file_name, lt.get_type());
|
||||||
|
}
|
||||||
|
|
||||||
#if TINS_IS_CXX11
|
#if TINS_IS_CXX11
|
||||||
/**
|
/**
|
||||||
@@ -136,6 +164,8 @@ private:
|
|||||||
PacketWriter(const PacketWriter&);
|
PacketWriter(const PacketWriter&);
|
||||||
PacketWriter& operator=(const PacketWriter&);
|
PacketWriter& operator=(const PacketWriter&);
|
||||||
|
|
||||||
|
void init(const std::string& file_name, int link_type);
|
||||||
|
|
||||||
pcap_t *handle;
|
pcap_t *handle;
|
||||||
pcap_dumper_t *dumper;
|
pcap_dumper_t *dumper;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -36,15 +36,7 @@
|
|||||||
|
|
||||||
namespace Tins {
|
namespace Tins {
|
||||||
PacketWriter::PacketWriter(const std::string &file_name, LinkType lt) {
|
PacketWriter::PacketWriter(const std::string &file_name, LinkType lt) {
|
||||||
handle = pcap_open_dead(lt, 65535);
|
init(file_name, lt);
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PacketWriter::~PacketWriter() {
|
PacketWriter::~PacketWriter() {
|
||||||
@@ -70,4 +62,17 @@ void PacketWriter::write(PDU &pdu) {
|
|||||||
};
|
};
|
||||||
pcap_dump((u_char*)dumper, &header, &buffer[0]);
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user