1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Added PacketWriter class.

This commit is contained in:
Matias Fontanini
2012-09-06 14:18:07 -03:00
parent 3d2cbf349a
commit 9981819b71
14 changed files with 130 additions and 8 deletions

59
include/packet_writer.h Normal file
View File

@@ -0,0 +1,59 @@
/*
* libtins is a net packet wrapper library for crafting and
* interpreting sniffed packets.
*
* Copyright (C) 2011 Nasel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef TINS_PACKET_WRITER_H
#define TINS_PACKET_WRITER_H
#include <pcap.h>
#include <string>
namespace Tins {
class PDU;
class PacketWriter {
public:
enum LinkType {
RADIOTAP = DLT_IEEE802_11_RADIO,
ETH2 = DLT_EN10MB
};
PacketWriter(const std::string &file_name, LinkType lt);
~PacketWriter();
void write(PDU *pdu);
template<typename ForwardIterator>
ForwardIterator write(ForwardIterator start, ForwardIterator end) {
while(start != end)
write(*start++);
return start;
}
private:
// You shall not copy
PacketWriter(const PacketWriter&);
PacketWriter& operator=(const PacketWriter&);
pcap_t *handle;
pcap_dumper_t *dumper;
};
}
#endif // TINS_PACKET_WRITER_H

View File

@@ -27,6 +27,7 @@
#include "network_interface.h" #include "network_interface.h"
namespace Tins { namespace Tins {
class PacketSender;
/** /**
* \brief Class that represents the IEEE 802.11 radio tap header. * \brief Class that represents the IEEE 802.11 radio tap header.

View File

@@ -32,7 +32,8 @@
#include "icmp.h" #include "icmp.h"
#include "dot11.h" #include "dot11.h"
#include "ip.h" #include "ip.h"
#include "packetsender.h" #include "packet_sender.h"
#include "packet_writer.h"
#include "pdu.h" #include "pdu.h"
#include "radiotap.h" #include "radiotap.h"
#include "rawpdu.h" #include "rawpdu.h"

View File

@@ -29,12 +29,13 @@
#include <set> #include <set>
#include <fstream> #include <fstream>
#include <stdint.h> #include <stdint.h>
#include "packetsender.h"
#include "ipaddress.h" #include "ipaddress.h"
#include "hwaddress.h" #include "hwaddress.h"
namespace Tins { namespace Tins {
class NetworkInterface; class NetworkInterface;
class PacketSender;
class PDU;
/** /**
* \brief Network utils namespace. * \brief Network utils namespace.

View File

@@ -33,7 +33,7 @@
#include "rawpdu.h" #include "rawpdu.h"
#include "radiotap.h" #include "radiotap.h"
#include "rsn_information.h" #include "rsn_information.h"
#include "packetsender.h" #include "packet_sender.h"
#include "snap.h" #include "snap.h"
using std::pair; using std::pair;

View File

@@ -29,7 +29,7 @@
#include <netpacket/packet.h> #include <netpacket/packet.h>
#endif #endif
#include "ethernetII.h" #include "ethernetII.h"
#include "packetsender.h" #include "packet_sender.h"
#include "rawpdu.h" #include "rawpdu.h"
#include "ip.h" #include "ip.h"
#include "arp.h" #include "arp.h"

View File

@@ -29,7 +29,7 @@
#include <netpacket/packet.h> #include <netpacket/packet.h>
#endif #endif
#include "ieee802_3.h" #include "ieee802_3.h"
#include "packetsender.h" #include "packet_sender.h"
#include "llc.h" #include "llc.h"
namespace Tins { namespace Tins {

View File

@@ -33,6 +33,7 @@
#include "icmp.h" #include "icmp.h"
#include "rawpdu.h" #include "rawpdu.h"
#include "utils.h" #include "utils.h"
#include "packet_sender.h"
#include "constants.h" #include "constants.h"
using std::list; using std::list;

View File

@@ -34,8 +34,8 @@
#include <errno.h> #include <errno.h>
#include <cstring> #include <cstring>
#include <ctime> #include <ctime>
#include "packetsender.h"
#include "pdu.h" #include "pdu.h"
#include "packet_sender.h"
const int Tins::PacketSender::INVALID_RAW_SOCKET = -1; const int Tins::PacketSender::INVALID_RAW_SOCKET = -1;
@@ -101,7 +101,6 @@ Tins::PDU *Tins::PacketSender::send_recv(PDU *pdu) {
} }
bool Tins::PacketSender::send_l2(PDU *pdu, struct sockaddr* link_addr, uint32_t len_addr) { bool Tins::PacketSender::send_l2(PDU *pdu, struct sockaddr* link_addr, uint32_t len_addr) {
if(!open_l2_socket()) if(!open_l2_socket())
return false; return false;

58
src/packet_writer.cpp Normal file
View File

@@ -0,0 +1,58 @@
/*
* libtins is a net packet wrapper library for crafting and
* interpreting sniffed packets.
*
* Copyright (C) 2011 Nasel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef WIN32
#include <sys/time.h>
#endif
#include <stdexcept>
#include "packet_writer.h"
#include "pdu.h"
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));
}
}
PacketWriter::~PacketWriter() {
pcap_dump_close(dumper);
pcap_close(handle);
}
void PacketWriter::write(PDU *pdu) {
PDU::serialization_type buffer = pdu->serialize();
struct timeval tm;
gettimeofday(&tm, 0);
struct pcap_pkthdr header = {
tm,
buffer.size(),
buffer.size()
};
pcap_dump((u_char*)dumper, &header, &buffer[0]);
}
}

View File

@@ -22,7 +22,7 @@
#include <cassert> #include <cassert>
#include "pdu.h" #include "pdu.h"
#include "rawpdu.h" #include "rawpdu.h"
#include "packetsender.h" #include "packet_sender.h"
namespace Tins { namespace Tins {

View File

@@ -29,6 +29,7 @@
#include "radiotap.h" #include "radiotap.h"
#include "dot11.h" #include "dot11.h"
#include "utils.h" #include "utils.h"
#include "packet_sender.h"
Tins::RadioTap::RadioTap(const NetworkInterface &iface, PDU *child) Tins::RadioTap::RadioTap(const NetworkInterface &iface, PDU *child)

View File

@@ -36,6 +36,7 @@
#include "arp.h" #include "arp.h"
#include "endianness.h" #include "endianness.h"
#include "network_interface.h" #include "network_interface.h"
#include "packet_sender.h"
using namespace std; using namespace std;