Browse Source

use shared_ptr for all incoming I2NP messages

pull/218/head
orignal 10 years ago
parent
commit
9c9401ce2f
  1. 8
      I2NPProtocol.cpp
  2. 2
      I2NPProtocol.h
  3. 10
      NTCPSession.cpp
  4. 2
      NTCPSession.h
  5. 9
      SSUData.cpp
  6. 5
      SSUData.h

8
I2NPProtocol.cpp

@ -582,20 +582,20 @@ namespace i2p @@ -582,20 +582,20 @@ namespace i2p
Flush ();
}
void I2NPMessagesHandler::PutNextMessage (I2NPMessage * msg)
void I2NPMessagesHandler::PutNextMessage (std::shared_ptr<I2NPMessage> msg)
{
if (msg)
{
switch (msg->GetTypeID ())
{
case eI2NPTunnelData:
m_TunnelMsgs.push_back (ToSharedI2NPMessage (msg));
m_TunnelMsgs.push_back (msg);
break;
case eI2NPTunnelGateway:
m_TunnelGatewayMsgs.push_back (ToSharedI2NPMessage (msg));
m_TunnelGatewayMsgs.push_back (msg);
break;
default:
HandleI2NPMessage (ToSharedI2NPMessage (msg));
HandleI2NPMessage (msg);
}
}
}

2
I2NPProtocol.h

@ -236,7 +236,7 @@ namespace tunnel @@ -236,7 +236,7 @@ namespace tunnel
public:
~I2NPMessagesHandler ();
void PutNextMessage (I2NPMessage * msg);
void PutNextMessage (std::shared_ptr<I2NPMessage> msg);
void Flush ();
private:

10
NTCPSession.cpp

@ -84,11 +84,7 @@ namespace transport @@ -84,11 +84,7 @@ namespace transport
transports.PeerDisconnected (shared_from_this ());
m_Server.RemoveNTCPSession (shared_from_this ());
m_SendQueue.clear ();
if (m_NextMessage)
{
i2p::DeleteI2NPMessage (m_NextMessage);
m_NextMessage = nullptr;
}
m_TerminationTimer.cancel ();
LogPrint (eLogInfo, "NTCP session terminated");
}
@ -564,7 +560,8 @@ namespace transport @@ -564,7 +560,8 @@ namespace transport
LogPrint (eLogError, "NTCP data size ", dataSize, " exceeds max size");
return false;
}
m_NextMessage = dataSize <= I2NP_MAX_SHORT_MESSAGE_SIZE - 2 ? NewI2NPShortMessage () : NewI2NPMessage ();
auto msg = dataSize <= I2NP_MAX_SHORT_MESSAGE_SIZE - 2 ? NewI2NPShortMessage () : NewI2NPMessage ();
m_NextMessage = ToSharedI2NPMessage (msg);
memcpy (m_NextMessage->buf, buf, 16);
m_NextMessageOffset = 16;
m_NextMessage->offset = 2; // size field
@ -589,10 +586,7 @@ namespace transport @@ -589,10 +586,7 @@ namespace transport
if (CryptoPP::Adler32().VerifyDigest (m_NextMessage->buf + m_NextMessageOffset - 4, m_NextMessage->buf, m_NextMessageOffset - 4))
m_Handler.PutNextMessage (m_NextMessage);
else
{
LogPrint (eLogWarning, "Incorrect adler checksum of NTCP message. Dropped");
DeleteI2NPMessage (m_NextMessage);
}
m_NextMessage = nullptr;
}
return true;

2
NTCPSession.h

@ -126,7 +126,7 @@ namespace transport @@ -126,7 +126,7 @@ namespace transport
i2p::crypto::AESAlignedBuffer<16> m_TimeSyncBuffer;
int m_ReceiveBufferOffset;
i2p::I2NPMessage * m_NextMessage;
std::shared_ptr<I2NPMessage> m_NextMessage;
size_t m_NextMessageOffset;
i2p::I2NPMessagesHandler m_Handler;

9
SSUData.cpp

@ -15,9 +15,8 @@ namespace transport @@ -15,9 +15,8 @@ namespace transport
if (msg->len + fragmentSize > msg->maxLen)
{
LogPrint (eLogInfo, "SSU I2NP message size ", msg->maxLen, " is not enough");
I2NPMessage * newMsg = NewI2NPMessage ();
auto newMsg = ToSharedI2NPMessage(NewI2NPMessage ());
*newMsg = *msg;
DeleteI2NPMessage (msg);
msg = newMsg;
}
memcpy (msg->buf + msg->len, fragment, fragmentSize);
@ -174,7 +173,7 @@ namespace transport @@ -174,7 +173,7 @@ namespace transport
if (it == m_IncompleteMessages.end ())
{
// create new message
auto msg = NewI2NPShortMessage ();
auto msg = ToSharedI2NPMessage (NewI2NPShortMessage ());
msg->len -= I2NP_SHORT_HEADER_SIZE;
it = m_IncompleteMessages.insert (std::make_pair (msgID,
std::unique_ptr<IncompleteMessage>(new IncompleteMessage (msg)))).first;
@ -244,10 +243,7 @@ namespace transport @@ -244,10 +243,7 @@ namespace transport
m_Handler.PutNextMessage (msg);
}
else
{
LogPrint (eLogWarning, "SSU message ", msgID, " already received");
i2p::DeleteI2NPMessage (msg);
}
}
else
{
@ -259,7 +255,6 @@ namespace transport @@ -259,7 +255,6 @@ namespace transport
}
else
LogPrint (eLogError, "SSU unexpected message ", (int)msg->GetTypeID ());
DeleteI2NPMessage (msg);
}
}
else

5
SSUData.h

@ -59,13 +59,12 @@ namespace transport @@ -59,13 +59,12 @@ namespace transport
struct IncompleteMessage
{
I2NPMessage * msg;
std::shared_ptr<I2NPMessage> msg;
int nextFragmentNum;
uint32_t lastFragmentInsertTime; // in seconds
std::set<std::unique_ptr<Fragment>, FragmentCmp> savedFragments;
IncompleteMessage (I2NPMessage * m): msg (m), nextFragmentNum (0), lastFragmentInsertTime (0) {};
~IncompleteMessage () { if (msg) DeleteI2NPMessage (msg); };
IncompleteMessage (std::shared_ptr<I2NPMessage> m): msg (m), nextFragmentNum (0), lastFragmentInsertTime (0) {};
void AttachNextFragment (const uint8_t * fragment, size_t fragmentSize);
};

Loading…
Cancel
Save