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

Modified IP, ARP, ICMP and EthernetII's clone_packet.

This commit is contained in:
Matias Fontanini
2011-09-15 09:17:35 -03:00
parent 6b88a9a944
commit 4d8fb5a4e5
8 changed files with 23 additions and 66 deletions

View File

@@ -334,13 +334,7 @@ namespace Tins {
uint8_t ar_tha[6]; /* target hardware address */ uint8_t ar_tha[6]; /* target hardware address */
uint32_t ar_tip; /* target IP address */ uint32_t ar_tip; /* target IP address */
} __attribute__((__packed__)); } __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 copy_fields(const ARP *other);
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent); void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);

View File

@@ -209,12 +209,6 @@ namespace Tins {
uint16_t payload_type; uint16_t payload_type;
} __attribute__((__packed__)); } __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 copy_fields(const EthernetII *other);
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent); void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);

View File

@@ -309,12 +309,6 @@ namespace Tins {
} un; } un;
} __attribute__((__packed__)); } __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); void copy_fields(const ICMP *other);
/** \brief Serialices this ICMP PDU. /** \brief Serialices this ICMP PDU.

View File

@@ -419,12 +419,6 @@ namespace Tins {
uint8_t* write(uint8_t* buffer); uint8_t* write(uint8_t* buffer);
} __attribute__((__packed__)); } __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 copy_fields(const IP *other);
void init_ip_fields(); void init_ip_fields();

View File

@@ -22,6 +22,7 @@
#include <string> #include <string>
#include <cstring> #include <cstring>
#include <cassert> #include <cassert>
#include <algorithm>
#include "arp.h" #include "arp.h"
#include "ip.h" #include "ip.h"
#include "ethernetII.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)); 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) { 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? 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) { Tins::PDU *Tins::ARP::clone_packet(const uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(arphdr)) if(total_sz < sizeof(arphdr))
return 0; return 0;
const arphdr *arp_ptr = (arphdr*)ptr;
PDU *child = 0, *cloned; PDU *child = 0, *cloned;
if(total_sz > sizeof(arphdr)) { if(total_sz > sizeof(arphdr)) {
if((child = PDU::clone_inner_pdu(ptr + sizeof(arphdr), total_sz - sizeof(arphdr))) == 0) if((child = PDU::clone_inner_pdu(ptr + sizeof(arphdr), total_sz - sizeof(arphdr))) == 0)
return 0; return 0;
} }
cloned = new ARP(arp_ptr); cloned = new ARP(ptr, std::min(total_sz, (uint32_t)sizeof(_arp)));
cloned->inner_pdu(child); cloned->inner_pdu(child);
return cloned; return cloned;
} }

View File

@@ -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."); throw std::runtime_error("Not enough size for an ethernetII header in the buffer.");
memcpy(&_eth, buffer, sizeof(ethhdr)); memcpy(&_eth, buffer, sizeof(ethhdr));
PDU *next = 0; PDU *next = 0;
switch(payload_type()) { buffer += sizeof(ethhdr);
case ETHERTYPE_IP: total_sz -= sizeof(ethhdr);
next = new Tins::IP(buffer + sizeof(ethhdr), total_sz - sizeof(ethhdr)); if(total_sz) {
break; switch(payload_type()) {
case ETHERTYPE_ARP: case ETHERTYPE_IP:
next = new Tins::ARP(buffer + sizeof(ethhdr), total_sz - sizeof(ethhdr)); next = new Tins::IP(buffer, total_sz);
break; break;
// Other protos plz 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) { Tins::EthernetII::EthernetII(const EthernetII &other) : PDU(other) {
@@ -83,10 +87,6 @@ Tins::EthernetII &Tins::EthernetII::operator= (const EthernetII &other) {
return *this; 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 { uint16_t Tins::EthernetII::payload_type() const {
return Utils::net_to_host_s(_eth.payload_type); 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; ethhdr *eth_ptr = (ethhdr*)ptr;
if(!memcmp(eth_ptr->dst_mac, _eth.src_mac, ADDR_SIZE)) { if(!memcmp(eth_ptr->dst_mac, _eth.src_mac, ADDR_SIZE)) {
// chequear broadcast en destino original... // chequear broadcast en destino original...
return true; return (inner_pdu()) ? inner_pdu()->matches_response(ptr + sizeof(_eth), total_sz - sizeof(_eth)) : true;
} }
return false; 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) { Tins::PDU *Tins::EthernetII::clone_packet(const uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(_eth)) if(total_sz < sizeof(_eth))
return 0; return 0;
const ethhdr *eth_ptr = (ethhdr*)ptr;
PDU *child = 0, *cloned; PDU *child = 0, *cloned;
if(total_sz > sizeof(_eth)) { if(total_sz > sizeof(_eth)) {
if((child = PDU::clone_inner_pdu(ptr + sizeof(_eth), total_sz - sizeof(_eth))) == 0) if((child = PDU::clone_inner_pdu(ptr + sizeof(_eth), total_sz - sizeof(_eth))) == 0)
return 0; return 0;
} }
cloned = new EthernetII(eth_ptr); cloned = new EthernetII(ptr, std::min(total_sz, (uint32_t)sizeof(_eth)));
cloned->inner_pdu(child); cloned->inner_pdu(child);
return cloned; return cloned;
} }

View File

@@ -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)); 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) { void Tins::ICMP::code(uint8_t new_code) {
_icmp.code = 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) { Tins::PDU *Tins::ICMP::clone_packet(const uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(icmphdr)) if(total_sz < sizeof(icmphdr))
return 0; return 0;
const icmphdr *icmp_ptr = (icmphdr*)ptr;
PDU *child = 0, *cloned; PDU *child = 0, *cloned;
if(total_sz > sizeof(icmphdr)) { if(total_sz > sizeof(icmphdr)) {
if((child = PDU::clone_inner_pdu(ptr + sizeof(icmphdr), total_sz - sizeof(icmphdr))) == 0) if((child = PDU::clone_inner_pdu(ptr + sizeof(icmphdr), total_sz - sizeof(icmphdr))) == 0)
return 0; return 0;
} }
cloned = new ICMP(icmp_ptr); cloned = new ICMP(ptr, std::min(total_sz, (uint32_t)sizeof(_icmp)));
cloned->inner_pdu(child); cloned->inner_pdu(child);
return cloned; return cloned;
} }

View File

@@ -22,6 +22,7 @@
#include <stdexcept> #include <stdexcept>
#include <cstring> #include <cstring>
#include <cassert> #include <cassert>
#include <algorithm>
#ifndef WIN32 #ifndef WIN32
#include <netdb.h> #include <netdb.h>
#include <sys/socket.h> #include <sys/socket.h>
@@ -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) { Tins::IP::IP(uint32_t ip_dst, uint32_t ip_src, PDU *child) : PDU(Constants::IP::PROTO_IP, child) {
init_ip_fields(); init_ip_fields();
_ip.daddr = ip_dst; _ip.daddr = ip_dst;
@@ -350,15 +346,10 @@ Tins::PDU *Tins::IP::clone_packet(const uint8_t *ptr, uint32_t total_sz) {
return 0; return 0;
PDU *child = 0, *cloned; PDU *child = 0, *cloned;
if(total_sz > sz) { if(total_sz > sz) {
if(inner_pdu()) { if((child = PDU::clone_inner_pdu(ptr + sizeof(_ip), total_sz - sizeof(_ip))) == 0)
child = inner_pdu()->clone_packet(ptr + sz, total_sz - sz); return 0;
if(!child)
return 0;
}
else
child = new RawPDU(ptr + sz, total_sz - sz);
} }
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); cloned->inner_pdu(child);
return cloned; return cloned;
} }