Browse Source

use shared_ptr for transit tunnels

pull/401/head
orignal 9 years ago
parent
commit
79190f313d
  1. 4
      HTTPServer.cpp
  2. 3
      I2NPProtocol.cpp
  3. 8
      TransitTunnel.cpp
  4. 2
      TransitTunnel.h
  5. 19
      Tunnel.cpp
  6. 8
      Tunnel.h

4
HTTPServer.cpp

@ -619,9 +619,9 @@ namespace util
s << "<b>Transit tunnels:</b><br>\r\n<br>\r\n"; s << "<b>Transit tunnels:</b><br>\r\n<br>\r\n";
for (auto it: i2p::tunnel::tunnels.GetTransitTunnels ()) for (auto it: i2p::tunnel::tunnels.GetTransitTunnels ())
{ {
if (dynamic_cast<i2p::tunnel::TransitTunnelGateway *>(it.second)) if (std::dynamic_pointer_cast<i2p::tunnel::TransitTunnelGateway>(it.second))
s << it.second->GetTunnelID () << ""; s << it.second->GetTunnelID () << "";
else if (dynamic_cast<i2p::tunnel::TransitTunnelEndpoint *>(it.second)) else if (std::dynamic_pointer_cast<i2p::tunnel::TransitTunnelEndpoint>(it.second))
s << "" << it.second->GetTunnelID (); s << "" << it.second->GetTunnelID ();
else else
s << "" << it.second->GetTunnelID () << ""; s << "" << it.second->GetTunnelID () << "";

3
I2NPProtocol.cpp

@ -301,8 +301,7 @@ namespace i2p
i2p::tunnel::tunnels.GetTransitTunnels ().size () <= MAX_NUM_TRANSIT_TUNNELS && i2p::tunnel::tunnels.GetTransitTunnels ().size () <= MAX_NUM_TRANSIT_TUNNELS &&
!i2p::transport::transports.IsBandwidthExceeded ()) !i2p::transport::transports.IsBandwidthExceeded ())
{ {
i2p::tunnel::TransitTunnel * transitTunnel = auto transitTunnel = i2p::tunnel::CreateTransitTunnel (
i2p::tunnel::CreateTransitTunnel (
bufbe32toh (clearText + BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET), bufbe32toh (clearText + BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET),
clearText + BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET, clearText + BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET,
bufbe32toh (clearText + BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET), bufbe32toh (clearText + BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET),

8
TransitTunnel.cpp

@ -85,7 +85,7 @@ namespace tunnel
m_Endpoint.HandleDecryptedTunnelDataMsg (newMsg); m_Endpoint.HandleDecryptedTunnelDataMsg (newMsg);
} }
TransitTunnel * CreateTransitTunnel (uint32_t receiveTunnelID, std::shared_ptr<TransitTunnel> CreateTransitTunnel (uint32_t receiveTunnelID,
const uint8_t * nextIdent, uint32_t nextTunnelID, const uint8_t * nextIdent, uint32_t nextTunnelID,
const uint8_t * layerKey,const uint8_t * ivKey, const uint8_t * layerKey,const uint8_t * ivKey,
bool isGateway, bool isEndpoint) bool isGateway, bool isEndpoint)
@ -93,17 +93,17 @@ namespace tunnel
if (isEndpoint) if (isEndpoint)
{ {
LogPrint (eLogInfo, "TransitTunnel: endpoint ", receiveTunnelID, " created"); LogPrint (eLogInfo, "TransitTunnel: endpoint ", receiveTunnelID, " created");
return new TransitTunnelEndpoint (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey); return std::make_shared<TransitTunnelEndpoint> (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
} }
else if (isGateway) else if (isGateway)
{ {
LogPrint (eLogInfo, "TransitTunnel: gateway ", receiveTunnelID, " created"); LogPrint (eLogInfo, "TransitTunnel: gateway ", receiveTunnelID, " created");
return new TransitTunnelGateway (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey); return std::make_shared<TransitTunnelGateway> (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
} }
else else
{ {
LogPrint (eLogInfo, "TransitTunnel: ", receiveTunnelID, "->", nextTunnelID, " created"); LogPrint (eLogInfo, "TransitTunnel: ", receiveTunnelID, "->", nextTunnelID, " created");
return new TransitTunnelParticipant (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey); return std::make_shared<TransitTunnelParticipant> (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
} }
} }
} }

2
TransitTunnel.h

@ -93,7 +93,7 @@ namespace tunnel
TunnelEndpoint m_Endpoint; TunnelEndpoint m_Endpoint;
}; };
TransitTunnel * CreateTransitTunnel (uint32_t receiveTunnelID, std::shared_ptr<TransitTunnel> CreateTransitTunnel (uint32_t receiveTunnelID,
const uint8_t * nextIdent, uint32_t nextTunnelID, const uint8_t * nextIdent, uint32_t nextTunnelID,
const uint8_t * layerKey,const uint8_t * ivKey, const uint8_t * layerKey,const uint8_t * ivKey,
bool isGateway, bool isEndpoint); bool isGateway, bool isEndpoint);

19
Tunnel.cpp

@ -257,9 +257,6 @@ namespace tunnel
Tunnels::~Tunnels () Tunnels::~Tunnels ()
{ {
for (auto& it : m_TransitTunnels)
delete it.second;
m_TransitTunnels.clear ();
} }
std::shared_ptr<InboundTunnel> Tunnels::GetInboundTunnel (uint32_t tunnelID) std::shared_ptr<InboundTunnel> Tunnels::GetInboundTunnel (uint32_t tunnelID)
@ -270,7 +267,7 @@ namespace tunnel
return nullptr; return nullptr;
} }
TransitTunnel * Tunnels::GetTransitTunnel (uint32_t tunnelID) std::shared_ptr<TransitTunnel> Tunnels::GetTransitTunnel (uint32_t tunnelID)
{ {
auto it = m_TransitTunnels.find(tunnelID); auto it = m_TransitTunnels.find(tunnelID);
if (it != m_TransitTunnels.end ()) if (it != m_TransitTunnels.end ())
@ -363,14 +360,11 @@ namespace tunnel
} }
} }
void Tunnels::AddTransitTunnel (TransitTunnel * tunnel) void Tunnels::AddTransitTunnel (std::shared_ptr<TransitTunnel> tunnel)
{ {
std::unique_lock<std::mutex> l(m_TransitTunnelsMutex); std::unique_lock<std::mutex> l(m_TransitTunnelsMutex);
if (!m_TransitTunnels.insert (std::make_pair (tunnel->GetTunnelID (), tunnel)).second) if (!m_TransitTunnels.insert (std::make_pair (tunnel->GetTunnelID (), tunnel)).second)
{
LogPrint (eLogError, "Tunnel: transit tunnel with id ", tunnel->GetTunnelID (), " already exists"); LogPrint (eLogError, "Tunnel: transit tunnel with id ", tunnel->GetTunnelID (), " already exists");
delete tunnel;
}
} }
void Tunnels::Start () void Tunnels::Start ()
@ -404,10 +398,10 @@ namespace tunnel
if (msg) if (msg)
{ {
uint32_t prevTunnelID = 0, tunnelID = 0; uint32_t prevTunnelID = 0, tunnelID = 0;
TunnelBase * prevTunnel = nullptr; std::shared_ptr<TunnelBase> prevTunnel;
do do
{ {
TunnelBase * tunnel = nullptr; std::shared_ptr<TunnelBase> tunnel;
uint8_t typeID = msg->GetTypeID (); uint8_t typeID = msg->GetTypeID ();
switch (typeID) switch (typeID)
{ {
@ -421,7 +415,7 @@ namespace tunnel
prevTunnel->FlushTunnelDataMsgs (); prevTunnel->FlushTunnelDataMsgs ();
if (!tunnel && typeID == eI2NPTunnelData) if (!tunnel && typeID == eI2NPTunnelData)
tunnel = GetInboundTunnel (tunnelID).get (); tunnel = GetInboundTunnel (tunnelID);
if (!tunnel) if (!tunnel)
tunnel = GetTransitTunnel (tunnelID); tunnel = GetTransitTunnel (tunnelID);
if (tunnel) if (tunnel)
@ -471,7 +465,7 @@ namespace tunnel
} }
} }
void Tunnels::HandleTunnelGatewayMsg (TunnelBase * tunnel, std::shared_ptr<I2NPMessage> msg) void Tunnels::HandleTunnelGatewayMsg (std::shared_ptr<TunnelBase> tunnel, std::shared_ptr<I2NPMessage> msg)
{ {
if (!tunnel) if (!tunnel)
{ {
@ -690,7 +684,6 @@ namespace tunnel
std::unique_lock<std::mutex> l(m_TransitTunnelsMutex); std::unique_lock<std::mutex> l(m_TransitTunnelsMutex);
it = m_TransitTunnels.erase (it); it = m_TransitTunnels.erase (it);
} }
delete tmp;
} }
else else
it++; it++;

8
Tunnel.h

@ -142,9 +142,9 @@ namespace tunnel
std::shared_ptr<InboundTunnel> GetNextInboundTunnel (); std::shared_ptr<InboundTunnel> GetNextInboundTunnel ();
std::shared_ptr<OutboundTunnel> GetNextOutboundTunnel (); std::shared_ptr<OutboundTunnel> GetNextOutboundTunnel ();
std::shared_ptr<TunnelPool> GetExploratoryPool () const { return m_ExploratoryPool; }; std::shared_ptr<TunnelPool> GetExploratoryPool () const { return m_ExploratoryPool; };
TransitTunnel * GetTransitTunnel (uint32_t tunnelID); std::shared_ptr<TransitTunnel> GetTransitTunnel (uint32_t tunnelID);
int GetTransitTunnelsExpirationTimeout (); int GetTransitTunnelsExpirationTimeout ();
void AddTransitTunnel (TransitTunnel * tunnel); void AddTransitTunnel (std::shared_ptr<TransitTunnel> tunnel);
void AddOutboundTunnel (std::shared_ptr<OutboundTunnel> newTunnel); void AddOutboundTunnel (std::shared_ptr<OutboundTunnel> newTunnel);
void AddInboundTunnel (std::shared_ptr<InboundTunnel> newTunnel); void AddInboundTunnel (std::shared_ptr<InboundTunnel> newTunnel);
void PostTunnelData (std::shared_ptr<I2NPMessage> msg); void PostTunnelData (std::shared_ptr<I2NPMessage> msg);
@ -163,7 +163,7 @@ namespace tunnel
template<class TTunnel> template<class TTunnel>
std::shared_ptr<TTunnel> GetPendingTunnel (uint32_t replyMsgID, const std::map<uint32_t, std::shared_ptr<TTunnel> >& pendingTunnels); std::shared_ptr<TTunnel> GetPendingTunnel (uint32_t replyMsgID, const std::map<uint32_t, std::shared_ptr<TTunnel> >& pendingTunnels);
void HandleTunnelGatewayMsg (TunnelBase * tunnel, std::shared_ptr<I2NPMessage> msg); void HandleTunnelGatewayMsg (std::shared_ptr<TunnelBase> tunnel, std::shared_ptr<I2NPMessage> msg);
void Run (); void Run ();
void ManageTunnels (); void ManageTunnels ();
@ -186,7 +186,7 @@ namespace tunnel
std::map<uint32_t, std::shared_ptr<InboundTunnel> > m_InboundTunnels; std::map<uint32_t, std::shared_ptr<InboundTunnel> > m_InboundTunnels;
std::list<std::shared_ptr<OutboundTunnel> > m_OutboundTunnels; std::list<std::shared_ptr<OutboundTunnel> > m_OutboundTunnels;
std::mutex m_TransitTunnelsMutex; std::mutex m_TransitTunnelsMutex;
std::map<uint32_t, TransitTunnel *> m_TransitTunnels; std::map<uint32_t, std::shared_ptr<TransitTunnel> > m_TransitTunnels;
std::mutex m_PoolsMutex; std::mutex m_PoolsMutex;
std::list<std::shared_ptr<TunnelPool>> m_Pools; std::list<std::shared_ptr<TunnelPool>> m_Pools;
std::shared_ptr<TunnelPool> m_ExploratoryPool; std::shared_ptr<TunnelPool> m_ExploratoryPool;

Loading…
Cancel
Save