mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
Added PacketWriter class.
This commit is contained in:
59
include/packet_writer.h
Normal file
59
include/packet_writer.h
Normal 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
|
||||
@@ -27,6 +27,7 @@
|
||||
#include "network_interface.h"
|
||||
|
||||
namespace Tins {
|
||||
class PacketSender;
|
||||
|
||||
/**
|
||||
* \brief Class that represents the IEEE 802.11 radio tap header.
|
||||
|
||||
@@ -32,7 +32,8 @@
|
||||
#include "icmp.h"
|
||||
#include "dot11.h"
|
||||
#include "ip.h"
|
||||
#include "packetsender.h"
|
||||
#include "packet_sender.h"
|
||||
#include "packet_writer.h"
|
||||
#include "pdu.h"
|
||||
#include "radiotap.h"
|
||||
#include "rawpdu.h"
|
||||
|
||||
@@ -29,12 +29,13 @@
|
||||
#include <set>
|
||||
#include <fstream>
|
||||
#include <stdint.h>
|
||||
#include "packetsender.h"
|
||||
#include "ipaddress.h"
|
||||
#include "hwaddress.h"
|
||||
|
||||
namespace Tins {
|
||||
class NetworkInterface;
|
||||
class PacketSender;
|
||||
class PDU;
|
||||
|
||||
/**
|
||||
* \brief Network utils namespace.
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
#include "rawpdu.h"
|
||||
#include "radiotap.h"
|
||||
#include "rsn_information.h"
|
||||
#include "packetsender.h"
|
||||
#include "packet_sender.h"
|
||||
#include "snap.h"
|
||||
|
||||
using std::pair;
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
#include <netpacket/packet.h>
|
||||
#endif
|
||||
#include "ethernetII.h"
|
||||
#include "packetsender.h"
|
||||
#include "packet_sender.h"
|
||||
#include "rawpdu.h"
|
||||
#include "ip.h"
|
||||
#include "arp.h"
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
#include <netpacket/packet.h>
|
||||
#endif
|
||||
#include "ieee802_3.h"
|
||||
#include "packetsender.h"
|
||||
#include "packet_sender.h"
|
||||
#include "llc.h"
|
||||
|
||||
namespace Tins {
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
#include "icmp.h"
|
||||
#include "rawpdu.h"
|
||||
#include "utils.h"
|
||||
#include "packet_sender.h"
|
||||
#include "constants.h"
|
||||
|
||||
using std::list;
|
||||
|
||||
@@ -34,8 +34,8 @@
|
||||
#include <errno.h>
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
#include "packetsender.h"
|
||||
#include "pdu.h"
|
||||
#include "packet_sender.h"
|
||||
|
||||
|
||||
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) {
|
||||
|
||||
if(!open_l2_socket())
|
||||
return false;
|
||||
|
||||
58
src/packet_writer.cpp
Normal file
58
src/packet_writer.cpp
Normal 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]);
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@
|
||||
#include <cassert>
|
||||
#include "pdu.h"
|
||||
#include "rawpdu.h"
|
||||
#include "packetsender.h"
|
||||
#include "packet_sender.h"
|
||||
|
||||
namespace Tins {
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include "radiotap.h"
|
||||
#include "dot11.h"
|
||||
#include "utils.h"
|
||||
#include "packet_sender.h"
|
||||
|
||||
|
||||
Tins::RadioTap::RadioTap(const NetworkInterface &iface, PDU *child)
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
#include "arp.h"
|
||||
#include "endianness.h"
|
||||
#include "network_interface.h"
|
||||
#include "packet_sender.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
Reference in New Issue
Block a user