mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 21:37:17 +01:00
indentation fixes and SAM datagrams
This commit is contained in:
parent
76fd1c5c58
commit
775b9f30f0
232
SAM.cpp
232
SAM.cpp
|
@ -15,9 +15,9 @@ namespace i2p
|
||||||
{
|
{
|
||||||
namespace client
|
namespace client
|
||||||
{
|
{
|
||||||
SAMSocket::SAMSocket (SAMBridge& owner):
|
SAMSocket::SAMSocket (SAMBridge& owner):
|
||||||
m_Owner (owner), m_Socket (m_Owner.GetService ()), m_Timer (m_Owner.GetService ()),
|
m_Owner (owner), m_Socket (m_Owner.GetService ()), m_Timer (m_Owner.GetService ()),
|
||||||
m_BufferOffset (0), m_SocketType (eSAMSocketTypeUnknown), m_IsSilent (false),
|
m_BufferOffset (0), m_SocketType (eSAMSocketTypeUnknown), m_IsSilent (false),
|
||||||
m_Stream (nullptr), m_Session (nullptr)
|
m_Stream (nullptr), m_Session (nullptr)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -25,21 +25,21 @@ namespace client
|
||||||
SAMSocket::~SAMSocket ()
|
SAMSocket::~SAMSocket ()
|
||||||
{
|
{
|
||||||
Terminate ();
|
Terminate ();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SAMSocket::CloseStream ()
|
void SAMSocket::CloseStream ()
|
||||||
{
|
{
|
||||||
if (m_Stream)
|
if (m_Stream)
|
||||||
{
|
{
|
||||||
m_Stream->Close ();
|
m_Stream->Close ();
|
||||||
m_Stream.reset ();
|
m_Stream.reset ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SAMSocket::Terminate ()
|
void SAMSocket::Terminate ()
|
||||||
{
|
{
|
||||||
CloseStream ();
|
CloseStream ();
|
||||||
|
|
||||||
switch (m_SocketType)
|
switch (m_SocketType)
|
||||||
{
|
{
|
||||||
case eSAMSocketTypeSession:
|
case eSAMSocketTypeSession:
|
||||||
|
@ -47,14 +47,14 @@ namespace client
|
||||||
break;
|
break;
|
||||||
case eSAMSocketTypeStream:
|
case eSAMSocketTypeStream:
|
||||||
{
|
{
|
||||||
if (m_Session)
|
if (m_Session)
|
||||||
m_Session->DelSocket (shared_from_this ());
|
m_Session->DelSocket (shared_from_this ());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case eSAMSocketTypeAcceptor:
|
case eSAMSocketTypeAcceptor:
|
||||||
{
|
{
|
||||||
if (m_Session)
|
if (m_Session)
|
||||||
{
|
{
|
||||||
m_Session->DelSocket (shared_from_this ());
|
m_Session->DelSocket (shared_from_this ());
|
||||||
if (m_Session->localDestination)
|
if (m_Session->localDestination)
|
||||||
m_Session->localDestination->StopAcceptingStreams ();
|
m_Session->localDestination->StopAcceptingStreams ();
|
||||||
|
@ -71,8 +71,8 @@ namespace client
|
||||||
|
|
||||||
void SAMSocket::ReceiveHandshake ()
|
void SAMSocket::ReceiveHandshake ()
|
||||||
{
|
{
|
||||||
m_Socket.async_read_some (boost::asio::buffer(m_Buffer, SAM_SOCKET_BUFFER_SIZE),
|
m_Socket.async_read_some (boost::asio::buffer(m_Buffer, SAM_SOCKET_BUFFER_SIZE),
|
||||||
std::bind(&SAMSocket::HandleHandshakeReceived, shared_from_this (),
|
std::bind(&SAMSocket::HandleHandshakeReceived, shared_from_this (),
|
||||||
std::placeholders::_1, std::placeholders::_2));
|
std::placeholders::_1, std::placeholders::_2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,7 +85,7 @@ namespace client
|
||||||
Terminate ();
|
Terminate ();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
m_Buffer[bytes_transferred] = 0;
|
m_Buffer[bytes_transferred] = 0;
|
||||||
char * eol = (char *)memchr (m_Buffer, '\n', bytes_transferred);
|
char * eol = (char *)memchr (m_Buffer, '\n', bytes_transferred);
|
||||||
if (eol)
|
if (eol)
|
||||||
|
@ -94,8 +94,8 @@ namespace client
|
||||||
char * separator = strchr (m_Buffer, ' ');
|
char * separator = strchr (m_Buffer, ' ');
|
||||||
if (separator)
|
if (separator)
|
||||||
{
|
{
|
||||||
separator = strchr (separator + 1, ' ');
|
separator = strchr (separator + 1, ' ');
|
||||||
if (separator)
|
if (separator)
|
||||||
*separator = 0;
|
*separator = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,13 +117,13 @@ namespace client
|
||||||
{
|
{
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
size_t l = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_HANDSHAKE_REPLY, version.c_str ());
|
size_t l = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_HANDSHAKE_REPLY, version.c_str ());
|
||||||
#else
|
#else
|
||||||
size_t l = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_HANDSHAKE_REPLY, version.c_str ());
|
size_t l = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_HANDSHAKE_REPLY, version.c_str ());
|
||||||
#endif
|
#endif
|
||||||
boost::asio::async_write (m_Socket, boost::asio::buffer (m_Buffer, l), boost::asio::transfer_all (),
|
boost::asio::async_write (m_Socket, boost::asio::buffer (m_Buffer, l), boost::asio::transfer_all (),
|
||||||
std::bind(&SAMSocket::HandleHandshakeReplySent, shared_from_this (),
|
std::bind(&SAMSocket::HandleHandshakeReplySent, shared_from_this (),
|
||||||
std::placeholders::_1, std::placeholders::_2));
|
std::placeholders::_1, std::placeholders::_2));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
SendMessageReply (SAM_HANDSHAKE_I2P_ERROR, strlen (SAM_HANDSHAKE_I2P_ERROR), true);
|
SendMessageReply (SAM_HANDSHAKE_I2P_ERROR, strlen (SAM_HANDSHAKE_I2P_ERROR), true);
|
||||||
}
|
}
|
||||||
|
@ -145,25 +145,25 @@ namespace client
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
m_Socket.async_read_some (boost::asio::buffer(m_Buffer, SAM_SOCKET_BUFFER_SIZE),
|
m_Socket.async_read_some (boost::asio::buffer(m_Buffer, SAM_SOCKET_BUFFER_SIZE),
|
||||||
std::bind(&SAMSocket::HandleMessage, shared_from_this (),
|
std::bind(&SAMSocket::HandleMessage, shared_from_this (),
|
||||||
std::placeholders::_1, std::placeholders::_2));
|
std::placeholders::_1, std::placeholders::_2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SAMSocket::SendMessageReply (const char * msg, size_t len, bool close)
|
void SAMSocket::SendMessageReply (const char * msg, size_t len, bool close)
|
||||||
{
|
{
|
||||||
if (!m_IsSilent)
|
if (!m_IsSilent)
|
||||||
boost::asio::async_write (m_Socket, boost::asio::buffer (msg, len), boost::asio::transfer_all (),
|
boost::asio::async_write (m_Socket, boost::asio::buffer (msg, len), boost::asio::transfer_all (),
|
||||||
std::bind(&SAMSocket::HandleMessageReplySent, shared_from_this (),
|
std::bind(&SAMSocket::HandleMessageReplySent, shared_from_this (),
|
||||||
std::placeholders::_1, std::placeholders::_2, close));
|
std::placeholders::_1, std::placeholders::_2, close));
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (close)
|
if (close)
|
||||||
Terminate ();
|
Terminate ();
|
||||||
else
|
else
|
||||||
Receive ();
|
Receive ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SAMSocket::HandleMessageReplySent (const boost::system::error_code& ecode, std::size_t bytes_transferred, bool close)
|
void SAMSocket::HandleMessageReplySent (const boost::system::error_code& ecode, std::size_t bytes_transferred, bool close)
|
||||||
|
@ -179,8 +179,8 @@ namespace client
|
||||||
if (close)
|
if (close)
|
||||||
Terminate ();
|
Terminate ();
|
||||||
else
|
else
|
||||||
Receive ();
|
Receive ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SAMSocket::HandleMessage (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
void SAMSocket::HandleMessage (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
||||||
|
@ -205,8 +205,8 @@ namespace client
|
||||||
char * separator = strchr (m_Buffer, ' ');
|
char * separator = strchr (m_Buffer, ' ');
|
||||||
if (separator)
|
if (separator)
|
||||||
{
|
{
|
||||||
separator = strchr (separator + 1, ' ');
|
separator = strchr (separator + 1, ' ');
|
||||||
if (separator)
|
if (separator)
|
||||||
*separator = 0;
|
*separator = 0;
|
||||||
else
|
else
|
||||||
separator = eol;
|
separator = eol;
|
||||||
|
@ -236,12 +236,12 @@ namespace client
|
||||||
*separator = ' ';
|
*separator = ' ';
|
||||||
*eol = '\n';
|
*eol = '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// since it's SAM v1 reply is not expected
|
// since it's SAM v1 reply is not expected
|
||||||
Receive ();
|
Receive ();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LogPrint (eLogError, "SAM: unexpected message ", m_Buffer);
|
LogPrint (eLogError, "SAM: unexpected message ", m_Buffer);
|
||||||
Terminate ();
|
Terminate ();
|
||||||
}
|
}
|
||||||
|
@ -254,7 +254,7 @@ namespace client
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LogPrint (eLogWarning, "SAM: incomplete message ", bytes_transferred);
|
LogPrint (eLogWarning, "SAM: incomplete message ", bytes_transferred);
|
||||||
m_BufferOffset = bytes_transferred;
|
m_BufferOffset = bytes_transferred;
|
||||||
// try to receive remaining message
|
// try to receive remaining message
|
||||||
|
@ -268,10 +268,10 @@ namespace client
|
||||||
LogPrint (eLogDebug, "SAM: session create: ", buf);
|
LogPrint (eLogDebug, "SAM: session create: ", buf);
|
||||||
std::map<std::string, std::string> params;
|
std::map<std::string, std::string> params;
|
||||||
ExtractParams (buf, params);
|
ExtractParams (buf, params);
|
||||||
std::string& style = params[SAM_PARAM_STYLE];
|
std::string& style = params[SAM_PARAM_STYLE];
|
||||||
std::string& id = params[SAM_PARAM_ID];
|
std::string& id = params[SAM_PARAM_ID];
|
||||||
std::string& destination = params[SAM_PARAM_DESTINATION];
|
std::string& destination = params[SAM_PARAM_DESTINATION];
|
||||||
m_ID = id;
|
m_ID = id;
|
||||||
if (m_Owner.FindSession (id))
|
if (m_Owner.FindSession (id))
|
||||||
{
|
{
|
||||||
// session exists
|
// session exists
|
||||||
|
@ -302,7 +302,7 @@ namespace client
|
||||||
forward = std::make_shared<boost::asio::ip::udp::endpoint>(addr, port);
|
forward = std::make_shared<boost::asio::ip::udp::endpoint>(addr, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
// create destination
|
// create destination
|
||||||
m_Session = m_Owner.CreateSession (id, destination == SAM_VALUE_TRANSIENT ? "" : destination, ¶ms);
|
m_Session = m_Owner.CreateSession (id, destination == SAM_VALUE_TRANSIENT ? "" : destination, ¶ms);
|
||||||
if (m_Session)
|
if (m_Session)
|
||||||
{
|
{
|
||||||
|
@ -311,7 +311,7 @@ namespace client
|
||||||
{
|
{
|
||||||
m_Session->UDPEndpoint = forward;
|
m_Session->UDPEndpoint = forward;
|
||||||
auto dest = m_Session->localDestination->CreateDatagramDestination ();
|
auto dest = m_Session->localDestination->CreateDatagramDestination ();
|
||||||
dest->SetReceiver (std::bind (&SAMSocket::HandleI2PDatagramReceive, shared_from_this (),
|
dest->SetReceiver (std::bind (&SAMSocket::HandleI2PDatagramReceive, shared_from_this (),
|
||||||
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5));
|
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -321,7 +321,7 @@ namespace client
|
||||||
{
|
{
|
||||||
m_Timer.expires_from_now (boost::posix_time::seconds(SAM_SESSION_READINESS_CHECK_INTERVAL));
|
m_Timer.expires_from_now (boost::posix_time::seconds(SAM_SESSION_READINESS_CHECK_INTERVAL));
|
||||||
m_Timer.async_wait (std::bind (&SAMSocket::HandleSessionReadinessCheckTimer,
|
m_Timer.async_wait (std::bind (&SAMSocket::HandleSessionReadinessCheckTimer,
|
||||||
shared_from_this (), std::placeholders::_1));
|
shared_from_this (), std::placeholders::_1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -339,7 +339,7 @@ namespace client
|
||||||
m_Timer.expires_from_now (boost::posix_time::seconds(SAM_SESSION_READINESS_CHECK_INTERVAL));
|
m_Timer.expires_from_now (boost::posix_time::seconds(SAM_SESSION_READINESS_CHECK_INTERVAL));
|
||||||
m_Timer.async_wait (std::bind (&SAMSocket::HandleSessionReadinessCheckTimer,
|
m_Timer.async_wait (std::bind (&SAMSocket::HandleSessionReadinessCheckTimer,
|
||||||
shared_from_this (), std::placeholders::_1));
|
shared_from_this (), std::placeholders::_1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -352,7 +352,7 @@ namespace client
|
||||||
priv[l1] = 0;
|
priv[l1] = 0;
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
size_t l2 = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_SESSION_CREATE_REPLY_OK, priv);
|
size_t l2 = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_SESSION_CREATE_REPLY_OK, priv);
|
||||||
#else
|
#else
|
||||||
size_t l2 = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_SESSION_CREATE_REPLY_OK, priv);
|
size_t l2 = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_SESSION_CREATE_REPLY_OK, priv);
|
||||||
#endif
|
#endif
|
||||||
SendMessageReply (m_Buffer, l2, false);
|
SendMessageReply (m_Buffer, l2, false);
|
||||||
|
@ -366,7 +366,7 @@ namespace client
|
||||||
std::string& id = params[SAM_PARAM_ID];
|
std::string& id = params[SAM_PARAM_ID];
|
||||||
std::string& destination = params[SAM_PARAM_DESTINATION];
|
std::string& destination = params[SAM_PARAM_DESTINATION];
|
||||||
std::string& silent = params[SAM_PARAM_SILENT];
|
std::string& silent = params[SAM_PARAM_SILENT];
|
||||||
if (silent == SAM_VALUE_TRUE) m_IsSilent = true;
|
if (silent == SAM_VALUE_TRUE) m_IsSilent = true;
|
||||||
m_ID = id;
|
m_ID = id;
|
||||||
m_Session = m_Owner.FindSession (id);
|
m_Session = m_Owner.FindSession (id);
|
||||||
if (m_Session)
|
if (m_Session)
|
||||||
|
@ -390,7 +390,7 @@ namespace client
|
||||||
SendMessageReply(SAM_SESSION_STATUS_INVALID_KEY, strlen(SAM_SESSION_STATUS_INVALID_KEY), true);
|
SendMessageReply(SAM_SESSION_STATUS_INVALID_KEY, strlen(SAM_SESSION_STATUS_INVALID_KEY), true);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
SendMessageReply (SAM_STREAM_STATUS_INVALID_ID, strlen(SAM_STREAM_STATUS_INVALID_ID), true);
|
SendMessageReply (SAM_STREAM_STATUS_INVALID_ID, strlen(SAM_STREAM_STATUS_INVALID_ID), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SAMSocket::Connect (std::shared_ptr<const i2p::data::LeaseSet> remote)
|
void SAMSocket::Connect (std::shared_ptr<const i2p::data::LeaseSet> remote)
|
||||||
|
@ -399,7 +399,7 @@ namespace client
|
||||||
m_Session->AddSocket (shared_from_this ());
|
m_Session->AddSocket (shared_from_this ());
|
||||||
m_Stream = m_Session->localDestination->CreateStream (remote);
|
m_Stream = m_Session->localDestination->CreateStream (remote);
|
||||||
m_Stream->Send ((uint8_t *)m_Buffer, 0); // connect
|
m_Stream->Send ((uint8_t *)m_Buffer, 0); // connect
|
||||||
I2PReceive ();
|
I2PReceive ();
|
||||||
SendMessageReply (SAM_STREAM_STATUS_OK, strlen(SAM_STREAM_STATUS_OK), false);
|
SendMessageReply (SAM_STREAM_STATUS_OK, strlen(SAM_STREAM_STATUS_OK), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -421,17 +421,17 @@ namespace client
|
||||||
ExtractParams (buf, params);
|
ExtractParams (buf, params);
|
||||||
std::string& id = params[SAM_PARAM_ID];
|
std::string& id = params[SAM_PARAM_ID];
|
||||||
std::string& silent = params[SAM_PARAM_SILENT];
|
std::string& silent = params[SAM_PARAM_SILENT];
|
||||||
if (silent == SAM_VALUE_TRUE) m_IsSilent = true;
|
if (silent == SAM_VALUE_TRUE) m_IsSilent = true;
|
||||||
m_ID = id;
|
m_ID = id;
|
||||||
m_Session = m_Owner.FindSession (id);
|
m_Session = m_Owner.FindSession (id);
|
||||||
if (m_Session)
|
if (m_Session)
|
||||||
{
|
{
|
||||||
m_SocketType = eSAMSocketTypeAcceptor;
|
m_SocketType = eSAMSocketTypeAcceptor;
|
||||||
m_Session->AddSocket (shared_from_this ());
|
m_Session->AddSocket (shared_from_this ());
|
||||||
if (!m_Session->localDestination->IsAcceptingStreams ())
|
if (!m_Session->localDestination->IsAcceptingStreams ())
|
||||||
m_Session->localDestination->AcceptOnce (std::bind (&SAMSocket::HandleI2PAccept, shared_from_this (), std::placeholders::_1));
|
m_Session->localDestination->AcceptOnce (std::bind (&SAMSocket::HandleI2PAccept, shared_from_this (), std::placeholders::_1));
|
||||||
SendMessageReply (SAM_STREAM_STATUS_OK, strlen(SAM_STREAM_STATUS_OK), false);
|
SendMessageReply (SAM_STREAM_STATUS_OK, strlen(SAM_STREAM_STATUS_OK), false);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
SendMessageReply (SAM_STREAM_STATUS_INVALID_ID, strlen(SAM_STREAM_STATUS_INVALID_ID), true);
|
SendMessageReply (SAM_STREAM_STATUS_INVALID_ID, strlen(SAM_STREAM_STATUS_INVALID_ID), true);
|
||||||
}
|
}
|
||||||
|
@ -443,9 +443,9 @@ namespace client
|
||||||
ExtractParams (buf, params);
|
ExtractParams (buf, params);
|
||||||
size_t size = std::stoi(params[SAM_PARAM_SIZE]), offset = data - buf;
|
size_t size = std::stoi(params[SAM_PARAM_SIZE]), offset = data - buf;
|
||||||
if (offset + size <= len)
|
if (offset + size <= len)
|
||||||
{
|
{
|
||||||
if (m_Session)
|
if (m_Session)
|
||||||
{
|
{
|
||||||
auto d = m_Session->localDestination->GetDatagramDestination ();
|
auto d = m_Session->localDestination->GetDatagramDestination ();
|
||||||
if (d)
|
if (d)
|
||||||
{
|
{
|
||||||
|
@ -458,24 +458,24 @@ namespace client
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
LogPrint (eLogError, "SAM: session is not created from DATAGRAM SEND");
|
LogPrint (eLogError, "SAM: session is not created from DATAGRAM SEND");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LogPrint (eLogWarning, "SAM: sent datagram size ", size, " exceeds buffer ", len - offset);
|
LogPrint (eLogWarning, "SAM: sent datagram size ", size, " exceeds buffer ", len - offset);
|
||||||
return 0; // try to receive more
|
return 0; // try to receive more
|
||||||
}
|
}
|
||||||
return offset + size;
|
return offset + size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SAMSocket::ProcessDestGenerate ()
|
void SAMSocket::ProcessDestGenerate ()
|
||||||
{
|
{
|
||||||
LogPrint (eLogDebug, "SAM: dest generate");
|
LogPrint (eLogDebug, "SAM: dest generate");
|
||||||
auto keys = i2p::data::PrivateKeys::CreateRandomKeys ();
|
auto keys = i2p::data::PrivateKeys::CreateRandomKeys ();
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
size_t len = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_DEST_REPLY,
|
size_t len = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_DEST_REPLY,
|
||||||
keys.GetPublic ()->ToBase64 ().c_str (), keys.ToBase64 ().c_str ());
|
keys.GetPublic ()->ToBase64 ().c_str (), keys.ToBase64 ().c_str ());
|
||||||
#else
|
#else
|
||||||
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_DEST_REPLY,
|
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_DEST_REPLY,
|
||||||
keys.GetPublic ()->ToBase64 ().c_str (), keys.ToBase64 ().c_str ());
|
keys.GetPublic ()->ToBase64 ().c_str (), keys.ToBase64 ().c_str ());
|
||||||
#endif
|
#endif
|
||||||
SendMessageReply (m_Buffer, len, false);
|
SendMessageReply (m_Buffer, len, false);
|
||||||
|
@ -504,12 +504,12 @@ namespace client
|
||||||
std::bind (&SAMSocket::HandleNamingLookupLeaseSetRequestComplete,
|
std::bind (&SAMSocket::HandleNamingLookupLeaseSetRequestComplete,
|
||||||
shared_from_this (), std::placeholders::_1, ident));
|
shared_from_this (), std::placeholders::_1, ident));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LogPrint (eLogError, "SAM: naming failed, unknown address ", name);
|
LogPrint (eLogError, "SAM: naming failed, unknown address ", name);
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
size_t len = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_INVALID_KEY, name.c_str());
|
size_t len = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_INVALID_KEY, name.c_str());
|
||||||
#else
|
#else
|
||||||
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_INVALID_KEY, name.c_str());
|
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_INVALID_KEY, name.c_str());
|
||||||
#endif
|
#endif
|
||||||
SendMessageReply (m_Buffer, len, false);
|
SendMessageReply (m_Buffer, len, false);
|
||||||
|
@ -521,7 +521,7 @@ namespace client
|
||||||
LogPrint (eLogError, "SAM: i2p error ", msg);
|
LogPrint (eLogError, "SAM: i2p error ", msg);
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
size_t len = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_SESSION_STATUS_I2P_ERROR, msg.c_str());
|
size_t len = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_SESSION_STATUS_I2P_ERROR, msg.c_str());
|
||||||
#else
|
#else
|
||||||
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_SESSION_STATUS_I2P_ERROR, msg.c_str());
|
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_SESSION_STATUS_I2P_ERROR, msg.c_str());
|
||||||
#endif
|
#endif
|
||||||
SendMessageReply (m_Buffer, len, true);
|
SendMessageReply (m_Buffer, len, true);
|
||||||
|
@ -530,30 +530,30 @@ namespace client
|
||||||
void SAMSocket::HandleNamingLookupLeaseSetRequestComplete (std::shared_ptr<i2p::data::LeaseSet> leaseSet, i2p::data::IdentHash ident)
|
void SAMSocket::HandleNamingLookupLeaseSetRequestComplete (std::shared_ptr<i2p::data::LeaseSet> leaseSet, i2p::data::IdentHash ident)
|
||||||
{
|
{
|
||||||
if (leaseSet)
|
if (leaseSet)
|
||||||
{
|
{
|
||||||
context.GetAddressBook ().InsertAddress (leaseSet->GetIdentity ());
|
context.GetAddressBook ().InsertAddress (leaseSet->GetIdentity ());
|
||||||
SendNamingLookupReply (leaseSet->GetIdentity ());
|
SendNamingLookupReply (leaseSet->GetIdentity ());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LogPrint (eLogError, "SAM: naming lookup failed. LeaseSet for ", ident.ToBase32 (), " not found");
|
LogPrint (eLogError, "SAM: naming lookup failed. LeaseSet for ", ident.ToBase32 (), " not found");
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
size_t len = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_INVALID_KEY,
|
size_t len = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_INVALID_KEY,
|
||||||
context.GetAddressBook ().ToAddress (ident).c_str());
|
context.GetAddressBook ().ToAddress (ident).c_str());
|
||||||
#else
|
#else
|
||||||
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_INVALID_KEY,
|
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_INVALID_KEY,
|
||||||
context.GetAddressBook ().ToAddress (ident).c_str());
|
context.GetAddressBook ().ToAddress (ident).c_str());
|
||||||
#endif
|
#endif
|
||||||
SendMessageReply (m_Buffer, len, false);
|
SendMessageReply (m_Buffer, len, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SAMSocket::SendNamingLookupReply (std::shared_ptr<const i2p::data::IdentityEx> identity)
|
void SAMSocket::SendNamingLookupReply (std::shared_ptr<const i2p::data::IdentityEx> identity)
|
||||||
{
|
{
|
||||||
auto base64 = identity->ToBase64 ();
|
auto base64 = identity->ToBase64 ();
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
size_t l = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY, base64.c_str ());
|
size_t l = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY, base64.c_str ());
|
||||||
#else
|
#else
|
||||||
size_t l = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY, base64.c_str ());
|
size_t l = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY, base64.c_str ());
|
||||||
#endif
|
#endif
|
||||||
SendMessageReply (m_Buffer, l, false);
|
SendMessageReply (m_Buffer, l, false);
|
||||||
|
@ -561,7 +561,7 @@ namespace client
|
||||||
|
|
||||||
void SAMSocket::ExtractParams (char * buf, std::map<std::string, std::string>& params)
|
void SAMSocket::ExtractParams (char * buf, std::map<std::string, std::string>& params)
|
||||||
{
|
{
|
||||||
char * separator;
|
char * separator;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
separator = strchr (buf, ' ');
|
separator = strchr (buf, ' ');
|
||||||
|
@ -572,11 +572,11 @@ namespace client
|
||||||
*value = 0;
|
*value = 0;
|
||||||
value++;
|
value++;
|
||||||
params[buf] = value;
|
params[buf] = value;
|
||||||
}
|
}
|
||||||
buf = separator + 1;
|
buf = separator + 1;
|
||||||
}
|
}
|
||||||
while (separator);
|
while (separator);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SAMSocket::Receive ()
|
void SAMSocket::Receive ()
|
||||||
{
|
{
|
||||||
|
@ -586,7 +586,7 @@ namespace client
|
||||||
Terminate ();
|
Terminate ();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
m_Socket.async_read_some (boost::asio::buffer(m_Buffer + m_BufferOffset, SAM_SOCKET_BUFFER_SIZE - m_BufferOffset),
|
m_Socket.async_read_some (boost::asio::buffer(m_Buffer + m_BufferOffset, SAM_SOCKET_BUFFER_SIZE - m_BufferOffset),
|
||||||
std::bind((m_SocketType == eSAMSocketTypeStream) ? &SAMSocket::HandleReceived : &SAMSocket::HandleMessage,
|
std::bind((m_SocketType == eSAMSocketTypeStream) ? &SAMSocket::HandleReceived : &SAMSocket::HandleMessage,
|
||||||
shared_from_this (), std::placeholders::_1, std::placeholders::_2));
|
shared_from_this (), std::placeholders::_1, std::placeholders::_2));
|
||||||
}
|
}
|
||||||
|
@ -602,17 +602,17 @@ namespace client
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (m_Stream)
|
if (m_Stream)
|
||||||
{
|
{
|
||||||
auto s = shared_from_this ();
|
auto s = shared_from_this ();
|
||||||
m_Stream->AsyncSend ((uint8_t *)m_Buffer, bytes_transferred,
|
m_Stream->AsyncSend ((uint8_t *)m_Buffer, bytes_transferred,
|
||||||
[s](const boost::system::error_code& ecode)
|
[s](const boost::system::error_code& ecode)
|
||||||
{
|
{
|
||||||
if (!ecode)
|
if (!ecode)
|
||||||
s->Receive ();
|
s->Receive ();
|
||||||
else
|
else
|
||||||
s->m_Owner.GetService ().post ([s] { s->Terminate (); });
|
s->m_Owner.GetService ().post ([s] { s->Terminate (); });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -622,7 +622,7 @@ namespace client
|
||||||
{
|
{
|
||||||
if (m_Stream->GetStatus () == i2p::stream::eStreamStatusNew ||
|
if (m_Stream->GetStatus () == i2p::stream::eStreamStatusNew ||
|
||||||
m_Stream->GetStatus () == i2p::stream::eStreamStatusOpen) // regular
|
m_Stream->GetStatus () == i2p::stream::eStreamStatusOpen) // regular
|
||||||
{
|
{
|
||||||
m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE),
|
m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE),
|
||||||
std::bind (&SAMSocket::HandleI2PReceive, shared_from_this (),
|
std::bind (&SAMSocket::HandleI2PReceive, shared_from_this (),
|
||||||
std::placeholders::_1, std::placeholders::_2),
|
std::placeholders::_1, std::placeholders::_2),
|
||||||
|
@ -638,10 +638,10 @@ namespace client
|
||||||
std::bind (&SAMSocket::HandleWriteI2PData, shared_from_this (), std::placeholders::_1));
|
std::bind (&SAMSocket::HandleWriteI2PData, shared_from_this (), std::placeholders::_1));
|
||||||
}
|
}
|
||||||
else // no more data
|
else // no more data
|
||||||
Terminate ();
|
Terminate ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SAMSocket::HandleI2PReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
void SAMSocket::HandleI2PReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
||||||
{
|
{
|
||||||
|
@ -653,17 +653,17 @@ namespace client
|
||||||
if (bytes_transferred > 0)
|
if (bytes_transferred > 0)
|
||||||
boost::asio::async_write (m_Socket, boost::asio::buffer (m_StreamBuffer, bytes_transferred),
|
boost::asio::async_write (m_Socket, boost::asio::buffer (m_StreamBuffer, bytes_transferred),
|
||||||
std::bind (&SAMSocket::HandleWriteI2PData, shared_from_this (), std::placeholders::_1)); // postpone termination
|
std::bind (&SAMSocket::HandleWriteI2PData, shared_from_this (), std::placeholders::_1)); // postpone termination
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
auto s = shared_from_this ();
|
auto s = shared_from_this ();
|
||||||
m_Owner.GetService ().post ([s] { s->Terminate (); });
|
m_Owner.GetService ().post ([s] { s->Terminate (); });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
auto s = shared_from_this ();
|
auto s = shared_from_this ();
|
||||||
m_Owner.GetService ().post ([s] { s->Terminate (); });
|
m_Owner.GetService ().post ([s] { s->Terminate (); });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -694,7 +694,7 @@ namespace client
|
||||||
context.GetAddressBook ().InsertAddress (stream->GetRemoteIdentity ());
|
context.GetAddressBook ().InsertAddress (stream->GetRemoteIdentity ());
|
||||||
auto session = m_Owner.FindSession (m_ID);
|
auto session = m_Owner.FindSession (m_ID);
|
||||||
if (session)
|
if (session)
|
||||||
{
|
{
|
||||||
// find more pending acceptors
|
// find more pending acceptors
|
||||||
for (auto it: session->ListSockets ())
|
for (auto it: session->ListSockets ())
|
||||||
if (it->m_SocketType == eSAMSocketTypeAcceptor)
|
if (it->m_SocketType == eSAMSocketTypeAcceptor)
|
||||||
|
@ -710,19 +710,19 @@ namespace client
|
||||||
const size_t ident_len = ident_ptr->GetFullLen();
|
const size_t ident_len = ident_ptr->GetFullLen();
|
||||||
uint8_t* ident = new uint8_t[ident_len];
|
uint8_t* ident = new uint8_t[ident_len];
|
||||||
|
|
||||||
// send remote peer address as base64
|
// send remote peer address as base64
|
||||||
const size_t l = ident_ptr->ToBuffer (ident, ident_len);
|
const size_t l = ident_ptr->ToBuffer (ident, ident_len);
|
||||||
const size_t l1 = i2p::data::ByteStreamToBase64 (ident, l, (char *)m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE);
|
const size_t l1 = i2p::data::ByteStreamToBase64 (ident, l, (char *)m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE);
|
||||||
delete[] ident;
|
delete[] ident;
|
||||||
m_StreamBuffer[l1] = '\n';
|
m_StreamBuffer[l1] = '\n';
|
||||||
HandleI2PReceive (boost::system::error_code (), l1 +1); // we send identity like it has been received from stream
|
HandleI2PReceive (boost::system::error_code (), l1 +1); // we send identity like it has been received from stream
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
I2PReceive ();
|
I2PReceive ();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
LogPrint (eLogWarning, "SAM: I2P acceptor has been reset");
|
LogPrint (eLogWarning, "SAM: I2P acceptor has been reset");
|
||||||
}
|
}
|
||||||
|
|
||||||
void SAMSocket::HandleI2PDatagramReceive (const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len)
|
void SAMSocket::HandleI2PDatagramReceive (const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len)
|
||||||
{
|
{
|
||||||
|
@ -750,12 +750,12 @@ namespace client
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
size_t l = sprintf_s ((char *)m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE, SAM_DATAGRAM_RECEIVED, base64.c_str (), (long unsigned int)len);
|
size_t l = sprintf_s ((char *)m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE, SAM_DATAGRAM_RECEIVED, base64.c_str (), (long unsigned int)len);
|
||||||
#else
|
#else
|
||||||
size_t l = snprintf ((char *)m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE, SAM_DATAGRAM_RECEIVED, base64.c_str (), (long unsigned int)len);
|
size_t l = snprintf ((char *)m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE, SAM_DATAGRAM_RECEIVED, base64.c_str (), (long unsigned int)len);
|
||||||
#endif
|
#endif
|
||||||
if (len < SAM_SOCKET_BUFFER_SIZE - l)
|
if (len < SAM_SOCKET_BUFFER_SIZE - l)
|
||||||
{
|
{
|
||||||
memcpy (m_StreamBuffer + l, buf, len);
|
memcpy (m_StreamBuffer + l, buf, len);
|
||||||
boost::asio::async_write (m_Socket, boost::asio::buffer (m_StreamBuffer, len + l),
|
boost::asio::async_write (m_Socket, boost::asio::buffer (m_StreamBuffer, len + l),
|
||||||
std::bind (&SAMSocket::HandleWriteI2PData, shared_from_this (), std::placeholders::_1));
|
std::bind (&SAMSocket::HandleWriteI2PData, shared_from_this (), std::placeholders::_1));
|
||||||
|
@ -770,7 +770,7 @@ namespace client
|
||||||
UDPEndpoint(nullptr)
|
UDPEndpoint(nullptr)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
SAMSession::~SAMSession ()
|
SAMSession::~SAMSession ()
|
||||||
{
|
{
|
||||||
CloseStreams();
|
CloseStreams();
|
||||||
|
@ -800,7 +800,7 @@ namespace client
|
||||||
{
|
{
|
||||||
if (m_IsRunning)
|
if (m_IsRunning)
|
||||||
Stop ();
|
Stop ();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SAMBridge::Start ()
|
void SAMBridge::Start ()
|
||||||
{
|
{
|
||||||
|
@ -819,26 +819,26 @@ namespace client
|
||||||
m_Sessions.clear ();
|
m_Sessions.clear ();
|
||||||
m_Service.stop ();
|
m_Service.stop ();
|
||||||
if (m_Thread)
|
if (m_Thread)
|
||||||
{
|
{
|
||||||
m_Thread->join ();
|
m_Thread->join ();
|
||||||
delete m_Thread;
|
delete m_Thread;
|
||||||
m_Thread = nullptr;
|
m_Thread = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SAMBridge::Run ()
|
void SAMBridge::Run ()
|
||||||
{
|
{
|
||||||
while (m_IsRunning)
|
while (m_IsRunning)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
m_Service.run ();
|
m_Service.run ();
|
||||||
}
|
}
|
||||||
catch (std::exception& ex)
|
catch (std::exception& ex)
|
||||||
{
|
{
|
||||||
LogPrint (eLogError, "SAM: runtime exception: ", ex.what ());
|
LogPrint (eLogError, "SAM: runtime exception: ", ex.what ());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SAMBridge::Accept ()
|
void SAMBridge::Accept ()
|
||||||
|
@ -855,7 +855,7 @@ namespace client
|
||||||
boost::system::error_code ec;
|
boost::system::error_code ec;
|
||||||
auto ep = socket->GetSocket ().remote_endpoint (ec);
|
auto ep = socket->GetSocket ().remote_endpoint (ec);
|
||||||
if (!ec)
|
if (!ec)
|
||||||
{
|
{
|
||||||
LogPrint (eLogDebug, "SAM: new connection from ", ep);
|
LogPrint (eLogDebug, "SAM: new connection from ", ep);
|
||||||
socket->ReceiveHandshake ();
|
socket->ReceiveHandshake ();
|
||||||
}
|
}
|
||||||
|
@ -869,10 +869,10 @@ namespace client
|
||||||
Accept ();
|
Accept ();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<SAMSession> SAMBridge::CreateSession (const std::string& id, const std::string& destination,
|
std::shared_ptr<SAMSession> SAMBridge::CreateSession (const std::string& id, const std::string& destination,
|
||||||
const std::map<std::string, std::string> * params)
|
const std::map<std::string, std::string> * params)
|
||||||
{
|
{
|
||||||
std::shared_ptr<ClientDestination> localDestination = nullptr;
|
std::shared_ptr<ClientDestination> localDestination = nullptr;
|
||||||
if (destination != "")
|
if (destination != "")
|
||||||
{
|
{
|
||||||
i2p::data::PrivateKeys keys;
|
i2p::data::PrivateKeys keys;
|
||||||
|
@ -887,10 +887,10 @@ namespace client
|
||||||
{
|
{
|
||||||
auto it = params->find (SAM_PARAM_SIGNATURE_TYPE);
|
auto it = params->find (SAM_PARAM_SIGNATURE_TYPE);
|
||||||
if (it != params->end ())
|
if (it != params->end ())
|
||||||
// TODO: extract string values
|
// TODO: extract string values
|
||||||
signatureType = std::stoi(it->second);
|
signatureType = std::stoi(it->second);
|
||||||
}
|
}
|
||||||
localDestination = i2p::client::context.CreateNewLocalDestination (true, signatureType, params);
|
localDestination = i2p::client::context.CreateNewLocalDestination (true, signatureType, params);
|
||||||
}
|
}
|
||||||
if (localDestination)
|
if (localDestination)
|
||||||
{
|
{
|
||||||
|
@ -911,13 +911,13 @@ namespace client
|
||||||
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
||||||
auto it = m_Sessions.find (id);
|
auto it = m_Sessions.find (id);
|
||||||
if (it != m_Sessions.end ())
|
if (it != m_Sessions.end ())
|
||||||
{
|
{
|
||||||
session = it->second;
|
session = it->second;
|
||||||
m_Sessions.erase (it);
|
m_Sessions.erase (it);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (session)
|
if (session)
|
||||||
{
|
{
|
||||||
session->localDestination->StopAcceptingStreams ();
|
session->localDestination->StopAcceptingStreams ();
|
||||||
session->CloseStreams ();
|
session->CloseStreams ();
|
||||||
}
|
}
|
||||||
|
@ -943,9 +943,9 @@ namespace client
|
||||||
void SAMBridge::ReceiveDatagram ()
|
void SAMBridge::ReceiveDatagram ()
|
||||||
{
|
{
|
||||||
m_DatagramSocket.async_receive_from (
|
m_DatagramSocket.async_receive_from (
|
||||||
boost::asio::buffer (m_DatagramReceiveBuffer, i2p::datagram::MAX_DATAGRAM_SIZE),
|
boost::asio::buffer (m_DatagramReceiveBuffer, i2p::datagram::MAX_DATAGRAM_SIZE),
|
||||||
m_SenderEndpoint,
|
m_SenderEndpoint,
|
||||||
std::bind (&SAMBridge::HandleReceivedDatagram, this, std::placeholders::_1, std::placeholders::_2));
|
std::bind (&SAMBridge::HandleReceivedDatagram, this, std::placeholders::_1, std::placeholders::_2));
|
||||||
}
|
}
|
||||||
|
|
||||||
void SAMBridge::HandleReceivedDatagram (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
void SAMBridge::HandleReceivedDatagram (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
||||||
|
@ -955,7 +955,7 @@ namespace client
|
||||||
m_DatagramReceiveBuffer[bytes_transferred] = 0;
|
m_DatagramReceiveBuffer[bytes_transferred] = 0;
|
||||||
char * eol = strchr ((char *)m_DatagramReceiveBuffer, '\n');
|
char * eol = strchr ((char *)m_DatagramReceiveBuffer, '\n');
|
||||||
*eol = 0; eol++;
|
*eol = 0; eol++;
|
||||||
size_t payloadLen = bytes_transferred - ((uint8_t *)eol - m_DatagramReceiveBuffer);
|
size_t payloadLen = bytes_transferred - ((uint8_t *)eol - m_DatagramReceiveBuffer);
|
||||||
LogPrint (eLogDebug, "SAM: datagram received ", m_DatagramReceiveBuffer," size=", payloadLen);
|
LogPrint (eLogDebug, "SAM: datagram received ", m_DatagramReceiveBuffer," size=", payloadLen);
|
||||||
char * sessionID = strchr ((char *)m_DatagramReceiveBuffer, ' ');
|
char * sessionID = strchr ((char *)m_DatagramReceiveBuffer, ' ');
|
||||||
if (sessionID)
|
if (sessionID)
|
||||||
|
@ -967,12 +967,12 @@ namespace client
|
||||||
*destination = 0; destination++;
|
*destination = 0; destination++;
|
||||||
auto session = FindSession (sessionID);
|
auto session = FindSession (sessionID);
|
||||||
if (session)
|
if (session)
|
||||||
{
|
{
|
||||||
i2p::data::IdentityEx dest;
|
i2p::data::IdentityEx dest;
|
||||||
dest.FromBase64 (destination);
|
dest.FromBase64 (destination);
|
||||||
session->localDestination->GetDatagramDestination ()->
|
session->localDestination->GetDatagramDestination ()->
|
||||||
SendDatagramTo ((uint8_t *)eol, payloadLen, dest.GetIdentHash ());
|
SendDatagramTo ((uint8_t *)eol, payloadLen, dest.GetIdentHash ());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
LogPrint (eLogError, "SAM: Session ", sessionID, " not found");
|
LogPrint (eLogError, "SAM: Session ", sessionID, " not found");
|
||||||
}
|
}
|
||||||
|
|
67
SAM.h
67
SAM.h
|
@ -20,48 +20,48 @@ namespace client
|
||||||
{
|
{
|
||||||
const size_t SAM_SOCKET_BUFFER_SIZE = 8192;
|
const size_t SAM_SOCKET_BUFFER_SIZE = 8192;
|
||||||
const int SAM_SOCKET_CONNECTION_MAX_IDLE = 3600; // in seconds
|
const int SAM_SOCKET_CONNECTION_MAX_IDLE = 3600; // in seconds
|
||||||
const int SAM_SESSION_READINESS_CHECK_INTERVAL = 20; // in seconds
|
const int SAM_SESSION_READINESS_CHECK_INTERVAL = 20; // in seconds
|
||||||
const char SAM_HANDSHAKE[] = "HELLO VERSION";
|
const char SAM_HANDSHAKE[] = "HELLO VERSION";
|
||||||
const char SAM_HANDSHAKE_REPLY[] = "HELLO REPLY RESULT=OK VERSION=%s\n";
|
const char SAM_HANDSHAKE_REPLY[] = "HELLO REPLY RESULT=OK VERSION=%s\n";
|
||||||
const char SAM_HANDSHAKE_I2P_ERROR[] = "HELLO REPLY RESULT=I2P_ERROR\n";
|
const char SAM_HANDSHAKE_I2P_ERROR[] = "HELLO REPLY RESULT=I2P_ERROR\n";
|
||||||
const char SAM_SESSION_CREATE[] = "SESSION CREATE";
|
const char SAM_SESSION_CREATE[] = "SESSION CREATE";
|
||||||
const char SAM_SESSION_CREATE_REPLY_OK[] = "SESSION STATUS RESULT=OK DESTINATION=%s\n";
|
const char SAM_SESSION_CREATE_REPLY_OK[] = "SESSION STATUS RESULT=OK DESTINATION=%s\n";
|
||||||
const char SAM_SESSION_CREATE_DUPLICATED_ID[] = "SESSION STATUS RESULT=DUPLICATED_ID\n";
|
const char SAM_SESSION_CREATE_DUPLICATED_ID[] = "SESSION STATUS RESULT=DUPLICATED_ID\n";
|
||||||
const char SAM_SESSION_CREATE_DUPLICATED_DEST[] = "SESSION STATUS RESULT=DUPLICATED_DEST\n";
|
const char SAM_SESSION_CREATE_DUPLICATED_DEST[] = "SESSION STATUS RESULT=DUPLICATED_DEST\n";
|
||||||
const char SAM_SESSION_STATUS_INVALID_KEY[] = "SESSION STATUS RESULT=INVALID_KEY\n";
|
const char SAM_SESSION_STATUS_INVALID_KEY[] = "SESSION STATUS RESULT=INVALID_KEY\n";
|
||||||
const char SAM_SESSION_STATUS_I2P_ERROR[] = "SESSION STATUS RESULT=I2P_ERROR MESSAGE=%s\n";
|
const char SAM_SESSION_STATUS_I2P_ERROR[] = "SESSION STATUS RESULT=I2P_ERROR MESSAGE=%s\n";
|
||||||
const char SAM_STREAM_CONNECT[] = "STREAM CONNECT";
|
const char SAM_STREAM_CONNECT[] = "STREAM CONNECT";
|
||||||
const char SAM_STREAM_STATUS_OK[] = "STREAM STATUS RESULT=OK\n";
|
const char SAM_STREAM_STATUS_OK[] = "STREAM STATUS RESULT=OK\n";
|
||||||
const char SAM_STREAM_STATUS_INVALID_ID[] = "STREAM STATUS RESULT=INVALID_ID\n";
|
const char SAM_STREAM_STATUS_INVALID_ID[] = "STREAM STATUS RESULT=INVALID_ID\n";
|
||||||
const char SAM_STREAM_STATUS_CANT_REACH_PEER[] = "STREAM STATUS RESULT=CANT_REACH_PEER\n";
|
const char SAM_STREAM_STATUS_CANT_REACH_PEER[] = "STREAM STATUS RESULT=CANT_REACH_PEER\n";
|
||||||
const char SAM_STREAM_STATUS_I2P_ERROR[] = "STREAM STATUS RESULT=I2P_ERROR\n";
|
const char SAM_STREAM_STATUS_I2P_ERROR[] = "STREAM STATUS RESULT=I2P_ERROR\n";
|
||||||
const char SAM_STREAM_ACCEPT[] = "STREAM ACCEPT";
|
const char SAM_STREAM_ACCEPT[] = "STREAM ACCEPT";
|
||||||
const char SAM_DATAGRAM_SEND[] = "DATAGRAM SEND";
|
const char SAM_DATAGRAM_SEND[] = "DATAGRAM SEND";
|
||||||
const char SAM_DEST_GENERATE[] = "DEST GENERATE";
|
const char SAM_DEST_GENERATE[] = "DEST GENERATE";
|
||||||
const char SAM_DEST_REPLY[] = "DEST REPLY PUB=%s PRIV=%s\n";
|
const char SAM_DEST_REPLY[] = "DEST REPLY PUB=%s PRIV=%s\n";
|
||||||
const char SAM_DEST_REPLY_I2P_ERROR[] = "DEST REPLY RESULT=I2P_ERROR\n";
|
const char SAM_DEST_REPLY_I2P_ERROR[] = "DEST REPLY RESULT=I2P_ERROR\n";
|
||||||
const char SAM_NAMING_LOOKUP[] = "NAMING LOOKUP";
|
const char SAM_NAMING_LOOKUP[] = "NAMING LOOKUP";
|
||||||
const char SAM_NAMING_REPLY[] = "NAMING REPLY RESULT=OK NAME=ME VALUE=%s\n";
|
const char SAM_NAMING_REPLY[] = "NAMING REPLY RESULT=OK NAME=ME VALUE=%s\n";
|
||||||
const char SAM_DATAGRAM_RECEIVED[] = "DATAGRAM RECEIVED DESTINATION=%s SIZE=%lu\n";
|
const char SAM_DATAGRAM_RECEIVED[] = "DATAGRAM RECEIVED DESTINATION=%s SIZE=%lu\n";
|
||||||
const char SAM_NAMING_REPLY_INVALID_KEY[] = "NAMING REPLY RESULT=INVALID_KEY NAME=%s\n";
|
const char SAM_NAMING_REPLY_INVALID_KEY[] = "NAMING REPLY RESULT=INVALID_KEY NAME=%s\n";
|
||||||
const char SAM_NAMING_REPLY_KEY_NOT_FOUND[] = "NAMING REPLY RESULT=INVALID_KEY_NOT_FOUND NAME=%s\n";
|
const char SAM_NAMING_REPLY_KEY_NOT_FOUND[] = "NAMING REPLY RESULT=INVALID_KEY_NOT_FOUND NAME=%s\n";
|
||||||
const char SAM_PARAM_MIN[] = "MIN";
|
const char SAM_PARAM_MIN[] = "MIN";
|
||||||
const char SAM_PARAM_MAX[] = "MAX";
|
const char SAM_PARAM_MAX[] = "MAX";
|
||||||
const char SAM_PARAM_STYLE[] = "STYLE";
|
const char SAM_PARAM_STYLE[] = "STYLE";
|
||||||
const char SAM_PARAM_ID[] = "ID";
|
const char SAM_PARAM_ID[] = "ID";
|
||||||
const char SAM_PARAM_SILENT[] = "SILENT";
|
const char SAM_PARAM_SILENT[] = "SILENT";
|
||||||
const char SAM_PARAM_DESTINATION[] = "DESTINATION";
|
const char SAM_PARAM_DESTINATION[] = "DESTINATION";
|
||||||
const char SAM_PARAM_NAME[] = "NAME";
|
const char SAM_PARAM_NAME[] = "NAME";
|
||||||
const char SAM_PARAM_SIGNATURE_TYPE[] = "SIGNATURE_TYPE";
|
const char SAM_PARAM_SIGNATURE_TYPE[] = "SIGNATURE_TYPE";
|
||||||
const char SAM_PARAM_SIZE[] = "SIZE";
|
const char SAM_PARAM_SIZE[] = "SIZE";
|
||||||
const char SAM_VALUE_TRANSIENT[] = "TRANSIENT";
|
const char SAM_VALUE_TRANSIENT[] = "TRANSIENT";
|
||||||
const char SAM_VALUE_STREAM[] = "STREAM";
|
const char SAM_VALUE_STREAM[] = "STREAM";
|
||||||
const char SAM_VALUE_DATAGRAM[] = "DATAGRAM";
|
const char SAM_VALUE_DATAGRAM[] = "DATAGRAM";
|
||||||
const char SAM_VALUE_RAW[] = "RAW";
|
const char SAM_VALUE_RAW[] = "RAW";
|
||||||
const char SAM_VALUE_TRUE[] = "true";
|
const char SAM_VALUE_TRUE[] = "true";
|
||||||
const char SAM_VALUE_FALSE[] = "false";
|
const char SAM_VALUE_FALSE[] = "false";
|
||||||
const char SAM_VALUE_HOST[] = "HOST";
|
const char SAM_VALUE_HOST[] = "HOST";
|
||||||
const char SAM_VALUE_PORT[] = "PORT";
|
const char SAM_VALUE_PORT[] = "PORT";
|
||||||
|
|
||||||
enum SAMSocketType
|
enum SAMSocketType
|
||||||
{
|
{
|
||||||
|
@ -79,8 +79,8 @@ namespace client
|
||||||
public:
|
public:
|
||||||
|
|
||||||
SAMSocket (SAMBridge& owner);
|
SAMSocket (SAMBridge& owner);
|
||||||
~SAMSocket ();
|
~SAMSocket ();
|
||||||
void CloseStream (); // TODO: implement it better
|
void CloseStream (); // TODO: implement it better
|
||||||
|
|
||||||
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
|
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
|
||||||
void ReceiveHandshake ();
|
void ReceiveHandshake ();
|
||||||
|
@ -89,16 +89,16 @@ namespace client
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
void Terminate ();
|
void Terminate ();
|
||||||
void HandleHandshakeReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
void HandleHandshakeReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
||||||
void HandleHandshakeReplySent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
void HandleHandshakeReplySent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
||||||
void HandleMessage (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
void HandleMessage (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
||||||
void SendMessageReply (const char * msg, size_t len, bool close);
|
void SendMessageReply (const char * msg, size_t len, bool close);
|
||||||
void HandleMessageReplySent (const boost::system::error_code& ecode, std::size_t bytes_transferred, bool close);
|
void HandleMessageReplySent (const boost::system::error_code& ecode, std::size_t bytes_transferred, bool close);
|
||||||
void Receive ();
|
void Receive ();
|
||||||
void HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
void HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
||||||
|
|
||||||
void I2PReceive ();
|
void I2PReceive ();
|
||||||
void HandleI2PReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
void HandleI2PReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
||||||
void HandleI2PAccept (std::shared_ptr<i2p::stream::Stream> stream);
|
void HandleI2PAccept (std::shared_ptr<i2p::stream::Stream> stream);
|
||||||
void HandleWriteI2PData (const boost::system::error_code& ecode);
|
void HandleWriteI2PData (const boost::system::error_code& ecode);
|
||||||
|
@ -109,8 +109,8 @@ namespace client
|
||||||
void ProcessStreamAccept (char * buf, size_t len);
|
void ProcessStreamAccept (char * buf, size_t len);
|
||||||
void ProcessDestGenerate ();
|
void ProcessDestGenerate ();
|
||||||
void ProcessNamingLookup (char * buf, size_t len);
|
void ProcessNamingLookup (char * buf, size_t len);
|
||||||
void SendI2PError(const std::string & msg);
|
void SendI2PError(const std::string & msg);
|
||||||
size_t ProcessDatagramSend (char * buf, size_t len, const char * data); // from SAM 1.0
|
size_t ProcessDatagramSend (char * buf, size_t len, const char * data); // from SAM 1.0
|
||||||
void ExtractParams (char * buf, std::map<std::string, std::string>& params);
|
void ExtractParams (char * buf, std::map<std::string, std::string>& params);
|
||||||
|
|
||||||
void Connect (std::shared_ptr<const i2p::data::LeaseSet> remote);
|
void Connect (std::shared_ptr<const i2p::data::LeaseSet> remote);
|
||||||
|
@ -133,13 +133,13 @@ namespace client
|
||||||
bool m_IsSilent;
|
bool m_IsSilent;
|
||||||
std::shared_ptr<i2p::stream::Stream> m_Stream;
|
std::shared_ptr<i2p::stream::Stream> m_Stream;
|
||||||
std::shared_ptr<SAMSession> m_Session;
|
std::shared_ptr<SAMSession> m_Session;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SAMSession
|
struct SAMSession
|
||||||
{
|
{
|
||||||
std::shared_ptr<ClientDestination> localDestination;
|
std::shared_ptr<ClientDestination> localDestination;
|
||||||
std::list<std::shared_ptr<SAMSocket> > m_Sockets;
|
std::list<std::shared_ptr<SAMSocket> > m_Sockets;
|
||||||
std::shared_ptr<boost::asio::ip::udp::endpoint> UDPEndpoint;
|
std::shared_ptr<boost::asio::ip::udp::endpoint> UDPEndpoint;
|
||||||
std::mutex m_SocketsMutex;
|
std::mutex m_SocketsMutex;
|
||||||
|
|
||||||
/** safely add a socket to this session */
|
/** safely add a socket to this session */
|
||||||
|
@ -163,8 +163,8 @@ namespace client
|
||||||
}
|
}
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
SAMSession (std::shared_ptr<ClientDestination> dest);
|
SAMSession (std::shared_ptr<ClientDestination> dest);
|
||||||
~SAMSession ();
|
~SAMSession ();
|
||||||
|
|
||||||
void CloseStreams ();
|
void CloseStreams ();
|
||||||
|
@ -179,15 +179,15 @@ namespace client
|
||||||
|
|
||||||
void Start ();
|
void Start ();
|
||||||
void Stop ();
|
void Stop ();
|
||||||
|
|
||||||
boost::asio::io_service& GetService () { return m_Service; };
|
boost::asio::io_service& GetService () { return m_Service; };
|
||||||
std::shared_ptr<SAMSession> CreateSession (const std::string& id, const std::string& destination, // empty string means transient
|
std::shared_ptr<SAMSession> CreateSession (const std::string& id, const std::string& destination, // empty string means transient
|
||||||
const std::map<std::string, std::string> * params);
|
const std::map<std::string, std::string> * params);
|
||||||
void CloseSession (const std::string& id);
|
void CloseSession (const std::string& id);
|
||||||
std::shared_ptr<SAMSession> FindSession (const std::string& id) const;
|
std::shared_ptr<SAMSession> FindSession (const std::string& id) const;
|
||||||
|
|
||||||
/** send raw data to remote endpoint from our UDP Socket */
|
/** send raw data to remote endpoint from our UDP Socket */
|
||||||
void SendTo(const uint8_t * buf, size_t len, std::shared_ptr<boost::asio::ip::udp::endpoint> remote);
|
void SendTo(const uint8_t * buf, size_t len, std::shared_ptr<boost::asio::ip::udp::endpoint> remote);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
@ -202,7 +202,7 @@ namespace client
|
||||||
private:
|
private:
|
||||||
|
|
||||||
bool m_IsRunning;
|
bool m_IsRunning;
|
||||||
std::thread * m_Thread;
|
std::thread * m_Thread;
|
||||||
boost::asio::io_service m_Service;
|
boost::asio::io_service m_Service;
|
||||||
boost::asio::ip::tcp::acceptor m_Acceptor;
|
boost::asio::ip::tcp::acceptor m_Acceptor;
|
||||||
boost::asio::ip::udp::endpoint m_DatagramEndpoint, m_SenderEndpoint;
|
boost::asio::ip::udp::endpoint m_DatagramEndpoint, m_SenderEndpoint;
|
||||||
|
@ -215,9 +215,8 @@ namespace client
|
||||||
|
|
||||||
// for HTTP
|
// for HTTP
|
||||||
const decltype(m_Sessions)& GetSessions () const { return m_Sessions; };
|
const decltype(m_Sessions)& GetSessions () const { return m_Sessions; };
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue