|
|
|
@ -82,55 +82,73 @@ namespace ssu
@@ -82,55 +82,73 @@ namespace ssu
|
|
|
|
|
bool isLast = fragmentInfo & 0x010000; // bit 16
|
|
|
|
|
uint8_t fragmentNum = fragmentInfo >> 17; // bits 23 - 17
|
|
|
|
|
LogPrint ("SSU data fragment ", (int)fragmentNum, " of message ", msgID, " size=", (int)fragmentSize, isLast ? " last" : " non-last"); |
|
|
|
|
|
|
|
|
|
// find message with msgID
|
|
|
|
|
I2NPMessage * msg = nullptr; |
|
|
|
|
if (fragmentNum > 0) // follow-up fragment
|
|
|
|
|
{ |
|
|
|
|
IncompleteMessage * incompleteMessage = nullptr; |
|
|
|
|
auto it = m_IncomleteMessages.find (msgID); |
|
|
|
|
if (it != m_IncomleteMessages.end ()) |
|
|
|
|
{ |
|
|
|
|
if (fragmentNum == it->second->nextFragmentNum) |
|
|
|
|
// message exists
|
|
|
|
|
incompleteMessage = it->second; |
|
|
|
|
msg = incompleteMessage->msg; |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
// create new message
|
|
|
|
|
msg = NewI2NPMessage (); |
|
|
|
|
msg->len -= sizeof (I2NPHeaderShort); |
|
|
|
|
incompleteMessage = new IncompleteMessage (msg); |
|
|
|
|
m_IncomleteMessages[msgID] = incompleteMessage; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// handle current fragment
|
|
|
|
|
if (fragmentNum == incompleteMessage->nextFragmentNum) |
|
|
|
|
{ |
|
|
|
|
// expected fragment
|
|
|
|
|
msg = it->second->msg; |
|
|
|
|
memcpy (msg->buf + msg->len, buf, fragmentSize); |
|
|
|
|
msg->len += fragmentSize; |
|
|
|
|
it->second->nextFragmentNum++; |
|
|
|
|
incompleteMessage->nextFragmentNum++; |
|
|
|
|
if (!isLast && !incompleteMessage->savedFragments.empty ()) |
|
|
|
|
{ |
|
|
|
|
// try saved fragments
|
|
|
|
|
for (auto it1 = incompleteMessage->savedFragments.begin (); it1 != incompleteMessage->savedFragments.end ();) |
|
|
|
|
{ |
|
|
|
|
auto savedFragment = *it1; |
|
|
|
|
if (savedFragment->fragmentNum == incompleteMessage->nextFragmentNum) |
|
|
|
|
{ |
|
|
|
|
memcpy (msg->buf + msg->len, savedFragment->buf, savedFragment->len); |
|
|
|
|
msg->len += savedFragment->len; |
|
|
|
|
isLast = savedFragment->isLast; |
|
|
|
|
incompleteMessage->nextFragmentNum++; |
|
|
|
|
incompleteMessage->savedFragments.erase (it1++); |
|
|
|
|
delete savedFragment; |
|
|
|
|
} |
|
|
|
|
else if (fragmentNum < it->second->nextFragmentNum) |
|
|
|
|
// duplicate fragment
|
|
|
|
|
LogPrint ("Duplicate fragment ", (int)fragmentNum, " of message ", msgID, ". Ignored"); |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
// missing fragment
|
|
|
|
|
LogPrint ("Missing fragments from ", it->second->nextFragmentNum, " to ", fragmentNum - 1, " of message ", msgID); |
|
|
|
|
//TODO
|
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (isLast) |
|
|
|
|
{ |
|
|
|
|
if (!msg) |
|
|
|
|
DeleteI2NPMessage (it->second->msg); |
|
|
|
|
delete it->second; |
|
|
|
|
m_IncomleteMessages.erase (it); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
// TODO:
|
|
|
|
|
LogPrint ("Unexpected follow-on fragment ", (int)fragmentNum, " of message ", msgID); |
|
|
|
|
} |
|
|
|
|
else // first fragment
|
|
|
|
|
{ |
|
|
|
|
msg = NewI2NPMessage (); |
|
|
|
|
memcpy (msg->GetSSUHeader (), buf, fragmentSize); |
|
|
|
|
msg->len += fragmentSize - sizeof (I2NPHeaderShort); |
|
|
|
|
if (fragmentNum < incompleteMessage->nextFragmentNum) |
|
|
|
|
// duplicate fragment
|
|
|
|
|
LogPrint ("Duplicate fragment ", (int)fragmentNum, " of message ", msgID, ". Ignored"); |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
// missing fragment
|
|
|
|
|
LogPrint ("Missing fragments from ", (int)incompleteMessage->nextFragmentNum, " to ", fragmentNum - 1, " of message ", msgID); |
|
|
|
|
incompleteMessage->savedFragments.insert (new Fragment (fragmentNum, buf, fragmentSize, isLast)); |
|
|
|
|
} |
|
|
|
|
isLast = false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (msg) |
|
|
|
|
{ |
|
|
|
|
if (!fragmentNum && !isLast) |
|
|
|
|
m_IncomleteMessages[msgID] = new IncompleteMessage (msg); |
|
|
|
|
if (isLast) |
|
|
|
|
{ |
|
|
|
|
// delete incomplete message
|
|
|
|
|
delete incompleteMessage; |
|
|
|
|
m_IncomleteMessages.erase (msgID); |
|
|
|
|
// process message
|
|
|
|
|
SendMsgAck (msgID); |
|
|
|
|
msg->FromSSU (msgID); |
|
|
|
|
if (m_Session.GetState () == eSessionStateEstablished) |
|
|
|
@ -148,7 +166,6 @@ namespace ssu
@@ -148,7 +166,6 @@ namespace ssu
|
|
|
|
|
DeleteI2NPMessage (msg); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
buf += fragmentSize; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|