|
|
@ -14,8 +14,8 @@ namespace i2p |
|
|
|
namespace ssu |
|
|
|
namespace ssu |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
SSUSession::SSUSession (SSUServer * server, const boost::asio::ip::udp::endpoint& remoteEndpoint, |
|
|
|
SSUSession::SSUSession (SSUServer * server, boost::asio::ip::udp::endpoint& remoteEndpoint, |
|
|
|
i2p::data::RouterInfo * router): m_Server (server), m_RemoteEndpoint (remoteEndpoint), |
|
|
|
const i2p::data::RouterInfo * router): m_Server (server), m_RemoteEndpoint (remoteEndpoint), |
|
|
|
m_RemoteRouter (router), m_State (eSessionStateUnknown) |
|
|
|
m_RemoteRouter (router), m_State (eSessionStateUnknown) |
|
|
|
{ |
|
|
|
{ |
|
|
|
} |
|
|
|
} |
|
|
@ -47,7 +47,8 @@ namespace ssu |
|
|
|
{ |
|
|
|
{ |
|
|
|
switch (m_State) |
|
|
|
switch (m_State) |
|
|
|
{ |
|
|
|
{ |
|
|
|
case eSessionStateEstablised: |
|
|
|
case eSessionStateConfirmedSent: |
|
|
|
|
|
|
|
case eSessionStateEstablished: |
|
|
|
// most common case
|
|
|
|
// most common case
|
|
|
|
ProcessMessage (buf, len); |
|
|
|
ProcessMessage (buf, len); |
|
|
|
break; |
|
|
|
break; |
|
|
@ -64,6 +65,21 @@ namespace ssu |
|
|
|
// session confirmed
|
|
|
|
// session confirmed
|
|
|
|
ProcessSessionConfirmed (buf, len); |
|
|
|
ProcessSessionConfirmed (buf, len); |
|
|
|
break; |
|
|
|
break; |
|
|
|
|
|
|
|
case eSessionRelayRequestSent: |
|
|
|
|
|
|
|
// relay response
|
|
|
|
|
|
|
|
ProcessRelayResponse (buf,len); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case eSessionRelayResponseReceived: |
|
|
|
|
|
|
|
// HolePunch received
|
|
|
|
|
|
|
|
LogPrint ("SSU HolePuch of ", len, " bytes received"); |
|
|
|
|
|
|
|
m_State = eSessionStateEstablished; |
|
|
|
|
|
|
|
Established (); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case eSessionRelayRequestReceived: |
|
|
|
|
|
|
|
// HolePunch
|
|
|
|
|
|
|
|
m_State = eSessionStateUnknown; |
|
|
|
|
|
|
|
Connect (); |
|
|
|
|
|
|
|
break; |
|
|
|
default: |
|
|
|
default: |
|
|
|
LogPrint ("SSU state not implemented yet"); |
|
|
|
LogPrint ("SSU state not implemented yet"); |
|
|
|
} |
|
|
|
} |
|
|
@ -85,24 +101,43 @@ namespace ssu |
|
|
|
case PAYLOAD_TYPE_TEST: |
|
|
|
case PAYLOAD_TYPE_TEST: |
|
|
|
LogPrint ("SSU test received"); |
|
|
|
LogPrint ("SSU test received"); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case PAYLOAD_TYPE_SESSION_DESTROY: |
|
|
|
case PAYLOAD_TYPE_SESSION_DESTROYED: |
|
|
|
|
|
|
|
{ |
|
|
|
LogPrint ("SSU session destroy received"); |
|
|
|
LogPrint ("SSU session destroy received"); |
|
|
|
|
|
|
|
if (m_Server) |
|
|
|
|
|
|
|
m_Server->DeleteSession (this); // delete this
|
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
case PAYLOAD_TYPE_RELAY_INTRO: |
|
|
|
|
|
|
|
LogPrint ("SSU relay intro received"); |
|
|
|
|
|
|
|
// TODO:
|
|
|
|
break; |
|
|
|
break; |
|
|
|
default: |
|
|
|
default: |
|
|
|
LogPrint ("Unexpected SSU payload type ", (int)payloadType); |
|
|
|
LogPrint ("Unexpected SSU payload type ", (int)payloadType); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// TODO: try intro key as well
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint ("MAC key failed. Trying intro key"); |
|
|
|
|
|
|
|
auto introKey = GetIntroKey (); |
|
|
|
|
|
|
|
if (introKey && Validate (buf, len, introKey)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Decrypt (buf, len, introKey); |
|
|
|
|
|
|
|
SSUHeader * header = (SSUHeader *)buf; |
|
|
|
|
|
|
|
LogPrint ("Unexpected SSU payload type ", (int)(header->flag >> 4)); |
|
|
|
|
|
|
|
// TODO:
|
|
|
|
|
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
LogPrint ("MAC verifcation failed"); |
|
|
|
LogPrint ("MAC verifcation failed"); |
|
|
|
|
|
|
|
m_State = eSessionStateUnknown; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SSUSession::ProcessSessionRequest (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint) |
|
|
|
void SSUSession::ProcessSessionRequest (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint) |
|
|
|
{ |
|
|
|
{ |
|
|
|
LogPrint ("Process session request"); |
|
|
|
LogPrint ("Process session request"); |
|
|
|
// use our intro key
|
|
|
|
// use our intro key
|
|
|
|
if (ProcessIntroKeyEncryptedMessage (PAYLOAD_TYPE_SESSION_REQUEST, |
|
|
|
if (ProcessIntroKeyEncryptedMessage (PAYLOAD_TYPE_SESSION_REQUEST, buf, len)) |
|
|
|
i2p::context.GetRouterInfo (), buf, len)) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
m_State = eSessionStateRequestReceived; |
|
|
|
m_State = eSessionStateRequestReceived; |
|
|
|
LogPrint ("Session request received"); |
|
|
|
LogPrint ("Session request received"); |
|
|
@ -121,17 +156,42 @@ namespace ssu |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// use remote intro key
|
|
|
|
// use remote intro key
|
|
|
|
if (ProcessIntroKeyEncryptedMessage (PAYLOAD_TYPE_SESSION_CREATED, *m_RemoteRouter, buf, len)) |
|
|
|
if (ProcessIntroKeyEncryptedMessage (PAYLOAD_TYPE_SESSION_CREATED, buf, len)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_State = eSessionStateCreatedReceived; |
|
|
|
m_State = eSessionStateCreatedReceived; |
|
|
|
LogPrint ("Session created received"); |
|
|
|
LogPrint ("Session created received"); |
|
|
|
uint8_t * ourAddress = buf + sizeof (SSUHeader) + 257; |
|
|
|
uint8_t signedData[532]; // x,y, our IP, our port, remote IP, remote port, relayTag, signed on time
|
|
|
|
boost::asio::ip::address_v4 ourIP (be32toh (*(uint32_t* )(ourAddress))); |
|
|
|
uint8_t * payload = buf + sizeof (SSUHeader); |
|
|
|
uint16_t ourPort = be16toh (*(uint16_t *)(ourAddress + 4)); |
|
|
|
uint8_t * y = payload; |
|
|
|
|
|
|
|
memcpy (signedData, i2p::context.GetRouterIdentity ().publicKey, 256); // x
|
|
|
|
|
|
|
|
memcpy (signedData + 256, y, 256); // y
|
|
|
|
|
|
|
|
payload += 256; |
|
|
|
|
|
|
|
payload += 1; // size, assume 4
|
|
|
|
|
|
|
|
uint8_t * ourAddress = payload; |
|
|
|
|
|
|
|
boost::asio::ip::address_v4 ourIP (be32toh (*(uint32_t* )ourAddress)); |
|
|
|
|
|
|
|
payload += 4; // address
|
|
|
|
|
|
|
|
uint16_t ourPort = be16toh (*(uint16_t *)payload); |
|
|
|
|
|
|
|
payload += 2; // port
|
|
|
|
|
|
|
|
memcpy (signedData + 512, ourAddress, 6); // our IP and port
|
|
|
|
LogPrint ("Our external address is ", ourIP.to_string (), ":", ourPort); |
|
|
|
LogPrint ("Our external address is ", ourIP.to_string (), ":", ourPort); |
|
|
|
uint32_t relayTag = be32toh (*(uint32_t *)(buf + sizeof (SSUHeader) + 263)); |
|
|
|
i2p::context.UpdateAddress (ourIP.to_string ().c_str ()); |
|
|
|
SendSessionConfirmed (buf + sizeof (SSUHeader), ourAddress, relayTag); |
|
|
|
*(uint32_t *)(signedData + 518) = htobe32 (m_RemoteEndpoint.address ().to_v4 ().to_ulong ()); // remote IP
|
|
|
|
m_State = eSessionStateEstablised; |
|
|
|
*(uint16_t *)(signedData + 522) = htobe16 (m_RemoteEndpoint.port ()); // remote port
|
|
|
|
|
|
|
|
memcpy (signedData + 524, payload, 8); // relayTag and signed on time
|
|
|
|
|
|
|
|
uint32_t relayTag = be32toh (*(uint32_t *)payload); |
|
|
|
|
|
|
|
payload += 4; // relayTag
|
|
|
|
|
|
|
|
payload += 4; // signed on time
|
|
|
|
|
|
|
|
// decrypt DSA signature
|
|
|
|
|
|
|
|
m_Decryption.SetKeyWithIV (m_SessionKey, 32, ((SSUHeader *)buf)->iv); |
|
|
|
|
|
|
|
m_Decryption.ProcessData (payload, payload, 48); |
|
|
|
|
|
|
|
// verify
|
|
|
|
|
|
|
|
CryptoPP::DSA::PublicKey pubKey; |
|
|
|
|
|
|
|
pubKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag, CryptoPP::Integer (m_RemoteRouter->GetRouterIdentity ().signingKey, 128)); |
|
|
|
|
|
|
|
CryptoPP::DSA::Verifier verifier (pubKey); |
|
|
|
|
|
|
|
if (!verifier.VerifyMessage (signedData, 532, payload, 40)) |
|
|
|
|
|
|
|
LogPrint ("SSU signature verification failed"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SendSessionConfirmed (y, ourAddress, relayTag); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -146,8 +206,9 @@ namespace ssu |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_State = eSessionStateConfirmedReceived; |
|
|
|
m_State = eSessionStateConfirmedReceived; |
|
|
|
LogPrint ("Session confirmed received"); |
|
|
|
LogPrint ("Session confirmed received"); |
|
|
|
// TODO:
|
|
|
|
m_State = eSessionStateEstablished; |
|
|
|
m_State = eSessionStateEstablised; |
|
|
|
SendI2NPMessage (CreateDeliveryStatusMsg (0)); |
|
|
|
|
|
|
|
Established (); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
LogPrint ("Unexpected payload type ", (int)(header->flag >> 4)); |
|
|
|
LogPrint ("Unexpected payload type ", (int)(header->flag >> 4)); |
|
|
@ -158,10 +219,10 @@ namespace ssu |
|
|
|
|
|
|
|
|
|
|
|
void SSUSession::SendSessionRequest () |
|
|
|
void SSUSession::SendSessionRequest () |
|
|
|
{ |
|
|
|
{ |
|
|
|
auto address = m_RemoteRouter ? m_RemoteRouter->GetSSUAddress () : nullptr; |
|
|
|
auto introKey = GetIntroKey (); |
|
|
|
if (!address) |
|
|
|
if (!introKey) |
|
|
|
{ |
|
|
|
{ |
|
|
|
LogPrint ("Missing remote SSU address"); |
|
|
|
LogPrint ("SSU is not supported"); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -174,18 +235,50 @@ namespace ssu |
|
|
|
uint8_t iv[16]; |
|
|
|
uint8_t iv[16]; |
|
|
|
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator (); |
|
|
|
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator (); |
|
|
|
rnd.GenerateBlock (iv, 16); // random iv
|
|
|
|
rnd.GenerateBlock (iv, 16); // random iv
|
|
|
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_SESSION_REQUEST, buf, 304, address->key, iv, address->key); |
|
|
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_SESSION_REQUEST, buf, 304, introKey, iv, introKey); |
|
|
|
|
|
|
|
|
|
|
|
m_State = eSessionStateRequestSent; |
|
|
|
m_State = eSessionStateRequestSent; |
|
|
|
m_Server->Send (buf, 304, m_RemoteEndpoint); |
|
|
|
m_Server->Send (buf, 304, m_RemoteEndpoint); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SSUSession::SendSessionCreated (const uint8_t * x) |
|
|
|
void SSUSession::SendRelayRequest (const i2p::data::RouterInfo::Introducer& introducer) |
|
|
|
{ |
|
|
|
{ |
|
|
|
auto address = m_RemoteRouter ? m_RemoteRouter->GetSSUAddress () : nullptr; |
|
|
|
auto address = i2p::context.GetRouterInfo ().GetSSUAddress (); |
|
|
|
if (!address) |
|
|
|
if (!address) |
|
|
|
{ |
|
|
|
{ |
|
|
|
LogPrint ("Missing remote SSU address"); |
|
|
|
LogPrint ("SSU is not supported"); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t buf[96 + 18]; |
|
|
|
|
|
|
|
uint8_t * payload = buf + sizeof (SSUHeader); |
|
|
|
|
|
|
|
*(uint32_t *)payload = htobe32 (introducer.iTag); |
|
|
|
|
|
|
|
payload += 4; |
|
|
|
|
|
|
|
*payload = 0; // no address
|
|
|
|
|
|
|
|
payload++; |
|
|
|
|
|
|
|
*(uint16_t *)payload = 0; // port = 0
|
|
|
|
|
|
|
|
payload += 2; |
|
|
|
|
|
|
|
*payload = 0; // challenge
|
|
|
|
|
|
|
|
payload++; |
|
|
|
|
|
|
|
memcpy (payload, address->key, 32); |
|
|
|
|
|
|
|
payload += 32; |
|
|
|
|
|
|
|
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator (); |
|
|
|
|
|
|
|
*(uint32_t *)payload = htobe32 (rnd.GenerateWord32 ()); // nonce
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t iv[16]; |
|
|
|
|
|
|
|
rnd.GenerateBlock (iv, 16); // random iv
|
|
|
|
|
|
|
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_RELAY_REQUEST, buf, 96, introducer.iKey, iv, introducer.iKey); |
|
|
|
|
|
|
|
m_State = eSessionRelayRequestSent; |
|
|
|
|
|
|
|
m_Server->Send (buf, 96, m_RemoteEndpoint); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SSUSession::SendSessionCreated (const uint8_t * x) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
auto introKey = GetIntroKey (); |
|
|
|
|
|
|
|
auto address = i2p::context.GetRouterInfo ().GetSSUAddress (); |
|
|
|
|
|
|
|
if (!introKey || !address) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint ("SSU is not supported"); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
uint8_t signedData[532]; // x,y, remote IP, remote port, our IP, our port, relayTag, signed on time
|
|
|
|
uint8_t signedData[532]; // x,y, remote IP, remote port, our IP, our port, relayTag, signed on time
|
|
|
@ -203,8 +296,8 @@ namespace ssu |
|
|
|
*(uint16_t *)(payload) = htobe16 (m_RemoteEndpoint.port ()); |
|
|
|
*(uint16_t *)(payload) = htobe16 (m_RemoteEndpoint.port ()); |
|
|
|
payload += 2; |
|
|
|
payload += 2; |
|
|
|
memcpy (signedData + 512, payload - 6, 6); // remote endpoint IP and port
|
|
|
|
memcpy (signedData + 512, payload - 6, 6); // remote endpoint IP and port
|
|
|
|
*(uint32_t *)(signedData + 518) = htobe32 (m_Server->GetEndpoint ().address ().to_v4 ().to_ulong ()); // our IP
|
|
|
|
*(uint32_t *)(signedData + 518) = htobe32 (address->host.to_v4 ().to_ulong ()); // our IP
|
|
|
|
*(uint16_t *)(signedData + 522) = htobe16 (m_Server->GetEndpoint ().port ()); // our port
|
|
|
|
*(uint16_t *)(signedData + 522) = htobe16 (address->port); // our port
|
|
|
|
*(uint32_t *)(payload) = 0; // relay tag, always 0 for now
|
|
|
|
*(uint32_t *)(payload) = 0; // relay tag, always 0 for now
|
|
|
|
payload += 4; |
|
|
|
payload += 4; |
|
|
|
*(uint32_t *)(payload) = htobe32 (i2p::util::GetSecondsSinceEpoch ()); // signed on time
|
|
|
|
*(uint32_t *)(payload) = htobe32 (i2p::util::GetSecondsSinceEpoch ()); // signed on time
|
|
|
@ -221,20 +314,13 @@ namespace ssu |
|
|
|
m_Encryption.ProcessData (payload, payload, 48); |
|
|
|
m_Encryption.ProcessData (payload, payload, 48); |
|
|
|
|
|
|
|
|
|
|
|
// encrypt message with intro key
|
|
|
|
// encrypt message with intro key
|
|
|
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_SESSION_CREATED, buf, 368, address->key, iv, address->key); |
|
|
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_SESSION_CREATED, buf, 368, introKey, iv, introKey); |
|
|
|
m_State = eSessionStateRequestSent; |
|
|
|
m_State = eSessionStateCreatedSent; |
|
|
|
m_Server->Send (buf, 368, m_RemoteEndpoint); |
|
|
|
m_Server->Send (buf, 368, m_RemoteEndpoint); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SSUSession::SendSessionConfirmed (const uint8_t * y, const uint8_t * ourAddress, uint32_t relayTag) |
|
|
|
void SSUSession::SendSessionConfirmed (const uint8_t * y, const uint8_t * ourAddress, uint32_t relayTag) |
|
|
|
{ |
|
|
|
{ |
|
|
|
auto address = m_RemoteRouter ? m_RemoteRouter->GetSSUAddress () : nullptr; |
|
|
|
|
|
|
|
if (!address) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint ("Missing remote SSU address"); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t buf[480 + 18]; |
|
|
|
uint8_t buf[480 + 18]; |
|
|
|
uint8_t * payload = buf + sizeof (SSUHeader); |
|
|
|
uint8_t * payload = buf + sizeof (SSUHeader); |
|
|
|
*payload = 1; // 1 fragment
|
|
|
|
*payload = 1; // 1 fragment
|
|
|
@ -272,16 +358,57 @@ namespace ssu |
|
|
|
m_Server->Send (buf, 480, m_RemoteEndpoint); |
|
|
|
m_Server->Send (buf, 480, m_RemoteEndpoint); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool SSUSession::ProcessIntroKeyEncryptedMessage (uint8_t expectedPayloadType, i2p::data::RouterInfo& r, uint8_t * buf, size_t len) |
|
|
|
void SSUSession::ProcessRelayResponse (uint8_t * buf, size_t len) |
|
|
|
{ |
|
|
|
{ |
|
|
|
auto address = r.GetSSUAddress (); |
|
|
|
LogPrint ("Process relay response"); |
|
|
|
if (address) |
|
|
|
auto address = i2p::context.GetRouterInfo ().GetSSUAddress (); |
|
|
|
|
|
|
|
if (!address) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// use intro key for verification and decryption
|
|
|
|
LogPrint ("SSU is not supported"); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (Validate (buf, len, address->key)) |
|
|
|
if (Validate (buf, len, address->key)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
Decrypt (buf, len, address->key); |
|
|
|
Decrypt (buf, len, address->key); |
|
|
|
SSUHeader * header = (SSUHeader *)buf; |
|
|
|
SSUHeader * header = (SSUHeader *)buf; |
|
|
|
|
|
|
|
if ((header->flag >> 4) == PAYLOAD_TYPE_RELAY_RESPONSE) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint ("Relay response received"); |
|
|
|
|
|
|
|
m_State = eSessionRelayRequestReceived; |
|
|
|
|
|
|
|
uint8_t * payload = buf + sizeof (SSUHeader); |
|
|
|
|
|
|
|
payload++; |
|
|
|
|
|
|
|
boost::asio::ip::address_v4 remoteIP (be32toh (*(uint32_t* )(payload))); |
|
|
|
|
|
|
|
payload += 4; |
|
|
|
|
|
|
|
uint16_t remotePort = be16toh (*(uint16_t *)(payload)); |
|
|
|
|
|
|
|
payload += 2; |
|
|
|
|
|
|
|
boost::asio::ip::udp::endpoint newRemoteEndpoint(remoteIP, remotePort); |
|
|
|
|
|
|
|
m_Server->ReassignSession (m_RemoteEndpoint, newRemoteEndpoint); |
|
|
|
|
|
|
|
m_RemoteEndpoint = newRemoteEndpoint; |
|
|
|
|
|
|
|
payload++; |
|
|
|
|
|
|
|
boost::asio::ip::address_v4 ourIP (be32toh (*(uint32_t* )(payload))); |
|
|
|
|
|
|
|
payload += 4; |
|
|
|
|
|
|
|
uint16_t ourPort = be16toh (*(uint16_t *)(payload)); |
|
|
|
|
|
|
|
payload += 2; |
|
|
|
|
|
|
|
LogPrint ("Our external address is ", ourIP.to_string (), ":", ourPort); |
|
|
|
|
|
|
|
i2p::context.UpdateAddress (ourIP.to_string ().c_str ()); |
|
|
|
|
|
|
|
m_State= eSessionRelayResponseReceived; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
LogPrint ("Unexpected payload type ", (int)(header->flag >> 4)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool SSUSession::ProcessIntroKeyEncryptedMessage (uint8_t expectedPayloadType, uint8_t * buf, size_t len) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
auto introKey = GetIntroKey (); |
|
|
|
|
|
|
|
if (introKey) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// use intro key for verification and decryption
|
|
|
|
|
|
|
|
if (Validate (buf, len, introKey)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Decrypt (buf, len, introKey); |
|
|
|
|
|
|
|
SSUHeader * header = (SSUHeader *)buf; |
|
|
|
if ((header->flag >> 4) == expectedPayloadType) |
|
|
|
if ((header->flag >> 4) == expectedPayloadType) |
|
|
|
{ |
|
|
|
{ |
|
|
|
CreateAESandMacKey (buf + sizeof (SSUHeader), m_SessionKey, m_MacKey); |
|
|
|
CreateAESandMacKey (buf + sizeof (SSUHeader), m_SessionKey, m_MacKey); |
|
|
@ -291,14 +418,15 @@ namespace ssu |
|
|
|
LogPrint ("Unexpected payload type ", (int)(header->flag >> 4)); |
|
|
|
LogPrint ("Unexpected payload type ", (int)(header->flag >> 4)); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
LogPrint ("MAC verifcation failed"); |
|
|
|
LogPrint ("MAC verification failed"); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
LogPrint ("SSU is not supported by ", r.GetIdentHashAbbreviation ()); |
|
|
|
LogPrint ("SSU is not supported"); |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SSUSession::FillHeaderAndEncrypt (uint8_t payloadType, uint8_t * buf, size_t len, uint8_t * aesKey, uint8_t * iv, uint8_t * macKey) |
|
|
|
void SSUSession::FillHeaderAndEncrypt (uint8_t payloadType, uint8_t * buf, size_t len, |
|
|
|
|
|
|
|
const uint8_t * aesKey, const uint8_t * iv, const uint8_t * macKey) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (len < sizeof (SSUHeader)) |
|
|
|
if (len < sizeof (SSUHeader)) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -319,7 +447,7 @@ namespace ssu |
|
|
|
i2p::crypto::HMACMD5Digest (encrypted, encryptedLen + 18, macKey, header->mac); |
|
|
|
i2p::crypto::HMACMD5Digest (encrypted, encryptedLen + 18, macKey, header->mac); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SSUSession::Decrypt (uint8_t * buf, size_t len, uint8_t * aesKey) |
|
|
|
void SSUSession::Decrypt (uint8_t * buf, size_t len, const uint8_t * aesKey) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (len < sizeof (SSUHeader)) |
|
|
|
if (len < sizeof (SSUHeader)) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -334,7 +462,7 @@ namespace ssu |
|
|
|
m_Decryption.ProcessData (encrypted, encrypted, encryptedLen); |
|
|
|
m_Decryption.ProcessData (encrypted, encrypted, encryptedLen); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool SSUSession::Validate (uint8_t * buf, size_t len, uint8_t * macKey) |
|
|
|
bool SSUSession::Validate (uint8_t * buf, size_t len, const uint8_t * macKey) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (len < sizeof (SSUHeader)) |
|
|
|
if (len < sizeof (SSUHeader)) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -357,9 +485,58 @@ namespace ssu |
|
|
|
SendSessionRequest (); |
|
|
|
SendSessionRequest (); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SSUSession::ConnectThroughIntroducer (const i2p::data::RouterInfo::Introducer& introducer) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
SendRelayRequest (introducer); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SSUSession::Close () |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
SendSesionDestroyed (); |
|
|
|
|
|
|
|
if (!m_DelayedMessages.empty ()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
for (auto it :m_DelayedMessages) |
|
|
|
|
|
|
|
delete it; |
|
|
|
|
|
|
|
m_DelayedMessages.clear (); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SSUSession::Established () |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
SendI2NPMessage (CreateDatabaseStoreMsg ()); |
|
|
|
|
|
|
|
if (!m_DelayedMessages.empty ()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
for (auto it :m_DelayedMessages) |
|
|
|
|
|
|
|
Send (it); |
|
|
|
|
|
|
|
m_DelayedMessages.clear (); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const uint8_t * SSUSession::GetIntroKey () const |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (m_RemoteRouter) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// we are client
|
|
|
|
|
|
|
|
auto address = m_RemoteRouter->GetSSUAddress (); |
|
|
|
|
|
|
|
return address ? address->key : nullptr; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// we are server
|
|
|
|
|
|
|
|
auto address = i2p::context.GetRouterInfo ().GetSSUAddress (); |
|
|
|
|
|
|
|
return address ? address->key : nullptr; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SSUSession::SendI2NPMessage (I2NPMessage * msg) |
|
|
|
void SSUSession::SendI2NPMessage (I2NPMessage * msg) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// TODO:
|
|
|
|
if (msg) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (m_State == eSessionStateEstablished) |
|
|
|
|
|
|
|
Send (msg); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
m_DelayedMessages.push_back (msg); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SSUSession::ProcessData (uint8_t * buf, size_t len) |
|
|
|
void SSUSession::ProcessData (uint8_t * buf, size_t len) |
|
|
@ -368,6 +545,28 @@ namespace ssu |
|
|
|
uint8_t flag = *buf; |
|
|
|
uint8_t flag = *buf; |
|
|
|
buf++; |
|
|
|
buf++; |
|
|
|
LogPrint ("Process SSU data flags=", (int)flag); |
|
|
|
LogPrint ("Process SSU data flags=", (int)flag); |
|
|
|
|
|
|
|
if (flag & DATA_FLAG_EXPLICIT_ACKS_INCLUDED) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// explicit ACKs
|
|
|
|
|
|
|
|
uint8_t numAcks =*buf; |
|
|
|
|
|
|
|
buf++; |
|
|
|
|
|
|
|
// TODO: process ACKs
|
|
|
|
|
|
|
|
buf += numAcks*4; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (flag & DATA_FLAG_ACK_BITFIELDS_INCLUDED) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// explicit ACK bitfields
|
|
|
|
|
|
|
|
uint8_t numBitfields =*buf; |
|
|
|
|
|
|
|
buf++; |
|
|
|
|
|
|
|
for (int i = 0; i < numBitfields; i++) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
buf += 4; // msgID
|
|
|
|
|
|
|
|
// TODO: process ACH bitfields
|
|
|
|
|
|
|
|
while (*buf & 0x80) // not last
|
|
|
|
|
|
|
|
buf++; |
|
|
|
|
|
|
|
buf++; // last byte
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
uint8_t numFragments = *buf; // number of fragments
|
|
|
|
uint8_t numFragments = *buf; // number of fragments
|
|
|
|
buf++; |
|
|
|
buf++; |
|
|
|
for (int i = 0; i < numFragments; i++) |
|
|
|
for (int i = 0; i < numFragments; i++) |
|
|
@ -383,13 +582,156 @@ namespace ssu |
|
|
|
bool isLast = fragmentInfo & 0x010000; // bit 16
|
|
|
|
bool isLast = fragmentInfo & 0x010000; // bit 16
|
|
|
|
uint8_t fragmentNum = fragmentInfo >> 17; // bits 23 - 17
|
|
|
|
uint8_t fragmentNum = fragmentInfo >> 17; // bits 23 - 17
|
|
|
|
LogPrint ("SSU data fragment ", (int)fragmentNum, " of message ", msgID, " size=", (int)fragmentSize, isLast ? " last" : " non-last"); |
|
|
|
LogPrint ("SSU data fragment ", (int)fragmentNum, " of message ", msgID, " size=", (int)fragmentSize, isLast ? " last" : " non-last"); |
|
|
|
|
|
|
|
I2NPMessage * msg = nullptr; |
|
|
|
|
|
|
|
if (fragmentNum > 0) // follow-up fragment
|
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
auto it = m_IncomleteMessages.find (msgID); |
|
|
|
|
|
|
|
if (it != m_IncomleteMessages.end ()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
msg = it->second; |
|
|
|
|
|
|
|
memcpy (msg->buf + msg->len, buf, fragmentSize); |
|
|
|
|
|
|
|
msg->len += fragmentSize; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
// TODO:
|
|
|
|
|
|
|
|
LogPrint ("Unexpected follow-on fragment ", fragmentNum, " of message ", msgID); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else // first fragment
|
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
msg = NewI2NPMessage (); |
|
|
|
|
|
|
|
memcpy (msg->GetSSUHeader (), buf, fragmentSize); |
|
|
|
|
|
|
|
msg->len += fragmentSize - sizeof (I2NPHeaderShort); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (msg) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (!fragmentNum && !isLast) |
|
|
|
|
|
|
|
m_IncomleteMessages[msgID] = msg; |
|
|
|
|
|
|
|
if (isLast) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
SendMsgAck (msgID); |
|
|
|
|
|
|
|
if (fragmentNum > 0) |
|
|
|
|
|
|
|
m_IncomleteMessages.erase (msgID); |
|
|
|
|
|
|
|
msg->FromSSU (msgID); |
|
|
|
|
|
|
|
if (m_State == eSessionStateEstablished) |
|
|
|
|
|
|
|
i2p::HandleI2NPMessage (msg); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// we expect DeliveryStatus
|
|
|
|
|
|
|
|
if (msg->GetHeader ()->typeID == eI2NPDeliveryStatus) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint ("SSU session established"); |
|
|
|
|
|
|
|
m_State = eSessionStateEstablished; |
|
|
|
|
|
|
|
Established (); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
LogPrint ("SSU unexpected message ", (int)msg->GetHeader ()->typeID); |
|
|
|
|
|
|
|
DeleteI2NPMessage (msg); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
buf += fragmentSize; |
|
|
|
buf += fragmentSize; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SSUSession::SendMsgAck (uint32_t msgID) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
uint8_t buf[48 + 18]; // actual length is 44 = 37 + 7 but pad it to multiple of 16
|
|
|
|
|
|
|
|
uint8_t iv[16]; |
|
|
|
|
|
|
|
uint8_t * payload = buf + sizeof (SSUHeader); |
|
|
|
|
|
|
|
*payload = DATA_FLAG_EXPLICIT_ACKS_INCLUDED; // flag
|
|
|
|
|
|
|
|
payload++; |
|
|
|
|
|
|
|
*payload = 1; // number of ACKs
|
|
|
|
|
|
|
|
payload++; |
|
|
|
|
|
|
|
*(uint32_t *)(payload) = htobe32 (msgID); // msgID
|
|
|
|
|
|
|
|
payload += 4; |
|
|
|
|
|
|
|
*payload = 0; // number of fragments
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator (); |
|
|
|
|
|
|
|
rnd.GenerateBlock (iv, 16); // random iv
|
|
|
|
|
|
|
|
// encrypt message with session key
|
|
|
|
|
|
|
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_DATA, buf, 48, m_SessionKey, iv, m_MacKey); |
|
|
|
|
|
|
|
m_Server->Send (buf, 48, m_RemoteEndpoint); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SSUSession::SendSesionDestroyed () |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
uint8_t buf[48 + 18], iv[16]; |
|
|
|
|
|
|
|
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator (); |
|
|
|
|
|
|
|
rnd.GenerateBlock (iv, 16); // random iv
|
|
|
|
|
|
|
|
if (m_State == eSessionStateEstablished) |
|
|
|
|
|
|
|
// encrypt message with session key
|
|
|
|
|
|
|
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_SESSION_DESTROYED, buf, 48, m_SessionKey, iv, m_MacKey); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
auto introKey = GetIntroKey (); |
|
|
|
|
|
|
|
if (introKey) |
|
|
|
|
|
|
|
// encrypt message with intro key
|
|
|
|
|
|
|
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_SESSION_DESTROYED, buf, 48, introKey, iv, introKey); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint ("SSU: can't send SessionDestroyed message"); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
m_Server->Send (buf, 48, m_RemoteEndpoint); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SSUSession::Send (i2p::I2NPMessage * msg) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
uint32_t msgID = htobe32 (msg->ToSSU ()); |
|
|
|
|
|
|
|
size_t payloadSize = SSU_MTU - sizeof (SSUHeader) - 9; // 9 = flag + #frg(1) + messageID(4) + frag info (3)
|
|
|
|
|
|
|
|
size_t len = msg->GetLength (); |
|
|
|
|
|
|
|
uint8_t * msgBuf = msg->GetSSUHeader (); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t fragmentNum = 0; |
|
|
|
|
|
|
|
while (len > 0) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
uint8_t buf[SSU_MTU + 18], iv[16], * payload = buf + sizeof (SSUHeader); |
|
|
|
|
|
|
|
*payload = DATA_FLAG_WANT_REPLY; // for compatibility
|
|
|
|
|
|
|
|
payload++; |
|
|
|
|
|
|
|
*payload = 1; // always 1 message fragment per message
|
|
|
|
|
|
|
|
payload++; |
|
|
|
|
|
|
|
*(uint32_t *)payload = msgID; |
|
|
|
|
|
|
|
payload += 4; |
|
|
|
|
|
|
|
bool isLast = (len <= payloadSize); |
|
|
|
|
|
|
|
size_t size = isLast ? len : payloadSize; |
|
|
|
|
|
|
|
uint32_t fragmentInfo = (fragmentNum << 17); |
|
|
|
|
|
|
|
if (isLast) |
|
|
|
|
|
|
|
fragmentInfo |= 0x010000; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fragmentInfo |= size; |
|
|
|
|
|
|
|
fragmentInfo = htobe32 (fragmentInfo); |
|
|
|
|
|
|
|
memcpy (payload, (uint8_t *)(&fragmentInfo) + 1, 3); |
|
|
|
|
|
|
|
payload += 3; |
|
|
|
|
|
|
|
memcpy (payload, msgBuf, size); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
size += payload - buf; |
|
|
|
|
|
|
|
if (size % 16) // make sure 16 bytes boundary
|
|
|
|
|
|
|
|
size = (size/16 + 1)*16; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator (); |
|
|
|
|
|
|
|
rnd.GenerateBlock (iv, 16); // random iv
|
|
|
|
|
|
|
|
// encrypt message with session key
|
|
|
|
|
|
|
|
FillHeaderAndEncrypt (PAYLOAD_TYPE_DATA, buf, size, m_SessionKey, iv, m_MacKey); |
|
|
|
|
|
|
|
m_Server->Send (buf, size, m_RemoteEndpoint); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!isLast) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
len -= payloadSize; |
|
|
|
|
|
|
|
msgBuf += payloadSize; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
len = 0; |
|
|
|
|
|
|
|
fragmentNum++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
SSUServer::SSUServer (boost::asio::io_service& service, int port): |
|
|
|
SSUServer::SSUServer (boost::asio::io_service& service, int port): |
|
|
|
m_Endpoint (boost::asio::ip::udp::v4 (), port), m_Socket (service, m_Endpoint) |
|
|
|
m_Endpoint (boost::asio::ip::udp::v4 (), port), m_Socket (service, m_Endpoint) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
m_Socket.set_option (boost::asio::socket_base::receive_buffer_size (65535)); |
|
|
|
|
|
|
|
m_Socket.set_option (boost::asio::socket_base::send_buffer_size (65535)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
SSUServer::~SSUServer () |
|
|
|
SSUServer::~SSUServer () |
|
|
@ -405,12 +747,14 @@ namespace ssu |
|
|
|
|
|
|
|
|
|
|
|
void SSUServer::Stop () |
|
|
|
void SSUServer::Stop () |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
DeleteAllSessions (); |
|
|
|
m_Socket.close (); |
|
|
|
m_Socket.close (); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SSUServer::Send (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& to) |
|
|
|
void SSUServer::Send (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& to) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_Socket.send_to (boost::asio::buffer (buf, len), to); |
|
|
|
m_Socket.send_to (boost::asio::buffer (buf, len), to); |
|
|
|
|
|
|
|
LogPrint ("SSU sent ", len, " bytes"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SSUServer::Receive () |
|
|
|
void SSUServer::Receive () |
|
|
@ -441,7 +785,7 @@ namespace ssu |
|
|
|
LogPrint ("SSU receive error: ", ecode.message ()); |
|
|
|
LogPrint ("SSU receive error: ", ecode.message ()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
SSUSession * SSUServer::GetSession (i2p::data::RouterInfo * router) |
|
|
|
SSUSession * SSUServer::GetSession (const i2p::data::RouterInfo * router) |
|
|
|
{ |
|
|
|
{ |
|
|
|
SSUSession * session = nullptr; |
|
|
|
SSUSession * session = nullptr; |
|
|
|
if (router) |
|
|
|
if (router) |
|
|
@ -456,18 +800,65 @@ namespace ssu |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
// otherwise create new session
|
|
|
|
// otherwise create new session
|
|
|
|
|
|
|
|
if (!router->UsesIntroducer ()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// connect directly
|
|
|
|
session = new SSUSession (this, remoteEndpoint, router); |
|
|
|
session = new SSUSession (this, remoteEndpoint, router); |
|
|
|
m_Sessions[remoteEndpoint] = session; |
|
|
|
m_Sessions[remoteEndpoint] = session; |
|
|
|
LogPrint ("New SSU session to [", router->GetIdentHashAbbreviation (), "] ", |
|
|
|
LogPrint ("New SSU session to [", router->GetIdentHashAbbreviation (), "] ", |
|
|
|
remoteEndpoint.address ().to_string (), ":", remoteEndpoint.port (), " created"); |
|
|
|
remoteEndpoint.address ().to_string (), ":", remoteEndpoint.port (), " created"); |
|
|
|
session->Connect (); |
|
|
|
session->Connect (); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// connect to introducer
|
|
|
|
|
|
|
|
auto& introducer = address->introducers[0]; // TODO:
|
|
|
|
|
|
|
|
boost::asio::ip::udp::endpoint introducerEndpoint (introducer.iHost, introducer.iPort); |
|
|
|
|
|
|
|
session = new SSUSession (this, introducerEndpoint, router); |
|
|
|
|
|
|
|
m_Sessions[introducerEndpoint] = session; |
|
|
|
|
|
|
|
LogPrint ("New SSU session to [", router->GetIdentHashAbbreviation (), |
|
|
|
|
|
|
|
"] created through introducer ", introducerEndpoint.address ().to_string (), ":", introducerEndpoint.port ()); |
|
|
|
|
|
|
|
session->ConnectThroughIntroducer (introducer); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
LogPrint ("Router ", router->GetIdentHashAbbreviation (), " doesn't have SSU address"); |
|
|
|
LogPrint ("Router ", router->GetIdentHashAbbreviation (), " doesn't have SSU address"); |
|
|
|
} |
|
|
|
} |
|
|
|
return session; |
|
|
|
return session; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SSUServer::DeleteSession (SSUSession * session) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (session) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
session->Close (); |
|
|
|
|
|
|
|
m_Sessions.erase (session->GetRemoteEndpoint ()); |
|
|
|
|
|
|
|
delete session; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SSUServer::DeleteAllSessions () |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
for (auto it: m_Sessions) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
it.second->Close (); |
|
|
|
|
|
|
|
delete it.second; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
m_Sessions.clear (); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SSUServer::ReassignSession (const boost::asio::ip::udp::endpoint& oldEndpoint, const boost::asio::ip::udp::endpoint& newEndpoint) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
auto it = m_Sessions.find (oldEndpoint); |
|
|
|
|
|
|
|
if (it != m_Sessions.end ()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
m_Sessions.erase (it); |
|
|
|
|
|
|
|
m_Sessions[newEndpoint] = it->second; |
|
|
|
|
|
|
|
LogPrint ("SSU session ressigned from ", oldEndpoint.address ().to_string (), ":", oldEndpoint.port (), |
|
|
|
|
|
|
|
" to ", newEndpoint.address ().to_string (), ":", newEndpoint.port ()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|