diff --git a/llarp/net/address_info.cpp b/llarp/net/address_info.cpp index ef1cacc0f..7852b38d6 100644 --- a/llarp/net/address_info.cpp +++ b/llarp/net/address_info.cpp @@ -22,7 +22,8 @@ namespace llarp dialect = other.dialect; pubkey = other.pubkey; memcpy(ip.s6_addr, other.ip.s6_addr, 16); - port = other.port; + port = other.port; + version = other.version; return *this; } diff --git a/llarp/net/address_info.hpp b/llarp/net/address_info.hpp index f36aeb140..170258469 100644 --- a/llarp/net/address_info.hpp +++ b/llarp/net/address_info.hpp @@ -32,13 +32,12 @@ namespace llarp } AddressInfo(const AddressInfo& other) - : IBEncodeMessage() + : IBEncodeMessage(other.version) , rank(other.rank) , dialect(other.dialect) , pubkey(other.pubkey) + , port(other.port) { - port = other.port; - version = other.version; memcpy(ip.s6_addr, other.ip.s6_addr, 16); } diff --git a/llarp/net/exit_info.cpp b/llarp/net/exit_info.cpp index 593c84797..ae31f127d 100644 --- a/llarp/net/exit_info.cpp +++ b/llarp/net/exit_info.cpp @@ -51,12 +51,34 @@ namespace llarp return bencode_end(buf); } + static bool + bdecode_ip_string(llarp_buffer_t* buf, in6_addr& ip) + { + char tmp[128] = {0}; + llarp_buffer_t strbuf; + if(!bencode_read_string(buf, &strbuf)) + return false; + + if(strbuf.sz >= sizeof(tmp)) + return false; + + memcpy(tmp, strbuf.base, strbuf.sz); + tmp[strbuf.sz] = 0; + return inet_pton(AF_INET6, tmp, &ip.s6_addr[0]) == 1; + } + bool - ExitInfo::DecodeKey(__attribute__((unused)) llarp_buffer_t k, - __attribute__((unused)) llarp_buffer_t* buf) + ExitInfo::DecodeKey(llarp_buffer_t k, llarp_buffer_t* buf) { bool read = false; - // TODO: implement me + if(!BEncodeMaybeReadDictEntry("k", pubkey, read, k, buf)) + return false; + if(!BEncodeMaybeReadDictInt("v", version, read, k, buf)) + return false; + if(llarp_buffer_eq(k, "a")) + return bdecode_ip_string(buf, address); + if(llarp_buffer_eq(k, "b")) + return bdecode_ip_string(buf, netmask); return read; } diff --git a/llarp/router_contact.cpp b/llarp/router_contact.cpp index 05031adc0..746f31da4 100644 --- a/llarp/router_contact.cpp +++ b/llarp/router_contact.cpp @@ -258,10 +258,10 @@ namespace llarp bool RouterContact::Verify(llarp::Crypto *crypto, llarp_time_t now) const { - static const NetID networkNetID; - if(netID != networkNetID) + if(netID != NetID::DefaultValue()) { - llarp::LogError("netid missmatch: '", netID, "' != '", networkNetID, "'"); + llarp::LogError("netid missmatch: '", netID, "' != '", + NetID::DefaultValue(), "'"); return false; } if(IsExpired(now)) diff --git a/llarp/router_contact.hpp b/llarp/router_contact.hpp index a110419df..5e01875f0 100644 --- a/llarp/router_contact.hpp +++ b/llarp/router_contact.hpp @@ -162,6 +162,22 @@ namespace llarp return last_updated < other.last_updated; } + friend std::ostream & + operator<<(std::ostream &out, const RouterContact &rc) + { + out << "[RouterContact k=" << rc.pubkey; + out << " updated=" << rc.last_updated; + out << " netid=" << rc.netID; + out << " v=" << rc.version; + out << " ai=[ "; + for(const auto &addr : rc.addrs) + out << addr << " "; + out << " ] xi=[ "; + for(const auto &xi : rc.exits) + out << xi << " "; + return out << " ] e=" << rc.enckey << " z=" << rc.signature << " ]"; + } + bool Read(const char *fname); diff --git a/test/test_llarp_router_contact.cpp b/test/test_llarp_router_contact.cpp index 937954218..52aca2e66 100644 --- a/test/test_llarp_router_contact.cpp +++ b/test/test_llarp_router_contact.cpp @@ -14,7 +14,6 @@ struct RCTest : public ::testing::Test : crypto(llarp::Crypto::sodium{}), oldval(llarp::NetID::DefaultValue()) { llarp::NetID::DefaultValue() = llarp::NetID(DEF_VALUE); - rc.Clear(); } ~RCTest() @@ -22,18 +21,22 @@ struct RCTest : public ::testing::Test llarp::NetID::DefaultValue() = oldval; } - RC_t rc; llarp::Crypto crypto; const llarp::NetID oldval; }; TEST_F(RCTest, TestSignVerify) { + llarp::NetID netid(DEF_VALUE); + RC_t rc; SecKey_t encr; SecKey_t sign; crypto.encryption_keygen(encr); crypto.identity_keygen(sign); rc.enckey = encr.toPublic(); + rc.pubkey = sign.toPublic(); + ASSERT_TRUE(rc.netID == netid); + ASSERT_TRUE(rc.netID == llarp::NetID::DefaultValue()); ASSERT_TRUE(rc.Sign(&crypto, sign)); ASSERT_TRUE(rc.Verify(&crypto, llarp::time_now_ms())); }