From 8d08f8dd79ac12874ba0da6b26973f5a33163131 Mon Sep 17 00:00:00 2001 From: Mauro Sardara Date: Thu, 4 Feb 2021 12:20:30 +0100 Subject: [HICN-679] Do not throw exception when receiving corrupted/not-hicn packet from network layer Signed-off-by: Mauro Sardara Change-Id: I082e3c1b5fc9f535089114cfe14940f094947b66 Signed-off-by: Mauro Sardara --- lib/includes/hicn/common.h | 2 +- lib/src/common.c | 15 ++++++++------- libtransport/includes/hicn/transport/core/packet.h | 11 ++++++++--- libtransport/src/core/packet.cc | 9 +++++---- libtransport/src/core/portal.h | 13 +++++++------ libtransport/src/core/tcp_socket_connector.cc | 9 ++++++--- 6 files changed, 35 insertions(+), 24 deletions(-) diff --git a/lib/includes/hicn/common.h b/lib/includes/hicn/common.h index 30f370241..6904c6314 100644 --- a/lib/includes/hicn/common.h +++ b/lib/includes/hicn/common.h @@ -250,7 +250,7 @@ u32 cumulative_hash32 (const void *data, size_t len, u32 lastValue); u32 hash32 (const void *data, size_t len); u64 cumulative_hash64 (const void *data, size_t len, u64 lastValue); u64 hash64 (const void *data, size_t len); -void hicn_packet_dump (uint8_t * buffer, size_t len); +void hicn_packet_dump (const uint8_t * buffer, size_t len); #endif /* ! HICN_VPP_PLUGIN */ diff --git a/lib/src/common.c b/lib/src/common.c index 78d25b770..228a59d1e 100644 --- a/lib/src/common.c +++ b/lib/src/common.c @@ -29,6 +29,7 @@ #include #include +#include @@ -109,7 +110,7 @@ hash64 (const void *data, size_t len) } void -hicn_packet_dump (uint8_t * buffer, size_t len) +hicn_packet_dump (const uint8_t * buffer, size_t len) { int i; unsigned char buff[17]; @@ -118,7 +119,7 @@ hicn_packet_dump (uint8_t * buffer, size_t len) // Output description if given. if (len == 0) { - printf (" ZERO LENGTH\n"); + TRACE (" ZERO LENGTH\n"); return; } @@ -131,14 +132,14 @@ hicn_packet_dump (uint8_t * buffer, size_t len) { // Just don't print ASCII for the zeroth line. if (i != 0) - printf (" %s\n", buff); + TRACE (" %s\n", buff); // Output the offset. - printf (" %04x ", i); + TRACE (" %04x ", i); } // Now the hex code for the specific character. - printf (" %02x", pc[i]); + TRACE (" %02x", pc[i]); // And store a printable ASCII character for later. if ((pc[i] < 0x20) || (pc[i] > 0x7e)) @@ -151,12 +152,12 @@ hicn_packet_dump (uint8_t * buffer, size_t len) // Pad out last line if not exactly 16 characters. while ((i % 16) != 0) { - printf (" "); + TRACE (" "); i++; } // And print the final ASCII bit. - printf (" %s\n", buff); + TRACE (" %s\n", buff); } /* diff --git a/libtransport/includes/hicn/transport/core/packet.h b/libtransport/includes/hicn/transport/core/packet.h index 2efd7439d..91f957964 100644 --- a/libtransport/includes/hicn/transport/core/packet.h +++ b/libtransport/includes/hicn/transport/core/packet.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -97,13 +98,16 @@ class Packet : public std::enable_shared_from_this { static bool isInterest(const uint8_t *buffer); - static Format getFormatFromBuffer(const uint8_t *buffer) { + static Format getFormatFromBuffer(const uint8_t *buffer, std::size_t length) { Format format = HF_UNSPEC; if (TRANSPORT_EXPECT_FALSE( hicn_packet_get_format((const hicn_header_t *)buffer, &format) < 0)) { - throw errors::MalformedPacketException(); + TRANSPORT_LOGE( + "Error while getting format from packet buffer. Packet will be " + "discarded."); + hicn_packet_dump(buffer, length); } return format; @@ -114,7 +118,8 @@ class Packet : public std::enable_shared_from_this { packet_start_ = reinterpret_cast(packet_->writableData()); header_head_ = packet_.get(); payload_head_ = nullptr; - format_ = getFormatFromBuffer(reinterpret_cast(packet_start_)); + format_ = getFormatFromBuffer(reinterpret_cast(packet_start_), + packet_->length()); name_.clear(); } diff --git a/libtransport/src/core/packet.cc b/libtransport/src/core/packet.cc index 6815868f0..cd2c5aa69 100644 --- a/libtransport/src/core/packet.cc +++ b/libtransport/src/core/packet.cc @@ -50,7 +50,8 @@ Packet::Packet(MemBufPtr &&buffer) packet_start_(reinterpret_cast(packet_->writableData())), header_head_(packet_.get()), payload_head_(nullptr), - format_(getFormatFromBuffer(packet_->writableData())) {} + format_(getFormatFromBuffer(packet_->writableData(), packet_->length())) { +} Packet::Packet(const uint8_t *buffer, std::size_t size) : Packet(MemBufPtr(utils::MemBuf::copyBuffer(buffer, size).release())) {} @@ -210,13 +211,13 @@ const std::shared_ptr Packet::acquireMemBufReference() const { void Packet::dump() const { const_cast(this)->separateHeaderPayload(); - std::cout << "HEADER -- Length: " << headerSize() << std::endl; + TRANSPORT_LOGI("HEADER -- Length: %zu", headerSize()); hicn_packet_dump((uint8_t *)header_head_->data(), headerSize()); - std::cout << std::endl << "PAYLOAD -- Length: " << payloadSize() << std::endl; + TRANSPORT_LOGI("PAYLOAD -- Length: %zu", payloadSize()); for (utils::MemBuf *current = payload_head_; current && current != header_head_; current = current->next()) { - std::cout << "MemBuf Length: " << current->length() << std::endl; + TRANSPORT_LOGI("MemBuf Length: %zu", current->length()); hicn_packet_dump((uint8_t *)current->data(), current->length()); } } diff --git a/libtransport/src/core/portal.h b/libtransport/src/core/portal.h index 364a36577..b63eab3af 100644 --- a/libtransport/src/core/portal.h +++ b/libtransport/src/core/portal.h @@ -96,13 +96,13 @@ class HandlerAllocator { HandlerAllocator(const HandlerAllocator &other) noexcept : memory_(other.memory_) {} - TRANSPORT_ALWAYS_INLINE bool operator==(const HandlerAllocator &other) const - noexcept { + TRANSPORT_ALWAYS_INLINE bool operator==( + const HandlerAllocator &other) const noexcept { return &memory_ == &other.memory_; } - TRANSPORT_ALWAYS_INLINE bool operator!=(const HandlerAllocator &other) const - noexcept { + TRANSPORT_ALWAYS_INLINE bool operator!=( + const HandlerAllocator &other) const noexcept { return &memory_ != &other.memory_; } @@ -139,7 +139,7 @@ class CustomAllocatorHandler { } template - void operator()(Args &&... args) { + void operator()(Args &&...args) { handler_(std::forward(args)...); } @@ -548,7 +548,8 @@ class Portal { return; } - Packet::Format format = Packet::getFormatFromBuffer(packet_buffer->data()); + Packet::Format format = Packet::getFormatFromBuffer( + packet_buffer->data(), packet_buffer->length()); if (TRANSPORT_EXPECT_TRUE(_is_tcp(format))) { if (!Packet::isInterest(packet_buffer->data())) { diff --git a/libtransport/src/core/tcp_socket_connector.cc b/libtransport/src/core/tcp_socket_connector.cc index 20b3d6ce6..fa029c6fc 100644 --- a/libtransport/src/core/tcp_socket_connector.cc +++ b/libtransport/src/core/tcp_socket_connector.cc @@ -33,6 +33,8 @@ namespace { class NetworkMessage { public: static constexpr std::size_t fixed_header_length = 10; + static constexpr std::uint8_t ccnx_flag = 102; + static constexpr std::size_t ccnx_packet_length = 44; static std::size_t decodeHeader(const uint8_t *packet) { // General checks @@ -40,11 +42,12 @@ class NetworkMessage { uint8_t first_byte = packet[0]; uint8_t ip_format = (packet[0] & 0xf0) >> 4; - if (TRANSPORT_EXPECT_FALSE(first_byte == 102)) { + if (TRANSPORT_EXPECT_FALSE(first_byte == ccnx_flag)) { // Get packet length - return 44; + return ccnx_packet_length; } else if (TRANSPORT_EXPECT_TRUE(ip_format == 6 || ip_format == 4)) { - Packet::Format format = Packet::getFormatFromBuffer(packet); + Packet::Format format = + Packet::getFormatFromBuffer(packet, fixed_header_length); return Packet::getHeaderSizeFromBuffer(format, packet) + Packet::getPayloadSizeFromBuffer(format, packet); } -- cgit 1.2.3-korg