1 /*
2 * Copyright (c) 2013-2021, The PurpleI2P Project
3 *
4 * This file is part of Purple i2pd project and licensed under BSD3
5 *
6 * See full license text in LICENSE file at top of project tree
7 */
8 
9 #ifndef ROUTER_CONTEXT_H__
10 #define ROUTER_CONTEXT_H__
11 
12 #include <inttypes.h>
13 #include <string>
14 #include <memory>
15 #include <mutex>
16 #include <chrono>
17 #include <boost/asio.hpp>
18 #include "Identity.h"
19 #include "RouterInfo.h"
20 #include "Garlic.h"
21 
22 namespace i2p
23 {
24 namespace garlic
25 {
26 	class RouterIncomingRatchetSession;
27 }
28 
29 	const char ROUTER_INFO[] = "router.info";
30 	const char ROUTER_KEYS[] = "router.keys";
31 	const char NTCP2_KEYS[] = "ntcp2.keys";
32 	const int ROUTER_INFO_UPDATE_INTERVAL = 1800; // 30 minutes
33 
34 	enum RouterStatus
35 	{
36 		eRouterStatusOK = 0,
37 		eRouterStatusTesting = 1,
38 		eRouterStatusFirewalled = 2,
39 		eRouterStatusError = 3,
40 		eRouterStatusUnknown = 4,
41 		eRouterStatusProxy = 5,
42 		eRouterStatusMesh = 6
43 	};
44 
45 	enum RouterError
46 	{
47 		eRouterErrorNone = 0,
48 		eRouterErrorClockSkew = 1,
49 		eRouterErrorOffline = 2,
50 		eRouterErrorSymmetricNAT = 3
51 	};
52 
53 	class RouterContext: public i2p::garlic::GarlicDestination
54 	{
55 		private:
56 
57 			struct NTCP2PrivateKeys
58 			{
59 				uint8_t staticPublicKey[32];
60 				uint8_t staticPrivateKey[32];
61 				uint8_t iv[16];
62 			};
63 
64 		public:
65 
66 			RouterContext ();
67 			void Init ();
68 
GetPrivateKeys()69 			const i2p::data::PrivateKeys& GetPrivateKeys () const { return m_Keys; };
GetRouterInfo()70 			i2p::data::RouterInfo& GetRouterInfo () { return m_RouterInfo; };
GetSharedRouterInfo()71 			std::shared_ptr<i2p::data::RouterInfo> GetSharedRouterInfo ()
72 			{
73 				return std::shared_ptr<i2p::data::RouterInfo> (&m_RouterInfo,
74 					[](i2p::data::RouterInfo *) {});
75 			}
GetSharedDestination()76 			std::shared_ptr<i2p::garlic::GarlicDestination> GetSharedDestination ()
77 			{
78 				return std::shared_ptr<i2p::garlic::GarlicDestination> (this,
79 					[](i2p::garlic::GarlicDestination *) {});
80 			}
GetNTCP2StaticPublicKey()81 			const uint8_t * GetNTCP2StaticPublicKey () const { return m_NTCP2Keys ? m_NTCP2Keys->staticPublicKey : nullptr; };
GetNTCP2StaticPrivateKey()82 			const uint8_t * GetNTCP2StaticPrivateKey () const { return m_NTCP2Keys ? m_NTCP2Keys->staticPrivateKey : nullptr; };
GetNTCP2IV()83 			const uint8_t * GetNTCP2IV () const { return m_NTCP2Keys ? m_NTCP2Keys->iv : nullptr; };
84 			i2p::crypto::X25519Keys& GetStaticKeys ();
85 
86 			uint32_t GetUptime () const; // in seconds
GetLastUpdateTime()87 			uint64_t GetLastUpdateTime () const { return m_LastUpdateTime; };
GetBandwidthLimit()88 			uint64_t GetBandwidthLimit () const { return m_BandwidthLimit; };
GetTransitBandwidthLimit()89 			uint64_t GetTransitBandwidthLimit () const { return (m_BandwidthLimit*m_ShareRatio)/100LL; };
GetStatus()90 			RouterStatus GetStatus () const { return m_Status; };
91 			void SetStatus (RouterStatus status);
GetError()92 			RouterError GetError () const { return m_Error; };
SetError(RouterError error)93 			void SetError (RouterError error) { m_Status = eRouterStatusError; m_Error = error; };
GetStatusV6()94 			RouterStatus GetStatusV6 () const { return m_StatusV6; };
95 			void SetStatusV6 (RouterStatus status);
GetNetID()96 			int GetNetID () const { return m_NetID; };
SetNetID(int netID)97 			void SetNetID (int netID) { m_NetID = netID; };
98 			bool DecryptTunnelBuildRecord (const uint8_t * encrypted, uint8_t * data);
99 			bool DecryptTunnelShortRequestRecord (const uint8_t * encrypted, uint8_t * data);
100 
101 			void UpdatePort (int port); // called from Daemon
102 			void UpdateAddress (const boost::asio::ip::address& host); // called from SSU or Daemon
103 			void PublishNTCP2Address (int port, bool publish, bool v4, bool v6, bool ygg);
104 			void UpdateNTCP2Address (bool enable);
105 			void RemoveNTCPAddress (bool v4only = true); // delete NTCP address for older routers. TODO: remove later
106 			bool AddIntroducer (const i2p::data::RouterInfo::Introducer& introducer);
107 			void RemoveIntroducer (const boost::asio::ip::udp::endpoint& e);
108 			bool IsUnreachable () const;
109 			void SetUnreachable (bool v4, bool v6);
110 			void SetReachable (bool v4, bool v6);
IsFloodfill()111 			bool IsFloodfill () const { return m_IsFloodfill; };
112 			void SetFloodfill (bool floodfill);
113 			void SetFamily (const std::string& family);
114 			std::string GetFamily () const;
115 			void SetBandwidth (int limit); /* in kilobytes */
116 			void SetBandwidth (char L); /* by letter */
117 			void SetShareRatio (int percents); // 0 - 100
AcceptsTunnels()118 			bool AcceptsTunnels () const { return m_AcceptsTunnels; };
SetAcceptsTunnels(bool acceptsTunnels)119 			void SetAcceptsTunnels (bool acceptsTunnels) { m_AcceptsTunnels = acceptsTunnels; };
SupportsV6()120 			bool SupportsV6 () const { return m_RouterInfo.IsV6 (); };
SupportsV4()121 			bool SupportsV4 () const { return m_RouterInfo.IsV4 (); };
SupportsMesh()122 			bool SupportsMesh () const { return m_RouterInfo.IsMesh (); };
123 			void SetSupportsV6 (bool supportsV6);
124 			void SetSupportsV4 (bool supportsV4);
125 			void SetSupportsMesh (bool supportsmesh, const boost::asio::ip::address_v6& host);
GetCurrentNoiseState()126 			i2p::crypto::NoiseSymmetricState& GetCurrentNoiseState () { return m_CurrentNoiseState; };
127 
128 			void UpdateNTCP2V6Address (const boost::asio::ip::address& host); // called from Daemon. TODO: remove
129 			void UpdateStats ();
130 			void UpdateTimestamp (uint64_t ts); // in seconds, called from NetDb before publishing
131 			void CleanupDestination (); // garlic destination
132 
133 			// implements LocalDestination
GetIdentity()134 			std::shared_ptr<const i2p::data::IdentityEx> GetIdentity () const { return m_Keys.GetPublic (); };
135 			bool Decrypt (const uint8_t * encrypted, uint8_t * data, i2p::data::CryptoKeyType preferredCrypto) const;
Sign(const uint8_t * buf,int len,uint8_t * signature)136 			void Sign (const uint8_t * buf, int len, uint8_t * signature) const { m_Keys.Sign (buf, len, signature); };
SetLeaseSetUpdated()137 			void SetLeaseSetUpdated () {};
138 
139 			// implements GarlicDestination
GetLeaseSet()140 			std::shared_ptr<const i2p::data::LocalLeaseSet> GetLeaseSet () { return nullptr; };
141 			std::shared_ptr<i2p::tunnel::TunnelPool> GetTunnelPool () const;
142 
143 			// override GarlicDestination
144 			void ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg);
145 			void ProcessDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg);
146 
147 		protected:
148 
149 			// implements GarlicDestination
150 			void HandleI2NPMessage (const uint8_t * buf, size_t len);
151 			bool HandleCloveI2NPMessage (I2NPMessageType typeID, const uint8_t * payload, size_t len, uint32_t msgID);
152 
153 		private:
154 
155 			void CreateNewRouter ();
156 			void NewRouterInfo ();
157 			void UpdateRouterInfo ();
158 			void NewNTCP2Keys ();
159 			bool Load ();
160 			void SaveKeys ();
161 
162 			bool DecryptECIESTunnelBuildRecord (const uint8_t * encrypted, uint8_t * data, size_t clearTextSize);
163 
164 		private:
165 
166 			i2p::data::RouterInfo m_RouterInfo;
167 			i2p::data::PrivateKeys m_Keys;
168 			std::shared_ptr<i2p::crypto::CryptoKeyDecryptor> m_Decryptor, m_TunnelDecryptor;
169 			std::shared_ptr<i2p::garlic::RouterIncomingRatchetSession> m_ECIESSession;
170 			uint64_t m_LastUpdateTime; // in seconds
171 			bool m_AcceptsTunnels, m_IsFloodfill;
172 			std::chrono::time_point<std::chrono::steady_clock> m_StartupTime;
173 			uint64_t m_BandwidthLimit; // allowed bandwidth
174 			int m_ShareRatio;
175 			RouterStatus m_Status, m_StatusV6;
176 			RouterError m_Error;
177 			int m_NetID;
178 			std::mutex m_GarlicMutex;
179 			std::unique_ptr<NTCP2PrivateKeys> m_NTCP2Keys;
180 			std::unique_ptr<i2p::crypto::X25519Keys> m_StaticKeys;
181 			// for ECIESx25519
182 			i2p::crypto::NoiseSymmetricState m_InitialNoiseState, m_CurrentNoiseState;
183 	};
184 
185 	extern RouterContext context;
186 }
187 
188 #endif
189