Browse Source

use shared_ptr for garlic messages

pull/218/head
orignal 10 years ago
parent
commit
2cbd6e85c6
  1. 4
      Datagram.cpp
  2. 10
      Destination.cpp
  3. 19
      Garlic.cpp
  4. 6
      Garlic.h
  5. 14
      NetDb.cpp
  6. 2
      NetDbRequests.cpp
  7. 8
      Streaming.cpp
  8. 2
      Streaming.h
  9. 6
      Tunnel.cpp
  10. 2
      Tunnel.h
  11. 2
      TunnelPool.cpp

4
Datagram.cpp

@ -60,12 +60,12 @@ namespace datagram @@ -60,12 +60,12 @@ namespace datagram
{
std::vector<i2p::tunnel::TunnelMessageBlock> msgs;
uint32_t i = i2p::context.GetRandomNumberGenerator ().GenerateWord32 (0, leases.size () - 1);
auto garlic = m_Owner.WrapMessage (remote, msg, true);
auto garlic = m_Owner.WrapMessage (remote, ToSharedI2NPMessage (msg), true);
msgs.push_back (i2p::tunnel::TunnelMessageBlock
{
i2p::tunnel::eDeliveryTypeTunnel,
leases[i].tunnelGateway, leases[i].tunnelID,
ToSharedI2NPMessage (garlic)
garlic
});
outboundTunnel->SendTunnelDataMsg (msgs);
}

10
Destination.cpp

@ -393,7 +393,7 @@ namespace client @@ -393,7 +393,7 @@ namespace client
m_ExcludedFloodfills.insert (floodfill->GetIdentHash ());
LogPrint (eLogDebug, "Publish LeaseSet of ", GetIdentHash ().ToBase32 ());
m_PublishReplyToken = i2p::context.GetRandomNumberGenerator ().GenerateWord32 ();
auto msg = WrapMessage (floodfill, i2p::CreateDatabaseStoreMsg (m_LeaseSet, m_PublishReplyToken));
auto msg = WrapMessage (floodfill, ToSharedI2NPMessage (i2p::CreateDatabaseStoreMsg (m_LeaseSet, m_PublishReplyToken)));
m_PublishConfirmationTimer.expires_from_now (boost::posix_time::seconds(PUBLISH_CONFIRMATION_TIMEOUT));
m_PublishConfirmationTimer.async_wait (std::bind (&ClientDestination::HandlePublishConfirmationTimer,
this, std::placeholders::_1));
@ -581,15 +581,15 @@ namespace client @@ -581,15 +581,15 @@ namespace client
rnd.GenerateBlock (replyTag, 32); // random session tag
AddSessionKey (replyKey, replyTag);
I2NPMessage * msg = WrapMessage (nextFloodfill,
CreateLeaseSetDatabaseLookupMsg (dest, request->excluded,
replyTunnel.get (), replyKey, replyTag));
auto msg = WrapMessage (nextFloodfill,
ToSharedI2NPMessage (CreateLeaseSetDatabaseLookupMsg (dest, request->excluded,
replyTunnel.get (), replyKey, replyTag)));
outboundTunnel->SendTunnelDataMsg (
{
i2p::tunnel::TunnelMessageBlock
{
i2p::tunnel::eDeliveryTypeRouter,
nextFloodfill->GetIdentHash (), 0, ToSharedI2NPMessage (msg)
nextFloodfill->GetIdentHash (), 0, msg
}
});
request->requestTimeoutTimer.expires_from_now (boost::posix_time::seconds(LEASESET_REQUEST_TIMEOUT));

19
Garlic.cpp

@ -107,9 +107,9 @@ namespace garlic @@ -107,9 +107,9 @@ namespace garlic
return !m_SessionTags.empty () || m_UnconfirmedTagsMsgs.empty ();
}
I2NPMessage * GarlicRoutingSession::WrapSingleMessage (I2NPMessage * msg)
std::shared_ptr<I2NPMessage> GarlicRoutingSession::WrapSingleMessage (std::shared_ptr<I2NPMessage> msg)
{
I2NPMessage * m = NewI2NPMessage ();
auto m = ToSharedI2NPMessage(NewI2NPMessage ());
m->Align (12); // in order to get buf aligned to 16 (12 + 4)
size_t len = 0;
uint8_t * buf = m->GetPayload () + 4; // 4 bytes for length
@ -164,12 +164,10 @@ namespace garlic @@ -164,12 +164,10 @@ namespace garlic
len += 32;
}
// AES block
len += CreateAESBlock (buf, msg);
len += CreateAESBlock (buf, msg.get ()); // TODO
htobe32buf (m->GetPayload (), len);
m->len += len + 4;
FillI2NPMessageHeader (m, eI2NPGarlic);
if (msg)
DeleteI2NPMessage (msg);
FillI2NPMessageHeader (m.get (), eI2NPGarlic); // TODO
return m;
}
@ -309,7 +307,7 @@ namespace garlic @@ -309,7 +307,7 @@ namespace garlic
htobe32buf (buf + size, inboundTunnel->GetNextTunnelID ()); // tunnelID
size += 4;
// create msg
I2NPMessage * msg = CreateDeliveryStatusMsg (msgID);
auto msg = ToSharedI2NPMessage (CreateDeliveryStatusMsg (msgID));
if (m_Owner)
{
//encrypt
@ -322,7 +320,6 @@ namespace garlic @@ -322,7 +320,6 @@ namespace garlic
}
memcpy (buf + size, msg->GetBuffer (), msg->GetLength ());
size += msg->GetLength ();
DeleteI2NPMessage (msg);
// fill clove
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 5000; // 5 sec
htobe32buf (buf + size, m_Rnd.GenerateWord32 ()); // CloveID
@ -512,7 +509,7 @@ namespace garlic @@ -512,7 +509,7 @@ namespace garlic
if (tunnel) // we have send it through an outbound tunnel
{
I2NPMessage * msg = CreateI2NPMessage (buf, GetI2NPMessageLength (buf), from);
tunnel->SendTunnelDataMsg (gwHash, gwTunnel, msg);
tunnel->SendTunnelDataMsg (gwHash, gwTunnel, ToSharedI2NPMessage (msg));
}
else
LogPrint ("No outbound tunnels available for garlic clove");
@ -537,8 +534,8 @@ namespace garlic @@ -537,8 +534,8 @@ namespace garlic
}
}
I2NPMessage * GarlicDestination::WrapMessage (std::shared_ptr<const i2p::data::RoutingDestination> destination,
I2NPMessage * msg, bool attachLeaseSet)
std::shared_ptr<I2NPMessage> GarlicDestination::WrapMessage (std::shared_ptr<const i2p::data::RoutingDestination> destination,
std::shared_ptr<I2NPMessage> msg, bool attachLeaseSet)
{
auto session = GetRoutingSession (destination, attachLeaseSet); // 32 tags by default
return session->WrapSingleMessage (msg);

6
Garlic.h

@ -80,7 +80,7 @@ namespace garlic @@ -80,7 +80,7 @@ namespace garlic
int numTags, bool attachLeaseSet);
GarlicRoutingSession (const uint8_t * sessionKey, const SessionTag& sessionTag); // one time encryption
~GarlicRoutingSession ();
I2NPMessage * WrapSingleMessage (I2NPMessage * msg);
std::shared_ptr<I2NPMessage> WrapSingleMessage (std::shared_ptr<I2NPMessage> msg);
void MessageConfirmed (uint32_t msgID);
bool CleanupExpiredTags (); // returns true if something left
@ -126,8 +126,8 @@ namespace garlic @@ -126,8 +126,8 @@ namespace garlic
std::shared_ptr<GarlicRoutingSession> GetRoutingSession (std::shared_ptr<const i2p::data::RoutingDestination> destination, bool attachLeaseSet);
void CleanupRoutingSessions ();
void RemoveCreatedSession (uint32_t msgID);
I2NPMessage * WrapMessage (std::shared_ptr<const i2p::data::RoutingDestination> destination,
I2NPMessage * msg, bool attachLeaseSet = false);
std::shared_ptr<I2NPMessage> WrapMessage (std::shared_ptr<const i2p::data::RoutingDestination> destination,
std::shared_ptr<I2NPMessage> msg, bool attachLeaseSet = false);
void AddSessionKey (const uint8_t * key, const uint8_t * tag); // one tag
virtual bool SubmitSessionKey (const uint8_t * key, const uint8_t * tag); // from different thread

14
NetDb.cpp

@ -477,7 +477,7 @@ namespace data @@ -477,7 +477,7 @@ namespace data
auto pool = i2p::tunnel::tunnels.GetExploratoryPool ();
auto outbound = pool ? pool->GetNextOutboundTunnel () : nullptr;
if (outbound)
outbound->SendTunnelDataMsg (buf + offset, tunnelID, deliveryStatus);
outbound->SendTunnelDataMsg (buf + offset, tunnelID, ToSharedI2NPMessage (deliveryStatus));
else
{
LogPrint (eLogError, "No outbound tunnels for DatabaseStore reply found");
@ -665,7 +665,7 @@ namespace data @@ -665,7 +665,7 @@ namespace data
numExcluded = 0; // TODO:
}
I2NPMessage * replyMsg = nullptr;
std::shared_ptr<I2NPMessage> replyMsg;
if (lookupType == DATABASE_LOOKUP_TYPE_EXPLORATORY_LOOKUP)
{
LogPrint ("Exploratory close to ", key, " ", numExcluded, " excluded");
@ -685,7 +685,7 @@ namespace data @@ -685,7 +685,7 @@ namespace data
excludedRouters.insert (r->GetIdentHash ());
}
}
replyMsg = CreateDatabaseSearchReply (ident, routers);
replyMsg = ToSharedI2NPMessage (CreateDatabaseSearchReply (ident, routers));
}
else
{
@ -698,7 +698,7 @@ namespace data @@ -698,7 +698,7 @@ namespace data
LogPrint ("Requested RouterInfo ", key, " found");
router->LoadBuffer ();
if (router->GetBuffer ())
replyMsg = CreateDatabaseStoreMsg (router);
replyMsg = ToSharedI2NPMessage (CreateDatabaseStoreMsg (router));
}
}
@ -709,7 +709,7 @@ namespace data @@ -709,7 +709,7 @@ namespace data
if (leaseSet) // we don't send back our LeaseSets
{
LogPrint ("Requested LeaseSet ", key, " found");
replyMsg = CreateDatabaseStoreMsg (leaseSet);
replyMsg = ToSharedI2NPMessage (CreateDatabaseStoreMsg (leaseSet));
}
}
@ -722,7 +722,7 @@ namespace data @@ -722,7 +722,7 @@ namespace data
excludedRouters.insert (excluded);
excluded += 32;
}
replyMsg = CreateDatabaseSearchReply (ident, GetClosestFloodfills (ident, 3, excludedRouters));
replyMsg = ToSharedI2NPMessage (CreateDatabaseSearchReply (ident, GetClosestFloodfills (ident, 3, excludedRouters)));
}
}
@ -747,7 +747,7 @@ namespace data @@ -747,7 +747,7 @@ namespace data
if (outbound)
outbound->SendTunnelDataMsg (buf+32, replyTunnelID, replyMsg);
else
transports.SendMessage (buf+32, i2p::CreateTunnelGatewayMsg (replyTunnelID, ToSharedI2NPMessage(replyMsg)));
transports.SendMessage (buf+32, i2p::CreateTunnelGatewayMsg (replyTunnelID, replyMsg));
}
else
transports.SendMessage (buf+32, replyMsg);

2
NetDbRequests.cpp

@ -118,7 +118,7 @@ namespace data @@ -118,7 +118,7 @@ namespace data
auto nextFloodfill = netdb.GetClosestFloodfill (dest->GetDestination (), dest->GetExcludedPeers ());
if (nextFloodfill && outbound && inbound)
outbound->SendTunnelDataMsg (nextFloodfill->GetIdentHash (), 0,
dest->CreateRequestMessage (nextFloodfill, inbound));
ToSharedI2NPMessage (dest->CreateRequestMessage (nextFloodfill, inbound)));
else
{
done = true;

8
Streaming.cpp

@ -617,7 +617,7 @@ namespace stream @@ -617,7 +617,7 @@ namespace stream
{
i2p::tunnel::eDeliveryTypeTunnel,
m_CurrentRemoteLease.tunnelGateway, m_CurrentRemoteLease.tunnelID,
ToSharedI2NPMessage (msg)
msg
});
m_NumSentBytes += it->GetLength ();
}
@ -761,9 +761,9 @@ namespace stream @@ -761,9 +761,9 @@ namespace stream
m_CurrentRemoteLease.endDate = 0;
}
I2NPMessage * Stream::CreateDataMessage (const uint8_t * payload, size_t len)
std::shared_ptr<I2NPMessage> Stream::CreateDataMessage (const uint8_t * payload, size_t len)
{
I2NPMessage * msg = NewI2NPShortMessage ();
auto msg = ToSharedI2NPMessage (NewI2NPShortMessage ());
CryptoPP::Gzip compressor;
if (len <= i2p::stream::COMPRESSION_THRESHOLD_SIZE)
compressor.SetDeflateLevel (CryptoPP::Gzip::MIN_DEFLATE_LEVEL);
@ -780,7 +780,7 @@ namespace stream @@ -780,7 +780,7 @@ namespace stream
htobe16buf (buf + 6, m_Port); // destination port
buf[9] = i2p::client::PROTOCOL_TYPE_STREAMING; // streaming protocol
msg->len += size + 4;
FillI2NPMessageHeader (msg, eI2NPData);
FillI2NPMessageHeader (msg.get (), eI2NPData); // TODO:
return msg;
}

2
Streaming.h

@ -156,7 +156,7 @@ namespace stream @@ -156,7 +156,7 @@ namespace stream
void HandleResendTimer (const boost::system::error_code& ecode);
void HandleAckSendTimer (const boost::system::error_code& ecode);
I2NPMessage * CreateDataMessage (const uint8_t * payload, size_t len);
std::shared_ptr<I2NPMessage> CreateDataMessage (const uint8_t * payload, size_t len);
private:

6
Tunnel.cpp

@ -81,7 +81,7 @@ namespace tunnel @@ -81,7 +81,7 @@ namespace tunnel
// send message
if (outboundTunnel)
outboundTunnel->SendTunnelDataMsg (GetNextIdentHash (), 0, msg);
outboundTunnel->SendTunnelDataMsg (GetNextIdentHash (), 0, ToSharedI2NPMessage (msg));
else
i2p::transport::transports.SendMessage (GetNextIdentHash (), ToSharedI2NPMessage (msg));
}
@ -164,7 +164,7 @@ namespace tunnel @@ -164,7 +164,7 @@ namespace tunnel
m_Endpoint.HandleDecryptedTunnelDataMsg (msg);
}
void OutboundTunnel::SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg)
void OutboundTunnel::SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, std::shared_ptr<i2p::I2NPMessage> msg)
{
TunnelMessageBlock block;
if (gwHash)
@ -180,7 +180,7 @@ namespace tunnel @@ -180,7 +180,7 @@ namespace tunnel
}
else
block.deliveryType = eDeliveryTypeLocal;
block.data = ToSharedI2NPMessage (msg);
block.data = msg;
std::unique_lock<std::mutex> l(m_SendMutex);
m_Gateway.SendTunnelDataMsg (block);

2
Tunnel.h

@ -83,7 +83,7 @@ namespace tunnel @@ -83,7 +83,7 @@ namespace tunnel
OutboundTunnel (std::shared_ptr<const TunnelConfig> config): Tunnel (config), m_Gateway (this) {};
void SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg);
void SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, std::shared_ptr<i2p::I2NPMessage> msg);
void SendTunnelDataMsg (const std::vector<TunnelMessageBlock>& msgs); // multiple messages
std::shared_ptr<const i2p::data::RouterInfo> GetEndpointRouter () const
{ return GetTunnelConfig ()->GetLastHop ()->router; };

2
TunnelPool.cpp

@ -254,7 +254,7 @@ namespace tunnel @@ -254,7 +254,7 @@ namespace tunnel
uint32_t msgID = rnd.GenerateWord32 ();
m_Tests[msgID] = std::make_pair (*it1, *it2);
(*it1)->SendTunnelDataMsg ((*it2)->GetNextIdentHash (), (*it2)->GetNextTunnelID (),
CreateDeliveryStatusMsg (msgID));
ToSharedI2NPMessage (CreateDeliveryStatusMsg (msgID)));
it1++; it2++;
}
}

Loading…
Cancel
Save