diff --git a/include/arp.h b/include/arp.h index c7495ae..1ac6eb8 100644 --- a/include/arp.h +++ b/include/arp.h @@ -269,15 +269,18 @@ namespace Tins { */ bool matches_response(uint8_t *ptr, uint32_t total_sz); - /** \brief Clones this pdu, filling the corresponding header with data + /** + * \brief Clones this pdu, filling the corresponding header with data * extracted from a buffer. * + * \deprecated This method is obsolete. + * * \param ptr The pointer to the from from which the data will be extracted. * \param total_sz The size of the buffer. * \return The cloned PDU. * \sa PDU::clone_packet */ - PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz); + TINS_DEPRECATED(PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz)); /** * \sa PDU::clone diff --git a/include/ethernetII.h b/include/ethernetII.h index 0e24e10..cb472cd 100644 --- a/include/ethernetII.h +++ b/include/ethernetII.h @@ -183,15 +183,18 @@ namespace Tins { */ PDUType pdu_type() const { return PDU::ETHERNET_II; } - /** \brief Clones this pdu, filling the corresponding header with data + /** + * \brief Clones this pdu, filling the corresponding header with data * extracted from a buffer. + * + * \deprecated This method is obsolete. * * \param ptr The pointer to the from from which the data will be extracted. * \param total_sz The size of the buffer. * \return The cloned PDU. * \sa PDU::clone_packet */ - PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz); + TINS_DEPRECATED(PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz)); /** * \sa PDU::clone diff --git a/include/icmp.h b/include/icmp.h index e451ab3..bdc994f 100644 --- a/include/icmp.h +++ b/include/icmp.h @@ -287,12 +287,14 @@ namespace Tins { * \brief Clones this pdu, filling the corresponding header with data * extracted from a buffer. * + * \deprecated This method is obsolete. + * * \param ptr The pointer to the from from which the data will be extracted. * \param total_sz The size of the buffer. * \return The cloned PDU. * \sa PDU::clone_packet */ - PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz); + TINS_DEPRECATED(PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz)); /** * \sa PDU::clone diff --git a/include/ieee802_3.h b/include/ieee802_3.h index 0788417..c64a88b 100644 --- a/include/ieee802_3.h +++ b/include/ieee802_3.h @@ -184,12 +184,14 @@ namespace Tins { * \brief Clones this pdu, filling the corresponding header with data * extracted from a buffer. * + * \deprecated This method is obsolete. + * * \param ptr The pointer to the from from which the data will be extracted. * \param total_sz The size of the buffer. * \return The cloned PDU. * \sa PDU::clone_packet */ - PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz); + TINS_DEPRECATED(PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz)); /** * \sa PDU::clone diff --git a/include/internals.h b/include/internals.h index c6132af..4a6321c 100644 --- a/include/internals.h +++ b/include/internals.h @@ -57,6 +57,8 @@ namespace Internals { PDU *pdu_from_flag(Constants::Ethernet::e flag, const uint8_t *buffer, uint32_t size, bool rawpdu_on_no_match = true); + PDU *pdu_from_flag(PDU::PDUType type, const uint8_t *buffer, uint32_t size); + Constants::Ethernet::e pdu_flag_to_ether_type(PDU::PDUType flag); } } diff --git a/include/ip.h b/include/ip.h index 8367eed..88b7d1f 100644 --- a/include/ip.h +++ b/include/ip.h @@ -595,12 +595,14 @@ namespace Tins { * \brief Clones this pdu, filling the corresponding header with data * extracted from a buffer. * + * \deprecated This method is obsolete. + * * \param ptr The pointer to the from from which the data will be extracted. * \param total_sz The size of the buffer. * \return The cloned PDU. * \sa PDU::clone_packet */ - PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz); + TINS_DEPRECATED(PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz)); /** * \sa PDU::clone diff --git a/include/pdu.h b/include/pdu.h index f40b64e..2e56c77 100644 --- a/include/pdu.h +++ b/include/pdu.h @@ -33,6 +33,7 @@ #include #include +#include "macros.h" /** \brief The Tins namespace. */ @@ -272,11 +273,13 @@ namespace Tins { /** \brief Clones this pdu, filling the corresponding header with data * extracted from a buffer. * + * \deprecated This method is obsolete. + * * \param ptr The pointer to the from from which the data will be extracted. * \param total_sz The size of the buffer. * \return The cloned PDU. */ - virtual PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz) { return 0; } + TINS_DEPRECATED(virtual PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz)); protected: /** * \brief Copy constructor. diff --git a/include/pdu_cacher.h b/include/pdu_cacher.h index 459adc1..257138c 100644 --- a/include/pdu_cacher.h +++ b/include/pdu_cacher.h @@ -32,6 +32,7 @@ #include #include "pdu.h" +#include "macros.h" namespace Tins { /** @@ -129,9 +130,11 @@ public: /** * Forwards the call to the cached PDU. \sa PDU::clone_packet. */ - PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz) { - return cached.clone_packet(ptr, total_sz); - } + TINS_DEPRECATED( + PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz) { + return 0; + } + ) private: void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) { if(cached_serialization.size() != total_sz) { diff --git a/src/arp.cpp b/src/arp.cpp index f2f38ba..5d22715 100644 --- a/src/arp.cpp +++ b/src/arp.cpp @@ -119,17 +119,7 @@ bool ARP::matches_response(uint8_t *ptr, uint32_t total_sz) { } PDU *ARP::clone_packet(const uint8_t *ptr, uint32_t total_sz) { - if(total_sz < sizeof(arphdr)) - return 0; - PDU *child = 0, *cloned; - if(total_sz > sizeof(arphdr)) { - child = PDU::clone_inner_pdu(ptr + sizeof(arphdr), total_sz - sizeof(arphdr)); - if(!child) - return 0; - } - cloned = new ARP(ptr, std::min(total_sz, (uint32_t)sizeof(_arp))); - cloned->inner_pdu(child); - return cloned; + return new ARP(ptr, total_sz); } EthernetII ARP::make_arp_request(const NetworkInterface& iface, diff --git a/src/ethernetII.cpp b/src/ethernetII.cpp index e9163fd..6d879dc 100644 --- a/src/ethernetII.cpp +++ b/src/ethernetII.cpp @@ -172,15 +172,6 @@ PDU *EthernetII::recv_response(PacketSender &sender) { #endif // WIN32 PDU *EthernetII::clone_packet(const uint8_t *ptr, uint32_t total_sz) { - if(total_sz < sizeof(_eth)) - return 0; - PDU *child = 0, *cloned; - if(total_sz > sizeof(_eth)) { - if((child = PDU::clone_inner_pdu(ptr + sizeof(_eth), total_sz - sizeof(_eth))) == 0) - return 0; - } - cloned = new EthernetII(ptr, std::min(total_sz, (uint32_t)sizeof(_eth))); - cloned->inner_pdu(child); - return cloned; + return new EthernetII(ptr, total_sz); } } diff --git a/src/icmp.cpp b/src/icmp.cpp index c813118..86cf74b 100644 --- a/src/icmp.cpp +++ b/src/icmp.cpp @@ -169,14 +169,5 @@ bool Tins::ICMP::matches_response(uint8_t *ptr, uint32_t total_sz) { } Tins::PDU *Tins::ICMP::clone_packet(const uint8_t *ptr, uint32_t total_sz) { - if(total_sz < sizeof(icmphdr)) - return 0; - PDU *child = 0, *cloned; - if(total_sz > sizeof(icmphdr)) { - if((child = PDU::clone_inner_pdu(ptr + sizeof(icmphdr), total_sz - sizeof(icmphdr))) == 0) - return 0; - } - cloned = new ICMP(ptr, std::min(total_sz, (uint32_t)sizeof(_icmp))); - cloned->inner_pdu(child); - return cloned; + return new ICMP(ptr, total_sz); } diff --git a/src/ieee802_3.cpp b/src/ieee802_3.cpp index e99898d..bf68a83 100644 --- a/src/ieee802_3.cpp +++ b/src/ieee802_3.cpp @@ -161,15 +161,6 @@ PDU *IEEE802_3::recv_response(PacketSender &sender) { #endif // WIN32 PDU *IEEE802_3::clone_packet(const uint8_t *ptr, uint32_t total_sz) { - if(total_sz < sizeof(_eth)) - return 0; - PDU *child = 0, *cloned; - if(total_sz > sizeof(_eth)) { - if((child = PDU::clone_inner_pdu(ptr + sizeof(_eth), total_sz - sizeof(_eth))) == 0) - return 0; - } - cloned = new IEEE802_3(ptr, std::min(total_sz, (uint32_t)sizeof(_eth))); - cloned->inner_pdu(child); - return cloned; + return new IEEE802_3(ptr, total_sz); } } diff --git a/src/internals.cpp b/src/internals.cpp index d787f7a..045edef 100644 --- a/src/internals.cpp +++ b/src/internals.cpp @@ -29,6 +29,10 @@ #include "internals.h" #include "ip.h" +#include "ethernetII.h" +#include "ieee802_3.h" +#include "radiotap.h" +#include "dot11.h" #include "ipv6.h" #include "arp.h" #include "eapol.h" @@ -81,6 +85,49 @@ Tins::PDU *pdu_from_flag(Constants::Ethernet::e flag, const uint8_t *buffer, }; } +Tins::PDU *pdu_from_flag(PDU::PDUType type, const uint8_t *buffer, uint32_t size) +{ + switch(type) { + case Tins::PDU::ETHERNET_II: + return new Tins::EthernetII(buffer, size); + case Tins::PDU::IP: + return new Tins::IP(buffer, size); + case Tins::PDU::IPv6: + return new Tins::IPv6(buffer, size); + case Tins::PDU::ARP: + return new Tins::ARP(buffer, size); + case Tins::PDU::IEEE802_3: + return new Tins::IEEE802_3(buffer, size); + case Tins::PDU::RADIOTAP: + return new Tins::RadioTap(buffer, size); + case Tins::PDU::DOT11: + case Tins::PDU::DOT11_ACK: + case Tins::PDU::DOT11_ASSOC_REQ: + case Tins::PDU::DOT11_ASSOC_RESP: + case Tins::PDU::DOT11_AUTH: + case Tins::PDU::DOT11_BEACON: + case Tins::PDU::DOT11_BLOCK_ACK: + case Tins::PDU::DOT11_BLOCK_ACK_REQ: + case Tins::PDU::DOT11_CF_END: + case Tins::PDU::DOT11_DATA: + case Tins::PDU::DOT11_CONTROL: + case Tins::PDU::DOT11_DEAUTH: + case Tins::PDU::DOT11_DIASSOC: + case Tins::PDU::DOT11_END_CF_ACK: + case Tins::PDU::DOT11_MANAGEMENT: + case Tins::PDU::DOT11_PROBE_REQ: + case Tins::PDU::DOT11_PROBE_RESP: + case Tins::PDU::DOT11_PS_POLL: + case Tins::PDU::DOT11_REASSOC_REQ: + case Tins::PDU::DOT11_REASSOC_RESP: + case Tins::PDU::DOT11_RTS: + case Tins::PDU::DOT11_QOS_DATA: + return Tins::Dot11::from_bytes(buffer, size); + default: + return 0; + }; +} + Constants::Ethernet::e pdu_flag_to_ether_type(PDU::PDUType flag) { switch (flag) { case PDU::IP: diff --git a/src/ip.cpp b/src/ip.cpp index 3cb3c3a..e599f73 100644 --- a/src/ip.cpp +++ b/src/ip.cpp @@ -436,19 +436,6 @@ bool IP::matches_response(uint8_t *ptr, uint32_t total_sz) { } PDU *IP::clone_packet(const uint8_t *ptr, uint32_t total_sz) { - if(total_sz < sizeof(iphdr)) - return 0; - const iphdr *ip_ptr = (iphdr*)ptr; - uint32_t sz = ip_ptr->ihl * sizeof(uint32_t); - if(total_sz < sz) - return 0; - PDU *child = 0, *cloned; - if(total_sz > sz) { - if((child = PDU::clone_inner_pdu(ptr + sizeof(_ip), total_sz - sizeof(_ip))) == 0) - return 0; - } - cloned = new IP(ptr, std::min(total_sz, (uint32_t)(Endian::be_to_host(ip_ptr->tot_len) * sizeof(uint32_t)))); - cloned->inner_pdu(child); - return cloned; + return new IP(ptr, total_sz); } } diff --git a/src/packet_sender.cpp b/src/packet_sender.cpp index b966d30..39add60 100644 --- a/src/packet_sender.cpp +++ b/src/packet_sender.cpp @@ -65,6 +65,7 @@ #include "dot11.h" #include "radiotap.h" #include "ieee802_3.h" +#include "internals.h" namespace Tins { @@ -307,7 +308,8 @@ PDU *PacketSender::recv_match_loop(int sock, PDU &pdu, struct sockaddr* link_add #endif size = recvfrom(sock, (char*)buffer, 2048, 0, link_addr, &length); if(pdu.matches_response(buffer, size)) { - return pdu.clone_packet(buffer, size); + return Internals::pdu_from_flag(pdu.pdu_type(), buffer, size); + //return pdu.clone_packet(buffer, size); } } struct timeval this_time, diff; diff --git a/src/pdu.cpp b/src/pdu.cpp index 068bb41..622c2ba 100644 --- a/src/pdu.cpp +++ b/src/pdu.cpp @@ -113,4 +113,8 @@ PDU *PDU::clone_inner_pdu(const uint8_t *ptr, uint32_t total_sz) { child = new RawPDU(ptr, total_sz); return child; } + +PDU *PDU::clone_packet(const uint8_t *ptr, uint32_t total_sz) { + return 0; +} }