From 9981819b711d8e1f1c18d4cc93cbd8b2de074b16 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Thu, 6 Sep 2012 14:18:07 -0300 Subject: [PATCH] Added PacketWriter class. --- include/{packetsender.h => packet_sender.h} | 0 include/packet_writer.h | 59 +++++++++++++++++++++ include/radiotap.h | 1 + include/tins.h | 3 +- include/utils.h | 3 +- src/dot11.cpp | 2 +- src/ethernetII.cpp | 2 +- src/ieee802_3.cpp | 2 +- src/ip.cpp | 1 + src/{packetsender.cpp => packet_sender.cpp} | 3 +- src/packet_writer.cpp | 58 ++++++++++++++++++++ src/pdu.cpp | 2 +- src/radiotap.cpp | 1 + src/utils.cpp | 1 + 14 files changed, 130 insertions(+), 8 deletions(-) rename include/{packetsender.h => packet_sender.h} (100%) create mode 100644 include/packet_writer.h rename src/{packetsender.cpp => packet_sender.cpp} (99%) create mode 100644 src/packet_writer.cpp diff --git a/include/packetsender.h b/include/packet_sender.h similarity index 100% rename from include/packetsender.h rename to include/packet_sender.h diff --git a/include/packet_writer.h b/include/packet_writer.h new file mode 100644 index 0000000..445dadb --- /dev/null +++ b/include/packet_writer.h @@ -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 +#include + +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 + 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 diff --git a/include/radiotap.h b/include/radiotap.h index 478343a..6661b41 100644 --- a/include/radiotap.h +++ b/include/radiotap.h @@ -27,6 +27,7 @@ #include "network_interface.h" namespace Tins { + class PacketSender; /** * \brief Class that represents the IEEE 802.11 radio tap header. diff --git a/include/tins.h b/include/tins.h index caa628e..fe950f2 100644 --- a/include/tins.h +++ b/include/tins.h @@ -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" diff --git a/include/utils.h b/include/utils.h index ffa26b4..3f20e42 100644 --- a/include/utils.h +++ b/include/utils.h @@ -29,12 +29,13 @@ #include #include #include -#include "packetsender.h" #include "ipaddress.h" #include "hwaddress.h" namespace Tins { class NetworkInterface; + class PacketSender; + class PDU; /** * \brief Network utils namespace. diff --git a/src/dot11.cpp b/src/dot11.cpp index 6cbf781..9872121 100644 --- a/src/dot11.cpp +++ b/src/dot11.cpp @@ -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; diff --git a/src/ethernetII.cpp b/src/ethernetII.cpp index f8b1e41..9283c2e 100644 --- a/src/ethernetII.cpp +++ b/src/ethernetII.cpp @@ -29,7 +29,7 @@ #include #endif #include "ethernetII.h" -#include "packetsender.h" +#include "packet_sender.h" #include "rawpdu.h" #include "ip.h" #include "arp.h" diff --git a/src/ieee802_3.cpp b/src/ieee802_3.cpp index 5fe0bd6..052823e 100644 --- a/src/ieee802_3.cpp +++ b/src/ieee802_3.cpp @@ -29,7 +29,7 @@ #include #endif #include "ieee802_3.h" -#include "packetsender.h" +#include "packet_sender.h" #include "llc.h" namespace Tins { diff --git a/src/ip.cpp b/src/ip.cpp index c253fba..3ec9073 100644 --- a/src/ip.cpp +++ b/src/ip.cpp @@ -33,6 +33,7 @@ #include "icmp.h" #include "rawpdu.h" #include "utils.h" +#include "packet_sender.h" #include "constants.h" using std::list; diff --git a/src/packetsender.cpp b/src/packet_sender.cpp similarity index 99% rename from src/packetsender.cpp rename to src/packet_sender.cpp index 1833df2..2c7fb99 100644 --- a/src/packetsender.cpp +++ b/src/packet_sender.cpp @@ -34,8 +34,8 @@ #include #include #include -#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; diff --git a/src/packet_writer.cpp b/src/packet_writer.cpp new file mode 100644 index 0000000..8c421e8 --- /dev/null +++ b/src/packet_writer.cpp @@ -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 +#endif +#include +#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]); +} +} diff --git a/src/pdu.cpp b/src/pdu.cpp index 17318ee..d5a7a03 100644 --- a/src/pdu.cpp +++ b/src/pdu.cpp @@ -22,7 +22,7 @@ #include #include "pdu.h" #include "rawpdu.h" -#include "packetsender.h" +#include "packet_sender.h" namespace Tins { diff --git a/src/radiotap.cpp b/src/radiotap.cpp index 75c427b..d614926 100644 --- a/src/radiotap.cpp +++ b/src/radiotap.cpp @@ -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) diff --git a/src/utils.cpp b/src/utils.cpp index f567d58..d9a9434 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -36,6 +36,7 @@ #include "arp.h" #include "endianness.h" #include "network_interface.h" +#include "packet_sender.h" using namespace std;