Skip to content

Commit

Permalink
differentiate garlic message received from tunnel and directly
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Jan 4, 2014
1 parent 4f1f08b commit 5997cb8
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 16 deletions.
8 changes: 5 additions & 3 deletions Garlic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ namespace garlic
return ret;
}

void GarlicRouting::HandleGarlicMessage (uint8_t * buf, size_t len)
void GarlicRouting::HandleGarlicMessage (uint8_t * buf, size_t len, bool isFromTunnel)
{
uint32_t length = be32toh (*(uint32_t *)buf);
buf += 4;
Expand All @@ -203,7 +203,9 @@ namespace garlic
{
// new session
ElGamalBlock elGamal;
i2p::crypto::ElGamalDecrypt (i2p::context.GetLeaseSetPrivateKey (), buf, (uint8_t *)&elGamal, true);
i2p::crypto::ElGamalDecrypt (
isFromTunnel ? i2p::context.GetLeaseSetPrivateKey () : i2p::context.GetPrivateKey (),
buf, (uint8_t *)&elGamal, true);
memcpy (m_SessionKey, elGamal.sessionKey, 32);
uint8_t iv[32]; // IV is first 16 bytes
CryptoPP::SHA256().CalculateDigest(iv, elGamal.preIV, 32);
Expand Down Expand Up @@ -252,7 +254,7 @@ namespace garlic
{
case eGarlicDeliveryTypeLocal:
LogPrint ("Garlic type local");
i2p::HandleI2NPMessage (buf, len);
i2p::HandleI2NPMessage (buf, len, false);
break;
case eGarlicDeliveryTypeDestination:
{
Expand Down
2 changes: 1 addition & 1 deletion Garlic.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace garlic
GarlicRouting ();
~GarlicRouting ();

void HandleGarlicMessage (uint8_t * buf, size_t len);
void HandleGarlicMessage (uint8_t * buf, size_t len, bool isFromTunnel);

I2NPMessage * WrapSingleMessage (const i2p::data::RoutingDestination * destination,
I2NPMessage * msg, I2NPMessage * leaseSet = nullptr);
Expand Down
8 changes: 4 additions & 4 deletions I2NPProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ namespace i2p
return be16toh (header->size) + sizeof (I2NPHeader);
}

void HandleI2NPMessage (uint8_t * msg, size_t len)
void HandleI2NPMessage (uint8_t * msg, size_t len, bool isFromTunnel)
{
I2NPHeader * header = (I2NPHeader *)msg;
uint32_t msgID = be32toh (header->msgID);
Expand All @@ -382,7 +382,7 @@ namespace i2p
{
case eI2NPGarlic:
LogPrint ("Garlic");
i2p::garlic::routing.HandleGarlicMessage (buf, size);
i2p::garlic::routing.HandleGarlicMessage (buf, size, isFromTunnel);
break;
break;
case eI2NPDeliveryStatus:
Expand All @@ -401,7 +401,7 @@ namespace i2p
}
}

void HandleI2NPMessage (I2NPMessage * msg)
void HandleI2NPMessage (I2NPMessage * msg, bool isFromTunnel)
{
if (msg)
{
Expand All @@ -424,7 +424,7 @@ namespace i2p
i2p::data::netdb.PostI2NPMsg (msg);
break;
default:
HandleI2NPMessage (msg->GetBuffer (), msg->GetLength ());
HandleI2NPMessage (msg->GetBuffer (), msg->GetLength (), isFromTunnel);
DeleteI2NPMessage (msg);
}
}
Expand Down
4 changes: 2 additions & 2 deletions I2NPProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ namespace i2p
I2NPMessage * CreateTunnelGatewayMsg (uint32_t tunnelID, I2NPMessage * msg);

size_t GetI2NPMessageLength (uint8_t * msg);
void HandleI2NPMessage (uint8_t * msg, size_t len);
void HandleI2NPMessage (I2NPMessage * msg);
void HandleI2NPMessage (uint8_t * msg, size_t len, bool isFromTunnel);
void HandleI2NPMessage (I2NPMessage * msg, bool isFromTunnel);
}

#endif
7 changes: 4 additions & 3 deletions NTCPSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ namespace ntcp
if (m_ReceiveBufferOffset > 0)
memcpy (m_ReceiveBuffer, nextBlock, m_ReceiveBufferOffset);
}


ScheduleTermination (); // reset termination timer
Receive ();
}
}
Expand Down Expand Up @@ -423,7 +424,7 @@ namespace ntcp
if (m_NextMessageOffset >= m_NextMessage->len + 4) // +checksum
{
// we have a complete I2NP message
i2p::HandleI2NPMessage (m_NextMessage);
i2p::HandleI2NPMessage (m_NextMessage, false);
m_NextMessage = nullptr;
}
}
Expand Down Expand Up @@ -464,7 +465,6 @@ namespace ntcp

boost::asio::async_write (m_Socket, boost::asio::buffer (sendBuffer, l), boost::asio::transfer_all (),
boost::bind(&NTCPSession::HandleSent, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, msg));
ScheduleTermination (); // reset termination timer
}

void NTCPSession::HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, i2p::I2NPMessage * msg)
Expand All @@ -479,6 +479,7 @@ namespace ntcp
else
{
LogPrint ("Msg sent: ", bytes_transferred);
ScheduleTermination (); // reset termination timer
}
}

Expand Down
2 changes: 1 addition & 1 deletion NetDb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace data
else // WTF?
{
LogPrint ("NetDb: unexpected message type ", msg->GetHeader ()->typeID);
i2p::HandleI2NPMessage (msg);
i2p::HandleI2NPMessage (msg, false);
}
msg = m_Queue.Get ();
}
Expand Down
2 changes: 1 addition & 1 deletion Transports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ namespace i2p
{
if (ident == i2p::context.GetRouterInfo ().GetIdentHash ())
// we send it to ourself
i2p::HandleI2NPMessage (msg);
i2p::HandleI2NPMessage (msg, false);
else
m_Service.post (boost::bind (&Transports::PostMessage, this, ident, msg));
}
Expand Down
2 changes: 1 addition & 1 deletion TunnelEndpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ namespace tunnel
switch (msg.deliveryType)
{
case eDeliveryTypeLocal:
i2p::HandleI2NPMessage (msg.data);
i2p::HandleI2NPMessage (msg.data, true);
break;
case eDeliveryTypeTunnel:
i2p::transports.SendMessage (msg.hash, i2p::CreateTunnelGatewayMsg (msg.tunnelID, msg.data));
Expand Down

0 comments on commit 5997cb8

Please sign in to comment.