Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Packet++/header/TcpReassembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -460,15 +460,15 @@ namespace pcpp
};

using ConnectionList = std::unordered_map<uint32_t, TcpReassemblyData>;
using CleanupList = std::map<time_t, std::list<uint32_t>>;
using CleanupMultiMap = std::multimap<time_t, uint32_t>;

OnTcpMessageReady m_OnMessageReadyCallback;
OnTcpConnectionStart m_OnConnStart;
OnTcpConnectionEnd m_OnConnEnd;
void* m_UserCookie;
ConnectionList m_ConnectionList;
ConnectionInfoList m_ConnectionInfo;
CleanupList m_CleanupList;
CleanupMultiMap m_CleanupMultimap;
bool m_RemoveConnInfo;
uint32_t m_ClosedConnectionDelay;
uint32_t m_MaxNumToClean;
Expand All @@ -483,7 +483,7 @@ namespace pcpp

void closeConnectionInternal(uint32_t flowKey, ConnectionEndReason reason);

void insertIntoCleanupList(uint32_t flowKey);
void scheduleCleanup(uint32_t flowKey);
};

} // namespace pcpp
49 changes: 20 additions & 29 deletions Packet++/src/TcpReassembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ namespace pcpp
m_OnConnEnd(tcpReassemblyData.connData, reason, m_UserCookie);

tcpReassemblyData.closed = true; // mark the connection as closed
insertIntoCleanupList(flowKey);
scheduleCleanup(flowKey);

PCPP_LOG_DEBUG("Connection with flow key 0x" << std::hex << flowKey << " is closed");
}
Expand Down Expand Up @@ -781,7 +781,7 @@ namespace pcpp
m_OnConnEnd(tcpReassemblyData.connData, TcpReassemblyConnectionClosedManually, m_UserCookie);

tcpReassemblyData.closed = true; // mark the connection as closed
insertIntoCleanupList(flowKey);
scheduleCleanup(flowKey);

PCPP_LOG_DEBUG("Connection with flow key 0x" << std::hex << flowKey << " is closed");
}
Expand All @@ -796,18 +796,16 @@ namespace pcpp
return -1;
}

void TcpReassembly::insertIntoCleanupList(uint32_t flowKey)
void TcpReassembly::scheduleCleanup(uint32_t flowKey)
{
// m_CleanupList is a map with key of type time_t (expiration time). The mapped type is a list that stores the
// flow keys to be cleared in certain point of time. m_CleanupList.insert inserts an empty list if the container
// does not already contain an element with an equivalent key, otherwise this method returns an iterator to the
// element that prevents insertion.
std::pair<CleanupList::iterator, bool> pair =
m_CleanupList.insert(std::make_pair(time(nullptr) + m_ClosedConnectionDelay, CleanupList::mapped_type()));

// getting the reference to list
CleanupList::mapped_type& keysList = pair.first->second;
keysList.push_front(flowKey);
// m_CleanupMultimap is a multimap with key of type time_t (expiration time) and a value type uint32_t (flow
// key). Due to being a multimap, multiple flow keys can have the same expiration time.
//
// During purge, up to 'maxNumToClean' entries with expiration time smaller than the current time will be
// removed from storage.

auto expireTime = time(nullptr) + m_ClosedConnectionDelay;
m_CleanupMultimap.emplace(expireTime, flowKey);
}

uint32_t TcpReassembly::purgeClosedConnections(uint32_t maxNumToClean)
Expand All @@ -817,25 +815,18 @@ namespace pcpp
if (maxNumToClean == 0)
maxNumToClean = m_MaxNumToClean;

CleanupList::iterator iterTime = m_CleanupList.begin(), iterTimeEnd = m_CleanupList.upper_bound(time(nullptr));
while (iterTime != iterTimeEnd && count < maxNumToClean)
{
CleanupList::mapped_type& keysList = iterTime->second;
auto it = m_CleanupMultimap.begin();
auto itEnd = m_CleanupMultimap.upper_bound(time(nullptr));

for (; !keysList.empty() && count < maxNumToClean; ++count)
{
CleanupList::mapped_type::const_reference key = keysList.front();
m_ConnectionInfo.erase(key);
m_ConnectionList.erase(key);
keysList.pop_front();
}

if (keysList.empty())
m_CleanupList.erase(iterTime++);
else
++iterTime;
for (; it != itEnd && count < maxNumToClean; ++it, ++count)
{
auto flowKey = it->second;
m_ConnectionInfo.erase(flowKey);
m_ConnectionList.erase(flowKey);
}

m_CleanupMultimap.erase(m_CleanupMultimap.begin(), it);

return count;
}

Expand Down
Loading