Browse Source

handle DATAGRAM SEND

pull/168/head
orignal 10 years ago
parent
commit
f07d29bc8a
  1. 27
      Datagram.cpp
  2. 4
      Datagram.h
  3. 54
      SAM.cpp
  4. 7
      SAM.h

27
Datagram.cpp

@ -17,7 +17,7 @@ namespace datagram
{ {
} }
void DatagramDestination::SendDatagramTo (const uint8_t * payload, size_t len, std::shared_ptr<const i2p::data::LeaseSet> remote) void DatagramDestination::SendDatagramTo (const uint8_t * payload, size_t len, const i2p::data::IdentHash& ident)
{ {
uint8_t buf[MAX_DATAGRAM_SIZE]; uint8_t buf[MAX_DATAGRAM_SIZE];
auto identityLen = m_Owner.GetIdentity ().ToBuffer (buf, MAX_DATAGRAM_SIZE); auto identityLen = m_Owner.GetIdentity ().ToBuffer (buf, MAX_DATAGRAM_SIZE);
@ -35,11 +35,30 @@ namespace datagram
} }
else else
m_Owner.Sign (buf1, len, signature); m_Owner.Sign (buf1, len, signature);
m_Owner.GetService ().post (std::bind (&DatagramDestination::SendMsg, this, auto msg = CreateDataMessage (buf, len + headerLen);
CreateDataMessage (buf, len + headerLen), remote)); auto remote = m_Owner.FindLeaseSet (ident);
if (remote)
m_Owner.GetService ().post (std::bind (&DatagramDestination::SendMsg, this, msg, remote));
else
m_Owner.RequestDestination (ident, std::bind (&DatagramDestination::HandleLeaseSetRequestComplete,
this, std::placeholders::_1, msg, ident));
} }
void DatagramDestination::HandleLeaseSetRequestComplete (bool success, I2NPMessage * msg, i2p::data::IdentHash ident)
{
if (success)
{
auto remote = m_Owner.FindLeaseSet (ident);
if (remote)
{
SendMsg (msg, remote);
return;
}
}
DeleteI2NPMessage (msg);
}
void DatagramDestination::SendMsg (I2NPMessage * msg, std::shared_ptr<const i2p::data::LeaseSet> remote) void DatagramDestination::SendMsg (I2NPMessage * msg, std::shared_ptr<const i2p::data::LeaseSet> remote)
{ {
auto outboundTunnel = m_Owner.GetTunnelPool ()->GetNextOutboundTunnel (); auto outboundTunnel = m_Owner.GetTunnelPool ()->GetNextOutboundTunnel ();

4
Datagram.h

@ -26,7 +26,7 @@ namespace datagram
DatagramDestination (i2p::client::ClientDestination& owner); DatagramDestination (i2p::client::ClientDestination& owner);
~DatagramDestination () {}; ~DatagramDestination () {};
void SendDatagramTo (const uint8_t * payload, size_t len, std::shared_ptr<const i2p::data::LeaseSet> remote); void SendDatagramTo (const uint8_t * payload, size_t len, const i2p::data::IdentHash& ident);
void HandleDataMessagePayload (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len); void HandleDataMessagePayload (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);
void SetReceiver (const Receiver& receiver) { m_Receiver = receiver; }; void SetReceiver (const Receiver& receiver) { m_Receiver = receiver; };
@ -34,6 +34,8 @@ namespace datagram
private: private:
void HandleLeaseSetRequestComplete (bool success, I2NPMessage * msg, i2p::data::IdentHash ident);
I2NPMessage * CreateDataMessage (const uint8_t * payload, size_t len); I2NPMessage * CreateDataMessage (const uint8_t * payload, size_t len);
void SendMsg (I2NPMessage * msg, std::shared_ptr<const i2p::data::LeaseSet> remote); void SendMsg (I2NPMessage * msg, std::shared_ptr<const i2p::data::LeaseSet> remote);
void HandleDatagram (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len); void HandleDatagram (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);

54
SAM.cpp

@ -102,7 +102,7 @@ namespace client
{ {
separator++; separator++;
std::map<std::string, std::string> params; std::map<std::string, std::string> params;
ExtractParams (separator, bytes_transferred - (separator - m_Buffer), params); ExtractParams (separator, params);
auto it = params.find (SAM_PARAM_MAX); auto it = params.find (SAM_PARAM_MAX);
// TODO: check MIN as well // TODO: check MIN as well
if (it != params.end ()) if (it != params.end ())
@ -208,6 +208,8 @@ namespace client
ProcessStreamConnect (separator + 1, bytes_transferred - (separator - m_Buffer) - 1); ProcessStreamConnect (separator + 1, bytes_transferred - (separator - m_Buffer) - 1);
else if (!strcmp (m_Buffer, SAM_STREAM_ACCEPT)) else if (!strcmp (m_Buffer, SAM_STREAM_ACCEPT))
ProcessStreamAccept (separator + 1, bytes_transferred - (separator - m_Buffer) - 1); ProcessStreamAccept (separator + 1, bytes_transferred - (separator - m_Buffer) - 1);
else if (!strcmp (m_Buffer, SAM_DATAGRAM_SEND))
ProcessDatagramSend (separator + 1, bytes_transferred - (separator - m_Buffer) - 1, eol + 1);
else if (!strcmp (m_Buffer, SAM_DEST_GENERATE)) else if (!strcmp (m_Buffer, SAM_DEST_GENERATE))
ProcessDestGenerate (); ProcessDestGenerate ();
else if (!strcmp (m_Buffer, SAM_NAMING_LOOKUP)) else if (!strcmp (m_Buffer, SAM_NAMING_LOOKUP))
@ -236,7 +238,7 @@ namespace client
{ {
LogPrint ("SAM session create: ", buf); LogPrint ("SAM session create: ", buf);
std::map<std::string, std::string> params; std::map<std::string, std::string> params;
ExtractParams (buf, len, 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];
@ -307,7 +309,7 @@ namespace client
{ {
LogPrint ("SAM stream connect: ", buf); LogPrint ("SAM stream connect: ", buf);
std::map<std::string, std::string> params; std::map<std::string, std::string> params;
ExtractParams (buf, len, params); ExtractParams (buf, params);
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];
@ -361,7 +363,7 @@ namespace client
{ {
LogPrint ("SAM stream accept: ", buf); LogPrint ("SAM stream accept: ", buf);
std::map<std::string, std::string> params; std::map<std::string, std::string> params;
ExtractParams (buf, len, 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;
@ -383,6 +385,35 @@ 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);
} }
void SAMSocket::ProcessDatagramSend (char * buf, size_t len, const char * data)
{
LogPrint ("SAM datagram send: ", buf);
std::map<std::string, std::string> params;
ExtractParams (buf, params);
size_t size = boost::lexical_cast<int>(params[SAM_PARAM_SIZE]);
if (size < len)
{
if (m_Session)
{
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
LogPrint (eLogError, "SAM missing datagram destination");
}
else
LogPrint (eLogError, "SAM session is not created from DATAGRAM SEND");
}
else
LogPrint (eLogError, "SAM datagram size ", size, " exceeds buffer");
// since it's SAM v1 reply is not expected
Receive ();
}
void SAMSocket::ProcessDestGenerate () void SAMSocket::ProcessDestGenerate ()
{ {
LogPrint ("SAM dest generate"); LogPrint ("SAM dest generate");
@ -401,7 +432,7 @@ namespace client
{ {
LogPrint ("SAM naming lookup: ", buf); LogPrint ("SAM naming lookup: ", buf);
std::map<std::string, std::string> params; std::map<std::string, std::string> params;
ExtractParams (buf, len, params); ExtractParams (buf, params);
std::string& name = params[SAM_PARAM_NAME]; std::string& name = params[SAM_PARAM_NAME];
i2p::data::IdentityEx identity; i2p::data::IdentityEx identity;
i2p::data::IdentHash ident; i2p::data::IdentHash ident;
@ -467,7 +498,7 @@ namespace client
SendMessageReply (m_Buffer, l, false); SendMessageReply (m_Buffer, l, false);
} }
void SAMSocket::ExtractParams (char * buf, size_t len, 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
@ -779,15 +810,8 @@ namespace client
{ {
i2p::data::IdentityEx dest; i2p::data::IdentityEx dest;
dest.FromBase64 (destination); dest.FromBase64 (destination);
auto leaseSet = i2p::data::netdb.FindLeaseSet (dest.GetIdentHash ()); session->localDestination->GetDatagramDestination ()->
if (leaseSet) SendDatagramTo ((uint8_t *)eol, payloadLen, dest.GetIdentHash ());
session->localDestination->GetDatagramDestination ()->
SendDatagramTo ((uint8_t *)eol, payloadLen, leaseSet);
else
{
LogPrint ("SAM datagram destination not found");
session->localDestination->RequestDestination (dest.GetIdentHash ());
}
} }
else else
LogPrint ("Session ", sessionID, " not found"); LogPrint ("Session ", sessionID, " not found");

7
SAM.h

@ -34,6 +34,7 @@ namespace client
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_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";
@ -49,7 +50,8 @@ namespace client
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_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";
@ -101,9 +103,10 @@ namespace client
void ProcessSessionCreate (char * buf, size_t len); void ProcessSessionCreate (char * buf, size_t len);
void ProcessStreamConnect (char * buf, size_t len); void ProcessStreamConnect (char * buf, size_t len);
void ProcessStreamAccept (char * buf, size_t len); void ProcessStreamAccept (char * buf, size_t len);
void ProcessDatagramSend (char * buf, size_t len, const char * data); // from SAM 1.0
void ProcessDestGenerate (); void ProcessDestGenerate ();
void ProcessNamingLookup (char * buf, size_t len); void ProcessNamingLookup (char * buf, size_t len);
void ExtractParams (char * buf, size_t len, 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);
void HandleConnectLeaseSetRequestComplete (bool success, i2p::data::IdentHash ident); void HandleConnectLeaseSetRequestComplete (bool success, i2p::data::IdentHash ident);

Loading…
Cancel
Save