mirror of
https://github.com/mfontanini/libtins
synced 2026-01-28 04:34:27 +01:00
Remove parent parameter from write_serialization
This is no longer needed as each PDU knows its parent PDU already
This commit is contained in:
@@ -113,7 +113,7 @@ uint32_t ARP::header_size() const {
|
||||
return sizeof(header_);
|
||||
}
|
||||
|
||||
void ARP::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU *) {
|
||||
void ARP::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
stream.write(header_);
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ void BootP::vend(const vend_type& newvend_) {
|
||||
vend_ = newvend_;
|
||||
}
|
||||
|
||||
void BootP::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* /*parent*/) {
|
||||
void BootP::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
stream.write(bootp_);
|
||||
stream.write(vend_.begin(), vend_.end());
|
||||
|
||||
@@ -247,7 +247,7 @@ uint32_t DHCP::header_size() const {
|
||||
return static_cast<uint32_t>(BootP::header_size() - vend().size() + size_);
|
||||
}
|
||||
|
||||
void DHCP::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent) {
|
||||
void DHCP::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
if (size_) {
|
||||
vend_type& result = BootP::vend();
|
||||
result.resize(size_);
|
||||
@@ -261,7 +261,7 @@ void DHCP::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* pa
|
||||
stream.write(it->data_ptr(), it->data_size());
|
||||
}
|
||||
}
|
||||
BootP::write_serialization(buffer, total_sz, parent);
|
||||
BootP::write_serialization(buffer, total_sz);
|
||||
}
|
||||
|
||||
} // Tins
|
||||
|
||||
@@ -161,7 +161,7 @@ bool DHCPv6::matches_response(const uint8_t* ptr, uint32_t total_sz) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
void DHCPv6::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU *) {
|
||||
void DHCPv6::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
const uint32_t required_size = is_relay_message() ? 2 : 4;
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
stream.write(header_data_, required_size);
|
||||
|
||||
@@ -386,7 +386,7 @@ uint32_t DNS::compose_name(const uint8_t* ptr, char* out_ptr) const {
|
||||
return end_ptr - start_ptr;
|
||||
}
|
||||
|
||||
void DNS::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* /*parent*/) {
|
||||
void DNS::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
stream.write(header_);
|
||||
stream.write(records_data_.begin(), records_data_.end());
|
||||
|
||||
@@ -213,7 +213,7 @@ void Dot11::send(PacketSender& sender, const NetworkInterface& iface) {
|
||||
}
|
||||
#endif // _WIN32
|
||||
|
||||
void Dot11::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* /*parent*/) {
|
||||
void Dot11::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
stream.write(header_);
|
||||
write_ext_header(stream);
|
||||
|
||||
@@ -105,7 +105,7 @@ uint32_t Dot1Q::trailer_size() const {
|
||||
}
|
||||
}
|
||||
|
||||
void Dot1Q::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU *) {
|
||||
void Dot1Q::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
if (inner_pdu()) {
|
||||
Constants::Ethernet::e flag;
|
||||
|
||||
@@ -134,7 +134,7 @@ bool Dot3::matches_response(const uint8_t* ptr, uint32_t total_sz) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
void Dot3::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* /*parent*/) {
|
||||
void Dot3::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
header_.length = Endian::host_to_be<uint16_t>(size() - sizeof(header_));
|
||||
stream.write(header_);
|
||||
|
||||
@@ -106,7 +106,7 @@ void EAPOL::type(uint8_t new_type) {
|
||||
header_.type = new_type;
|
||||
}
|
||||
|
||||
void EAPOL::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU *) {
|
||||
void EAPOL::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
length(total_sz - 4);
|
||||
stream.write(header_);
|
||||
|
||||
@@ -161,7 +161,7 @@ bool EthernetII::matches_response(const uint8_t* ptr, uint32_t total_sz) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
void EthernetII::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* /*parent*/) {
|
||||
void EthernetII::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
if (inner_pdu()) {
|
||||
Constants::Ethernet::e flag;
|
||||
|
||||
@@ -216,7 +216,7 @@ void ICMP::use_length_field(bool value) {
|
||||
header_.un.rfc4884.length = value ? 1 : 0;
|
||||
}
|
||||
|
||||
void ICMP::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU *) {
|
||||
void ICMP::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
|
||||
// If extensions are allowed and we have to set the length field
|
||||
|
||||
@@ -281,7 +281,7 @@ bool ICMPv6::matches_response(const uint8_t* ptr, uint32_t total_sz) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
void ICMPv6::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent) {
|
||||
void ICMPv6::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
|
||||
// If extensions are allowed and we have to set the length field
|
||||
@@ -361,7 +361,7 @@ void ICMPv6::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU*
|
||||
);
|
||||
}
|
||||
|
||||
const Tins::IPv6* ipv6 = tins_cast<const Tins::IPv6*>(parent);
|
||||
const Tins::IPv6* ipv6 = tins_cast<const Tins::IPv6*>(parent_pdu());
|
||||
if (ipv6) {
|
||||
uint32_t checksum = Utils::pseudoheader_checksum(
|
||||
ipv6->src_addr(),
|
||||
|
||||
10
src/ip.cpp
10
src/ip.cpp
@@ -418,14 +418,14 @@ PDU* IP::recv_response(PacketSender& sender, const NetworkInterface &) {
|
||||
return sender.recv_l3(*this, 0, sizeof(link_addr), type);
|
||||
}
|
||||
|
||||
void IP::prepare_for_serialize(const PDU* parent) {
|
||||
if (!parent && header_.saddr == 0) {
|
||||
void IP::prepare_for_serialize() {
|
||||
if (!parent_pdu()&& header_.saddr == 0) {
|
||||
NetworkInterface iface(dst_addr());
|
||||
src_addr(iface.addresses().ip_addr);
|
||||
}
|
||||
}
|
||||
|
||||
void IP::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent) {
|
||||
void IP::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
checksum(0);
|
||||
if (inner_pdu()) {
|
||||
@@ -446,12 +446,10 @@ void IP::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* pare
|
||||
uint16_t original_frag_off = header_.frag_off;
|
||||
|
||||
#if __FreeBSD__ || defined(__FreeBSD_kernel__) || __APPLE__
|
||||
if (!parent) {
|
||||
if (!parent_pdu()) {
|
||||
total_sz = Endian::host_to_be<uint16_t>(total_sz);
|
||||
header_.frag_off = Endian::be_to_host(header_.frag_off);
|
||||
}
|
||||
#else
|
||||
Internals::unused(parent);
|
||||
#endif
|
||||
tot_len(total_sz);
|
||||
head_len(static_cast<uint8_t>(header_size() / sizeof(uint32_t)));
|
||||
|
||||
@@ -94,7 +94,7 @@ uint32_t IPSecAH::header_size() const {
|
||||
return static_cast<uint32_t>(sizeof(header_) + icv_.size());
|
||||
}
|
||||
|
||||
void IPSecAH::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU *) {
|
||||
void IPSecAH::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
if (inner_pdu()) {
|
||||
next_header(Internals::pdu_flag_to_ip_type(inner_pdu()->pdu_type()));
|
||||
}
|
||||
@@ -131,7 +131,7 @@ uint32_t IPSecESP::header_size() const {
|
||||
return sizeof(header_);
|
||||
}
|
||||
|
||||
void IPSecESP::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU *) {
|
||||
void IPSecESP::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream output(buffer, total_sz);
|
||||
output.write(header_);
|
||||
}
|
||||
|
||||
@@ -243,7 +243,7 @@ bool IPv6::matches_response(const uint8_t* ptr, uint32_t total_sz) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
void IPv6::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* /*parent*/) {
|
||||
void IPv6::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
if (inner_pdu()) {
|
||||
uint8_t new_flag = Internals::pdu_flag_to_ip_type(inner_pdu()->pdu_type());
|
||||
|
||||
@@ -196,7 +196,7 @@ void LLC::clear_information_fields() {
|
||||
information_fields_.clear();
|
||||
}
|
||||
|
||||
void LLC::write_serialization(uint8_t* buffer, uint32_t total_sz, const Tins::PDU* /*parent*/) {
|
||||
void LLC::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
if (inner_pdu() && inner_pdu()->pdu_type() == PDU::STP) {
|
||||
dsap(0x42);
|
||||
|
||||
@@ -90,7 +90,7 @@ uint32_t Loopback::header_size() const {
|
||||
return sizeof(family_);
|
||||
}
|
||||
|
||||
void Loopback::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU *) {
|
||||
void Loopback::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
#ifndef _WIN32
|
||||
if (tins_cast<const Tins::IP*>(inner_pdu())) {
|
||||
|
||||
@@ -91,10 +91,10 @@ uint32_t MPLS::header_size() const {
|
||||
return sizeof(header_);
|
||||
}
|
||||
|
||||
void MPLS::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent) {
|
||||
void MPLS::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
// If we have a parent PDU, we might set the bottom-of-stack field
|
||||
if (parent) {
|
||||
if (parent_pdu()) {
|
||||
// We'll set it if we either don't have a child or we have one and it's not MPLS
|
||||
if (!inner_pdu() || inner_pdu()->pdu_type() != PDU::MPLS) {
|
||||
bottom_of_stack(1);
|
||||
|
||||
12
src/pdu.cpp
12
src/pdu.cpp
@@ -73,7 +73,7 @@ void PDU::copy_inner_pdu(const PDU& pdu) {
|
||||
}
|
||||
}
|
||||
|
||||
void PDU::prepare_for_serialize(const PDU* /*parent*/) {
|
||||
void PDU::prepare_for_serialize() {
|
||||
}
|
||||
|
||||
uint32_t PDU::size() const {
|
||||
@@ -121,21 +121,21 @@ PDU* PDU::release_inner_pdu() {
|
||||
|
||||
PDU::serialization_type PDU::serialize() {
|
||||
vector<uint8_t> buffer(size());
|
||||
serialize(&buffer[0], static_cast<uint32_t>(buffer.size()), 0);
|
||||
serialize(&buffer[0], static_cast<uint32_t>(buffer.size()));
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void PDU::serialize(uint8_t* buffer, uint32_t total_sz, const PDU* parent) {
|
||||
void PDU::serialize(uint8_t* buffer, uint32_t total_sz) {
|
||||
uint32_t sz = header_size() + trailer_size();
|
||||
// Must not happen...
|
||||
#ifdef TINS_DEBUG
|
||||
assert(total_sz >= sz);
|
||||
#endif
|
||||
prepare_for_serialize(parent);
|
||||
prepare_for_serialize();
|
||||
if (inner_pdu_) {
|
||||
inner_pdu_->serialize(buffer + header_size(), total_sz - sz, this);
|
||||
inner_pdu_->serialize(buffer + header_size(), total_sz - sz);
|
||||
}
|
||||
write_serialization(buffer, total_sz, parent);
|
||||
write_serialization(buffer, total_sz);
|
||||
}
|
||||
|
||||
void PDU::parent_pdu(PDU* parent) {
|
||||
|
||||
@@ -65,7 +65,7 @@ uint32_t PKTAP::header_size() const {
|
||||
return sizeof(header_);
|
||||
}
|
||||
|
||||
void PKTAP::write_serialization(uint8_t* /*buffer*/, uint32_t /*total_sz*/, const PDU* /*parent*/) {
|
||||
void PKTAP::write_serialization(uint8_t* /*buffer*/, uint32_t /*total_sz*/) {
|
||||
throw pdu_not_serializable();
|
||||
}
|
||||
|
||||
|
||||
@@ -94,7 +94,7 @@ uint32_t PPI::header_size() const {
|
||||
return static_cast<uint32_t>(sizeof(header_) + data_.size());
|
||||
}
|
||||
|
||||
void PPI::write_serialization(uint8_t* /*buffer*/, uint32_t /*total_sz*/, const PDU *) {
|
||||
void PPI::write_serialization(uint8_t* /*buffer*/, uint32_t /*total_sz*/) {
|
||||
throw pdu_not_serializable();
|
||||
}
|
||||
|
||||
|
||||
@@ -108,7 +108,7 @@ uint32_t PPPoE::header_size() const {
|
||||
return sizeof(header_) + tags_size_;
|
||||
}
|
||||
|
||||
void PPPoE::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU *) {
|
||||
void PPPoE::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
if (tags_size_ > 0) {
|
||||
payload_length(tags_size_);
|
||||
|
||||
@@ -507,7 +507,7 @@ bool RadioTap::matches_response(const uint8_t* ptr, uint32_t total_sz) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
void RadioTap::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* /*parent*/) {
|
||||
void RadioTap::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
uint8_t* buffer_start = buffer;
|
||||
radio_.it_len = Endian::host_to_le<uint16_t>(header_size());
|
||||
|
||||
@@ -48,7 +48,7 @@ uint32_t RawPDU::header_size() const {
|
||||
return static_cast<uint32_t>(payload_.size());
|
||||
}
|
||||
|
||||
void RawPDU::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU *) {
|
||||
void RawPDU::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
stream.write(payload_.begin(), payload_.end());
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ uint32_t SLL::header_size() const {
|
||||
return sizeof(header_);
|
||||
}
|
||||
|
||||
void SLL::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU *) {
|
||||
void SLL::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
if (inner_pdu()) {
|
||||
Constants::Ethernet::e flag = Internals::pdu_flag_to_ether_type(
|
||||
|
||||
@@ -91,7 +91,7 @@ uint32_t SNAP::header_size() const {
|
||||
return sizeof(snap_);
|
||||
}
|
||||
|
||||
void SNAP::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* /*parent*/) {
|
||||
void SNAP::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
if (inner_pdu()) {
|
||||
Constants::Ethernet::e flag = Internals::pdu_flag_to_ether_type(
|
||||
|
||||
@@ -106,7 +106,7 @@ void STP::bridge_id(const bpdu_id_type& id) {
|
||||
header_.bridge_id = convert(id);
|
||||
}
|
||||
|
||||
void STP::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU *) {
|
||||
void STP::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
stream.write(header_);
|
||||
}
|
||||
|
||||
@@ -295,7 +295,7 @@ uint32_t TCP::header_size() const {
|
||||
return sizeof(header_) + total_options_size_;
|
||||
}
|
||||
|
||||
void TCP::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent) {
|
||||
void TCP::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
// Set checksum to 0, we'll calculate it at the end
|
||||
checksum(0);
|
||||
@@ -311,6 +311,7 @@ void TCP::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* par
|
||||
}
|
||||
|
||||
uint32_t check = 0;
|
||||
const PDU* parent = parent_pdu();
|
||||
if (const Tins::IP* ip_packet = tins_cast<const Tins::IP*>(parent)) {
|
||||
check = Utils::pseudoheader_checksum(
|
||||
ip_packet->src_addr(),
|
||||
|
||||
@@ -117,7 +117,7 @@ uint32_t pseudoheader_checksum(IPv4Address source_ip, IPv4Address dest_ip, uint3
|
||||
return checksum;
|
||||
}
|
||||
|
||||
void UDP::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent) {
|
||||
void UDP::write_serialization(uint8_t* buffer, uint32_t total_sz) {
|
||||
OutputMemoryStream stream(buffer, total_sz);
|
||||
// Set checksum to 0, we'll calculate it at the end
|
||||
header_.check = 0;
|
||||
@@ -129,6 +129,7 @@ void UDP::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* par
|
||||
}
|
||||
stream.write(header_);
|
||||
uint32_t checksum = 0;
|
||||
const PDU* parent = parent_pdu();
|
||||
if (const Tins::IP* ip_packet = tins_cast<const Tins::IP*>(parent)) {
|
||||
checksum = Utils::pseudoheader_checksum(
|
||||
ip_packet->src_addr(),
|
||||
|
||||
Reference in New Issue
Block a user