mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1015 lines
30 KiB
1015 lines
30 KiB
10 years ago
|
#include <string.h>
|
||
10 years ago
|
#include <stdio.h>
|
||
10 years ago
|
#ifdef _MSC_VER
|
||
|
#include <stdlib.h>
|
||
|
#endif
|
||
9 years ago
|
#include "Base.h"
|
||
10 years ago
|
#include "Identity.h"
|
||
10 years ago
|
#include "Log.h"
|
||
10 years ago
|
#include "Destination.h"
|
||
10 years ago
|
#include "ClientContext.h"
|
||
8 years ago
|
#include "util.h"
|
||
10 years ago
|
#include "SAM.h"
|
||
|
|
||
|
namespace i2p
|
||
|
{
|
||
10 years ago
|
namespace client
|
||
10 years ago
|
{
|
||
8 years ago
|
SAMSocket::SAMSocket (SAMBridge& owner):
|
||
10 years ago
|
m_Owner (owner), m_Socket (m_Owner.GetService ()), m_Timer (m_Owner.GetService ()),
|
||
8 years ago
|
m_BufferOffset (0), m_SocketType (eSAMSocketTypeUnknown), m_IsSilent (false),
|
||
8 years ago
|
m_IsAccepting (false), m_Stream (nullptr), m_Session (nullptr)
|
||
10 years ago
|
{
|
||
|
}
|
||
|
|
||
|
SAMSocket::~SAMSocket ()
|
||
|
{
|
||
10 years ago
|
Terminate ();
|
||
8 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void SAMSocket::CloseStream ()
|
||
10 years ago
|
{
|
||
|
if (m_Stream)
|
||
8 years ago
|
{
|
||
10 years ago
|
m_Stream->Close ();
|
||
10 years ago
|
m_Stream.reset ();
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void SAMSocket::Terminate ()
|
||
|
{
|
||
|
CloseStream ();
|
||
8 years ago
|
|
||
10 years ago
|
switch (m_SocketType)
|
||
10 years ago
|
{
|
||
10 years ago
|
case eSAMSocketTypeSession:
|
||
|
m_Owner.CloseSession (m_ID);
|
||
|
break;
|
||
|
case eSAMSocketTypeStream:
|
||
|
{
|
||
8 years ago
|
if (m_Session)
|
||
9 years ago
|
m_Session->DelSocket (shared_from_this ());
|
||
10 years ago
|
break;
|
||
|
}
|
||
|
case eSAMSocketTypeAcceptor:
|
||
|
{
|
||
10 years ago
|
if (m_Session)
|
||
8 years ago
|
{
|
||
9 years ago
|
m_Session->DelSocket (shared_from_this ());
|
||
8 years ago
|
if (m_IsAccepting && m_Session->localDestination)
|
||
8 years ago
|
m_Session->localDestination->StopAcceptingStreams ();
|
||
|
}
|
||
10 years ago
|
break;
|
||
|
}
|
||
|
default:
|
||
|
;
|
||
10 years ago
|
}
|
||
10 years ago
|
m_SocketType = eSAMSocketTypeTerminated;
|
||
9 years ago
|
if (m_Socket.is_open()) m_Socket.close ();
|
||
9 years ago
|
m_Session = nullptr;
|
||
10 years ago
|
}
|
||
|
|
||
|
void SAMSocket::ReceiveHandshake ()
|
||
|
{
|
||
8 years ago
|
m_Socket.async_read_some (boost::asio::buffer(m_Buffer, SAM_SOCKET_BUFFER_SIZE),
|
||
|
std::bind(&SAMSocket::HandleHandshakeReceived, shared_from_this (),
|
||
10 years ago
|
std::placeholders::_1, std::placeholders::_2));
|
||
10 years ago
|
}
|
||
|
|
||
|
void SAMSocket::HandleHandshakeReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
||
|
{
|
||
|
if (ecode)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SAM: handshake read error: ", ecode.message ());
|
||
10 years ago
|
if (ecode != boost::asio::error::operation_aborted)
|
||
|
Terminate ();
|
||
|
}
|
||
|
else
|
||
8 years ago
|
{
|
||
10 years ago
|
m_Buffer[bytes_transferred] = 0;
|
||
10 years ago
|
char * eol = (char *)memchr (m_Buffer, '\n', bytes_transferred);
|
||
|
if (eol)
|
||
|
*eol = 0;
|
||
9 years ago
|
LogPrint (eLogDebug, "SAM: handshake ", m_Buffer);
|
||
10 years ago
|
char * separator = strchr (m_Buffer, ' ');
|
||
|
if (separator)
|
||
10 years ago
|
{
|
||
8 years ago
|
separator = strchr (separator + 1, ' ');
|
||
|
if (separator)
|
||
10 years ago
|
*separator = 0;
|
||
|
}
|
||
|
|
||
|
if (!strcmp (m_Buffer, SAM_HANDSHAKE))
|
||
|
{
|
||
|
std::string version("3.0");
|
||
|
// try to find MIN and MAX, 3.0 if not found
|
||
|
if (separator)
|
||
|
{
|
||
|
separator++;
|
||
|
std::map<std::string, std::string> params;
|
||
10 years ago
|
ExtractParams (separator, params);
|
||
8 years ago
|
//auto it = params.find (SAM_PARAM_MAX);
|
||
10 years ago
|
// TODO: check MIN as well
|
||
8 years ago
|
//if (it != params.end ())
|
||
|
// version = it->second;
|
||
10 years ago
|
}
|
||
|
if (version[0] == '3') // we support v3 (3.0 and 3.1) only
|
||
|
{
|
||
|
#ifdef _MSC_VER
|
||
|
size_t l = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_HANDSHAKE_REPLY, version.c_str ());
|
||
8 years ago
|
#else
|
||
10 years ago
|
size_t l = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_HANDSHAKE_REPLY, version.c_str ());
|
||
|
#endif
|
||
10 years ago
|
boost::asio::async_write (m_Socket, boost::asio::buffer (m_Buffer, l), boost::asio::transfer_all (),
|
||
8 years ago
|
std::bind(&SAMSocket::HandleHandshakeReplySent, shared_from_this (),
|
||
10 years ago
|
std::placeholders::_1, std::placeholders::_2));
|
||
8 years ago
|
}
|
||
10 years ago
|
else
|
||
|
SendMessageReply (SAM_HANDSHAKE_I2P_ERROR, strlen (SAM_HANDSHAKE_I2P_ERROR), true);
|
||
10 years ago
|
}
|
||
|
else
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SAM: handshake mismatch");
|
||
10 years ago
|
Terminate ();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void SAMSocket::HandleHandshakeReplySent (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
||
|
{
|
||
|
if (ecode)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SAM: handshake reply send error: ", ecode.message ());
|
||
10 years ago
|
if (ecode != boost::asio::error::operation_aborted)
|
||
|
Terminate ();
|
||
|
}
|
||
|
else
|
||
10 years ago
|
{
|
||
8 years ago
|
m_Socket.async_read_some (boost::asio::buffer(m_Buffer, SAM_SOCKET_BUFFER_SIZE),
|
||
|
std::bind(&SAMSocket::HandleMessage, shared_from_this (),
|
||
|
std::placeholders::_1, std::placeholders::_2));
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
void SAMSocket::SendMessageReply (const char * msg, size_t len, bool close)
|
||
|
{
|
||
8 years ago
|
if (!m_IsSilent)
|
||
10 years ago
|
boost::asio::async_write (m_Socket, boost::asio::buffer (msg, len), boost::asio::transfer_all (),
|
||
8 years ago
|
std::bind(&SAMSocket::HandleMessageReplySent, shared_from_this (),
|
||
10 years ago
|
std::placeholders::_1, std::placeholders::_2, close));
|
||
10 years ago
|
else
|
||
|
{
|
||
|
if (close)
|
||
|
Terminate ();
|
||
|
else
|
||
8 years ago
|
Receive ();
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
void SAMSocket::HandleMessageReplySent (const boost::system::error_code& ecode, std::size_t bytes_transferred, bool close)
|
||
|
{
|
||
|
if (ecode)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SAM: reply send error: ", ecode.message ());
|
||
10 years ago
|
if (ecode != boost::asio::error::operation_aborted)
|
||
|
Terminate ();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (close)
|
||
|
Terminate ();
|
||
|
else
|
||
8 years ago
|
Receive ();
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void SAMSocket::HandleMessage (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
||
|
{
|
||
|
if (ecode)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SAM: read error: ", ecode.message ());
|
||
10 years ago
|
if (ecode != boost::asio::error::operation_aborted)
|
||
|
Terminate ();
|
||
|
}
|
||
9 years ago
|
else if (m_SocketType == eSAMSocketTypeStream)
|
||
|
HandleReceived (ecode, bytes_transferred);
|
||
10 years ago
|
else
|
||
|
{
|
||
10 years ago
|
bytes_transferred += m_BufferOffset;
|
||
|
m_BufferOffset = 0;
|
||
10 years ago
|
m_Buffer[bytes_transferred] = 0;
|
||
10 years ago
|
char * eol = (char *)memchr (m_Buffer, '\n', bytes_transferred);
|
||
10 years ago
|
if (eol)
|
||
|
{
|
||
|
*eol = 0;
|
||
10 years ago
|
char * separator = strchr (m_Buffer, ' ');
|
||
|
if (separator)
|
||
10 years ago
|
{
|
||
8 years ago
|
separator = strchr (separator + 1, ' ');
|
||
|
if (separator)
|
||
10 years ago
|
*separator = 0;
|
||
|
else
|
||
|
separator = eol;
|
||
|
|
||
10 years ago
|
if (!strcmp (m_Buffer, SAM_SESSION_CREATE))
|
||
|
ProcessSessionCreate (separator + 1, bytes_transferred - (separator - m_Buffer) - 1);
|
||
|
else if (!strcmp (m_Buffer, SAM_STREAM_CONNECT))
|
||
8 years ago
|
ProcessStreamConnect (separator + 1, bytes_transferred - (separator - m_Buffer) - 1, bytes_transferred - (eol - m_Buffer) - 1);
|
||
10 years ago
|
else if (!strcmp (m_Buffer, SAM_STREAM_ACCEPT))
|
||
|
ProcessStreamAccept (separator + 1, bytes_transferred - (separator - m_Buffer) - 1);
|
||
10 years ago
|
else if (!strcmp (m_Buffer, SAM_DEST_GENERATE))
|
||
8 years ago
|
ProcessDestGenerate (separator + 1, bytes_transferred - (separator - m_Buffer) - 1);
|
||
10 years ago
|
else if (!strcmp (m_Buffer, SAM_NAMING_LOOKUP))
|
||
|
ProcessNamingLookup (separator + 1, bytes_transferred - (separator - m_Buffer) - 1);
|
||
10 years ago
|
else if (!strcmp (m_Buffer, SAM_DATAGRAM_SEND))
|
||
|
{
|
||
10 years ago
|
size_t len = bytes_transferred - (separator - m_Buffer) - 1;
|
||
|
size_t processed = ProcessDatagramSend (separator + 1, len, eol + 1);
|
||
|
if (processed < len)
|
||
10 years ago
|
{
|
||
10 years ago
|
m_BufferOffset = len - processed;
|
||
10 years ago
|
if (processed > 0)
|
||
10 years ago
|
memmove (m_Buffer, separator + 1 + processed, m_BufferOffset);
|
||
10 years ago
|
else
|
||
|
{
|
||
|
// restore string back
|
||
|
*separator = ' ';
|
||
|
*eol = '\n';
|
||
|
}
|
||
8 years ago
|
}
|
||
10 years ago
|
// since it's SAM v1 reply is not expected
|
||
|
Receive ();
|
||
|
}
|
||
8 years ago
|
else
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SAM: unexpected message ", m_Buffer);
|
||
10 years ago
|
Terminate ();
|
||
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
else
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SAM: malformed message ", m_Buffer);
|
||
10 years ago
|
Terminate ();
|
||
|
}
|
||
|
}
|
||
8 years ago
|
|
||
10 years ago
|
else
|
||
8 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogWarning, "SAM: incomplete message ", bytes_transferred);
|
||
10 years ago
|
m_BufferOffset = bytes_transferred;
|
||
|
// try to receive remaining message
|
||
|
Receive ();
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void SAMSocket::ProcessSessionCreate (char * buf, size_t len)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "SAM: session create: ", buf);
|
||
10 years ago
|
std::map<std::string, std::string> params;
|
||
10 years ago
|
ExtractParams (buf, params);
|
||
8 years ago
|
std::string& style = params[SAM_PARAM_STYLE];
|
||
10 years ago
|
std::string& id = params[SAM_PARAM_ID];
|
||
|
std::string& destination = params[SAM_PARAM_DESTINATION];
|
||
8 years ago
|
m_ID = id;
|
||
10 years ago
|
if (m_Owner.FindSession (id))
|
||
|
{
|
||
|
// session exists
|
||
|
SendMessageReply (SAM_SESSION_CREATE_DUPLICATED_ID, strlen(SAM_SESSION_CREATE_DUPLICATED_ID), true);
|
||
|
return;
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
std::shared_ptr<boost::asio::ip::udp::endpoint> forward = nullptr;
|
||
|
if (style == SAM_VALUE_DATAGRAM && params.find(SAM_VALUE_HOST) != params.end() && params.find(SAM_VALUE_PORT) != params.end())
|
||
|
{
|
||
|
// udp forward selected
|
||
|
boost::system::error_code e;
|
||
|
// TODO: support hostnames in udp forward
|
||
|
auto addr = boost::asio::ip::address::from_string(params[SAM_VALUE_HOST], e);
|
||
|
if (e)
|
||
|
{
|
||
|
// not an ip address
|
||
|
SendI2PError("Invalid IP Address in HOST");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
auto port = std::stoi(params[SAM_VALUE_PORT]);
|
||
|
if (port == -1)
|
||
|
{
|
||
|
SendI2PError("Invalid port");
|
||
|
return;
|
||
|
}
|
||
|
forward = std::make_shared<boost::asio::ip::udp::endpoint>(addr, port);
|
||
|
}
|
||
|
|
||
8 years ago
|
// create destination
|
||
8 years ago
|
m_Session = m_Owner.CreateSession (id, destination == SAM_VALUE_TRANSIENT ? "" : destination, ¶ms);
|
||
10 years ago
|
if (m_Session)
|
||
10 years ago
|
{
|
||
10 years ago
|
m_SocketType = eSAMSocketTypeSession;
|
||
10 years ago
|
if (style == SAM_VALUE_DATAGRAM)
|
||
10 years ago
|
{
|
||
8 years ago
|
m_Session->UDPEndpoint = forward;
|
||
10 years ago
|
auto dest = m_Session->localDestination->CreateDatagramDestination ();
|
||
8 years ago
|
dest->SetReceiver (std::bind (&SAMSocket::HandleI2PDatagramReceive, shared_from_this (),
|
||
10 years ago
|
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5));
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
if (m_Session->localDestination->IsReady ())
|
||
|
SendSessionCreateReplyOk ();
|
||
10 years ago
|
else
|
||
|
{
|
||
|
m_Timer.expires_from_now (boost::posix_time::seconds(SAM_SESSION_READINESS_CHECK_INTERVAL));
|
||
10 years ago
|
m_Timer.async_wait (std::bind (&SAMSocket::HandleSessionReadinessCheckTimer,
|
||
8 years ago
|
shared_from_this (), std::placeholders::_1));
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
else
|
||
10 years ago
|
SendMessageReply (SAM_SESSION_CREATE_DUPLICATED_DEST, strlen(SAM_SESSION_CREATE_DUPLICATED_DEST), true);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void SAMSocket::HandleSessionReadinessCheckTimer (const boost::system::error_code& ecode)
|
||
|
{
|
||
|
if (ecode != boost::asio::error::operation_aborted)
|
||
|
{
|
||
|
if (m_Session->localDestination->IsReady ())
|
||
|
SendSessionCreateReplyOk ();
|
||
|
else
|
||
|
{
|
||
|
m_Timer.expires_from_now (boost::posix_time::seconds(SAM_SESSION_READINESS_CHECK_INTERVAL));
|
||
10 years ago
|
m_Timer.async_wait (std::bind (&SAMSocket::HandleSessionReadinessCheckTimer,
|
||
|
shared_from_this (), std::placeholders::_1));
|
||
8 years ago
|
}
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void SAMSocket::SendSessionCreateReplyOk ()
|
||
|
{
|
||
|
uint8_t buf[1024];
|
||
|
char priv[1024];
|
||
|
size_t l = m_Session->localDestination->GetPrivateKeys ().ToBuffer (buf, 1024);
|
||
|
size_t l1 = i2p::data::ByteStreamToBase64 (buf, l, priv, 1024);
|
||
|
priv[l1] = 0;
|
||
10 years ago
|
#ifdef _MSC_VER
|
||
|
size_t l2 = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_SESSION_CREATE_REPLY_OK, priv);
|
||
8 years ago
|
#else
|
||
10 years ago
|
size_t l2 = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_SESSION_CREATE_REPLY_OK, priv);
|
||
10 years ago
|
#endif
|
||
10 years ago
|
SendMessageReply (m_Buffer, l2, false);
|
||
|
}
|
||
|
|
||
8 years ago
|
void SAMSocket::ProcessStreamConnect (char * buf, size_t len, size_t rem)
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "SAM: stream connect: ", buf);
|
||
10 years ago
|
std::map<std::string, std::string> params;
|
||
10 years ago
|
ExtractParams (buf, params);
|
||
10 years ago
|
std::string& id = params[SAM_PARAM_ID];
|
||
|
std::string& destination = params[SAM_PARAM_DESTINATION];
|
||
10 years ago
|
std::string& silent = params[SAM_PARAM_SILENT];
|
||
8 years ago
|
if (silent == SAM_VALUE_TRUE) m_IsSilent = true;
|
||
10 years ago
|
m_ID = id;
|
||
10 years ago
|
m_Session = m_Owner.FindSession (id);
|
||
|
if (m_Session)
|
||
10 years ago
|
{
|
||
8 years ago
|
if (rem > 0) // handle follow on data
|
||
|
{
|
||
|
memmove (m_Buffer, buf + len + 1, rem); // buf is a pointer to m_Buffer's content
|
||
|
m_BufferOffset = rem;
|
||
|
}
|
||
|
else
|
||
|
m_BufferOffset = 0;
|
||
|
|
||
9 years ago
|
auto dest = std::make_shared<i2p::data::IdentityEx> ();
|
||
8 years ago
|
size_t l = dest->FromBase64(destination);
|
||
|
if (l > 0)
|
||
10 years ago
|
{
|
||
10 years ago
|
context.GetAddressBook().InsertAddress(dest);
|
||
9 years ago
|
auto leaseSet = m_Session->localDestination->FindLeaseSet(dest->GetIdentHash());
|
||
10 years ago
|
if (leaseSet)
|
||
|
Connect(leaseSet);
|
||
|
else
|
||
|
{
|
||
9 years ago
|
m_Session->localDestination->RequestDestination(dest->GetIdentHash(),
|
||
10 years ago
|
std::bind(&SAMSocket::HandleConnectLeaseSetRequestComplete,
|
||
|
shared_from_this(), std::placeholders::_1));
|
||
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
else
|
||
|
SendMessageReply(SAM_SESSION_STATUS_INVALID_KEY, strlen(SAM_SESSION_STATUS_INVALID_KEY), true);
|
||
10 years ago
|
}
|
||
10 years ago
|
else
|
||
8 years ago
|
SendMessageReply (SAM_STREAM_STATUS_INVALID_ID, strlen(SAM_STREAM_STATUS_INVALID_ID), true);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void SAMSocket::Connect (std::shared_ptr<const i2p::data::LeaseSet> remote)
|
||
10 years ago
|
{
|
||
|
m_SocketType = eSAMSocketTypeStream;
|
||
9 years ago
|
m_Session->AddSocket (shared_from_this ());
|
||
10 years ago
|
m_Stream = m_Session->localDestination->CreateStream (remote);
|
||
8 years ago
|
m_Stream->Send ((uint8_t *)m_Buffer, m_BufferOffset); // connect and send
|
||
|
m_BufferOffset = 0;
|
||
8 years ago
|
I2PReceive ();
|
||
10 years ago
|
SendMessageReply (SAM_STREAM_STATUS_OK, strlen(SAM_STREAM_STATUS_OK), false);
|
||
|
}
|
||
|
|
||
10 years ago
|
void SAMSocket::HandleConnectLeaseSetRequestComplete (std::shared_ptr<i2p::data::LeaseSet> leaseSet)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (leaseSet)
|
||
10 years ago
|
Connect (leaseSet);
|
||
10 years ago
|
else
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SAM: destination to connect not found");
|
||
10 years ago
|
SendMessageReply (SAM_STREAM_STATUS_CANT_REACH_PEER, strlen(SAM_STREAM_STATUS_CANT_REACH_PEER), true);
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
void SAMSocket::ProcessStreamAccept (char * buf, size_t len)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "SAM: stream accept: ", buf);
|
||
10 years ago
|
std::map<std::string, std::string> params;
|
||
10 years ago
|
ExtractParams (buf, params);
|
||
10 years ago
|
std::string& id = params[SAM_PARAM_ID];
|
||
10 years ago
|
std::string& silent = params[SAM_PARAM_SILENT];
|
||
8 years ago
|
if (silent == SAM_VALUE_TRUE) m_IsSilent = true;
|
||
10 years ago
|
m_ID = id;
|
||
10 years ago
|
m_Session = m_Owner.FindSession (id);
|
||
|
if (m_Session)
|
||
8 years ago
|
{
|
||
8 years ago
|
m_SocketType = eSAMSocketTypeAcceptor;
|
||
|
m_Session->AddSocket (shared_from_this ());
|
||
8 years ago
|
if (!m_Session->localDestination->IsAcceptingStreams ())
|
||
8 years ago
|
{
|
||
|
m_IsAccepting = true;
|
||
8 years ago
|
m_Session->localDestination->AcceptOnce (std::bind (&SAMSocket::HandleI2PAccept, shared_from_this (), std::placeholders::_1));
|
||
8 years ago
|
}
|
||
8 years ago
|
SendMessageReply (SAM_STREAM_STATUS_OK, strlen(SAM_STREAM_STATUS_OK), false);
|
||
8 years ago
|
}
|
||
10 years ago
|
else
|
||
10 years ago
|
SendMessageReply (SAM_STREAM_STATUS_INVALID_ID, strlen(SAM_STREAM_STATUS_INVALID_ID), true);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
size_t SAMSocket::ProcessDatagramSend (char * buf, size_t len, const char * data)
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "SAM: datagram send: ", buf, " ", len);
|
||
10 years ago
|
std::map<std::string, std::string> params;
|
||
|
ExtractParams (buf, params);
|
||
8 years ago
|
size_t size = std::stoi(params[SAM_PARAM_SIZE]), offset = data - buf;
|
||
10 years ago
|
if (offset + size <= len)
|
||
8 years ago
|
{
|
||
10 years ago
|
if (m_Session)
|
||
8 years ago
|
{
|
||
10 years ago
|
auto d = m_Session->localDestination->GetDatagramDestination ();
|
||
|
if (d)
|
||
|
{
|
||
|
i2p::data::IdentityEx dest;
|
||
|
dest.FromBase64 (params[SAM_PARAM_DESTINATION]);
|
||
|
d->SendDatagramTo ((const uint8_t *)data, size, dest.GetIdentHash ());
|
||
|
}
|
||
|
else
|
||
9 years ago
|
LogPrint (eLogError, "SAM: missing datagram destination");
|
||
10 years ago
|
}
|
||
|
else
|
||
9 years ago
|
LogPrint (eLogError, "SAM: session is not created from DATAGRAM SEND");
|
||
8 years ago
|
}
|
||
10 years ago
|
else
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogWarning, "SAM: sent datagram size ", size, " exceeds buffer ", len - offset);
|
||
10 years ago
|
return 0; // try to receive more
|
||
8 years ago
|
}
|
||
10 years ago
|
return offset + size;
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
void SAMSocket::ProcessDestGenerate (char * buf, size_t len)
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "SAM: dest generate");
|
||
8 years ago
|
std::map<std::string, std::string> params;
|
||
|
ExtractParams (buf, params);
|
||
|
// extract signature type
|
||
|
i2p::data::SigningKeyType signatureType = i2p::data::SIGNING_KEY_TYPE_DSA_SHA1;
|
||
|
auto it = params.find (SAM_PARAM_SIGNATURE_TYPE);
|
||
|
if (it != params.end ())
|
||
|
// TODO: extract string values
|
||
|
signatureType = std::stoi(it->second);
|
||
|
auto keys = i2p::data::PrivateKeys::CreateRandomKeys (signatureType);
|
||
10 years ago
|
#ifdef _MSC_VER
|
||
8 years ago
|
size_t l = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_DEST_REPLY,
|
||
9 years ago
|
keys.GetPublic ()->ToBase64 ().c_str (), keys.ToBase64 ().c_str ());
|
||
8 years ago
|
#else
|
||
8 years ago
|
size_t l = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_DEST_REPLY,
|
||
9 years ago
|
keys.GetPublic ()->ToBase64 ().c_str (), keys.ToBase64 ().c_str ());
|
||
10 years ago
|
#endif
|
||
8 years ago
|
SendMessageReply (m_Buffer, l, false);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void SAMSocket::ProcessNamingLookup (char * buf, size_t len)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "SAM: naming lookup: ", buf);
|
||
10 years ago
|
std::map<std::string, std::string> params;
|
||
10 years ago
|
ExtractParams (buf, params);
|
||
10 years ago
|
std::string& name = params[SAM_PARAM_NAME];
|
||
9 years ago
|
std::shared_ptr<const i2p::data::IdentityEx> identity;
|
||
10 years ago
|
i2p::data::IdentHash ident;
|
||
8 years ago
|
auto dest = m_Session == nullptr ? context.GetSharedLocalDestination() : m_Session->localDestination;
|
||
10 years ago
|
if (name == "ME")
|
||
8 years ago
|
SendNamingLookupReply (dest->GetIdentity ());
|
||
9 years ago
|
else if ((identity = context.GetAddressBook ().GetAddress (name)) != nullptr)
|
||
10 years ago
|
SendNamingLookupReply (identity);
|
||
8 years ago
|
else if (context.GetAddressBook ().GetIdentHash (name, ident))
|
||
10 years ago
|
{
|
||
8 years ago
|
auto leaseSet = dest->FindLeaseSet (ident);
|
||
10 years ago
|
if (leaseSet)
|
||
|
SendNamingLookupReply (leaseSet->GetIdentity ());
|
||
|
else
|
||
8 years ago
|
dest->RequestDestination (ident,
|
||
10 years ago
|
std::bind (&SAMSocket::HandleNamingLookupLeaseSetRequestComplete,
|
||
8 years ago
|
shared_from_this (), std::placeholders::_1, ident));
|
||
|
}
|
||
8 years ago
|
else
|
||
8 years ago
|
{
|
||
|
LogPrint (eLogError, "SAM: naming failed, unknown address ", name);
|
||
10 years ago
|
#ifdef _MSC_VER
|
||
8 years ago
|
size_t len = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_INVALID_KEY, name.c_str());
|
||
8 years ago
|
#else
|
||
8 years ago
|
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_INVALID_KEY, name.c_str());
|
||
10 years ago
|
#endif
|
||
8 years ago
|
SendMessageReply (m_Buffer, len, false);
|
||
10 years ago
|
}
|
||
8 years ago
|
}
|
||
10 years ago
|
|
||
8 years ago
|
void SAMSocket::SendI2PError(const std::string & msg)
|
||
|
{
|
||
|
LogPrint (eLogError, "SAM: i2p error ", msg);
|
||
|
#ifdef _MSC_VER
|
||
|
size_t len = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_SESSION_STATUS_I2P_ERROR, msg.c_str());
|
||
8 years ago
|
#else
|
||
8 years ago
|
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_SESSION_STATUS_I2P_ERROR, msg.c_str());
|
||
|
#endif
|
||
|
SendMessageReply (m_Buffer, len, true);
|
||
|
}
|
||
|
|
||
|
void SAMSocket::HandleNamingLookupLeaseSetRequestComplete (std::shared_ptr<i2p::data::LeaseSet> leaseSet, i2p::data::IdentHash ident)
|
||
10 years ago
|
{
|
||
|
if (leaseSet)
|
||
8 years ago
|
{
|
||
10 years ago
|
context.GetAddressBook ().InsertAddress (leaseSet->GetIdentity ());
|
||
|
SendNamingLookupReply (leaseSet->GetIdentity ());
|
||
8 years ago
|
}
|
||
10 years ago
|
else
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SAM: naming lookup failed. LeaseSet for ", ident.ToBase32 (), " not found");
|
||
10 years ago
|
#ifdef _MSC_VER
|
||
8 years ago
|
size_t len = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_INVALID_KEY,
|
||
10 years ago
|
context.GetAddressBook ().ToAddress (ident).c_str());
|
||
8 years ago
|
#else
|
||
10 years ago
|
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_INVALID_KEY,
|
||
|
context.GetAddressBook ().ToAddress (ident).c_str());
|
||
|
#endif
|
||
|
SendMessageReply (m_Buffer, len, false);
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
9 years ago
|
void SAMSocket::SendNamingLookupReply (std::shared_ptr<const i2p::data::IdentityEx> identity)
|
||
10 years ago
|
{
|
||
9 years ago
|
auto base64 = identity->ToBase64 ();
|
||
10 years ago
|
#ifdef _MSC_VER
|
||
8 years ago
|
size_t l = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY, base64.c_str ());
|
||
|
#else
|
||
10 years ago
|
size_t l = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY, base64.c_str ());
|
||
10 years ago
|
#endif
|
||
10 years ago
|
SendMessageReply (m_Buffer, l, false);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void SAMSocket::ExtractParams (char * buf, std::map<std::string, std::string>& params)
|
||
10 years ago
|
{
|
||
8 years ago
|
char * separator;
|
||
10 years ago
|
do
|
||
10 years ago
|
{
|
||
10 years ago
|
separator = strchr (buf, ' ');
|
||
|
if (separator) *separator = 0;
|
||
10 years ago
|
char * value = strchr (buf, '=');
|
||
|
if (value)
|
||
|
{
|
||
|
*value = 0;
|
||
|
value++;
|
||
|
params[buf] = value;
|
||
8 years ago
|
}
|
||
10 years ago
|
buf = separator + 1;
|
||
10 years ago
|
}
|
||
10 years ago
|
while (separator);
|
||
8 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void SAMSocket::Receive ()
|
||
|
{
|
||
10 years ago
|
if (m_BufferOffset >= SAM_SOCKET_BUFFER_SIZE)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SAM: Buffer is full, terminate");
|
||
10 years ago
|
Terminate ();
|
||
|
return;
|
||
|
}
|
||
8 years ago
|
m_Socket.async_read_some (boost::asio::buffer(m_Buffer + m_BufferOffset, SAM_SOCKET_BUFFER_SIZE - m_BufferOffset),
|
||
10 years ago
|
std::bind((m_SocketType == eSAMSocketTypeStream) ? &SAMSocket::HandleReceived : &SAMSocket::HandleMessage,
|
||
10 years ago
|
shared_from_this (), std::placeholders::_1, std::placeholders::_2));
|
||
10 years ago
|
}
|
||
|
|
||
|
void SAMSocket::HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
||
|
{
|
||
|
if (ecode)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SAM: read error: ", ecode.message ());
|
||
10 years ago
|
if (ecode != boost::asio::error::operation_aborted)
|
||
|
Terminate ();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (m_Stream)
|
||
8 years ago
|
{
|
||
8 years ago
|
bytes_transferred += m_BufferOffset;
|
||
|
m_BufferOffset = 0;
|
||
10 years ago
|
auto s = shared_from_this ();
|
||
|
m_Stream->AsyncSend ((uint8_t *)m_Buffer, bytes_transferred,
|
||
|
[s](const boost::system::error_code& ecode)
|
||
|
{
|
||
|
if (!ecode)
|
||
|
s->Receive ();
|
||
8 years ago
|
else
|
||
8 years ago
|
s->m_Owner.GetService ().post ([s] { s->Terminate (); });
|
||
10 years ago
|
});
|
||
8 years ago
|
}
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void SAMSocket::I2PReceive ()
|
||
10 years ago
|
{
|
||
|
if (m_Stream)
|
||
8 years ago
|
{
|
||
|
if (m_Stream->GetStatus () == i2p::stream::eStreamStatusNew ||
|
||
|
m_Stream->GetStatus () == i2p::stream::eStreamStatusOpen) // regular
|
||
8 years ago
|
{
|
||
8 years ago
|
m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE),
|
||
|
std::bind (&SAMSocket::HandleI2PReceive, shared_from_this (),
|
||
|
std::placeholders::_1, std::placeholders::_2),
|
||
|
SAM_SOCKET_CONNECTION_MAX_IDLE);
|
||
|
}
|
||
|
else // closed by peer
|
||
|
{
|
||
|
// get remaning data
|
||
|
auto len = m_Stream->ReadSome (m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE);
|
||
|
if (len > 0) // still some data
|
||
|
{
|
||
|
boost::asio::async_write (m_Socket, boost::asio::buffer (m_StreamBuffer, len),
|
||
|
std::bind (&SAMSocket::HandleWriteI2PData, shared_from_this (), std::placeholders::_1));
|
||
|
}
|
||
|
else // no more data
|
||
8 years ago
|
Terminate ();
|
||
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void SAMSocket::HandleI2PReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
||
10 years ago
|
{
|
||
|
if (ecode)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SAM: stream read error: ", ecode.message ());
|
||
10 years ago
|
if (ecode != boost::asio::error::operation_aborted)
|
||
8 years ago
|
{
|
||
|
if (bytes_transferred > 0)
|
||
|
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
|
||
8 years ago
|
else
|
||
|
{
|
||
8 years ago
|
auto s = shared_from_this ();
|
||
|
m_Owner.GetService ().post ([s] { s->Terminate (); });
|
||
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
else
|
||
|
{
|
||
8 years ago
|
auto s = shared_from_this ();
|
||
|
m_Owner.GetService ().post ([s] { s->Terminate (); });
|
||
8 years ago
|
}
|
||
10 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
boost::asio::async_write (m_Socket, boost::asio::buffer (m_StreamBuffer, bytes_transferred),
|
||
10 years ago
|
std::bind (&SAMSocket::HandleWriteI2PData, shared_from_this (), std::placeholders::_1));
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void SAMSocket::HandleWriteI2PData (const boost::system::error_code& ecode)
|
||
10 years ago
|
{
|
||
|
if (ecode)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SAM: socket write error: ", ecode.message ());
|
||
10 years ago
|
if (ecode != boost::asio::error::operation_aborted)
|
||
|
Terminate ();
|
||
|
}
|
||
|
else
|
||
10 years ago
|
I2PReceive ();
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void SAMSocket::HandleI2PAccept (std::shared_ptr<i2p::stream::Stream> stream)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (stream)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "SAM: incoming I2P connection for session ", m_ID);
|
||
8 years ago
|
m_SocketType = eSAMSocketTypeStream;
|
||
8 years ago
|
m_IsAccepting = false;
|
||
10 years ago
|
m_Stream = stream;
|
||
10 years ago
|
context.GetAddressBook ().InsertAddress (stream->GetRemoteIdentity ());
|
||
8 years ago
|
auto session = m_Owner.FindSession (m_ID);
|
||
8 years ago
|
if (session)
|
||
8 years ago
|
{
|
||
8 years ago
|
// find more pending acceptors
|
||
|
for (auto it: session->ListSockets ())
|
||
|
if (it->m_SocketType == eSAMSocketTypeAcceptor)
|
||
|
{
|
||
8 years ago
|
it->m_IsAccepting = true;
|
||
8 years ago
|
session->localDestination->AcceptOnce (std::bind (&SAMSocket::HandleI2PAccept, it, std::placeholders::_1));
|
||
8 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
10 years ago
|
if (!m_IsSilent)
|
||
|
{
|
||
9 years ago
|
// get remote peer address
|
||
|
auto ident_ptr = stream->GetRemoteIdentity();
|
||
9 years ago
|
const size_t ident_len = ident_ptr->GetFullLen();
|
||
9 years ago
|
uint8_t* ident = new uint8_t[ident_len];
|
||
|
|
||
8 years ago
|
// send remote peer address as base64
|
||
9 years ago
|
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);
|
||
9 years ago
|
delete[] ident;
|
||
10 years ago
|
m_StreamBuffer[l1] = '\n';
|
||
|
HandleI2PReceive (boost::system::error_code (), l1 +1); // we send identity like it has been received from stream
|
||
8 years ago
|
}
|
||
10 years ago
|
else
|
||
|
I2PReceive ();
|
||
10 years ago
|
}
|
||
10 years ago
|
else
|
||
9 years ago
|
LogPrint (eLogWarning, "SAM: I2P acceptor has been reset");
|
||
8 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void SAMSocket::HandleI2PDatagramReceive (const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len)
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "SAM: datagram received ", len);
|
||
10 years ago
|
auto base64 = from.ToBase64 ();
|
||
8 years ago
|
auto ep = m_Session->UDPEndpoint;
|
||
|
if (ep)
|
||
|
{
|
||
|
// udp forward enabled
|
||
|
size_t bsz = base64.size();
|
||
8 years ago
|
size_t sz = bsz + 1 + len;
|
||
8 years ago
|
// build datagram body
|
||
|
uint8_t * data = new uint8_t[sz];
|
||
8 years ago
|
// Destination
|
||
|
memcpy(data, base64.c_str(), bsz);
|
||
|
// linefeed
|
||
|
data[bsz] = '\n';
|
||
|
// Payload
|
||
|
memcpy(data+bsz+1, buf, len);
|
||
8 years ago
|
// send to remote endpoint
|
||
|
m_Owner.SendTo(data, sz, ep);
|
||
8 years ago
|
delete [] data;
|
||
8 years ago
|
}
|
||
|
else
|
||
|
{
|
||
10 years ago
|
#ifdef _MSC_VER
|
||
8 years ago
|
size_t l = sprintf_s ((char *)m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE, SAM_DATAGRAM_RECEIVED, base64.c_str (), (long unsigned int)len);
|
||
|
#else
|
||
|
size_t l = snprintf ((char *)m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE, SAM_DATAGRAM_RECEIVED, base64.c_str (), (long unsigned int)len);
|
||
10 years ago
|
#endif
|
||
8 years ago
|
if (len < SAM_SOCKET_BUFFER_SIZE - l)
|
||
|
{
|
||
8 years ago
|
memcpy (m_StreamBuffer + l, buf, len);
|
||
|
boost::asio::async_write (m_Socket, boost::asio::buffer (m_StreamBuffer, len + l),
|
||
|
std::bind (&SAMSocket::HandleWriteI2PData, shared_from_this (), std::placeholders::_1));
|
||
|
}
|
||
|
else
|
||
|
LogPrint (eLogWarning, "SAM: received datagram size ", len," exceeds buffer");
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
SAMSession::SAMSession (std::shared_ptr<ClientDestination> dest):
|
||
8 years ago
|
localDestination (dest),
|
||
|
UDPEndpoint(nullptr)
|
||
10 years ago
|
{
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
SAMSession::~SAMSession ()
|
||
8 years ago
|
{
|
||
8 years ago
|
CloseStreams();
|
||
|
i2p::client::context.DeleteLocalDestination (localDestination);
|
||
8 years ago
|
}
|
||
|
|
||
10 years ago
|
void SAMSession::CloseStreams ()
|
||
|
{
|
||
8 years ago
|
std::vector<std::shared_ptr<SAMSocket> > socks;
|
||
8 years ago
|
{
|
||
8 years ago
|
std::lock_guard<std::mutex> lock(m_SocketsMutex);
|
||
8 years ago
|
for (const auto& sock : m_Sockets) {
|
||
|
socks.push_back(sock);
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
for (auto & sock : socks ) sock->Terminate();
|
||
8 years ago
|
m_Sockets.clear();
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
SAMBridge::SAMBridge (const std::string& address, int port):
|
||
10 years ago
|
m_IsRunning (false), m_Thread (nullptr),
|
||
9 years ago
|
m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(address), port)),
|
||
|
m_DatagramEndpoint (boost::asio::ip::address::from_string(address), port-1), m_DatagramSocket (m_Service, m_DatagramEndpoint)
|
||
10 years ago
|
{
|
||
|
}
|
||
|
|
||
|
SAMBridge::~SAMBridge ()
|
||
|
{
|
||
10 years ago
|
if (m_IsRunning)
|
||
|
Stop ();
|
||
8 years ago
|
}
|
||
10 years ago
|
|
||
|
void SAMBridge::Start ()
|
||
|
{
|
||
|
Accept ();
|
||
10 years ago
|
ReceiveDatagram ();
|
||
10 years ago
|
m_IsRunning = true;
|
||
10 years ago
|
m_Thread = new std::thread (std::bind (&SAMBridge::Run, this));
|
||
|
}
|
||
|
|
||
|
void SAMBridge::Stop ()
|
||
|
{
|
||
|
m_IsRunning = false;
|
||
10 years ago
|
m_Acceptor.cancel ();
|
||
8 years ago
|
for (auto& it: m_Sessions)
|
||
9 years ago
|
it.second->CloseStreams ();
|
||
10 years ago
|
m_Sessions.clear ();
|
||
10 years ago
|
m_Service.stop ();
|
||
|
if (m_Thread)
|
||
8 years ago
|
{
|
||
|
m_Thread->join ();
|
||
10 years ago
|
delete m_Thread;
|
||
|
m_Thread = nullptr;
|
||
8 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
void SAMBridge::Run ()
|
||
|
{
|
||
10 years ago
|
while (m_IsRunning)
|
||
|
{
|
||
|
try
|
||
8 years ago
|
{
|
||
10 years ago
|
m_Service.run ();
|
||
|
}
|
||
|
catch (std::exception& ex)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SAM: runtime exception: ", ex.what ());
|
||
8 years ago
|
}
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
void SAMBridge::Accept ()
|
||
|
{
|
||
10 years ago
|
auto newSocket = std::make_shared<SAMSocket> (*this);
|
||
10 years ago
|
m_Acceptor.async_accept (newSocket->GetSocket (), std::bind (&SAMBridge::HandleAccept, this,
|
||
|
std::placeholders::_1, newSocket));
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void SAMBridge::HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<SAMSocket> socket)
|
||
10 years ago
|
{
|
||
|
if (!ecode)
|
||
|
{
|
||
10 years ago
|
boost::system::error_code ec;
|
||
|
auto ep = socket->GetSocket ().remote_endpoint (ec);
|
||
|
if (!ec)
|
||
8 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "SAM: new connection from ", ep);
|
||
10 years ago
|
socket->ReceiveHandshake ();
|
||
|
}
|
||
|
else
|
||
9 years ago
|
LogPrint (eLogError, "SAM: incoming connection error ", ec.message ());
|
||
10 years ago
|
}
|
||
|
else
|
||
9 years ago
|
LogPrint (eLogError, "SAM: accept error: ", ecode.message ());
|
||
10 years ago
|
|
||
|
if (ecode != boost::asio::error::operation_aborted)
|
||
|
Accept ();
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
std::shared_ptr<SAMSession> SAMBridge::CreateSession (const std::string& id, const std::string& destination,
|
||
10 years ago
|
const std::map<std::string, std::string> * params)
|
||
10 years ago
|
{
|
||
8 years ago
|
std::shared_ptr<ClientDestination> localDestination = nullptr;
|
||
10 years ago
|
if (destination != "")
|
||
10 years ago
|
{
|
||
|
i2p::data::PrivateKeys keys;
|
||
8 years ago
|
if (!keys.FromBase64 (destination)) return nullptr;
|
||
10 years ago
|
localDestination = i2p::client::context.CreateNewLocalDestination (keys, true, params);
|
||
10 years ago
|
}
|
||
|
else // transient
|
||
10 years ago
|
{
|
||
|
// extract signature type
|
||
|
i2p::data::SigningKeyType signatureType = i2p::data::SIGNING_KEY_TYPE_DSA_SHA1;
|
||
|
if (params)
|
||
|
{
|
||
|
auto it = params->find (SAM_PARAM_SIGNATURE_TYPE);
|
||
|
if (it != params->end ())
|
||
8 years ago
|
// TODO: extract string values
|
||
8 years ago
|
signatureType = std::stoi(it->second);
|
||
10 years ago
|
}
|
||
8 years ago
|
localDestination = i2p::client::context.CreateNewLocalDestination (true, signatureType, params);
|
||
10 years ago
|
}
|
||
10 years ago
|
if (localDestination)
|
||
|
{
|
||
9 years ago
|
auto session = std::make_shared<SAMSession>(localDestination);
|
||
10 years ago
|
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
||
9 years ago
|
auto ret = m_Sessions.insert (std::make_pair(id, session));
|
||
8 years ago
|
if (!ret.second)
|
||
9 years ago
|
LogPrint (eLogWarning, "SAM: Session ", id, " already exists");
|
||
10 years ago
|
return ret.first->second;
|
||
10 years ago
|
}
|
||
10 years ago
|
return nullptr;
|
||
10 years ago
|
}
|
||
|
|
||
|
void SAMBridge::CloseSession (const std::string& id)
|
||
|
{
|
||
9 years ago
|
std::shared_ptr<SAMSession> session;
|
||
10 years ago
|
{
|
||
9 years ago
|
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
||
|
auto it = m_Sessions.find (id);
|
||
|
if (it != m_Sessions.end ())
|
||
8 years ago
|
{
|
||
9 years ago
|
session = it->second;
|
||
|
m_Sessions.erase (it);
|
||
8 years ago
|
}
|
||
|
}
|
||
9 years ago
|
if (session)
|
||
8 years ago
|
{
|
||
10 years ago
|
session->localDestination->StopAcceptingStreams ();
|
||
10 years ago
|
session->CloseStreams ();
|
||
10 years ago
|
}
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
std::shared_ptr<SAMSession> SAMBridge::FindSession (const std::string& id) const
|
||
10 years ago
|
{
|
||
10 years ago
|
std::unique_lock<std::mutex> l(m_SessionsMutex);
|
||
10 years ago
|
auto it = m_Sessions.find (id);
|
||
|
if (it != m_Sessions.end ())
|
||
10 years ago
|
return it->second;
|
||
10 years ago
|
return nullptr;
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
void SAMBridge::SendTo(const uint8_t * buf, size_t len, std::shared_ptr<boost::asio::ip::udp::endpoint> remote)
|
||
|
{
|
||
|
if(remote)
|
||
|
{
|
||
|
m_DatagramSocket.send_to(boost::asio::buffer(buf, len), *remote);
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void SAMBridge::ReceiveDatagram ()
|
||
|
{
|
||
|
m_DatagramSocket.async_receive_from (
|
||
8 years ago
|
boost::asio::buffer (m_DatagramReceiveBuffer, i2p::datagram::MAX_DATAGRAM_SIZE),
|
||
10 years ago
|
m_SenderEndpoint,
|
||
8 years ago
|
std::bind (&SAMBridge::HandleReceivedDatagram, this, std::placeholders::_1, std::placeholders::_2));
|
||
10 years ago
|
}
|
||
|
|
||
|
void SAMBridge::HandleReceivedDatagram (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
||
|
{
|
||
|
if (!ecode)
|
||
|
{
|
||
10 years ago
|
m_DatagramReceiveBuffer[bytes_transferred] = 0;
|
||
|
char * eol = strchr ((char *)m_DatagramReceiveBuffer, '\n');
|
||
|
*eol = 0; eol++;
|
||
8 years ago
|
size_t payloadLen = bytes_transferred - ((uint8_t *)eol - m_DatagramReceiveBuffer);
|
||
9 years ago
|
LogPrint (eLogDebug, "SAM: datagram received ", m_DatagramReceiveBuffer," size=", payloadLen);
|
||
10 years ago
|
char * sessionID = strchr ((char *)m_DatagramReceiveBuffer, ' ');
|
||
|
if (sessionID)
|
||
|
{
|
||
|
sessionID++;
|
||
|
char * destination = strchr (sessionID, ' ');
|
||
|
if (destination)
|
||
|
{
|
||
|
*destination = 0; destination++;
|
||
|
auto session = FindSession (sessionID);
|
||
|
if (session)
|
||
8 years ago
|
{
|
||
10 years ago
|
i2p::data::IdentityEx dest;
|
||
10 years ago
|
dest.FromBase64 (destination);
|
||
10 years ago
|
session->localDestination->GetDatagramDestination ()->
|
||
|
SendDatagramTo ((uint8_t *)eol, payloadLen, dest.GetIdentHash ());
|
||
8 years ago
|
}
|
||
10 years ago
|
else
|
||
9 years ago
|
LogPrint (eLogError, "SAM: Session ", sessionID, " not found");
|
||
10 years ago
|
}
|
||
|
else
|
||
9 years ago
|
LogPrint (eLogError, "SAM: Missing destination key");
|
||
10 years ago
|
}
|
||
|
else
|
||
9 years ago
|
LogPrint (eLogError, "SAM: Missing sessionID");
|
||
10 years ago
|
ReceiveDatagram ();
|
||
|
}
|
||
|
else
|
||
9 years ago
|
LogPrint (eLogError, "SAM: datagram receive error: ", ecode.message ());
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
}
|