Skip to content

Commit

Permalink
Merge "[HICN-679] Do not throw exception when receiving corrupted/not…
Browse files Browse the repository at this point in the history
…-hicn packet from network layer"
  • Loading branch information
msardara authored and Gerrit Code Review committed Feb 10, 2021
2 parents db76bbc + 8d08f8d commit 71a5c39
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/includes/hicn/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
15 changes: 8 additions & 7 deletions lib/src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <stdio.h>

#include <hicn/common.h>
#include <hicn/util/log.h>



Expand Down Expand Up @@ -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];
Expand All @@ -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;
}

Expand All @@ -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))
Expand All @@ -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);
}

/*
Expand Down
11 changes: 8 additions & 3 deletions libtransport/includes/hicn/transport/core/packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <hicn/transport/security/crypto_suite.h>
#include <hicn/transport/security/key_id.h>
#include <hicn/transport/utils/branch_prediction.h>
#include <hicn/transport/utils/log.h>
#include <hicn/transport/utils/membuf.h>
#include <hicn/transport/utils/object_pool.h>

Expand Down Expand Up @@ -97,13 +98,16 @@ class Packet : public std::enable_shared_from_this<Packet> {

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;
Expand All @@ -114,7 +118,8 @@ class Packet : public std::enable_shared_from_this<Packet> {
packet_start_ = reinterpret_cast<hicn_header_t *>(packet_->writableData());
header_head_ = packet_.get();
payload_head_ = nullptr;
format_ = getFormatFromBuffer(reinterpret_cast<uint8_t *>(packet_start_));
format_ = getFormatFromBuffer(reinterpret_cast<uint8_t *>(packet_start_),
packet_->length());
name_.clear();
}

Expand Down
9 changes: 5 additions & 4 deletions libtransport/src/core/packet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ Packet::Packet(MemBufPtr &&buffer)
packet_start_(reinterpret_cast<hicn_header_t *>(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())) {}
Expand Down Expand Up @@ -210,13 +211,13 @@ const std::shared_ptr<utils::MemBuf> Packet::acquireMemBufReference() const {
void Packet::dump() const {
const_cast<Packet *>(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());
}
}
Expand Down
13 changes: 7 additions & 6 deletions libtransport/src/core/portal.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ class HandlerAllocator {
HandlerAllocator(const HandlerAllocator<U> &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_;
}

Expand Down Expand Up @@ -139,7 +139,7 @@ class CustomAllocatorHandler {
}

template <typename... Args>
void operator()(Args &&... args) {
void operator()(Args &&...args) {
handler_(std::forward<Args>(args)...);
}

Expand Down Expand Up @@ -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())) {
Expand Down
3 changes: 2 additions & 1 deletion libtransport/src/core/tcp_socket_connector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class NetworkMessage {
// Get packet length
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);
}
Expand Down

0 comments on commit 71a5c39

Please sign in to comment.