check I2NP message size for saved fragments

This commit is contained in:
orignal 2015-03-11 14:34:39 -04:00
parent 1d7fd8ac96
commit 2f9e510f4f
2 changed files with 21 additions and 21 deletions

View File

@ -10,6 +10,21 @@ namespace i2p
{ {
namespace transport namespace transport
{ {
void IncompleteMessage::AttachNextFragment (const uint8_t * fragment, size_t fragmentSize)
{
if (msg->len + fragmentSize > msg->maxLen)
{
LogPrint (eLogInfo, "SSU I2NP message size ", msg->maxLen, " is not enough");
I2NPMessage * newMsg = NewI2NPMessage ();
*newMsg = *msg;
DeleteI2NPMessage (msg);
msg = newMsg;
}
memcpy (msg->buf + msg->len, fragment, fragmentSize);
msg->len += fragmentSize;
nextFragmentNum++;
}
SSUData::SSUData (SSUSession& session): SSUData::SSUData (SSUSession& session):
m_Session (session), m_ResendTimer (session.GetService ()), m_DecayTimer (session.GetService ()), m_Session (session), m_ResendTimer (session.GetService ()), m_DecayTimer (session.GetService ()),
m_IncompleteMessagesCleanupTimer (session.GetService ()) m_IncompleteMessagesCleanupTimer (session.GetService ())
@ -155,15 +170,11 @@ namespace transport
} }
// find message with msgID // find message with msgID
I2NPMessage * msg = nullptr;
auto it = m_IncompleteMessages.find (msgID); auto it = m_IncompleteMessages.find (msgID);
if (it != m_IncompleteMessages.end ()) if (it == m_IncompleteMessages.end ())
// message exists
msg = it->second->msg;
else
{ {
// create new message // create new message
msg = NewI2NPShortMessage (); auto msg = NewI2NPShortMessage ();
msg->len -= I2NP_SHORT_HEADER_SIZE; msg->len -= I2NP_SHORT_HEADER_SIZE;
it = m_IncompleteMessages.insert (std::make_pair (msgID, it = m_IncompleteMessages.insert (std::make_pair (msgID,
std::unique_ptr<IncompleteMessage>(new IncompleteMessage (msg)))).first; std::unique_ptr<IncompleteMessage>(new IncompleteMessage (msg)))).first;
@ -174,18 +185,7 @@ namespace transport
if (fragmentNum == incompleteMessage->nextFragmentNum) if (fragmentNum == incompleteMessage->nextFragmentNum)
{ {
// expected fragment // expected fragment
if (msg->len + fragmentSize > msg->maxLen) incompleteMessage->AttachNextFragment (buf, fragmentSize);
{
LogPrint (eLogInfo, "SSU I2NP message size ", msg->maxLen, " is not enough");
I2NPMessage * newMsg = NewI2NPMessage ();
*newMsg = *msg;
DeleteI2NPMessage (msg);
msg = newMsg;
it->second->msg = msg;
}
memcpy (msg->buf + msg->len, buf, fragmentSize);
msg->len += fragmentSize;
incompleteMessage->nextFragmentNum++;
if (!isLast && !incompleteMessage->savedFragments.empty ()) if (!isLast && !incompleteMessage->savedFragments.empty ())
{ {
// try saved fragments // try saved fragments
@ -194,10 +194,8 @@ namespace transport
auto& savedFragment = *it1; auto& savedFragment = *it1;
if (savedFragment->fragmentNum == incompleteMessage->nextFragmentNum) if (savedFragment->fragmentNum == incompleteMessage->nextFragmentNum)
{ {
memcpy (msg->buf + msg->len, savedFragment->buf, savedFragment->len); incompleteMessage->AttachNextFragment (savedFragment->buf, savedFragment->len);
msg->len += savedFragment->len;
isLast = savedFragment->isLast; isLast = savedFragment->isLast;
incompleteMessage->nextFragmentNum++;
incompleteMessage->savedFragments.erase (it1++); incompleteMessage->savedFragments.erase (it1++);
} }
else else
@ -228,6 +226,7 @@ namespace transport
if (isLast) if (isLast)
{ {
// delete incomplete message // delete incomplete message
auto msg = incompleteMessage->msg;
incompleteMessage->msg = nullptr; incompleteMessage->msg = nullptr;
m_IncompleteMessages.erase (msgID); m_IncompleteMessages.erase (msgID);
// process message // process message

View File

@ -66,6 +66,7 @@ namespace transport
IncompleteMessage (I2NPMessage * m): msg (m), nextFragmentNum (0), lastFragmentInsertTime (0) {}; IncompleteMessage (I2NPMessage * m): msg (m), nextFragmentNum (0), lastFragmentInsertTime (0) {};
~IncompleteMessage () { if (msg) DeleteI2NPMessage (msg); }; ~IncompleteMessage () { if (msg) DeleteI2NPMessage (msg); };
void AttachNextFragment (const uint8_t * fragment, size_t fragmentSize);
}; };
struct SentMessage struct SentMessage