Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension for IPv4Reassembler to prevent a possible memory leak #301

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Corrected the current update
  • Loading branch information
erikbasargin committed Jul 3, 2018
commit 59571a2154a9846b5f9e7e23074861e7348f4b78
8 changes: 5 additions & 3 deletions include/tins/ip_reassembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,12 @@ class TINS_API IPv4Reassembler {
* but on each incoming package.
*
* \param stream_timeout_ms The lifetime of a single stream (milliseconds)
* \param time_to_check_s Time step for verification (seconds)
* If stream_timeout_ms == 0, then there will be no verification
* \param time_to_check_ms Time step for verification (milliseconds)
* If time_to_check_ms == 0 and stream_timeout_ms != 0, then the check will be with each new package
* \param callback If set, it is called for each expired valid stream
*/
void set_timeout_to_stream(uint64_t stream_timeout_ms, uint64_t time_to_check_s = 60, StreamCallback callback = 0);
void set_timeout_to_stream(uint64_t stream_timeout_ms, uint64_t time_to_check_ms = 1000, StreamCallback callback = 0);

/**
* \brief Return the total number of complete packets
Expand Down Expand Up @@ -265,7 +267,7 @@ class TINS_API IPv4Reassembler {
OverlappingTechnique technique_;
uint64_t max_number_packets_to_stream_;
uint64_t stream_timeout_ms_;
uint64_t time_to_check_s_;
uint64_t time_to_check_ms_;
streams_history streams_history_;

StreamCallback stream_overflow_callback_;
Expand Down
10 changes: 5 additions & 5 deletions src/ip_reassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,14 @@ void IPv4Reassembler::removal_expired_streams()

#if TINS_WITH_MONOTONIC_CHRONO
Internals::IPv4Stream::time_point now = std::chrono::system_clock::now();
auto step = std::chrono::duration_cast<std::chrono::seconds>(now - origin_cycle_time_);
if (std::chrono::seconds(time_to_check_s_) < step) {
auto step = std::chrono::duration_cast<std::chrono::milliseconds>(now - origin_cycle_time_);
if (step < std::chrono::milliseconds(time_to_check_ms_)) {
return;
}
#else
uint64_t now = Internals::IPv4Stream::current_time();
uint64_t step = now - origin_cycle_time_;
if (time_to_check_s_ * 1000 < step) {
if (step < time_to_check_ms_) {
return;
}
#endif
Expand Down Expand Up @@ -319,9 +319,9 @@ void IPv4Reassembler::set_max_number_packets_to_stream(uint64_t max_number, Stre
stream_overflow_callback_ = callback;
}

void IPv4Reassembler::set_timeout_to_stream(uint64_t stream_timeout_ms, uint64_t time_to_check_s, StreamCallback callback) {
void IPv4Reassembler::set_timeout_to_stream(uint64_t stream_timeout_ms, uint64_t time_to_check_ms, StreamCallback callback) {
stream_timeout_ms_ = stream_timeout_ms;
time_to_check_s_ = time_to_check_s;
time_to_check_ms_ = time_to_check_ms;
stream_timeout_callback_ = callback;
}

Expand Down
6 changes: 3 additions & 3 deletions tests/src/ip_reassembler_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const size_t IPv4ReassemblerTest::orderings[][11] = {
void IPv4ReassemblerTest::test_packets(const vector<pair<const uint8_t*, size_t> >& vt) {
IPv4Reassembler reassembler;
reassembler.set_max_number_packets_to_stream(500);
reassembler.set_timeout_to_stream(100);
reassembler.set_timeout_to_stream(500, 100);
for(size_t i = 0; i < vt.size(); ++i) {
EthernetII eth(vt[i].first, (uint32_t)vt[i].second);
// Set the TTL for the first fragment to 32 so we can make sure the right "base"
Expand Down Expand Up @@ -130,7 +130,7 @@ TEST_F(IPv4ReassemblerTest, PacketHasMFAndDF) {
EXPECT_TRUE(packet2.rfind_pdu<IP>().is_fragmented());
IPv4Reassembler reassembler;
reassembler.set_max_number_packets_to_stream(500);
reassembler.set_timeout_to_stream(100);
reassembler.set_timeout_to_stream(500, 100);

EXPECT_EQ(IPv4Reassembler::FRAGMENTED, reassembler.process(packet1));
EXPECT_EQ(IPv4Reassembler::REASSEMBLED, reassembler.process(packet2));
Expand Down Expand Up @@ -161,7 +161,7 @@ TEST_F(IPv4ReassemblerTest, PackageWasDeletedByTimeout) {
EXPECT_TRUE(packet2.rfind_pdu<IP>().is_fragmented());
IPv4Reassembler reassembler;
reassembler.set_max_number_packets_to_stream(500);
reassembler.set_timeout_to_stream(100);
reassembler.set_timeout_to_stream(500, 100);

EXPECT_EQ(IPv4Reassembler::FRAGMENTED, reassembler.process(packet1));
EXPECT_EQ(0, reassembler.total_number_complete_packages());
Expand Down