/ libi2pd / SSU2OutOfSession.h
SSU2OutOfSession.h
 1  /*
 2  * Copyright (c) 2024, 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 SSU2_OUT_OF_SESSION_H__
10  #define SSU2_OUT_OF_SESSION_H__
11  
12  #include <vector>
13  #include "SSU2Session.h"
14  
15  namespace i2p
16  {
17  namespace transport
18  {
19  	const int SSU2_PEER_TEST_RESEND_INTERVAL = 3000; // in milliseconds
20  	const int SSU2_PEER_TEST_RESEND_INTERVAL_VARIANCE = 2000; // in milliseconds
21  	const int SSU2_PEER_TEST_MAX_NUM_RESENDS = 3;
22  	
23  	class SSU2PeerTestSession: public SSU2Session // for PeerTest msgs 5,6,7
24  	{
25  		public:
26  
27  			SSU2PeerTestSession (SSU2Server& server, uint64_t sourceConnID, uint64_t destConnID);
28  
29  			uint8_t GetMsgNumReceived () const { return m_MsgNumReceived; }	
30  			bool IsConnectedRecently () const { return m_IsConnectedRecently; }
31  			void SetStatusChanged () { m_IsStatusChanged = true; }
32  			
33  			void SendPeerTest (uint8_t msg, const uint8_t * signedData, size_t signedDataLen, 
34  				std::shared_ptr<const i2p::data::RouterInfo::Address> addr, bool delayed = false);
35  			bool ProcessPeerTest (uint8_t * buf, size_t len) override;
36  			void Connect () override; // outgoing
37  			bool ProcessFirstIncomingMessage (uint64_t connID, uint8_t * buf, size_t len) override; // incoming
38  			
39  		private:
40  
41  			void SendPeerTest (uint8_t msg, const uint8_t * signedData, size_t signedDataLen, bool delayed = false); // PeerTest message
42  			void SendPeerTest (uint8_t msg); // send or resend m_SignedData
43  			void HandlePeerTest (const uint8_t * buf, size_t len) override;
44  			void HandleAddress (const uint8_t * buf, size_t len) override;
45  
46  			void ScheduleResend (uint8_t msg);
47  			
48  		private:
49  
50  			uint8_t m_MsgNumReceived, m_NumResends;
51  			bool m_IsConnectedRecently, m_IsStatusChanged;
52  			std::vector<uint8_t> m_SignedData; // for resends
53  			boost::asio::deadline_timer m_PeerTestResendTimer;
54  			boost::asio::ip::udp::endpoint m_OurEndpoint; // as seen by peer
55  	};	
56  
57  	const int SSU2_HOLE_PUNCH_RESEND_INTERVAL = 1000; // in milliseconds
58  	const int SSU2_HOLE_PUNCH_RESEND_INTERVAL_VARIANCE = 500; // in milliseconds
59  	const int SSU2_HOLE_PUNCH_MAX_NUM_RESENDS = 3;
60  	
61  	class SSU2HolePunchSession: public SSU2Session // Charlie
62  	{
63  		public:
64  
65  			SSU2HolePunchSession (SSU2Server& server, uint32_t nonce, const boost::asio::ip::udp::endpoint& remoteEndpoint,
66  				std::shared_ptr<const i2p::data::RouterInfo::Address> addr);
67  
68  			void SendHolePunch (const uint8_t * relayResponseBlock, size_t relayResponseBlockLen);
69  
70  			bool ProcessFirstIncomingMessage (uint64_t connID, uint8_t * buf, size_t len) override; // SessionRequest
71  			
72  		private:
73  			
74  			void SendHolePunch ();
75  			void ScheduleResend ();
76  			
77  		private:
78  
79  			int m_NumResends;
80  			std::vector<uint8_t> m_RelayResponseBlock;
81  			boost::asio::deadline_timer m_HolePunchResendTimer;
82  	};	
83  }
84  }
85  	
86  #endif