i2pd/SAM.cpp

647 lines
18 KiB
C++
Raw Normal View History

2014-09-24 18:59:03 +00:00
#include <string.h>
2014-09-30 15:08:38 +00:00
#include <stdio.h>
2014-09-24 16:01:26 +00:00
#include <boost/bind.hpp>
2014-09-24 20:39:31 +00:00
#include "base64.h"
2014-09-25 17:58:09 +00:00
#include "Identity.h"
2014-09-24 16:01:26 +00:00
#include "Log.h"
2014-09-25 17:58:09 +00:00
#include "NetDb.h"
#include "Destination.h"
2014-10-16 00:52:17 +00:00
#include "ClientContext.h"
2014-09-24 16:01:26 +00:00
#include "SAM.h"
namespace i2p
{
namespace client
2014-09-24 16:01:26 +00:00
{
2014-09-24 18:59:03 +00:00
SAMSocket::SAMSocket (SAMBridge& owner):
m_Owner (owner), m_Socket (m_Owner.GetService ()), m_Timer (m_Owner.GetService ()),
2014-10-03 01:40:15 +00:00
m_SocketType (eSAMSocketTypeUnknown), m_IsSilent (false), m_Stream (nullptr),
m_Session (nullptr)
2014-09-24 18:59:03 +00:00
{
}
SAMSocket::~SAMSocket ()
{
2014-09-24 20:39:31 +00:00
if (m_Stream)
{
m_Stream->Close ();
if (m_Session && m_Session->localDestination)
m_Session->localDestination->DeleteStream (m_Stream);
2014-09-24 20:39:31 +00:00
}
2014-09-24 18:59:03 +00:00
}
void SAMSocket::Terminate ()
{
if (m_Stream)
{
m_Stream->Close ();
if (m_Session && m_Session->localDestination)
m_Session->localDestination->DeleteStream (m_Stream);
2014-09-24 18:59:03 +00:00
m_Stream = nullptr;
}
2014-09-26 19:40:57 +00:00
switch (m_SocketType)
2014-09-25 17:58:09 +00:00
{
2014-09-26 19:40:57 +00:00
case eSAMSocketTypeSession:
m_Owner.CloseSession (m_ID);
break;
case eSAMSocketTypeStream:
{
2014-10-07 16:07:10 +00:00
if (m_Session)
m_Session->sockets.remove (this);
2014-09-26 19:40:57 +00:00
break;
}
case eSAMSocketTypeAcceptor:
{
2014-10-07 16:07:10 +00:00
if (m_Session)
2014-09-26 19:40:57 +00:00
{
2014-10-07 16:07:10 +00:00
m_Session->sockets.remove (this);
m_Session->localDestination->ResetAcceptor ();
2014-09-26 19:40:57 +00:00
}
break;
}
default:
;
2014-09-25 17:58:09 +00:00
}
2014-10-10 01:40:45 +00:00
m_Socket.close ();
2014-10-12 20:22:14 +00:00
// delete this;
2014-09-24 18:59:03 +00:00
}
void SAMSocket::ReceiveHandshake ()
{
m_Socket.async_read_some (boost::asio::buffer(m_Buffer, SAM_SOCKET_BUFFER_SIZE),
boost::bind(&SAMSocket::HandleHandshakeReceived, this,
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
void SAMSocket::HandleHandshakeReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred)
{
if (ecode)
{
LogPrint ("SAM handshake read error: ", ecode.message ());
if (ecode != boost::asio::error::operation_aborted)
Terminate ();
}
else
{
m_Buffer[bytes_transferred] = 0;
LogPrint ("SAM handshake ", m_Buffer);
2014-09-28 13:05:37 +00:00
if (!memcmp (m_Buffer, SAM_HANDSHAKE, strlen (SAM_HANDSHAKE)))
2014-09-24 18:59:03 +00:00
{
// TODO: check version
2014-09-28 13:05:37 +00:00
boost::asio::async_write (m_Socket, boost::asio::buffer (SAM_HANDSHAKE_REPLY, strlen (SAM_HANDSHAKE_REPLY)), boost::asio::transfer_all (),
2014-09-24 18:59:03 +00:00
boost::bind(&SAMSocket::HandleHandshakeReplySent, this,
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
else
{
2014-09-28 13:05:37 +00:00
LogPrint ("SAM handshake mismatch");
2014-09-24 18:59:03 +00:00
Terminate ();
}
}
}
void SAMSocket::HandleHandshakeReplySent (const boost::system::error_code& ecode, std::size_t bytes_transferred)
{
if (ecode)
{
LogPrint ("SAM handshake reply send error: ", ecode.message ());
if (ecode != boost::asio::error::operation_aborted)
Terminate ();
}
else
2014-09-25 17:22:25 +00:00
{
m_Socket.async_read_some (boost::asio::buffer(m_Buffer, SAM_SOCKET_BUFFER_SIZE),
boost::bind(&SAMSocket::HandleMessage, this,
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
}
void SAMSocket::SendMessageReply (const char * msg, size_t len, bool close)
{
2014-09-29 18:18:06 +00:00
if (!m_IsSilent || m_SocketType == eSAMSocketTypeAcceptor)
boost::asio::async_write (m_Socket, boost::asio::buffer (msg, len), boost::asio::transfer_all (),
boost::bind(&SAMSocket::HandleMessageReplySent, this,
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, close));
else
{
if (close)
Terminate ();
else
Receive ();
}
2014-09-25 17:22:25 +00:00
}
void SAMSocket::HandleMessageReplySent (const boost::system::error_code& ecode, std::size_t bytes_transferred, bool close)
{
if (ecode)
{
LogPrint ("SAM reply send error: ", ecode.message ());
if (ecode != boost::asio::error::operation_aborted)
Terminate ();
}
else
{
if (close)
Terminate ();
else
Receive ();
}
2014-09-24 18:59:03 +00:00
}
2014-09-25 17:22:25 +00:00
void SAMSocket::HandleMessage (const boost::system::error_code& ecode, std::size_t bytes_transferred)
{
if (ecode)
{
LogPrint ("SAM read error: ", ecode.message ());
if (ecode != boost::asio::error::operation_aborted)
Terminate ();
}
else
{
m_Buffer[bytes_transferred] = 0;
char * eol = strchr (m_Buffer, '\n');
if (eol)
{
*eol = 0;
2014-09-28 13:05:37 +00:00
char * separator = strchr (m_Buffer, ' ');
if (separator)
2014-09-30 15:08:38 +00:00
{
separator = strchr (separator + 1, ' ');
if (separator)
*separator = 0;
else
separator = eol;
2014-09-28 13:05:37 +00:00
if (!strcmp (m_Buffer, SAM_SESSION_CREATE))
ProcessSessionCreate (separator + 1, bytes_transferred - (separator - m_Buffer) - 1);
else if (!strcmp (m_Buffer, SAM_STREAM_CONNECT))
ProcessStreamConnect (separator + 1, bytes_transferred - (separator - m_Buffer) - 1);
else if (!strcmp (m_Buffer, SAM_STREAM_ACCEPT))
ProcessStreamAccept (separator + 1, bytes_transferred - (separator - m_Buffer) - 1);
2014-09-30 15:08:38 +00:00
else if (!strcmp (m_Buffer, SAM_DEST_GENERATE))
ProcessDestGenerate ();
2014-10-02 20:55:01 +00:00
else if (!strcmp (m_Buffer, SAM_NAMING_LOOKUP))
ProcessNamingLookup (separator + 1, bytes_transferred - (separator - m_Buffer) - 1);
2014-09-28 13:05:37 +00:00
else
{
LogPrint ("SAM unexpected message ", m_Buffer);
Terminate ();
}
2014-09-30 15:08:38 +00:00
}
2014-09-28 13:05:37 +00:00
else
2014-09-30 15:08:38 +00:00
{
LogPrint ("SAM malformed message ", m_Buffer);
2014-09-25 17:22:25 +00:00
Terminate ();
}
}
else
{
2014-09-30 15:08:38 +00:00
LogPrint ("SAM malformed message ", m_Buffer);
2014-09-25 17:22:25 +00:00
Terminate ();
}
}
}
void SAMSocket::ProcessSessionCreate (char * buf, size_t len)
{
2014-09-28 13:05:37 +00:00
LogPrint ("SAM session create: ", buf);
2014-09-25 17:22:25 +00:00
std::map<std::string, std::string> params;
ExtractParams (buf, len, params);
std::string& id = params[SAM_PARAM_ID];
std::string& destination = params[SAM_PARAM_DESTINATION];
m_ID = id;
if (m_Owner.FindSession (id))
{
// session exists
SendMessageReply (SAM_SESSION_CREATE_DUPLICATED_ID, strlen(SAM_SESSION_CREATE_DUPLICATED_ID), true);
return;
}
2014-10-03 01:40:15 +00:00
m_Session = m_Owner.CreateSession (id, destination == SAM_VALUE_TRANSIENT ? "" : destination);
if (m_Session)
2014-09-25 17:22:25 +00:00
{
2014-09-25 17:58:09 +00:00
m_SocketType = eSAMSocketTypeSession;
if (m_Session->localDestination->IsReady ())
SendSessionCreateReplyOk ();
else
{
m_Timer.expires_from_now (boost::posix_time::seconds(SAM_SESSION_READINESS_CHECK_INTERVAL));
m_Timer.async_wait (boost::bind (&SAMSocket::HandleSessionReadinessCheckTimer,
this, boost::asio::placeholders::error));
}
2014-09-25 17:22:25 +00:00
}
else
SendMessageReply (SAM_SESSION_CREATE_DUPLICATED_DEST, strlen(SAM_SESSION_CREATE_DUPLICATED_DEST), true);
2014-09-25 17:22:25 +00:00
}
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));
m_Timer.async_wait (boost::bind (&SAMSocket::HandleSessionReadinessCheckTimer,
this, boost::asio::placeholders::error));
}
}
}
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;
size_t l2 = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_SESSION_CREATE_REPLY_OK, priv);
SendMessageReply (m_Buffer, l2, false);
}
2014-09-25 17:22:25 +00:00
void SAMSocket::ProcessStreamConnect (char * buf, size_t len)
{
2014-09-28 13:05:37 +00:00
LogPrint ("SAM stream connect: ", buf);
2014-09-25 17:58:09 +00:00
std::map<std::string, std::string> params;
ExtractParams (buf, len, params);
std::string& id = params[SAM_PARAM_ID];
std::string& destination = params[SAM_PARAM_DESTINATION];
2014-09-29 18:18:06 +00:00
std::string& silent = params[SAM_PARAM_SILENT];
if (silent == SAM_VALUE_TRUE) m_IsSilent = true;
2014-09-25 17:58:09 +00:00
m_ID = id;
2014-10-07 16:07:10 +00:00
m_Session = m_Owner.FindSession (id);
if (m_Session)
2014-09-25 17:58:09 +00:00
{
uint8_t ident[1024];
size_t l = i2p::data::Base64ToByteStream (destination.c_str (), destination.length (), ident, 1024);
i2p::data::IdentityEx dest;
dest.FromBuffer (ident, l);
auto leaseSet = i2p::data::netdb.FindLeaseSet (dest.GetIdentHash ());
if (leaseSet)
2014-10-07 16:07:10 +00:00
Connect (*leaseSet);
else
2014-09-25 17:58:09 +00:00
{
2014-10-07 16:07:10 +00:00
i2p::data::netdb.RequestDestination (dest.GetIdentHash (), true, m_Session->localDestination->GetTunnelPool ());
m_Timer.expires_from_now (boost::posix_time::seconds(SAM_CONNECT_TIMEOUT));
2014-10-03 19:08:41 +00:00
m_Timer.async_wait (boost::bind (&SAMSocket::HandleStreamDestinationRequestTimer,
2014-10-07 16:07:10 +00:00
this, boost::asio::placeholders::error, dest.GetIdentHash ()));
2014-09-25 17:58:09 +00:00
}
}
else
SendMessageReply (SAM_STREAM_STATUS_INVALID_ID, strlen(SAM_STREAM_STATUS_INVALID_ID), true);
}
2014-10-07 16:07:10 +00:00
void SAMSocket::Connect (const i2p::data::LeaseSet& remote)
{
m_SocketType = eSAMSocketTypeStream;
2014-10-07 16:07:10 +00:00
m_Session->sockets.push_back (this);
m_Stream = m_Session->localDestination->CreateNewOutgoingStream (remote);
m_Stream->Send ((uint8_t *)m_Buffer, 0); // connect
I2PReceive ();
SendMessageReply (SAM_STREAM_STATUS_OK, strlen(SAM_STREAM_STATUS_OK), false);
}
2014-10-07 16:07:10 +00:00
void SAMSocket::HandleStreamDestinationRequestTimer (const boost::system::error_code& ecode, i2p::data::IdentHash ident)
{
if (!ecode) // timeout expired
{
2014-10-12 20:22:14 +00:00
auto leaseSet = m_Session->localDestination->FindLeaseSet (ident);
if (leaseSet)
2014-10-07 16:07:10 +00:00
Connect (*leaseSet);
2014-09-25 17:58:09 +00:00
else
{
LogPrint ("SAM destination to connect not found");
2014-09-28 13:05:37 +00:00
SendMessageReply (SAM_STREAM_STATUS_CANT_REACH_PEER, strlen(SAM_STREAM_STATUS_CANT_REACH_PEER), true);
2014-09-25 17:58:09 +00:00
}
}
2014-09-26 19:40:57 +00:00
}
2014-10-03 19:08:41 +00:00
void SAMSocket::HandleNamingLookupDestinationRequestTimer (const boost::system::error_code& ecode, i2p::data::IdentHash ident)
{
if (!ecode) // timeout expired
{
2014-10-12 20:22:14 +00:00
auto leaseSet = m_Session->localDestination->FindLeaseSet (ident);
2014-10-03 19:08:41 +00:00
if (leaseSet)
SendNamingLookupReply (leaseSet);
else
{
LogPrint ("SAM name destination not found");
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_KEY_NOT_FOUND, (ident.ToBase32 () + ".b32.i2p").c_str ());
SendMessageReply (m_Buffer, len, false);
}
}
}
2014-09-26 19:40:57 +00:00
void SAMSocket::ProcessStreamAccept (char * buf, size_t len)
{
2014-09-28 13:05:37 +00:00
LogPrint ("SAM stream accept: ", buf);
2014-09-26 19:40:57 +00:00
std::map<std::string, std::string> params;
ExtractParams (buf, len, params);
std::string& id = params[SAM_PARAM_ID];
2014-09-29 18:18:06 +00:00
std::string& silent = params[SAM_PARAM_SILENT];
if (silent == SAM_VALUE_TRUE) m_IsSilent = true;
2014-09-26 19:40:57 +00:00
m_ID = id;
2014-10-07 16:07:10 +00:00
m_Session = m_Owner.FindSession (id);
if (m_Session)
2014-09-26 19:40:57 +00:00
{
2014-10-07 16:07:10 +00:00
if (!m_Session->localDestination->IsAcceptorSet ())
2014-09-26 19:40:57 +00:00
{
m_SocketType = eSAMSocketTypeAcceptor;
2014-10-07 16:07:10 +00:00
m_Session->sockets.push_back (this);
m_Session->localDestination->SetAcceptor (std::bind (&SAMSocket::HandleI2PAccept, this, std::placeholders::_1));
2014-09-28 13:05:37 +00:00
SendMessageReply (SAM_STREAM_STATUS_OK, strlen(SAM_STREAM_STATUS_OK), false);
2014-09-26 19:40:57 +00:00
}
else
2014-09-28 13:05:37 +00:00
SendMessageReply (SAM_STREAM_STATUS_I2P_ERROR, strlen(SAM_STREAM_STATUS_I2P_ERROR), true);
2014-09-26 19:40:57 +00:00
}
else
2014-09-28 13:05:37 +00:00
SendMessageReply (SAM_STREAM_STATUS_INVALID_ID, strlen(SAM_STREAM_STATUS_INVALID_ID), true);
2014-09-25 17:22:25 +00:00
}
2014-09-30 15:08:38 +00:00
void SAMSocket::ProcessDestGenerate ()
{
LogPrint ("SAM dest generate");
auto localDestination = i2p::client::context.CreateNewLocalDestination ();
2014-09-30 15:08:38 +00:00
if (localDestination)
{
uint8_t buf[1024];
char priv[1024], pub[1024];
size_t l = localDestination->GetPrivateKeys ().ToBuffer (buf, 1024);
size_t l1 = i2p::data::ByteStreamToBase64 (buf, l, priv, 1024);
priv[l1] = 0;
l = localDestination->GetIdentity ().ToBuffer (buf, 1024);
l1 = i2p::data::ByteStreamToBase64 (buf, l, pub, 1024);
pub[l1] = 0;
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_DEST_REPLY, pub, priv);
SendMessageReply (m_Buffer, len, true);
}
else
SendMessageReply (SAM_DEST_REPLY_I2P_ERROR, strlen(SAM_DEST_REPLY_I2P_ERROR), true);
}
2014-10-02 20:55:01 +00:00
void SAMSocket::ProcessNamingLookup (char * buf, size_t len)
{
LogPrint ("SAM naming lookup: ", buf);
std::map<std::string, std::string> params;
ExtractParams (buf, len, params);
std::string& name = params[SAM_PARAM_NAME];
2014-10-03 19:08:41 +00:00
i2p::data::IdentHash ident;
if (name == "ME")
SendNamingLookupReply (nullptr);
2014-10-15 21:45:30 +00:00
else if (m_Session && i2p::data::netdb.GetAddressBook ().GetIdentHash (name, ident))
2014-10-02 20:55:01 +00:00
{
2014-10-12 20:22:14 +00:00
auto leaseSet = m_Session->localDestination->FindLeaseSet (ident);
2014-10-03 19:08:41 +00:00
if (leaseSet)
SendNamingLookupReply (leaseSet);
else
{
i2p::data::netdb.RequestDestination (ident, true, m_Session->localDestination->GetTunnelPool ());
2014-10-03 19:08:41 +00:00
m_Timer.expires_from_now (boost::posix_time::seconds(SAM_NAMING_LOOKUP_TIMEOUT));
m_Timer.async_wait (boost::bind (&SAMSocket::HandleNamingLookupDestinationRequestTimer,
this, boost::asio::placeholders::error, ident));
}
2014-10-02 20:55:01 +00:00
}
else
{
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_INVALID_KEY, name.c_str());
2014-10-03 19:08:41 +00:00
SendMessageReply (m_Buffer, len, false);
2014-10-02 20:55:01 +00:00
}
}
2014-10-12 20:22:14 +00:00
void SAMSocket::SendNamingLookupReply (const i2p::data::LeaseSet * leaseSet)
2014-10-03 19:08:41 +00:00
{
uint8_t buf[1024];
char pub[1024];
const i2p::data::IdentityEx& identity = leaseSet ? leaseSet->GetIdentity () : m_Session->localDestination->GetIdentity ();
size_t l = identity.ToBuffer (buf, 1024);
size_t l1 = i2p::data::ByteStreamToBase64 (buf, l, pub, 1024);
pub[l1] = 0;
size_t l2 = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY, pub);
SendMessageReply (m_Buffer, l2, false);
}
2014-09-25 17:22:25 +00:00
void SAMSocket::ExtractParams (char * buf, size_t len, std::map<std::string, std::string>& params)
{
2014-10-03 13:43:18 +00:00
char * separator;
do
2014-09-25 17:22:25 +00:00
{
2014-10-03 13:43:18 +00:00
separator = strchr (buf, ' ');
if (separator) *separator = 0;
2014-09-25 17:22:25 +00:00
char * value = strchr (buf, '=');
if (value)
{
*value = 0;
value++;
params[buf] = value;
}
2014-09-28 13:05:37 +00:00
buf = separator + 1;
2014-09-25 17:22:25 +00:00
}
2014-10-03 13:43:18 +00:00
while (separator);
2014-09-25 17:22:25 +00:00
}
2014-09-24 18:59:03 +00:00
void SAMSocket::Receive ()
{
m_Socket.async_read_some (boost::asio::buffer(m_Buffer, SAM_SOCKET_BUFFER_SIZE),
2014-10-02 20:55:01 +00:00
boost::bind((m_SocketType == eSAMSocketTypeSession) ? &SAMSocket::HandleMessage : &SAMSocket::HandleReceived,
this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
2014-09-24 18:59:03 +00:00
}
void SAMSocket::HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred)
{
if (ecode)
{
LogPrint ("SAM read error: ", ecode.message ());
if (ecode != boost::asio::error::operation_aborted)
Terminate ();
}
else
{
if (m_Stream)
2014-10-02 01:18:41 +00:00
m_Stream->Send ((uint8_t *)m_Buffer, bytes_transferred);
2014-09-24 18:59:03 +00:00
Receive ();
}
}
2014-09-26 19:40:57 +00:00
void SAMSocket::I2PReceive ()
2014-09-24 18:59:03 +00:00
{
if (m_Stream)
m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE),
2014-09-26 19:40:57 +00:00
boost::bind (&SAMSocket::HandleI2PReceive, this,
2014-09-24 18:59:03 +00:00
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred),
SAM_SOCKET_CONNECTION_MAX_IDLE);
}
2014-09-26 19:40:57 +00:00
void SAMSocket::HandleI2PReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred)
2014-09-24 18:59:03 +00:00
{
if (ecode)
{
LogPrint ("SAM stream read error: ", ecode.message ());
if (ecode != boost::asio::error::operation_aborted)
2014-10-12 20:22:14 +00:00
Terminate ();
2014-09-24 18:59:03 +00:00
}
else
{
boost::asio::async_write (m_Socket, boost::asio::buffer (m_StreamBuffer, bytes_transferred),
2014-09-26 19:40:57 +00:00
boost::bind (&SAMSocket::HandleWriteI2PData, this, boost::asio::placeholders::error));
2014-09-24 18:59:03 +00:00
}
}
2014-09-26 19:40:57 +00:00
void SAMSocket::HandleWriteI2PData (const boost::system::error_code& ecode)
2014-09-24 18:59:03 +00:00
{
if (ecode)
{
LogPrint ("SAM socket write error: ", ecode.message ());
if (ecode != boost::asio::error::operation_aborted)
Terminate ();
}
else
2014-09-26 19:40:57 +00:00
I2PReceive ();
2014-09-24 18:59:03 +00:00
}
2014-09-26 19:40:57 +00:00
void SAMSocket::HandleI2PAccept (i2p::stream::Stream * stream)
{
2014-09-29 18:18:06 +00:00
if (stream)
{
2014-10-05 20:19:25 +00:00
LogPrint ("SAM incoming I2P connection for session ", m_ID);
2014-09-29 18:18:06 +00:00
m_Stream = stream;
auto session = m_Owner.FindSession (m_ID);
if (session)
session->localDestination->ResetAcceptor ();
if (!m_IsSilent)
{
// send remote peer address
uint8_t ident[1024];
size_t l = stream->GetRemoteIdentity ().ToBuffer (ident, 1024);
size_t l1 = i2p::data::ByteStreamToBase64 (ident, l, m_Buffer, SAM_SOCKET_BUFFER_SIZE);
m_Buffer[l1] = '\n';
SendMessageReply (m_Buffer, l1 + 1, false);
}
I2PReceive ();
}
2014-09-26 19:40:57 +00:00
}
2014-09-24 16:01:26 +00:00
SAMBridge::SAMBridge (int port):
m_IsRunning (false), m_Thread (nullptr),
m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)),
m_NewSocket (nullptr)
{
}
SAMBridge::~SAMBridge ()
{
Stop ();
delete m_NewSocket;
}
void SAMBridge::Start ()
{
Accept ();
2014-09-28 13:05:37 +00:00
m_IsRunning = true;
2014-09-24 16:01:26 +00:00
m_Thread = new std::thread (std::bind (&SAMBridge::Run, this));
}
void SAMBridge::Stop ()
{
m_IsRunning = false;
m_Service.stop ();
if (m_Thread)
{
m_Thread->join ();
delete m_Thread;
m_Thread = nullptr;
}
}
void SAMBridge::Run ()
{
while (m_IsRunning)
{
try
{
m_Service.run ();
}
catch (std::exception& ex)
{
LogPrint ("SAM: ", ex.what ());
}
}
}
void SAMBridge::Accept ()
{
2014-09-24 18:59:03 +00:00
m_NewSocket = new SAMSocket (*this);
m_Acceptor.async_accept (m_NewSocket->GetSocket (), boost::bind (&SAMBridge::HandleAccept, this,
2014-09-24 16:01:26 +00:00
boost::asio::placeholders::error));
}
void SAMBridge::HandleAccept(const boost::system::error_code& ecode)
{
if (!ecode)
{
2014-09-24 18:59:03 +00:00
LogPrint ("New SAM connection from ", m_NewSocket->GetSocket ().remote_endpoint ());
m_NewSocket->ReceiveHandshake ();
2014-09-24 16:01:26 +00:00
}
else
{
2014-09-28 13:05:37 +00:00
LogPrint ("SAM accept error: ", ecode.message ());
2014-09-24 16:01:26 +00:00
delete m_NewSocket;
m_NewSocket = nullptr;
}
if (ecode != boost::asio::error::operation_aborted)
Accept ();
}
2014-09-24 20:39:31 +00:00
2014-09-25 17:22:25 +00:00
SAMSession * SAMBridge::CreateSession (const std::string& id, const std::string& destination)
2014-09-24 20:39:31 +00:00
{
i2p::stream::StreamingDestination * localDestination = nullptr;
2014-09-25 17:22:25 +00:00
if (destination != "")
2014-09-24 20:39:31 +00:00
{
2014-09-25 17:22:25 +00:00
uint8_t * buf = new uint8_t[destination.length ()];
size_t l = i2p::data::Base64ToByteStream (destination.c_str (), destination.length (), buf, destination.length ());
2014-09-24 20:39:31 +00:00
i2p::data::PrivateKeys keys;
keys.FromBuffer (buf, l);
delete[] buf;
localDestination = i2p::client::context.CreateNewLocalDestination (keys);
2014-09-24 20:39:31 +00:00
}
else // transient
localDestination = i2p::client::context.CreateNewLocalDestination ();
2014-09-24 20:39:31 +00:00
if (localDestination)
{
SAMSession session;
session.localDestination = localDestination;
2014-10-06 01:59:05 +00:00
std::unique_lock<std::mutex> l(m_SessionsMutex);
auto ret = m_Sessions.insert (std::pair<std::string, SAMSession>(id, session));
if (!ret.second)
LogPrint ("Session ", id, " already exists");
return &(ret.first->second);
2014-09-24 20:39:31 +00:00
}
2014-09-25 17:22:25 +00:00
return nullptr;
2014-09-24 20:39:31 +00:00
}
void SAMBridge::CloseSession (const std::string& id)
{
2014-10-06 01:59:05 +00:00
std::unique_lock<std::mutex> l(m_SessionsMutex);
2014-09-24 20:39:31 +00:00
auto it = m_Sessions.find (id);
if (it != m_Sessions.end ())
{
for (auto it1 : it->second.sockets)
delete it1;
it->second.sockets.clear ();
it->second.localDestination->Stop ();
2014-09-24 20:39:31 +00:00
m_Sessions.erase (it);
}
}
2014-09-25 17:22:25 +00:00
SAMSession * SAMBridge::FindSession (const std::string& id)
{
2014-10-06 01:59:05 +00:00
std::unique_lock<std::mutex> l(m_SessionsMutex);
2014-09-25 17:22:25 +00:00
auto it = m_Sessions.find (id);
if (it != m_Sessions.end ())
return &it->second;
return nullptr;
}
2014-09-24 16:01:26 +00:00
}
}