|
|
@ -415,12 +415,17 @@ namespace client
|
|
|
|
{
|
|
|
|
{
|
|
|
|
session->UDPEndpoint = forward;
|
|
|
|
session->UDPEndpoint = forward;
|
|
|
|
auto dest = session->GetLocalDestination ()->CreateDatagramDestination ();
|
|
|
|
auto dest = session->GetLocalDestination ()->CreateDatagramDestination ();
|
|
|
|
|
|
|
|
auto port = std::stoi(params[SAM_PARAM_PORT]);
|
|
|
|
if (type == eSAMSessionTypeDatagram)
|
|
|
|
if (type == eSAMSessionTypeDatagram)
|
|
|
|
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),
|
|
|
|
|
|
|
|
port
|
|
|
|
|
|
|
|
);
|
|
|
|
else // raw
|
|
|
|
else // raw
|
|
|
|
dest->SetRawReceiver (std::bind (&SAMSocket::HandleI2PRawDatagramReceive, shared_from_this (),
|
|
|
|
dest->SetRawReceiver (std::bind (&SAMSocket::HandleI2PRawDatagramReceive, shared_from_this (),
|
|
|
|
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
|
|
|
|
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4),
|
|
|
|
|
|
|
|
port
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (session->GetLocalDestination ()->IsReady ())
|
|
|
|
if (session->GetLocalDestination ()->IsReady ())
|
|
|
@ -524,7 +529,7 @@ namespace client
|
|
|
|
if (addr->IsIdentHash ())
|
|
|
|
if (addr->IsIdentHash ())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (session->GetLocalDestination ()->GetIdentHash () != addr->identHash)
|
|
|
|
if (session->GetLocalDestination ()->GetIdentHash () != addr->identHash)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
auto leaseSet = session->GetLocalDestination ()->FindLeaseSet(addr->identHash);
|
|
|
|
auto leaseSet = session->GetLocalDestination ()->FindLeaseSet(addr->identHash);
|
|
|
|
if (leaseSet)
|
|
|
|
if (leaseSet)
|
|
|
|
Connect(leaseSet, session);
|
|
|
|
Connect(leaseSet, session);
|
|
|
@ -556,7 +561,7 @@ namespace client
|
|
|
|
if (session)
|
|
|
|
if (session)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (session->GetLocalDestination ()->SupportsEncryptionType (remote->GetEncryptionType ()))
|
|
|
|
if (session->GetLocalDestination ()->SupportsEncryptionType (remote->GetEncryptionType ()))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
m_SocketType = eSAMSocketTypeStream;
|
|
|
|
m_SocketType = eSAMSocketTypeStream;
|
|
|
|
m_Stream = session->GetLocalDestination ()->CreateStream (remote);
|
|
|
|
m_Stream = session->GetLocalDestination ()->CreateStream (remote);
|
|
|
|
if (m_Stream)
|
|
|
|
if (m_Stream)
|
|
|
@ -570,7 +575,7 @@ namespace client
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
SendStreamCantReachPeer ("Incompatible crypto");
|
|
|
|
SendStreamCantReachPeer ("Incompatible crypto");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
|
@ -583,7 +588,7 @@ namespace client
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "SAM: Destination to connect not found");
|
|
|
|
LogPrint (eLogError, "SAM: Destination to connect not found");
|
|
|
|
SendStreamCantReachPeer ("LeaseSet not found");
|
|
|
|
SendStreamCantReachPeer ("LeaseSet not found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -612,27 +617,27 @@ namespace client
|
|
|
|
session->GetLocalDestination ()->AcceptOnce (std::bind (&SAMSocket::HandleI2PAccept, shared_from_this (), std::placeholders::_1));
|
|
|
|
session->GetLocalDestination ()->AcceptOnce (std::bind (&SAMSocket::HandleI2PAccept, shared_from_this (), std::placeholders::_1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
while (!session->acceptQueue.empty () && session->acceptQueue.front ().second + SAM_SESSION_MAX_ACCEPT_INTERVAL > ts)
|
|
|
|
while (!session->acceptQueue.empty () && session->acceptQueue.front ().second + SAM_SESSION_MAX_ACCEPT_INTERVAL > ts)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
auto socket = session->acceptQueue.front ().first;
|
|
|
|
auto socket = session->acceptQueue.front ().first;
|
|
|
|
session->acceptQueue.pop_front ();
|
|
|
|
session->acceptQueue.pop_front ();
|
|
|
|
if (socket)
|
|
|
|
if (socket)
|
|
|
|
m_Owner.GetService ().post (std::bind(&SAMSocket::TerminateClose, socket));
|
|
|
|
m_Owner.GetService ().post (std::bind(&SAMSocket::TerminateClose, socket));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (session->acceptQueue.size () < SAM_SESSION_MAX_ACCEPT_QUEUE_SIZE)
|
|
|
|
if (session->acceptQueue.size () < SAM_SESSION_MAX_ACCEPT_QUEUE_SIZE)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// already accepting, queue up
|
|
|
|
// already accepting, queue up
|
|
|
|
SendMessageReply (SAM_STREAM_STATUS_OK, strlen(SAM_STREAM_STATUS_OK), false);
|
|
|
|
SendMessageReply (SAM_STREAM_STATUS_OK, strlen(SAM_STREAM_STATUS_OK), false);
|
|
|
|
session->acceptQueue.push_back (std::make_pair(shared_from_this(), ts));
|
|
|
|
session->acceptQueue.push_back (std::make_pair(shared_from_this(), ts));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
LogPrint (eLogInfo, "SAM: Session ", m_ID, " accept queue is full ", session->acceptQueue.size ());
|
|
|
|
LogPrint (eLogInfo, "SAM: Session ", m_ID, " accept queue is full ", session->acceptQueue.size ());
|
|
|
|
SendStreamI2PError ("Already accepting");
|
|
|
|
SendStreamI2PError ("Already accepting");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
|
@ -875,8 +880,8 @@ namespace client
|
|
|
|
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, reply, msg.c_str());
|
|
|
|
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, reply, msg.c_str());
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
SendMessageReply (m_Buffer, len, true);
|
|
|
|
SendMessageReply (m_Buffer, len, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SAMSocket::SendSessionI2PError(const std::string & msg)
|
|
|
|
void SAMSocket::SendSessionI2PError(const std::string & msg)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "SAM: Session I2P error: ", msg);
|
|
|
|
LogPrint (eLogError, "SAM: Session I2P error: ", msg);
|
|
|
@ -886,14 +891,14 @@ namespace client
|
|
|
|
void SAMSocket::SendStreamI2PError(const std::string & msg)
|
|
|
|
void SAMSocket::SendStreamI2PError(const std::string & msg)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "SAM: Stream I2P error: ", msg);
|
|
|
|
LogPrint (eLogError, "SAM: Stream I2P error: ", msg);
|
|
|
|
SendReplyWithMessage (SAM_STREAM_STATUS_I2P_ERROR, msg);
|
|
|
|
SendReplyWithMessage (SAM_STREAM_STATUS_I2P_ERROR, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SAMSocket::SendStreamCantReachPeer(const std::string & msg)
|
|
|
|
void SAMSocket::SendStreamCantReachPeer(const std::string & msg)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
SendReplyWithMessage (SAM_STREAM_STATUS_CANT_REACH_PEER, msg);
|
|
|
|
SendReplyWithMessage (SAM_STREAM_STATUS_CANT_REACH_PEER, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SAMSocket::HandleNamingLookupLeaseSetRequestComplete (std::shared_ptr<i2p::data::LeaseSet> leaseSet, std::string name)
|
|
|
|
void SAMSocket::HandleNamingLookupLeaseSetRequestComplete (std::shared_ptr<i2p::data::LeaseSet> leaseSet, std::string name)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (leaseSet)
|
|
|
|
if (leaseSet)
|
|
|
@ -1093,14 +1098,14 @@ namespace client
|
|
|
|
// pending acceptors
|
|
|
|
// pending acceptors
|
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
while (!session->acceptQueue.empty () && session->acceptQueue.front ().second + SAM_SESSION_MAX_ACCEPT_INTERVAL > ts)
|
|
|
|
while (!session->acceptQueue.empty () && session->acceptQueue.front ().second + SAM_SESSION_MAX_ACCEPT_INTERVAL > ts)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
auto socket = session->acceptQueue.front ().first;
|
|
|
|
auto socket = session->acceptQueue.front ().first;
|
|
|
|
session->acceptQueue.pop_front ();
|
|
|
|
session->acceptQueue.pop_front ();
|
|
|
|
if (socket)
|
|
|
|
if (socket)
|
|
|
|
m_Owner.GetService ().post (std::bind(&SAMSocket::TerminateClose, socket));
|
|
|
|
m_Owner.GetService ().post (std::bind(&SAMSocket::TerminateClose, socket));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!session->acceptQueue.empty ())
|
|
|
|
if (!session->acceptQueue.empty ())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
auto socket = session->acceptQueue.front ().first;
|
|
|
|
auto socket = session->acceptQueue.front ().first;
|
|
|
|
session->acceptQueue.pop_front ();
|
|
|
|
session->acceptQueue.pop_front ();
|
|
|
|
if (socket && socket->GetSocketType () == eSAMSocketTypeAcceptor)
|
|
|
|
if (socket && socket->GetSocketType () == eSAMSocketTypeAcceptor)
|
|
|
@ -1108,7 +1113,7 @@ namespace client
|
|
|
|
socket->m_IsAccepting = true;
|
|
|
|
socket->m_IsAccepting = true;
|
|
|
|
session->GetLocalDestination ()->AcceptOnce (std::bind (&SAMSocket::HandleI2PAccept, socket, std::placeholders::_1));
|
|
|
|
session->GetLocalDestination ()->AcceptOnce (std::bind (&SAMSocket::HandleI2PAccept, socket, std::placeholders::_1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!m_IsSilent)
|
|
|
|
if (!m_IsSilent)
|
|
|
|
{
|
|
|
|
{
|
|
|
|