Browse Source

use shared_ptr for Address

pull/433/head
orignal 9 years ago
parent
commit
996f61efe1
  1. 10
      HTTPServer.cpp
  2. 14
      NTCPSession.cpp
  3. 36
      RouterContext.cpp
  4. 79
      RouterInfo.cpp
  5. 12
      RouterInfo.h
  6. 10
      Transports.cpp

10
HTTPServer.cpp

@ -427,18 +427,18 @@ namespace util
s << " (" << i2p::transport::transports.GetOutBandwidth () <<" Bps)<br>\r\n"; s << " (" << i2p::transport::transports.GetOutBandwidth () <<" Bps)<br>\r\n";
s << "<b>Data path:</b> " << i2p::fs::GetDataDir() << "<br>\r\n<br>\r\n"; s << "<b>Data path:</b> " << i2p::fs::GetDataDir() << "<br>\r\n<br>\r\n";
s << "<b>Our external address:</b>" << "<br>\r\n" ; s << "<b>Our external address:</b>" << "<br>\r\n" ;
for (auto& address : i2p::context.GetRouterInfo().GetAddresses()) for (auto address : i2p::context.GetRouterInfo().GetAddresses())
{ {
switch (address.transportStyle) switch (address->transportStyle)
{ {
case i2p::data::RouterInfo::eTransportNTCP: case i2p::data::RouterInfo::eTransportNTCP:
if (address.host.is_v6 ()) if (address->host.is_v6 ())
s << "NTCP6&nbsp;&nbsp;"; s << "NTCP6&nbsp;&nbsp;";
else else
s << "NTCP&nbsp;&nbsp;"; s << "NTCP&nbsp;&nbsp;";
break; break;
case i2p::data::RouterInfo::eTransportSSU: case i2p::data::RouterInfo::eTransportSSU:
if (address.host.is_v6 ()) if (address->host.is_v6 ())
s << "SSU6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; s << "SSU6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
else else
s << "SSU&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; s << "SSU&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
@ -446,7 +446,7 @@ namespace util
default: default:
s << "Unknown&nbsp;&nbsp;"; s << "Unknown&nbsp;&nbsp;";
} }
s << address.host.to_string() << ":" << address.port << "<br>\r\n"; s << address->host.to_string() << ":" << address->port << "<br>\r\n";
} }
s << "<br>\r\n<b>Routers:</b> " << i2p::data::netdb.GetNumRouters () << " "; s << "<br>\r\n<b>Routers:</b> " << i2p::data::netdb.GetNumRouters () << " ";
s << "<b>Floodfills:</b> " << i2p::data::netdb.GetNumFloodfills () << " "; s << "<b>Floodfills:</b> " << i2p::data::netdb.GetNumFloodfills () << " ";

14
NTCPSession.cpp

@ -759,15 +759,15 @@ namespace transport
m_IsRunning = true; m_IsRunning = true;
m_Thread = new std::thread (std::bind (&NTCPServer::Run, this)); m_Thread = new std::thread (std::bind (&NTCPServer::Run, this));
// create acceptors // create acceptors
auto addresses = context.GetRouterInfo ().GetAddresses (); auto& addresses = context.GetRouterInfo ().GetAddresses ();
for (auto& address : addresses) for (auto address: addresses)
{ {
if (address.transportStyle == i2p::data::RouterInfo::eTransportNTCP && address.host.is_v4 ()) if (address->transportStyle == i2p::data::RouterInfo::eTransportNTCP && address->host.is_v4 ())
{ {
m_NTCPAcceptor = new boost::asio::ip::tcp::acceptor (m_Service, m_NTCPAcceptor = new boost::asio::ip::tcp::acceptor (m_Service,
boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), address.port)); boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), address->port));
LogPrint (eLogInfo, "NTCP: Start listening TCP port ", address.port); LogPrint (eLogInfo, "NTCP: Start listening TCP port ", address->port);
auto conn = std::make_shared<NTCPSession>(*this); auto conn = std::make_shared<NTCPSession>(*this);
m_NTCPAcceptor->async_accept(conn->GetSocket (), std::bind (&NTCPServer::HandleAccept, this, m_NTCPAcceptor->async_accept(conn->GetSocket (), std::bind (&NTCPServer::HandleAccept, this,
conn, std::placeholders::_1)); conn, std::placeholders::_1));
@ -777,10 +777,10 @@ namespace transport
m_NTCPV6Acceptor = new boost::asio::ip::tcp::acceptor (m_Service); m_NTCPV6Acceptor = new boost::asio::ip::tcp::acceptor (m_Service);
m_NTCPV6Acceptor->open (boost::asio::ip::tcp::v6()); m_NTCPV6Acceptor->open (boost::asio::ip::tcp::v6());
m_NTCPV6Acceptor->set_option (boost::asio::ip::v6_only (true)); m_NTCPV6Acceptor->set_option (boost::asio::ip::v6_only (true));
m_NTCPV6Acceptor->bind (boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v6(), address.port)); m_NTCPV6Acceptor->bind (boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v6(), address->port));
m_NTCPV6Acceptor->listen (); m_NTCPV6Acceptor->listen ();
LogPrint (eLogInfo, "NTCP: Start listening V6 TCP port ", address.port); LogPrint (eLogInfo, "NTCP: Start listening V6 TCP port ", address->port);
auto conn = std::make_shared<NTCPSession> (*this); auto conn = std::make_shared<NTCPSession> (*this);
m_NTCPV6Acceptor->async_accept(conn->GetSocket (), std::bind (&NTCPServer::HandleAcceptV6, m_NTCPV6Acceptor->async_accept(conn->GetSocket (), std::bind (&NTCPServer::HandleAcceptV6,
this, conn, std::placeholders::_1)); this, conn, std::placeholders::_1));

36
RouterContext.cpp

@ -92,11 +92,11 @@ namespace i2p
void RouterContext::UpdatePort (int port) void RouterContext::UpdatePort (int port)
{ {
bool updated = false; bool updated = false;
for (auto& address : m_RouterInfo.GetAddresses ()) for (auto address : m_RouterInfo.GetAddresses ())
{ {
if (address.port != port) if (address->port != port)
{ {
address.port = port; address->port = port;
updated = true; updated = true;
} }
} }
@ -107,11 +107,11 @@ namespace i2p
void RouterContext::UpdateAddress (const boost::asio::ip::address& host) void RouterContext::UpdateAddress (const boost::asio::ip::address& host)
{ {
bool updated = false; bool updated = false;
for (auto& address : m_RouterInfo.GetAddresses ()) for (auto address : m_RouterInfo.GetAddresses ())
{ {
if (address.host != host && address.IsCompatible (host)) if (address->host != host && address->IsCompatible (host))
{ {
address.host = host; address->host = host;
updated = true; updated = true;
} }
} }
@ -206,15 +206,15 @@ namespace i2p
auto& addresses = m_RouterInfo.GetAddresses (); auto& addresses = m_RouterInfo.GetAddresses ();
for (size_t i = 0; i < addresses.size (); i++) for (size_t i = 0; i < addresses.size (); i++)
{ {
if (addresses[i].transportStyle == i2p::data::RouterInfo::eTransportNTCP) if (addresses[i]->transportStyle == i2p::data::RouterInfo::eTransportNTCP)
{ {
addresses.erase (addresses.begin () + i); addresses.erase (addresses.begin () + i);
break; break;
} }
} }
// delete previous introducers // delete previous introducers
for (auto& addr : addresses) for (auto addr : addresses)
addr.introducers.clear (); addr->introducers.clear ();
// update // update
UpdateRouterInfo (); UpdateRouterInfo ();
@ -235,16 +235,16 @@ namespace i2p
auto& addresses = m_RouterInfo.GetAddresses (); auto& addresses = m_RouterInfo.GetAddresses ();
for (size_t i = 0; i < addresses.size (); i++) for (size_t i = 0; i < addresses.size (); i++)
{ {
if (addresses[i].transportStyle == i2p::data::RouterInfo::eTransportSSU) if (addresses[i]->transportStyle == i2p::data::RouterInfo::eTransportSSU)
{ {
// insert NTCP address with host/port from SSU // insert NTCP address with host/port from SSU
m_RouterInfo.AddNTCPAddress (addresses[i].host.to_string ().c_str (), addresses[i].port); m_RouterInfo.AddNTCPAddress (addresses[i]->host.to_string ().c_str (), addresses[i]->port);
break; break;
} }
} }
// delete previous introducers // delete previous introducers
for (auto& addr : addresses) for (auto addr : addresses)
addr.introducers.clear (); addr->introducers.clear ();
// update // update
UpdateRouterInfo (); UpdateRouterInfo ();
@ -264,19 +264,19 @@ namespace i2p
bool updated = false, found = false; bool updated = false, found = false;
int port = 0; int port = 0;
auto& addresses = m_RouterInfo.GetAddresses (); auto& addresses = m_RouterInfo.GetAddresses ();
for (auto& addr : addresses) for (auto addr: addresses)
{ {
if (addr.host.is_v6 () && addr.transportStyle == i2p::data::RouterInfo::eTransportNTCP) if (addr->host.is_v6 () && addr->transportStyle == i2p::data::RouterInfo::eTransportNTCP)
{ {
if (addr.host != host) if (addr->host != host)
{ {
addr.host = host; addr->host = host;
updated = true; updated = true;
} }
found = true; found = true;
} }
else else
port = addr.port; port = addr->port;
} }
if (!found) if (!found)
{ {

79
RouterInfo.cpp

@ -232,7 +232,7 @@ namespace data
} }
if (isValidAddress) if (isValidAddress)
{ {
m_Addresses.push_back(address); m_Addresses.push_back(std::make_shared<Address>(address));
m_SupportedTransports |= supportedTransports; m_SupportedTransports |= supportedTransports;
} }
} }
@ -359,8 +359,9 @@ namespace data
// addresses // addresses
uint8_t numAddresses = m_Addresses.size (); uint8_t numAddresses = m_Addresses.size ();
s.write ((char *)&numAddresses, sizeof (numAddresses)); s.write ((char *)&numAddresses, sizeof (numAddresses));
for (auto& address : m_Addresses) for (auto addr : m_Addresses)
{ {
Address& address = *addr;
s.write ((char *)&address.cost, sizeof (address.cost)); s.write ((char *)&address.cost, sizeof (address.cost));
s.write ((char *)&address.date, sizeof (address.date)); s.write ((char *)&address.date, sizeof (address.date));
std::stringstream properties; std::stringstream properties;
@ -543,46 +544,46 @@ namespace data
void RouterInfo::AddNTCPAddress (const char * host, int port) void RouterInfo::AddNTCPAddress (const char * host, int port)
{ {
Address addr; auto addr = std::make_shared<Address>();
addr.host = boost::asio::ip::address::from_string (host); addr->host = boost::asio::ip::address::from_string (host);
addr.port = port; addr->port = port;
addr.transportStyle = eTransportNTCP; addr->transportStyle = eTransportNTCP;
addr.cost = 2; addr->cost = 2;
addr.date = 0; addr->date = 0;
addr.mtu = 0; addr->mtu = 0;
for (auto it: m_Addresses) // don't insert same address twice for (auto it: m_Addresses) // don't insert same address twice
if (it == addr) return; if (it == addr) return;
m_Addresses.push_back(addr); m_Addresses.push_back(addr);
m_SupportedTransports |= addr.host.is_v6 () ? eNTCPV6 : eNTCPV4; m_SupportedTransports |= addr->host.is_v6 () ? eNTCPV6 : eNTCPV4;
} }
void RouterInfo::AddSSUAddress (const char * host, int port, const uint8_t * key, int mtu) void RouterInfo::AddSSUAddress (const char * host, int port, const uint8_t * key, int mtu)
{ {
Address addr; auto addr = std::make_shared<Address>();
addr.host = boost::asio::ip::address::from_string (host); addr->host = boost::asio::ip::address::from_string (host);
addr.port = port; addr->port = port;
addr.transportStyle = eTransportSSU; addr->transportStyle = eTransportSSU;
addr.cost = 10; // NTCP should have priority over SSU addr->cost = 10; // NTCP should have priority over SSU
addr.date = 0; addr->date = 0;
addr.mtu = mtu; addr->mtu = mtu;
memcpy (addr.key, key, 32); memcpy (addr->key, key, 32);
for (auto it: m_Addresses) // don't insert same address twice for (auto it: m_Addresses) // don't insert same address twice
if (it == addr) return; if (it == addr) return;
m_Addresses.push_back(addr); m_Addresses.push_back(addr);
m_SupportedTransports |= addr.host.is_v6 () ? eSSUV6 : eSSUV4; m_SupportedTransports |= addr->host.is_v6 () ? eSSUV6 : eSSUV4;
m_Caps |= eSSUTesting; m_Caps |= eSSUTesting;
m_Caps |= eSSUIntroducer; m_Caps |= eSSUIntroducer;
} }
bool RouterInfo::AddIntroducer (const Introducer& introducer) bool RouterInfo::AddIntroducer (const Introducer& introducer)
{ {
for (auto& addr : m_Addresses) for (auto addr : m_Addresses)
{ {
if (addr.transportStyle == eTransportSSU && addr.host.is_v4 ()) if (addr->transportStyle == eTransportSSU && addr->host.is_v4 ())
{ {
for (auto intro: addr.introducers) for (auto intro: addr->introducers)
if (intro.iTag == introducer.iTag) return false; // already presented if (intro.iTag == introducer.iTag) return false; // already presented
addr.introducers.push_back (introducer); addr->introducers.push_back (introducer);
return true; return true;
} }
} }
@ -591,14 +592,14 @@ namespace data
bool RouterInfo::RemoveIntroducer (const boost::asio::ip::udp::endpoint& e) bool RouterInfo::RemoveIntroducer (const boost::asio::ip::udp::endpoint& e)
{ {
for (auto& addr : m_Addresses) for (auto addr: m_Addresses)
{ {
if (addr.transportStyle == eTransportSSU && addr.host.is_v4 ()) if (addr->transportStyle == eTransportSSU && addr->host.is_v4 ())
{ {
for (std::vector<Introducer>::iterator it = addr.introducers.begin (); it != addr.introducers.end (); it++) for (std::vector<Introducer>::iterator it = addr->introducers.begin (); it != addr->introducers.end (); it++)
if ( boost::asio::ip::udp::endpoint (it->iHost, it->iPort) == e) if ( boost::asio::ip::udp::endpoint (it->iHost, it->iPort) == e)
{ {
addr.introducers.erase (it); addr->introducers.erase (it);
return true; return true;
} }
} }
@ -664,8 +665,8 @@ namespace data
m_SupportedTransports &= ~eNTCPV6; m_SupportedTransports &= ~eNTCPV6;
for (size_t i = 0; i < m_Addresses.size (); i++) for (size_t i = 0; i < m_Addresses.size (); i++)
{ {
if (m_Addresses[i].transportStyle == i2p::data::RouterInfo::eTransportNTCP && if (m_Addresses[i]->transportStyle == i2p::data::RouterInfo::eTransportNTCP &&
m_Addresses[i].host.is_v6 ()) m_Addresses[i]->host.is_v6 ())
{ {
m_Addresses.erase (m_Addresses.begin () + i); m_Addresses.erase (m_Addresses.begin () + i);
break; break;
@ -676,8 +677,8 @@ namespace data
m_SupportedTransports &= ~eSSUV6; m_SupportedTransports &= ~eSSUV6;
for (size_t i = 0; i < m_Addresses.size (); i++) for (size_t i = 0; i < m_Addresses.size (); i++)
{ {
if (m_Addresses[i].transportStyle == i2p::data::RouterInfo::eTransportSSU && if (m_Addresses[i]->transportStyle == i2p::data::RouterInfo::eTransportSSU &&
m_Addresses[i].host.is_v6 ()) m_Addresses[i]->host.is_v6 ())
{ {
m_Addresses.erase (m_Addresses.begin () + i); m_Addresses.erase (m_Addresses.begin () + i);
break; break;
@ -691,29 +692,29 @@ namespace data
return m_Caps & Caps::eUnreachable; // non-reachable return m_Caps & Caps::eUnreachable; // non-reachable
} }
const RouterInfo::Address * RouterInfo::GetNTCPAddress (bool v4only) const std::shared_ptr<const RouterInfo::Address> RouterInfo::GetNTCPAddress (bool v4only) const
{ {
return GetAddress (eTransportNTCP, v4only); return GetAddress (eTransportNTCP, v4only);
} }
const RouterInfo::Address * RouterInfo::GetSSUAddress (bool v4only) const std::shared_ptr<const RouterInfo::Address> RouterInfo::GetSSUAddress (bool v4only) const
{ {
return GetAddress (eTransportSSU, v4only); return GetAddress (eTransportSSU, v4only);
} }
const RouterInfo::Address * RouterInfo::GetSSUV6Address () const std::shared_ptr<const RouterInfo::Address> RouterInfo::GetSSUV6Address () const
{ {
return GetAddress (eTransportSSU, false, true); return GetAddress (eTransportSSU, false, true);
} }
const RouterInfo::Address * RouterInfo::GetAddress (TransportStyle s, bool v4only, bool v6only) const std::shared_ptr<const RouterInfo::Address> RouterInfo::GetAddress (TransportStyle s, bool v4only, bool v6only) const
{ {
for (auto& address : m_Addresses) for (auto address : m_Addresses)
{ {
if (address.transportStyle == s) if (address->transportStyle == s)
{ {
if ((!v4only || address.host.is_v4 ()) && (!v6only || address.host.is_v6 ())) if ((!v4only || address->host.is_v4 ()) && (!v6only || address->host.is_v6 ()))
return &address; return address;
} }
} }
return nullptr; return nullptr;

12
RouterInfo.h

@ -116,10 +116,10 @@ namespace data
void SetRouterIdentity (std::shared_ptr<const IdentityEx> identity); void SetRouterIdentity (std::shared_ptr<const IdentityEx> identity);
std::string GetIdentHashBase64 () const { return GetIdentHash ().ToBase64 (); }; std::string GetIdentHashBase64 () const { return GetIdentHash ().ToBase64 (); };
uint64_t GetTimestamp () const { return m_Timestamp; }; uint64_t GetTimestamp () const { return m_Timestamp; };
std::vector<Address>& GetAddresses () { return m_Addresses; }; std::vector<std::shared_ptr<Address> >& GetAddresses () { return m_Addresses; };
const Address * GetNTCPAddress (bool v4only = true) const; std::shared_ptr<const Address> GetNTCPAddress (bool v4only = true) const;
const Address * GetSSUAddress (bool v4only = true) const; std::shared_ptr<const Address> GetSSUAddress (bool v4only = true) const;
const Address * GetSSUV6Address () const; std::shared_ptr<const Address> GetSSUV6Address () const;
void AddNTCPAddress (const char * host, int port); void AddNTCPAddress (const char * host, int port);
void AddSSUAddress (const char * host, int port, const uint8_t * key, int mtu = 0); void AddSSUAddress (const char * host, int port, const uint8_t * key, int mtu = 0);
@ -182,7 +182,7 @@ namespace data
size_t ReadString (char * str, std::istream& s); size_t ReadString (char * str, std::istream& s);
void WriteString (const std::string& str, std::ostream& s); void WriteString (const std::string& str, std::ostream& s);
void ExtractCaps (const char * value); void ExtractCaps (const char * value);
const Address * GetAddress (TransportStyle s, bool v4only, bool v6only = false) const; std::shared_ptr<const Address> GetAddress (TransportStyle s, bool v4only, bool v6only = false) const;
void UpdateCapsProperty (); void UpdateCapsProperty ();
private: private:
@ -192,7 +192,7 @@ namespace data
uint8_t * m_Buffer; uint8_t * m_Buffer;
size_t m_BufferLen; size_t m_BufferLen;
uint64_t m_Timestamp; uint64_t m_Timestamp;
std::vector<Address> m_Addresses; std::vector<std::shared_ptr<Address> > m_Addresses;
std::map<std::string, std::string> m_Properties; std::map<std::string, std::string> m_Properties;
bool m_IsUpdated, m_IsUnreachable; bool m_IsUpdated, m_IsUnreachable;
uint8_t m_SupportedTransports, m_Caps; uint8_t m_SupportedTransports, m_Caps;

10
Transports.cpp

@ -112,8 +112,8 @@ namespace transport
m_IsRunning = true; m_IsRunning = true;
m_Thread = new std::thread (std::bind (&Transports::Run, this)); m_Thread = new std::thread (std::bind (&Transports::Run, this));
// create acceptors // create acceptors
auto addresses = context.GetRouterInfo ().GetAddresses (); auto& addresses = context.GetRouterInfo ().GetAddresses ();
for (auto& address : addresses) for (auto address : addresses)
{ {
if (!m_NTCPServer) if (!m_NTCPServer)
{ {
@ -121,12 +121,12 @@ namespace transport
m_NTCPServer->Start (); m_NTCPServer->Start ();
} }
if (address.transportStyle == RouterInfo::eTransportSSU && address.host.is_v4 ()) if (address->transportStyle == RouterInfo::eTransportSSU && address->host.is_v4 ())
{ {
if (!m_SSUServer) if (!m_SSUServer)
{ {
m_SSUServer = new SSUServer (address.port); m_SSUServer = new SSUServer (address->port);
LogPrint (eLogInfo, "Transports: Start listening UDP port ", address.port); LogPrint (eLogInfo, "Transports: Start listening UDP port ", address->port);
m_SSUServer->Start (); m_SSUServer->Start ();
DetectExternalIP (); DetectExternalIP ();
} }

Loading…
Cancel
Save