Browse Source

Merge pull request #4 from orignal/master

Merge pull request from orignal/master
pull/12/head
chertov 11 years ago
parent
commit
27e448290d
  1. 4
      NTCPSession.cpp
  2. 2
      NTCPSession.h
  3. 4
      RouterContext.cpp
  4. 29
      RouterInfo.cpp
  5. 6
      RouterInfo.h
  6. 2
      Transports.cpp

4
NTCPSession.cpp

@ -517,10 +517,10 @@ namespace ntcp
} }
NTCPClient::NTCPClient (boost::asio::io_service& service, const char * address, NTCPClient::NTCPClient (boost::asio::io_service& service, const boost::asio::ip::address& address,
int port, i2p::data::RouterInfo& in_RouterInfo): int port, i2p::data::RouterInfo& in_RouterInfo):
NTCPSession (service, in_RouterInfo), NTCPSession (service, in_RouterInfo),
m_Endpoint (boost::asio::ip::address::from_string (address), port) m_Endpoint (address, port)
{ {
Connect (); Connect ();
} }

2
NTCPSession.h

@ -144,7 +144,7 @@ namespace ntcp
{ {
public: public:
NTCPClient (boost::asio::io_service& service, const char * address, int port, i2p::data::RouterInfo& in_RouterInfo); NTCPClient (boost::asio::io_service& service, const boost::asio::ip::address& address, int port, i2p::data::RouterInfo& in_RouterInfo);
private: private:

4
RouterContext.cpp

@ -45,7 +45,7 @@ namespace i2p
auto address = m_RouterInfo.GetNTCPAddress (); auto address = m_RouterInfo.GetNTCPAddress ();
if (address) if (address)
{ {
address->host = host; address->host = boost::asio::ip::address::from_string (host);
address->port = port; address->port = port;
} }
@ -80,4 +80,4 @@ namespace i2p
std::ofstream fi(ROUTER_INFO, std::ios::binary); std::ofstream fi(ROUTER_INFO, std::ios::binary);
fi.write ((char *)m_RouterInfo.GetBuffer (), m_RouterInfo.GetBufferLen ()); fi.write ((char *)m_RouterInfo.GetBuffer (), m_RouterInfo.GetBufferLen ());
} }
} }

29
RouterInfo.cpp

@ -101,7 +101,16 @@ namespace data
r += ReadString (value, s); r += ReadString (value, s);
s.seekg (1, std::ios_base::cur); r++; // ; s.seekg (1, std::ios_base::cur); r++; // ;
if (!strcmp (key, "host")) if (!strcmp (key, "host"))
address.host = value; {
boost::system::error_code ecode;
address.host = boost::asio::ip::address::from_string (value, ecode);
if (ecode)
{
// TODO: we should try to resolve address here
LogPrint ("Unexpected address ", value);
SetUnreachable (true);
}
}
else if (!strcmp (key, "port")) else if (!strcmp (key, "port"))
address.port = boost::lexical_cast<int>(value); address.port = boost::lexical_cast<int>(value);
} }
@ -166,7 +175,7 @@ namespace data
std::stringstream properties; std::stringstream properties;
WriteString ("host", properties); WriteString ("host", properties);
properties << '='; properties << '=';
WriteString (address.host, properties); WriteString (address.host.to_string (), properties);
properties << ';'; properties << ';';
WriteString ("port", properties); WriteString ("port", properties);
properties << '='; properties << '=';
@ -237,7 +246,7 @@ namespace data
void RouterInfo::AddNTCPAddress (const char * host, int port) void RouterInfo::AddNTCPAddress (const char * host, int port)
{ {
Address addr; Address addr;
addr.host = 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;
@ -266,22 +275,28 @@ namespace data
return false; return false;
} }
bool RouterInfo::IsNTCP () const bool RouterInfo::IsNTCP (bool v4only) const
{ {
for (auto& address : m_Addresses) for (auto& address : m_Addresses)
{ {
if (address.transportStyle == eTransportNTCP) if (address.transportStyle == eTransportNTCP)
return true; {
if (!v4only || address.host.is_v4 ())
return true;
}
} }
return false; return false;
} }
RouterInfo::Address * RouterInfo::GetNTCPAddress () RouterInfo::Address * RouterInfo::GetNTCPAddress (bool v4only)
{ {
for (auto& address : m_Addresses) for (auto& address : m_Addresses)
{ {
if (address.transportStyle == eTransportNTCP) if (address.transportStyle == eTransportNTCP)
return &address; {
if (!v4only || address.host.is_v4 ())
return &address;
}
} }
return nullptr; return nullptr;
} }

6
RouterInfo.h

@ -27,7 +27,7 @@ namespace data
struct Address struct Address
{ {
TransportStyle transportStyle; TransportStyle transportStyle;
std::string host; boost::asio::ip::address host;
int port; int port;
uint64_t date; uint64_t date;
uint8_t cost; uint8_t cost;
@ -45,14 +45,14 @@ namespace data
const char * GetIdentHashAbbreviation () const { return m_IdentHashAbbreviation; }; const char * GetIdentHashAbbreviation () const { return m_IdentHashAbbreviation; };
uint64_t GetTimestamp () const { return m_Timestamp; }; uint64_t GetTimestamp () const { return m_Timestamp; };
const std::vector<Address>& GetAddresses () const { return m_Addresses; }; const std::vector<Address>& GetAddresses () const { return m_Addresses; };
Address * GetNTCPAddress (); Address * GetNTCPAddress (bool v4only = true);
const RoutingKey& GetRoutingKey () const { return m_RoutingKey; }; const RoutingKey& GetRoutingKey () const { return m_RoutingKey; };
void AddNTCPAddress (const char * host, int port); void AddNTCPAddress (const char * host, int port);
void SetProperty (const char * key, const char * value); void SetProperty (const char * key, const char * value);
const char * GetProperty (const char * key) const; const char * GetProperty (const char * key) const;
bool IsFloodfill () const; bool IsFloodfill () const;
bool IsNTCP () const; bool IsNTCP (bool v4only = true) const;
void SetUnreachable (bool unreachable) { m_IsUnreachable = unreachable; }; void SetUnreachable (bool unreachable) { m_IsUnreachable = unreachable; };
bool IsUnreachable () const { return m_IsUnreachable; }; bool IsUnreachable () const { return m_IsUnreachable; };

2
Transports.cpp

@ -139,7 +139,7 @@ namespace i2p
auto address = r->GetNTCPAddress (); auto address = r->GetNTCPAddress ();
if (address) if (address)
{ {
session = new i2p::ntcp::NTCPClient (m_Service, address->host.c_str (), address->port, *r); session = new i2p::ntcp::NTCPClient (m_Service, address->host, address->port, *r);
AddNTCPSession (session); AddNTCPSession (session);
} }
else else

Loading…
Cancel
Save