mirror of
				https://github.com/PurpleI2P/i2pd.git
				synced 2025-11-04 08:30:46 +00:00 
			
		
		
		
	common Identity for LeaseSet and RouterInfo
This commit is contained in:
		
							parent
							
								
									6f1d4f4300
								
							
						
					
					
						commit
						d714d7fe6c
					
				
					 9 changed files with 105 additions and 28 deletions
				
			
		| 
						 | 
					@ -1,6 +1,5 @@
 | 
				
			||||||
#include <cryptopp/sha.h>
 | 
					#include <cryptopp/sha.h>
 | 
				
			||||||
#include "Log.h"
 | 
					#include "Log.h"
 | 
				
			||||||
#include "RouterInfo.h"
 | 
					 | 
				
			||||||
#include "LeaseSet.h"
 | 
					#include "LeaseSet.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace i2p
 | 
					namespace i2p
 | 
				
			||||||
| 
						 | 
					@ -12,7 +11,7 @@ namespace data
 | 
				
			||||||
#pragma pack(1)
 | 
					#pragma pack(1)
 | 
				
			||||||
		struct H
 | 
							struct H
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			RouterIdentity destination;
 | 
								Identity destination;
 | 
				
			||||||
			uint8_t encryptionKey[256];
 | 
								uint8_t encryptionKey[256];
 | 
				
			||||||
			uint8_t signingKey[128];
 | 
								uint8_t signingKey[128];
 | 
				
			||||||
			uint8_t num;
 | 
								uint8_t num;
 | 
				
			||||||
| 
						 | 
					@ -20,7 +19,7 @@ namespace data
 | 
				
			||||||
#pragma pack ()	
 | 
					#pragma pack ()	
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		const H * header = (const H *)buf;
 | 
							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);
 | 
							memcpy (m_EncryptionKey, header->encryptionKey, 256);
 | 
				
			||||||
		LogPrint ("LeaseSet num=", (int)header->num);
 | 
							LogPrint ("LeaseSet num=", (int)header->num);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -11,12 +11,21 @@ namespace data
 | 
				
			||||||
{	
 | 
					{	
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
#pragma pack(1)
 | 
					#pragma pack(1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						struct Identity
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							uint8_t publicKey[256];
 | 
				
			||||||
 | 
							uint8_t signingKey[128];
 | 
				
			||||||
 | 
							uint8_t certificate[3];
 | 
				
			||||||
 | 
						};
 | 
				
			||||||
 | 
						
 | 
				
			||||||
	struct Lease
 | 
						struct Lease
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		uint8_t tunnelGateway[32];
 | 
							uint8_t tunnelGateway[32];
 | 
				
			||||||
		uint32_t tunnelID;
 | 
							uint32_t tunnelID;
 | 
				
			||||||
		uint64_t endDate;
 | 
							uint64_t endDate;
 | 
				
			||||||
	};	
 | 
						};	
 | 
				
			||||||
 | 
						
 | 
				
			||||||
#pragma pack()	
 | 
					#pragma pack()	
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	class IdentHash
 | 
						class IdentHash
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -37,7 +37,7 @@ namespace ntcp
 | 
				
			||||||
	struct NTCPPhase3
 | 
						struct NTCPPhase3
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		uint16_t size;
 | 
							uint16_t size;
 | 
				
			||||||
		i2p::data::RouterIdentity ident;
 | 
							i2p::data::Identity ident;
 | 
				
			||||||
		uint32_t timestamp; 
 | 
							uint32_t timestamp; 
 | 
				
			||||||
		uint8_t padding[15];
 | 
							uint8_t padding[15];
 | 
				
			||||||
		uint8_t signature[40];
 | 
							uint8_t signature[40];
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -26,7 +26,7 @@ namespace i2p
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	void RouterContext::CreateNewRouter ()
 | 
						void RouterContext::CreateNewRouter ()
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		i2p::data::RouterIdentity ident;
 | 
							i2p::data::Identity ident;
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
 | 
							CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
 | 
				
			||||||
		dh.GenerateKeyPair(m_Rnd, m_PrivateKey, ident.publicKey);
 | 
							dh.GenerateKeyPair(m_Rnd, m_PrivateKey, ident.publicKey);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -22,7 +22,7 @@ namespace i2p
 | 
				
			||||||
			const uint8_t * GetSigningPrivateKey () const;
 | 
								const uint8_t * GetSigningPrivateKey () const;
 | 
				
			||||||
			const uint8_t * GetLeaseSetPrivateKey () const { return m_LeaseSetPrivateKey; };
 | 
								const uint8_t * GetLeaseSetPrivateKey () const { return m_LeaseSetPrivateKey; };
 | 
				
			||||||
			const uint8_t * GetLeaseSetPublicKey () const { return m_LeaseSetPublicKey; };
 | 
								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; };	
 | 
								CryptoPP::RandomNumberGenerator& GetRandomNumberGenerator () { return m_Rnd; };	
 | 
				
			||||||
			
 | 
								
 | 
				
			||||||
			void Sign (uint8_t * buf, int len, uint8_t * signature);
 | 
								void Sign (uint8_t * buf, int len, uint8_t * signature);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -30,7 +30,7 @@ namespace data
 | 
				
			||||||
		ReadFromBuffer ();
 | 
							ReadFromBuffer ();
 | 
				
			||||||
	}	
 | 
						}	
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	void RouterInfo::SetRouterIdentity (const RouterIdentity& identity)
 | 
						void RouterInfo::SetRouterIdentity (const Identity& identity)
 | 
				
			||||||
	{	
 | 
						{	
 | 
				
			||||||
		m_RouterIdentity = identity;
 | 
							m_RouterIdentity = identity;
 | 
				
			||||||
		CryptoPP::SHA256().CalculateDigest(m_IdentHash, (uint8_t *)&m_RouterIdentity, sizeof (m_RouterIdentity));
 | 
							CryptoPP::SHA256().CalculateDigest(m_IdentHash, (uint8_t *)&m_RouterIdentity, sizeof (m_RouterIdentity));
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										19
									
								
								RouterInfo.h
									
										
									
									
									
								
							
							
						
						
									
										19
									
								
								RouterInfo.h
									
										
									
									
									
								
							| 
						 | 
					@ -12,18 +12,7 @@
 | 
				
			||||||
namespace i2p
 | 
					namespace i2p
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
namespace data
 | 
					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
 | 
						class RouterInfo: public RoutingDestination
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		public:
 | 
							public:
 | 
				
			||||||
| 
						 | 
					@ -49,8 +38,8 @@ namespace data
 | 
				
			||||||
			RouterInfo (const RouterInfo& ) = default;
 | 
								RouterInfo (const RouterInfo& ) = default;
 | 
				
			||||||
			RouterInfo (const uint8_t * buf, int len);
 | 
								RouterInfo (const uint8_t * buf, int len);
 | 
				
			||||||
			
 | 
								
 | 
				
			||||||
			const RouterIdentity& GetRouterIdentity () const { return m_RouterIdentity; };
 | 
								const Identity& GetRouterIdentity () const { return m_RouterIdentity; };
 | 
				
			||||||
			void SetRouterIdentity (const RouterIdentity& identity);
 | 
								void SetRouterIdentity (const Identity& identity);
 | 
				
			||||||
			const char * GetIdentHashBase64 () const { return m_IdentHashBase64; };
 | 
								const char * GetIdentHashBase64 () const { return m_IdentHashBase64; };
 | 
				
			||||||
			const char * GetIdentHashAbbreviation () const { return m_IdentHashAbbreviation; };
 | 
								const char * GetIdentHashAbbreviation () const { return m_IdentHashAbbreviation; };
 | 
				
			||||||
			uint64_t GetTimestamp () const { return m_Timestamp; };
 | 
								uint64_t GetTimestamp () const { return m_Timestamp; };
 | 
				
			||||||
| 
						 | 
					@ -88,7 +77,7 @@ namespace data
 | 
				
			||||||
			
 | 
								
 | 
				
			||||||
		private:
 | 
							private:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			RouterIdentity m_RouterIdentity;
 | 
								Identity m_RouterIdentity;
 | 
				
			||||||
			IdentHash m_IdentHash;
 | 
								IdentHash m_IdentHash;
 | 
				
			||||||
			char m_IdentHashBase64[48], m_IdentHashAbbreviation[5];
 | 
								char m_IdentHashBase64[48], m_IdentHashAbbreviation[5];
 | 
				
			||||||
			char m_Buffer[2048];
 | 
								char m_Buffer[2048];
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3,13 +3,20 @@
 | 
				
			||||||
#include <cryptopp/gzip.h>
 | 
					#include <cryptopp/gzip.h>
 | 
				
			||||||
#include "Log.h"
 | 
					#include "Log.h"
 | 
				
			||||||
#include "RouterInfo.h"
 | 
					#include "RouterInfo.h"
 | 
				
			||||||
 | 
					#include "RouterContext.h"
 | 
				
			||||||
#include "Streaming.h"
 | 
					#include "Streaming.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace i2p
 | 
					namespace i2p
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
namespace stream
 | 
					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;
 | 
							const uint8_t * end = buf + len;
 | 
				
			||||||
		buf += 4; // sendStreamID
 | 
							buf += 4; // sendStreamID
 | 
				
			||||||
| 
						 | 
					@ -42,15 +49,45 @@ namespace stream
 | 
				
			||||||
		if (flags & PACKET_FLAG_FROM_INCLUDED)
 | 
							if (flags & PACKET_FLAG_FROM_INCLUDED)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			LogPrint ("From identity");
 | 
								LogPrint ("From identity");
 | 
				
			||||||
			optionalData += sizeof (i2p::data::RouterIdentity);
 | 
								optionalData += sizeof (i2p::data::Identity);
 | 
				
			||||||
		}	
 | 
							}	
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// we have reached payload section
 | 
							// we have reached payload section
 | 
				
			||||||
		std::string str((const char *)buf, end-buf);
 | 
							std::string str((const char *)buf, end-buf);
 | 
				
			||||||
		LogPrint ("Payload: ", str);
 | 
							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)
 | 
						void HandleDataMessage (i2p::data::IdentHash * destination, const uint8_t * buf, size_t len)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		uint32_t length = be32toh (*(uint32_t *)buf);
 | 
							uint32_t length = be32toh (*(uint32_t *)buf);
 | 
				
			||||||
| 
						 | 
					@ -63,12 +100,32 @@ namespace stream
 | 
				
			||||||
			decompressor.Put (buf, length);
 | 
								decompressor.Put (buf, length);
 | 
				
			||||||
			decompressor.MessageEnd();
 | 
								decompressor.MessageEnd();
 | 
				
			||||||
			uint8_t uncompressed[2048];
 | 
								uint8_t uncompressed[2048];
 | 
				
			||||||
			int uncomressedSize = decompressor.MaxRetrievable ();
 | 
								int uncompressedSize = decompressor.MaxRetrievable ();
 | 
				
			||||||
			decompressor.Get (uncompressed, uncomressedSize);
 | 
								decompressor.Get (uncompressed, uncompressedSize);
 | 
				
			||||||
			// then forward to streaming engine
 | 
								// then forward to streaming engine
 | 
				
			||||||
 | 
								// TODO: we have onle one destination, might be more
 | 
				
			||||||
 | 
								m_SharedLocalDestination.HandleNextPacket (uncompressed, uncompressedSize);
 | 
				
			||||||
		}	
 | 
							}	
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
			LogPrint ("Data: protocol ", buf[9], " is not supported");
 | 
								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
									
								
								Streaming.h
									
										
									
									
									
								
							
							
						
						
									
										25
									
								
								Streaming.h
									
										
									
									
									
								
							| 
						 | 
					@ -2,7 +2,9 @@
 | 
				
			||||||
#define STREAMING_H__
 | 
					#define STREAMING_H__
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <inttypes.h>
 | 
					#include <inttypes.h>
 | 
				
			||||||
 | 
					#include <map>
 | 
				
			||||||
#include "LeaseSet.h"
 | 
					#include "LeaseSet.h"
 | 
				
			||||||
 | 
					#include "I2NPProtocol.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace i2p
 | 
					namespace i2p
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					@ -20,15 +22,36 @@ namespace stream
 | 
				
			||||||
	const uint16_t PACKET_FLAG_ECHO = 0x0200;
 | 
						const uint16_t PACKET_FLAG_ECHO = 0x0200;
 | 
				
			||||||
	const uint16_t PACKET_FLAG_NO_ACK = 0x0400;
 | 
						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
 | 
						class StreamingDestination
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		public:
 | 
							public:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								Stream * CreateNewStream (const i2p::data::IdentHash& destination);
 | 
				
			||||||
			void HandleNextPacket (const uint8_t * buf, size_t len);
 | 
								void HandleNextPacket (const uint8_t * buf, size_t len);
 | 
				
			||||||
	};	
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							private:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								std::map<uint32_t, Stream *> m_Streams;
 | 
				
			||||||
 | 
						};	
 | 
				
			||||||
 | 
						
 | 
				
			||||||
	// assuming data is I2CP message
 | 
						// assuming data is I2CP message
 | 
				
			||||||
	void HandleDataMessage (i2p::data::IdentHash * destination, const uint8_t * buf, size_t len);
 | 
						void HandleDataMessage (i2p::data::IdentHash * destination, const uint8_t * buf, size_t len);
 | 
				
			||||||
 | 
						I2NPMessage * CreateDataMessage (Stream * s, uint8_t * payload, size_t len);
 | 
				
			||||||
}		
 | 
					}		
 | 
				
			||||||
}	
 | 
					}	
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue