diff --git a/include/arp.h b/include/arp.h index f15f947..bfbbd0c 100644 --- a/include/arp.h +++ b/include/arp.h @@ -334,13 +334,7 @@ namespace Tins { uint8_t ar_tha[6]; /* target hardware address */ uint32_t ar_tip; /* target IP address */ } __attribute__((__packed__)); - - /** \brief Creates an instance of ARP using an arphdr pointer. - * - * \param arp_ptr The pointer to the arphdr. - */ - ARP(const arphdr *arp_ptr); - + void copy_fields(const ARP *other); void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent); diff --git a/include/ethernetII.h b/include/ethernetII.h index eab8e92..18edd09 100644 --- a/include/ethernetII.h +++ b/include/ethernetII.h @@ -209,12 +209,6 @@ namespace Tins { uint16_t payload_type; } __attribute__((__packed__)); - /** \brief Creates an instance of EthernetII using an ethhdr pointer. - * - * \param eth_ptr The pointer to the ethhdr. - */ - EthernetII(const ethhdr *eth_ptr); - void copy_fields(const EthernetII *other); void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent); diff --git a/include/icmp.h b/include/icmp.h index 9340248..cbc7ebd 100644 --- a/include/icmp.h +++ b/include/icmp.h @@ -309,12 +309,6 @@ namespace Tins { } un; } __attribute__((__packed__)); - /** \brief Creates an instance of ICMP from a icmphdr pointer. - * - * \param ptr The icmphdr to clone. - */ - ICMP(const icmphdr *ptr); - void copy_fields(const ICMP *other); /** \brief Serialices this ICMP PDU. diff --git a/include/ip.h b/include/ip.h index 03c33f7..85fa04c 100644 --- a/include/ip.h +++ b/include/ip.h @@ -419,12 +419,6 @@ namespace Tins { uint8_t* write(uint8_t* buffer); } __attribute__((__packed__)); - - /** \brief Creates an instance of IP from an iphdr pointer. - * - * \param ptr The ip header pointer. - */ - IP(const iphdr *ptr); void copy_fields(const IP *other); void init_ip_fields(); diff --git a/src/arp.cpp b/src/arp.cpp index 4730260..7630289 100644 --- a/src/arp.cpp +++ b/src/arp.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include "arp.h" #include "ip.h" #include "ethernetII.h" @@ -56,10 +57,6 @@ Tins::ARP::ARP(const uint8_t *buffer, uint32_t total_sz) : PDU(Utils::net_to_hos inner_pdu(new RawPDU(buffer + sizeof(arphdr), total_sz)); } -Tins::ARP::ARP(const arphdr *arp_ptr) : PDU(Utils::net_to_host_s(Constants::Ethernet::ARP)) { - memcpy(&_arp, arp_ptr, sizeof(arphdr)); -} - void Tins::ARP::sender_hw_addr(const uint8_t* new_snd_hw_addr) { memcpy(this->_arp.ar_sha, new_snd_hw_addr, 6); //Should this use hardware address' length? } @@ -144,13 +141,12 @@ bool Tins::ARP::matches_response(uint8_t *ptr, uint32_t total_sz) { Tins::PDU *Tins::ARP::clone_packet(const uint8_t *ptr, uint32_t total_sz) { if(total_sz < sizeof(arphdr)) return 0; - const arphdr *arp_ptr = (arphdr*)ptr; PDU *child = 0, *cloned; if(total_sz > sizeof(arphdr)) { if((child = PDU::clone_inner_pdu(ptr + sizeof(arphdr), total_sz - sizeof(arphdr))) == 0) return 0; } - cloned = new ARP(arp_ptr); + cloned = new ARP(ptr, std::min(total_sz, (uint32_t)sizeof(_arp))); cloned->inner_pdu(child); return cloned; } diff --git a/src/ethernetII.cpp b/src/ethernetII.cpp index 17816e6..b20011e 100644 --- a/src/ethernetII.cpp +++ b/src/ethernetII.cpp @@ -61,16 +61,20 @@ Tins::EthernetII::EthernetII(const uint8_t *buffer, uint32_t total_sz) : PDU(ETH throw std::runtime_error("Not enough size for an ethernetII header in the buffer."); memcpy(&_eth, buffer, sizeof(ethhdr)); PDU *next = 0; - switch(payload_type()) { - case ETHERTYPE_IP: - next = new Tins::IP(buffer + sizeof(ethhdr), total_sz - sizeof(ethhdr)); - break; - case ETHERTYPE_ARP: - next = new Tins::ARP(buffer + sizeof(ethhdr), total_sz - sizeof(ethhdr)); - break; - // Other protos plz + buffer += sizeof(ethhdr); + total_sz -= sizeof(ethhdr); + if(total_sz) { + switch(payload_type()) { + case ETHERTYPE_IP: + next = new Tins::IP(buffer, total_sz); + break; + case ETHERTYPE_ARP: + next = new Tins::ARP(buffer, total_sz); + break; + // Other protos plz + } + inner_pdu(next); } - inner_pdu(next); } Tins::EthernetII::EthernetII(const EthernetII &other) : PDU(other) { @@ -83,10 +87,6 @@ Tins::EthernetII &Tins::EthernetII::operator= (const EthernetII &other) { return *this; } -Tins::EthernetII::EthernetII(const ethhdr *eth_ptr) : PDU(ETHERTYPE_IP) { - memcpy(&_eth, eth_ptr, sizeof(ethhdr)); -} - uint16_t Tins::EthernetII::payload_type() const { return Utils::net_to_host_s(_eth.payload_type); } @@ -133,7 +133,7 @@ bool Tins::EthernetII::matches_response(uint8_t *ptr, uint32_t total_sz) { ethhdr *eth_ptr = (ethhdr*)ptr; if(!memcmp(eth_ptr->dst_mac, _eth.src_mac, ADDR_SIZE)) { // chequear broadcast en destino original... - return true; + return (inner_pdu()) ? inner_pdu()->matches_response(ptr + sizeof(_eth), total_sz - sizeof(_eth)) : true; } return false; } @@ -176,13 +176,12 @@ Tins::PDU *Tins::EthernetII::recv_response(PacketSender *sender) { Tins::PDU *Tins::EthernetII::clone_packet(const uint8_t *ptr, uint32_t total_sz) { if(total_sz < sizeof(_eth)) return 0; - const ethhdr *eth_ptr = (ethhdr*)ptr; 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(eth_ptr); + cloned = new EthernetII(ptr, std::min(total_sz, (uint32_t)sizeof(_eth))); cloned->inner_pdu(child); return cloned; } diff --git a/src/icmp.cpp b/src/icmp.cpp index 8791cad..a2fe642 100644 --- a/src/icmp.cpp +++ b/src/icmp.cpp @@ -57,10 +57,6 @@ Tins::ICMP::ICMP(const uint8_t *buffer, uint32_t total_sz) : PDU(IPPROTO_ICMP) { inner_pdu(new RawPDU(buffer + sizeof(icmphdr), total_sz)); } -Tins::ICMP::ICMP(const icmphdr *ptr) : PDU(IPPROTO_ICMP) { - std::memcpy(&_icmp, ptr, sizeof(icmphdr)); -} - void Tins::ICMP::code(uint8_t new_code) { _icmp.code = new_code; } @@ -190,13 +186,12 @@ 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; - const icmphdr *icmp_ptr = (icmphdr*)ptr; 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(icmp_ptr); + cloned = new ICMP(ptr, std::min(total_sz, (uint32_t)sizeof(_icmp))); cloned->inner_pdu(child); return cloned; } diff --git a/src/ip.cpp b/src/ip.cpp index 7970255..1839d92 100644 --- a/src/ip.cpp +++ b/src/ip.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #ifndef WIN32 #include #include @@ -143,11 +144,6 @@ Tins::IP::IP(const uint8_t *buffer, uint32_t total_sz) : PDU(Constants::IP::PROT } } -Tins::IP::IP(const iphdr *ptr) : PDU(Constants::IP::PROTO_IP) { - std::memcpy(&_ip, ptr, sizeof(iphdr)); - /* Options... */ -} - Tins::IP::IP(uint32_t ip_dst, uint32_t ip_src, PDU *child) : PDU(Constants::IP::PROTO_IP, child) { init_ip_fields(); _ip.daddr = ip_dst; @@ -350,15 +346,10 @@ Tins::PDU *Tins::IP::clone_packet(const uint8_t *ptr, uint32_t total_sz) { return 0; PDU *child = 0, *cloned; if(total_sz > sz) { - if(inner_pdu()) { - child = inner_pdu()->clone_packet(ptr + sz, total_sz - sz); - if(!child) - return 0; - } - else - child = new RawPDU(ptr + sz, total_sz - sz); + if((child = PDU::clone_inner_pdu(ptr + sizeof(_ip), total_sz - sizeof(_ip))) == 0) + return 0; } - cloned = new IP(ip_ptr); + cloned = new IP(ptr, std::min(total_sz, (uint32_t)(Utils::net_to_host_s(ip_ptr->tot_len) * sizeof(uint32_t)))); cloned->inner_pdu(child); return cloned; }