Skip to content

Commit

Permalink
pass delivery intructions to tunnel gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Feb 20, 2014
1 parent f5fa953 commit 154105a
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 41 deletions.
2 changes: 1 addition & 1 deletion I2NPProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ namespace i2p
LogPrint ("TunnelGateway of ", (int)len, " bytes for tunnel ", (unsigned int)tunnelID, ". Msg type ", (int)msg->GetHeader()->typeID);
i2p::tunnel::TransitTunnel * tunnel = i2p::tunnel::tunnels.GetTransitTunnel (tunnelID);
if (tunnel)
tunnel->SendTunnelDataMsg (nullptr, 0, msg);
tunnel->SendTunnelDataMsg (msg);
else
{
LogPrint ("Tunnel ", (unsigned int)tunnelID, " not found");
Expand Down
2 changes: 1 addition & 1 deletion NTCPSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace ntcp

#pragma pack()

const int TERMINATION_TIMEOUT = 60; // 1 minute
const int TERMINATION_TIMEOUT = 150; // 2.5 minutes
class NTCPSession
{
public:
Expand Down
6 changes: 3 additions & 3 deletions TransitTunnel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ namespace tunnel
m_NumTransmittedBytes += tunnelMsg->GetLength ();
}

void TransitTunnel::SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg)
void TransitTunnel::SendTunnelDataMsg (i2p::I2NPMessage * msg)
{
LogPrint ("We are not a gateway for transit tunnel ", m_TunnelID);
i2p::DeleteI2NPMessage (msg);
}

void TransitTunnelGateway::SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg)
void TransitTunnelGateway::SendTunnelDataMsg (i2p::I2NPMessage * msg)
{
m_Gateway.SendTunnelDataMsg (gwHash, gwTunnel, msg);
m_Gateway.SendTunnelDataMsg (msg);
}

void TransitTunnelEndpoint::HandleTunnelDataMsg (i2p::I2NPMessage * tunnelMsg)
Expand Down
4 changes: 2 additions & 2 deletions TransitTunnel.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace tunnel
const uint8_t * layerKey,const uint8_t * ivKey);

virtual void HandleTunnelDataMsg (i2p::I2NPMessage * tunnelMsg);
virtual void SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg);
virtual void SendTunnelDataMsg (i2p::I2NPMessage * msg);
virtual size_t GetNumTransmittedBytes () const { return m_NumTransmittedBytes; };

uint32_t GetTunnelID () const { return m_TunnelID; };
Expand Down Expand Up @@ -54,7 +54,7 @@ namespace tunnel
TransitTunnel (receiveTunnelID, nextIdent, nextTunnelID,
layerKey, ivKey), m_Gateway(this) {};

void SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg);
void SendTunnelDataMsg (i2p::I2NPMessage * msg);
size_t GetNumTransmittedBytes () const { return m_Gateway.GetNumSentBytes (); };

private:
Expand Down
17 changes: 1 addition & 16 deletions Tunnel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,7 @@ namespace tunnel
void OutboundTunnel::SendTunnelDataMsg (std::vector<TunnelMessageBlock> msgs)
{
for (auto& it : msgs)
{
switch (it.deliveryType)
{
case eDeliveryTypeLocal:
m_Gateway.SendTunnelDataMsg (nullptr, 0, it.data);
break;
case eDeliveryTypeTunnel:
m_Gateway.SendTunnelDataMsg (it.hash, it.tunnelID, it.data);
break;
case eDeliveryTypeRouter:
m_Gateway.SendTunnelDataMsg (it.hash, 0, it.data);
break;
default:
LogPrint ("Unexpected delivery type ", (int)it.deliveryType);
}
}
m_Gateway.PutTunnelDataMsg (it);
m_Gateway.SendBuffer ();
}

Expand Down
2 changes: 1 addition & 1 deletion Tunnel.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace i2p
{
namespace tunnel
{
const int TUNNEL_EXPIRATION_TIMEOUT = 600; // 10 minutes
const int TUNNEL_EXPIRATION_TIMEOUT = 660; // 11 minutes

class OutboundTunnel;
class InboundTunnel;
Expand Down
52 changes: 37 additions & 15 deletions TunnelGateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,29 @@ namespace i2p
{
namespace tunnel
{
void TunnelGatewayBuffer::PutI2NPMsg (const uint8_t * gwHash, uint32_t gwTunnel, I2NPMessage * msg)
void TunnelGatewayBuffer::PutI2NPMsg (const TunnelMessageBlock& block)
{
if (!m_CurrentTunnelDataMsg)
CreateCurrentTunnelDataMessage ();

// create delivery instructions
uint8_t di[43]; // max delivery instruction length is 43 for tunnel
size_t diLen = 1;// flag
TunnelDeliveryType dt = eDeliveryTypeLocal;
if (gwHash)
if (block.deliveryType != eDeliveryTypeLocal) // tunnel or router
{
if (gwTunnel)
if (block.deliveryType == eDeliveryTypeTunnel)
{
*(uint32_t *)(di + diLen) = htobe32 (gwTunnel);
*(uint32_t *)(di + diLen) = htobe32 (block.tunnelID);
diLen += 4; // tunnelID
dt = eDeliveryTypeTunnel;
}
else
dt = eDeliveryTypeRouter;

memcpy (di + diLen, gwHash, 32);
memcpy (di + diLen, block.hash, 32);
diLen += 32; //len
}
di[0] = dt << 5; // set delivery type
di[0] = block.deliveryType << 5; // set delivery type

// create fragments
I2NPMessage * msg = block.data;
if (diLen + msg->GetLength () + 2<= m_RemainingSize)
{
// message fits. First and last fragment
Expand Down Expand Up @@ -104,7 +101,7 @@ namespace tunnel
{
// delivery instructions don't fit. Create new message
CompleteCurrentTunnelDataMessage ();
PutI2NPMsg (gwHash, gwTunnel, msg);
PutI2NPMsg (block);
// don't delete msg because it's taken care inside
}
}
Expand Down Expand Up @@ -151,16 +148,41 @@ namespace tunnel
m_TunnelDataMsgs.push_back (m_CurrentTunnelDataMsg);
m_CurrentTunnelDataMsg = nullptr;
}


void TunnelGateway::SendTunnelDataMsg (i2p::I2NPMessage * msg)
{
SendTunnelDataMsg (nullptr, 0, msg);
}

void TunnelGateway::SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg)
{
PutTunnelDataMsg (gwHash, gwTunnel, msg);
TunnelMessageBlock block;
if (gwHash)
{
block.hash = gwHash;
if (gwTunnel)
{
block.deliveryType = eDeliveryTypeTunnel;
block.tunnelID = gwTunnel;
}
else
block.deliveryType = eDeliveryTypeRouter;
}
else
block.deliveryType = eDeliveryTypeLocal;
block.data = msg;
SendTunnelDataMsg (block);
}

void TunnelGateway::SendTunnelDataMsg (const TunnelMessageBlock& block)
{
PutTunnelDataMsg (block);
SendBuffer ();
}

void TunnelGateway::PutTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg)
void TunnelGateway::PutTunnelDataMsg (const TunnelMessageBlock& block)
{
m_Buffer.PutI2NPMsg (gwHash, gwTunnel, msg);
m_Buffer.PutI2NPMsg (block);
}

void TunnelGateway::SendBuffer ()
Expand Down
6 changes: 4 additions & 2 deletions TunnelGateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace tunnel
public:
TunnelGatewayBuffer (uint32_t tunnelID): m_TunnelID (tunnelID),
m_CurrentTunnelDataMsg (nullptr), m_RemainingSize (0) {};
void PutI2NPMsg (const uint8_t * gwHash, uint32_t gwTunnel, I2NPMessage * msg);
void PutI2NPMsg (const TunnelMessageBlock& block);
std::vector<I2NPMessage *> GetTunnelDataMsgs ();

private:
Expand All @@ -37,8 +37,10 @@ namespace tunnel

TunnelGateway (TunnelBase * tunnel):
m_Tunnel (tunnel), m_Buffer (tunnel->GetNextTunnelID ()), m_NumSentBytes (0) {};
void SendTunnelDataMsg (i2p::I2NPMessage * msg);
void SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg);
void PutTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg);
void SendTunnelDataMsg (const TunnelMessageBlock& block);
void PutTunnelDataMsg (const TunnelMessageBlock& block);
void SendBuffer ();
size_t GetNumSentBytes () const { return m_NumSentBytes; };

Expand Down

0 comments on commit 154105a

Please sign in to comment.