Browse Source

TunnelGateway implementation

pull/6/head
orignal 11 years ago
parent
commit
0d33010335
  1. 101
      I2NPProtocol.cpp
  2. 15
      I2NPProtocol.h

101
I2NPProtocol.cpp

@ -58,6 +58,14 @@ namespace i2p
return msg; return msg;
} }
I2NPMessage * CreateI2NPMessage (const uint8_t * buf, int len)
{
I2NPMessage * msg = NewI2NPMessage ();
memcpy (msg->GetBuffer (), buf, len);
msg->len += msg->offset + len;
return msg;
}
I2NPMessage * CreateDeliveryStatusMsg () I2NPMessage * CreateDeliveryStatusMsg ()
{ {
#pragma pack(1) #pragma pack(1)
@ -238,6 +246,15 @@ namespace i2p
encryption.ProcessData((uint8_t *)(records + j), (uint8_t *)(records + j), sizeof (records[j])); encryption.ProcessData((uint8_t *)(records + j), (uint8_t *)(records + j), sizeof (records[j]));
} }
if (clearText.flag & 0x40) // we are endpoint of outboud tunnel
{
// so we send it to reply tunnel
i2p::transports.SendMessage (clearText.nextIdent,
CreateTunnelGatewayMsg (be32toh (clearText.nextTunnel),
eI2NPVariableTunnelBuildReply, buf, len,
be32toh (clearText.nextMessageID)));
}
else
i2p::transports.SendMessage (clearText.nextIdent, i2p::transports.SendMessage (clearText.nextIdent,
CreateI2NPMessage (eI2NPVariableTunnelBuild, buf, len, be32toh (clearText.nextMessageID))); CreateI2NPMessage (eI2NPVariableTunnelBuild, buf, len, be32toh (clearText.nextMessageID)));
return; return;
@ -279,20 +296,78 @@ namespace i2p
return msg; return msg;
} }
I2NPMessage * CreateTunnelGatewayMsg (uint32_t tunnelID, const uint8_t * buf, size_t len)
{
I2NPMessage * msg = NewI2NPMessage ();
TunnelGatewayHeader * header = (TunnelGatewayHeader *)msg->GetPayload ();
header->tunnelID = htobe32 (tunnelID);
header->length = htobe16 (len);
memcpy (msg->GetPayload () + sizeof (TunnelGatewayHeader), buf, len);
msg->len += sizeof (TunnelGatewayHeader) + len;
FillI2NPMessageHeader (msg, eI2NPTunnelGateway);
return msg;
}
I2NPMessage * CreateTunnelGatewayMsg (uint32_t tunnelID, I2NPMessage * msg)
{
if (msg->offset >= sizeof (I2NPHeader) + sizeof (TunnelGatewayHeader))
{
// message is capable to be used without copying
TunnelGatewayHeader * header = (TunnelGatewayHeader *)(msg->GetBuffer () - sizeof (TunnelGatewayHeader));
header->tunnelID = htobe32 (tunnelID);
int len = msg->GetLength ();
header->length = htobe16 (len);
msg->offset -= (sizeof (I2NPHeader) + sizeof (TunnelGatewayHeader));
msg->len = msg->offset + sizeof (I2NPHeader) + sizeof (TunnelGatewayHeader) +len;
FillI2NPMessageHeader (msg, eI2NPTunnelGateway);
return msg;
}
else
{
I2NPMessage * msg1 = CreateTunnelGatewayMsg (tunnelID, msg->GetBuffer (), msg->GetLength ());
DeleteI2NPMessage (msg);
return msg1;
}
}
I2NPMessage * CreateTunnelGatewayMsg (uint32_t tunnelID, I2NPMessageType msgType,
const uint8_t * buf, size_t len, uint32_t replyMsgID)
{
I2NPMessage * msg = NewI2NPMessage ();
size_t gatewayMsgOffset = sizeof (I2NPHeader) + sizeof (TunnelGatewayHeader);
msg->offset += gatewayMsgOffset;
msg->len += gatewayMsgOffset;
memcpy (msg->GetPayload (), buf, len);
msg->len += len;
FillI2NPMessageHeader (msg, msgType, replyMsgID); // create content message
len = msg->GetLength ();
msg->offset -= gatewayMsgOffset;
TunnelGatewayHeader * header = (TunnelGatewayHeader *)msg->GetPayload ();
header->tunnelID = htobe32 (tunnelID);
header->length = htobe16 (len);
FillI2NPMessageHeader (msg, eI2NPTunnelGateway); // gateway message
return msg;
}
void HandleTunnelGatewayMsg (uint8_t * buf, size_t len) void HandleTunnelGatewayMsg (I2NPMessage * msg)
{ {
TunnelGatewayHeader * header = (TunnelGatewayHeader *)buf; TunnelGatewayHeader * header = (TunnelGatewayHeader *)msg->GetPayload ();
uint32_t tunnelID = be32toh(header->tunnelID); uint32_t tunnelID = be32toh(header->tunnelID);
LogPrint ("TunnelGateway of ", (int)be16toh(header->length), uint16_t len = be16toh(header->length);
" bytes for tunnel ", (unsigned int)tunnelID); LogPrint ("TunnelGateway of ", (int)len, " bytes for tunnel ", (unsigned int)tunnelID);
i2p::tunnel::Tunnel * tunnel = i2p::tunnel::tunnels.GetTunnel (tunnelID); i2p::tunnel::TransitTunnel * tunnel = i2p::tunnel::tunnels.GetTransitTunnel (tunnelID);
if (tunnel) if (tunnel)
{ {
// TODO: we must submit message to tunnel // we make payload as new I2NP message to send
msg->offset += sizeof (I2NPHeader) + sizeof (TunnelGatewayHeader);
msg->len = msg->offset + len;
tunnel->SendTunnelDataMsg (nullptr, 0, msg);
} }
else else
{
LogPrint ("Tunnel ", (unsigned int)tunnelID, " not found"); LogPrint ("Tunnel ", (unsigned int)tunnelID, " not found");
i2p::DeleteI2NPMessage (msg);
}
} }
void HandleI2NPMessage (uint8_t * msg, size_t len) void HandleI2NPMessage (uint8_t * msg, size_t len)
@ -305,9 +380,6 @@ namespace i2p
int size = be16toh (header->size); int size = be16toh (header->size);
switch (header->typeID) switch (header->typeID)
{ {
case eI2NPTunnelData: // main message
LogPrint ("Unexpected TunnelData");
break;
case eI2NPGarlic: case eI2NPGarlic:
LogPrint ("Garlic"); LogPrint ("Garlic");
break; break;
@ -326,16 +398,12 @@ namespace i2p
LogPrint ("VariableTunnelBuild"); LogPrint ("VariableTunnelBuild");
HandleVariableTunnelBuildMsg (msgID, buf, size); HandleVariableTunnelBuildMsg (msgID, buf, size);
break; break;
case eI2NPTunnelGateway:
LogPrint ("TunnelGateway");
HandleTunnelGatewayMsg (buf, size);
break;
case eI2NPVariableTunnelBuildReply: case eI2NPVariableTunnelBuildReply:
LogPrint ("VariableTunnelBuildReply"); LogPrint ("VariableTunnelBuildReply");
HandleVariableTunnelBuildReplyMsg (msgID, buf, size); HandleVariableTunnelBuildReplyMsg (msgID, buf, size);
break; break;
default: default:
; LogPrint ("Unexpected message ", (int)header->typeID);
} }
} }
@ -348,6 +416,11 @@ namespace i2p
LogPrint ("TunnelData"); LogPrint ("TunnelData");
i2p::tunnel::tunnels.PostTunnelData (msg); i2p::tunnel::tunnels.PostTunnelData (msg);
} }
else if (msg->GetHeader ()->typeID == eI2NPTunnelGateway)
{
LogPrint ("TunnelGateway");
HandleTunnelGatewayMsg (msg);
}
else else
{ {
HandleI2NPMessage (msg->GetBuffer (), msg->GetLength ()); HandleI2NPMessage (msg->GetBuffer (), msg->GetLength ());

15
I2NPProtocol.h

@ -3,6 +3,7 @@
#include <inttypes.h> #include <inttypes.h>
#include <vector> #include <vector>
#include <string.h>
#include "RouterInfo.h" #include "RouterInfo.h"
namespace i2p namespace i2p
@ -88,11 +89,19 @@ namespace i2p
uint8_t * GetPayload () { return buf + offset + sizeof(I2NPHeader); }; uint8_t * GetPayload () { return buf + offset + sizeof(I2NPHeader); };
uint8_t * GetBuffer () { return buf + offset; }; uint8_t * GetBuffer () { return buf + offset; };
size_t GetLength () const { return len - offset; }; size_t GetLength () const { return len - offset; };
I2NPMessage& operator=(const I2NPMessage& other)
{
memcpy (buf + offset, other.buf + other.offset, other.GetLength ());
len = offset + other.GetLength ();
return *this;
}
}; };
I2NPMessage * NewI2NPMessage (); I2NPMessage * NewI2NPMessage ();
void DeleteI2NPMessage (I2NPMessage * msg); void DeleteI2NPMessage (I2NPMessage * msg);
void FillI2NPMessageHeader (I2NPMessage * msg, I2NPMessageType msgType, uint32_t replyMsgID = 0); void FillI2NPMessageHeader (I2NPMessage * msg, I2NPMessageType msgType, uint32_t replyMsgID = 0);
I2NPMessage * CreateI2NPMessage (I2NPMessageType msgType, const uint8_t * buf, int len, uint32_t replyMsgID = 0); I2NPMessage * CreateI2NPMessage (I2NPMessageType msgType, const uint8_t * buf, int len, uint32_t replyMsgID = 0);
I2NPMessage * CreateI2NPMessage (const uint8_t * buf, int len);
I2NPMessage * CreateDeliveryStatusMsg (); I2NPMessage * CreateDeliveryStatusMsg ();
I2NPMessage * CreateDatabaseLookupMsg (const uint8_t * key, const uint8_t * from, uint32_t replyTunnelID); I2NPMessage * CreateDatabaseLookupMsg (const uint8_t * key, const uint8_t * from, uint32_t replyTunnelID);
@ -118,7 +127,11 @@ namespace i2p
I2NPMessage * CreateTunnelDataMsg (const uint8_t * buf); I2NPMessage * CreateTunnelDataMsg (const uint8_t * buf);
I2NPMessage * CreateTunnelDataMsg (uint32_t tunnelID, const uint8_t * payload); I2NPMessage * CreateTunnelDataMsg (uint32_t tunnelID, const uint8_t * payload);
void HandleTunnelGatewayMsg (uint8_t * buf, size_t len); void HandleTunnelGatewayMsg (I2NPMessage * msg);
I2NPMessage * CreateTunnelGatewayMsg (uint32_t tunnelID, const uint8_t * buf, size_t len);
I2NPMessage * CreateTunnelGatewayMsg (uint32_t tunnelID, I2NPMessageType msgType,
const uint8_t * buf, size_t len, uint32_t replyMsgID = 0);
I2NPMessage * CreateTunnelGatewayMsg (uint32_t tunnelID, I2NPMessage * msg);
void HandleI2NPMessage (uint8_t * msg, size_t len); void HandleI2NPMessage (uint8_t * msg, size_t len);
void HandleI2NPMessage (I2NPMessage * msg); void HandleI2NPMessage (I2NPMessage * msg);

Loading…
Cancel
Save