Skip to content

Commit

Permalink
common Identity for LeaseSet and RouterInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Dec 20, 2013
1 parent 6f1d4f4 commit d714d7f
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 28 deletions.
5 changes: 2 additions & 3 deletions LeaseSet.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <cryptopp/sha.h>
#include "Log.h"
#include "RouterInfo.h"
#include "LeaseSet.h"

namespace i2p
Expand All @@ -12,15 +11,15 @@ namespace data
#pragma pack(1)
struct H
{
RouterIdentity destination;
Identity destination;
uint8_t encryptionKey[256];
uint8_t signingKey[128];
uint8_t num;
};
#pragma pack ()

const H * header = (const H *)buf;
CryptoPP::SHA256().CalculateDigest(m_IdentHash, (uint8_t *)&header->destination, sizeof (RouterIdentity));
CryptoPP::SHA256().CalculateDigest(m_IdentHash, (uint8_t *)&header->destination, sizeof (Identity));
memcpy (m_EncryptionKey, header->encryptionKey, 256);
LogPrint ("LeaseSet num=", (int)header->num);

Expand Down
9 changes: 9 additions & 0 deletions LeaseSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@ namespace data
{

#pragma pack(1)

struct Identity
{
uint8_t publicKey[256];
uint8_t signingKey[128];
uint8_t certificate[3];
};

struct Lease
{
uint8_t tunnelGateway[32];
uint32_t tunnelID;
uint64_t endDate;
};

#pragma pack()

class IdentHash
Expand Down
2 changes: 1 addition & 1 deletion NTCPSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace ntcp
struct NTCPPhase3
{
uint16_t size;
i2p::data::RouterIdentity ident;
i2p::data::Identity ident;
uint32_t timestamp;
uint8_t padding[15];
uint8_t signature[40];
Expand Down
2 changes: 1 addition & 1 deletion RouterContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace i2p

void RouterContext::CreateNewRouter ()
{
i2p::data::RouterIdentity ident;
i2p::data::Identity ident;

CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
dh.GenerateKeyPair(m_Rnd, m_PrivateKey, ident.publicKey);
Expand Down
2 changes: 1 addition & 1 deletion RouterContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace i2p
const uint8_t * GetSigningPrivateKey () const;
const uint8_t * GetLeaseSetPrivateKey () const { return m_LeaseSetPrivateKey; };
const uint8_t * GetLeaseSetPublicKey () const { return m_LeaseSetPublicKey; };
const i2p::data::RouterIdentity& GetRouterIdentity () const { return m_RouterInfo.GetRouterIdentity (); };
const i2p::data::Identity& GetRouterIdentity () const { return m_RouterInfo.GetRouterIdentity (); };
CryptoPP::RandomNumberGenerator& GetRandomNumberGenerator () { return m_Rnd; };

void Sign (uint8_t * buf, int len, uint8_t * signature);
Expand Down
2 changes: 1 addition & 1 deletion RouterInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace data
ReadFromBuffer ();
}

void RouterInfo::SetRouterIdentity (const RouterIdentity& identity)
void RouterInfo::SetRouterIdentity (const Identity& identity)
{
m_RouterIdentity = identity;
CryptoPP::SHA256().CalculateDigest(m_IdentHash, (uint8_t *)&m_RouterIdentity, sizeof (m_RouterIdentity));
Expand Down
19 changes: 4 additions & 15 deletions RouterInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,7 @@
namespace i2p
{
namespace data
{
#pragma pack (1)

struct RouterIdentity
{
uint8_t publicKey[256];
uint8_t signingKey[128];
uint8_t certificate[3];
};
#pragma pack ()


{
class RouterInfo: public RoutingDestination
{
public:
Expand All @@ -49,8 +38,8 @@ namespace data
RouterInfo (const RouterInfo& ) = default;
RouterInfo (const uint8_t * buf, int len);

const RouterIdentity& GetRouterIdentity () const { return m_RouterIdentity; };
void SetRouterIdentity (const RouterIdentity& identity);
const Identity& GetRouterIdentity () const { return m_RouterIdentity; };
void SetRouterIdentity (const Identity& identity);
const char * GetIdentHashBase64 () const { return m_IdentHashBase64; };
const char * GetIdentHashAbbreviation () const { return m_IdentHashAbbreviation; };
uint64_t GetTimestamp () const { return m_Timestamp; };
Expand Down Expand Up @@ -88,7 +77,7 @@ namespace data

private:

RouterIdentity m_RouterIdentity;
Identity m_RouterIdentity;
IdentHash m_IdentHash;
char m_IdentHashBase64[48], m_IdentHashAbbreviation[5];
char m_Buffer[2048];
Expand Down
67 changes: 62 additions & 5 deletions Streaming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
#include <cryptopp/gzip.h>
#include "Log.h"
#include "RouterInfo.h"
#include "RouterContext.h"
#include "Streaming.h"

namespace i2p
{
namespace stream
{
void StreamingDestination::HandleNextPacket (const uint8_t * buf, size_t len)
Stream::Stream (const i2p::data::IdentHash& destination):
m_SendStreamID (0)
{
m_RecvStreamID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 ();
}

void Stream::HandleNextPacket (const uint8_t * buf, size_t len)
{
const uint8_t * end = buf + len;
buf += 4; // sendStreamID
Expand Down Expand Up @@ -42,15 +49,45 @@ namespace stream
if (flags & PACKET_FLAG_FROM_INCLUDED)
{
LogPrint ("From identity");
optionalData += sizeof (i2p::data::RouterIdentity);
optionalData += sizeof (i2p::data::Identity);
}

// we have reached payload section
std::string str((const char *)buf, end-buf);
LogPrint ("Payload: ", str);
}

StreamingDestination m_SharedLocalDestination;

void StreamingDestination::HandleNextPacket (const uint8_t * buf, size_t len)
{
uint32_t sendStreamID = *(uint32_t *)(buf);
auto it = m_Streams.find (sendStreamID);
if (it != m_Streams.end ())
it->second->HandleNextPacket (buf, len);
else
LogPrint ("Unknown stream ", sendStreamID);
}


Stream * StreamingDestination::CreateNewStream (const i2p::data::IdentHash& destination)
{
/*i2p::data::LeaseSet * leaseSet = i2p::data::netdb.FindLeaseSet (destination);
if (!leaseSet)
{
i2p::data::netdb.RequestDestination (destination);
sleep (5); // wait for 5 seconds
leaseSet = i2p::data::netdb.FindLeaseSet (destination);
if (!leaseSet)
{
LogPrint ("Couldn't find LeaseSet");
return nullptr;
}
} */
Stream * s = new Stream (destination);
m_Streams[s->GetRecvStreamID ()] = s;
return s;
}

void HandleDataMessage (i2p::data::IdentHash * destination, const uint8_t * buf, size_t len)
{
uint32_t length = be32toh (*(uint32_t *)buf);
Expand All @@ -63,12 +100,32 @@ namespace stream
decompressor.Put (buf, length);
decompressor.MessageEnd();
uint8_t uncompressed[2048];
int uncomressedSize = decompressor.MaxRetrievable ();
decompressor.Get (uncompressed, uncomressedSize);
int uncompressedSize = decompressor.MaxRetrievable ();
decompressor.Get (uncompressed, uncompressedSize);
// then forward to streaming engine
// TODO: we have onle one destination, might be more
m_SharedLocalDestination.HandleNextPacket (uncompressed, uncompressedSize);
}
else
LogPrint ("Data: protocol ", buf[9], " is not supported");
}

I2NPMessage * CreateDataMessage (Stream * s, uint8_t * payload, size_t len)
{
I2NPMessage * msg = NewI2NPMessage ();
CryptoPP::Gzip compressor;
compressor.Put (payload, len);
compressor.MessageEnd();
int size = compressor.MaxRetrievable ();
uint8_t * buf = msg->GetPayload ();
*(uint16_t *)buf = htobe32 (size); // length
buf += 4;
compressor.Get (buf, size);
buf[9] = 6; // streaming protocol
msg->len += size + 4;
FillI2NPMessageHeader (msg, eI2NPData);

return msg;
}
}
}
25 changes: 24 additions & 1 deletion Streaming.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#define STREAMING_H__

#include <inttypes.h>
#include <map>
#include "LeaseSet.h"
#include "I2NPProtocol.h"

namespace i2p
{
Expand All @@ -20,15 +22,36 @@ namespace stream
const uint16_t PACKET_FLAG_ECHO = 0x0200;
const uint16_t PACKET_FLAG_NO_ACK = 0x0400;

class Stream
{
public:

Stream (const i2p::data::IdentHash& destination);
uint32_t GetSendStreamID () const { return m_SendStreamID; };
uint32_t GetRecvStreamID () const { return m_RecvStreamID; };

void HandleNextPacket (const uint8_t * buf, size_t len);

private:

uint32_t m_SendStreamID, m_RecvStreamID;
};

class StreamingDestination
{
public:

Stream * CreateNewStream (const i2p::data::IdentHash& destination);
void HandleNextPacket (const uint8_t * buf, size_t len);
};

private:

std::map<uint32_t, Stream *> m_Streams;
};

// assuming data is I2CP message
void HandleDataMessage (i2p::data::IdentHash * destination, const uint8_t * buf, size_t len);
I2NPMessage * CreateDataMessage (Stream * s, uint8_t * payload, size_t len);
}
}

Expand Down

0 comments on commit d714d7f

Please sign in to comment.