mirror of
https://github.com/mfontanini/libtins
synced 2026-01-27 04:11:35 +01:00
Added PDU::clone_pdu.
This commit is contained in:
@@ -73,6 +73,15 @@ Tins::EthernetII::EthernetII(const uint8_t *buffer, uint32_t total_sz) : PDU(ETH
|
||||
inner_pdu(next);
|
||||
}
|
||||
|
||||
Tins::EthernetII::EthernetII(const EthernetII &other) : PDU(ETHERTYPE_IP) {
|
||||
*this = other;
|
||||
}
|
||||
|
||||
Tins::EthernetII &Tins::EthernetII::operator= (const EthernetII &other) {
|
||||
copy_fields(&other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Tins::EthernetII::EthernetII(const ethhdr *eth_ptr) : PDU(ETHERTYPE_IP) {
|
||||
memcpy(&_eth, eth_ptr, sizeof(ethhdr));
|
||||
}
|
||||
@@ -176,3 +185,14 @@ Tins::PDU *Tins::EthernetII::clone_packet(const uint8_t *ptr, uint32_t total_sz)
|
||||
cloned->inner_pdu(child);
|
||||
return cloned;
|
||||
}
|
||||
|
||||
void Tins::EthernetII::copy_fields(const EthernetII *other) {
|
||||
memcpy(&_eth, &other->_eth, sizeof(_eth));
|
||||
_iface_index = other->_iface_index;
|
||||
}
|
||||
|
||||
Tins::PDU *Tins::EthernetII::clone_pdu() const {
|
||||
EthernetII *new_pdu = new EthernetII(_iface_index);
|
||||
new_pdu->copy_fields(this);
|
||||
return new_pdu;
|
||||
}
|
||||
|
||||
34
src/ip.cpp
34
src/ip.cpp
@@ -46,6 +46,21 @@ Tins::IP::IP(const string &ip_dst, const string &ip_src, PDU *child) : PDU(IPPRO
|
||||
|
||||
}
|
||||
|
||||
Tins::IP::IP(const IP &other) : PDU(IPPROTO_IP) {
|
||||
*this = other;
|
||||
}
|
||||
|
||||
Tins::IP::IP() : PDU(IPPROTO_IP) {
|
||||
init_ip_fields();
|
||||
}
|
||||
|
||||
Tins::IP &Tins::IP::operator= (const IP &other) {
|
||||
copy_fields(&other);
|
||||
if(other.inner_pdu())
|
||||
inner_pdu(other.inner_pdu()->clone_packet());
|
||||
return *this;
|
||||
}
|
||||
|
||||
Tins::IP::IP(const uint8_t *buffer, uint32_t total_sz) : PDU(IPPROTO_IP) {
|
||||
if(total_sz < sizeof(iphdr))
|
||||
throw std::runtime_error("Not enough size for an IP header in the buffer.");
|
||||
@@ -330,3 +345,22 @@ Tins::PDU *Tins::IP::clone_packet(const uint8_t *ptr, uint32_t total_sz) {
|
||||
cloned->inner_pdu(child);
|
||||
return cloned;
|
||||
}
|
||||
|
||||
void Tins::IP::copy_fields(const IP *other) {
|
||||
memcpy(&_ip, &other->_ip, sizeof(_ip));
|
||||
for(vector<IpOption>::const_iterator it = other->_ip_options.begin(); it != other->_ip_options.end(); ++it) {
|
||||
IpOption new_opt;
|
||||
if(it->optional_data) {
|
||||
new_opt.optional_data = new uint8_t[it->optional_data_size];
|
||||
memcpy(new_opt.optional_data, it->optional_data, it->optional_data_size);
|
||||
}
|
||||
new_opt.optional_data_size = it->optional_data_size;
|
||||
_ip_options.push_back(new_opt);
|
||||
}
|
||||
}
|
||||
|
||||
Tins::PDU *Tins::IP::clone_pdu() const {
|
||||
IP *new_pdu = new IP();
|
||||
new_pdu->copy_fields(this);
|
||||
return new_pdu;
|
||||
}
|
||||
|
||||
13
src/pdu.cpp
13
src/pdu.cpp
@@ -80,6 +80,19 @@ Tins::PDU *Tins::PDU::clone_inner_pdu(const uint8_t *ptr, uint32_t total_sz) {
|
||||
return child;
|
||||
}
|
||||
|
||||
Tins::PDU *Tins::PDU::clone_packet() const {
|
||||
PDU *ret = clone_pdu();
|
||||
if(ret) {
|
||||
PDU *ptr = 0, *last = ret;
|
||||
while(last && last->inner_pdu()) {
|
||||
ptr = last->inner_pdu()->clone_pdu();
|
||||
last->inner_pdu(ptr);
|
||||
last = ptr;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Static methods */
|
||||
uint32_t Tins::PDU::do_checksum(uint8_t *start, uint8_t *end) {
|
||||
uint32_t checksum(0);
|
||||
|
||||
Reference in New Issue
Block a user