From e4cf1f245c8be5511504dfbc5c14fe520e041b55 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 24 Feb 2019 23:46:37 +0000 Subject: [PATCH] Convert most of llarp to use Printer --- llarp/crypto/types.hpp | 8 ++++++++ llarp/dns/message.cpp | 16 ++++++++++++++++ llarp/dns/message.hpp | 27 +++++++++------------------ llarp/dns/question.cpp | 12 ++++++++++++ llarp/dns/question.hpp | 17 ++++++++++------- llarp/dns/rr.cpp | 14 ++++++++++++++ llarp/dns/rr.hpp | 15 ++++++++------- llarp/net/address_info.cpp | 14 ++++++++++++++ llarp/net/address_info.hpp | 21 ++++++++------------- llarp/net/exit_info.cpp | 23 ++++++++++++++--------- llarp/net/exit_info.hpp | 10 ++++++++-- llarp/path/path.cpp | 23 +++++++++++++++++++++++ llarp/path/path.hpp | 30 ++++++++++++++++-------------- llarp/pow.cpp | 12 ++++++++++++ llarp/pow.hpp | 15 ++++++++------- llarp/router_contact.cpp | 17 +++++++++++++++++ llarp/router_contact.hpp | 37 +++++++++++++++++++------------------ llarp/service/Info.hpp | 14 ++++++++------ llarp/service/Intro.cpp | 12 ++++++++++++ llarp/service/Intro.hpp | 14 ++++++++------ llarp/service/IntroSet.cpp | 27 +++++++++++++++++++++++++++ llarp/service/IntroSet.hpp | 34 ++++++++-------------------------- llarp/service/info.cpp | 12 ++++++++++++ llarp/util/aligned.hpp | 12 +++++++++++- 24 files changed, 302 insertions(+), 134 deletions(-) diff --git a/llarp/crypto/types.hpp b/llarp/crypto/types.hpp index b8d724ecf..30c509b2b 100644 --- a/llarp/crypto/types.hpp +++ b/llarp/crypto/types.hpp @@ -95,6 +95,14 @@ namespace llarp return out << "[secretkey]"; } + std::ostream & + print(std::ostream &stream, int level, int spaces) const + { + Printer printer(stream, level, spaces); + printer.printValue("secretkey"); + return stream; + } + PubKey toPublic() const { diff --git a/llarp/dns/message.cpp b/llarp/dns/message.cpp index a23d635c3..5efc5ebe4 100644 --- a/llarp/dns/message.cpp +++ b/llarp/dns/message.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -270,5 +271,20 @@ namespace llarp } } + std::ostream& + Message::print(std::ostream& stream, int level, int spaces) const + { + Printer printer(stream, level, spaces); + + printer.printAttributeAsHex("dns message id", hdr_id); + printer.printAttributeAsHex("fields", hdr_fields); + printer.printAttribute("questions", questions); + printer.printAttribute("answers", answers); + printer.printAttribute("nameserer", authorities); + printer.printAttribute("additional", additional); + + return stream; + } + } // namespace dns } // namespace llarp diff --git a/llarp/dns/message.hpp b/llarp/dns/message.hpp index e9652634f..dadcdd402 100644 --- a/llarp/dns/message.hpp +++ b/llarp/dns/message.hpp @@ -72,24 +72,8 @@ namespace llarp bool Decode(llarp_buffer_t* buf) override; - friend std::ostream& - operator<<(std::ostream& out, const Message& msg) - { - out << "[dns message id=" << std::hex << msg.hdr_id - << " fields=" << msg.hdr_fields << " questions=[ "; - for(const auto& qd : msg.questions) - out << qd << ", "; - out << "] answers=[ "; - for(const auto& an : msg.answers) - out << an << ", "; - out << "] nameserver=[ "; - for(const auto& ns : msg.authorities) - out << ns << ", "; - out << "] additional=[ "; - for(const auto& ar : msg.additional) - out << ar << ", "; - return out << "]"; - } + std::ostream& + print(std::ostream& stream, int level, int spaces) const; MsgID_t hdr_id; Fields_t hdr_fields; @@ -98,6 +82,13 @@ namespace llarp std::vector< ResourceRecord > authorities; std::vector< ResourceRecord > additional; }; + + inline std::ostream& + operator<<(std::ostream& out, const Message& msg) + { + msg.print(out, -1, -1); + return out; + } } // namespace dns } // namespace llarp diff --git a/llarp/dns/question.cpp b/llarp/dns/question.cpp index b1ea92f62..2f7082af5 100644 --- a/llarp/dns/question.cpp +++ b/llarp/dns/question.cpp @@ -1,6 +1,7 @@ #include #include +#include namespace llarp { @@ -47,5 +48,16 @@ namespace llarp } return true; } + + std::ostream& + Question::print(std::ostream& stream, int level, int spaces) const + { + Printer printer(stream, level, spaces); + printer.printAttribute("qname", qname); + printer.printAttributeAsHex("qtype", qtype); + printer.printAttributeAsHex("qclass", qclass); + + return stream; + } } // namespace dns } // namespace llarp diff --git a/llarp/dns/question.hpp b/llarp/dns/question.hpp index 20c749f46..ac93ea422 100644 --- a/llarp/dns/question.hpp +++ b/llarp/dns/question.hpp @@ -23,6 +23,9 @@ namespace llarp bool Decode(llarp_buffer_t* buf) override; + std::ostream& + print(std::ostream& stream, int level, int spaces) const; + bool operator==(const Question& other) const { @@ -30,17 +33,17 @@ namespace llarp && qclass == other.qclass; } - friend std::ostream& - operator<<(std::ostream& out, const Question& q) - { - return out << "qname=" << q.qname << " qtype=" << (int)q.qtype - << " qclass=" << (int)q.qclass; - } - Name_t qname; QType_t qtype; QClass_t qclass; }; + + inline std::ostream& + operator<<(std::ostream& out, const Question& q) + { + q.print(out, -1, -1); + return out; + } } // namespace dns } // namespace llarp diff --git a/llarp/dns/rr.cpp b/llarp/dns/rr.cpp index 4beae892d..645709ffd 100644 --- a/llarp/dns/rr.cpp +++ b/llarp/dns/rr.cpp @@ -1,6 +1,7 @@ #include #include +#include namespace llarp { @@ -80,5 +81,18 @@ namespace llarp } return true; } + + std::ostream& + ResourceRecord::print(std::ostream& stream, int level, int spaces) const + { + Printer printer(stream, level, spaces); + printer.printAttribute("RR name", rr_name); + printer.printAttribute("type", rr_type); + printer.printAttribute("class", rr_class); + printer.printAttribute("ttl", ttl); + printer.printAttribute("rdata", rData.size()); + + return stream; + } } // namespace dns } // namespace llarp diff --git a/llarp/dns/rr.hpp b/llarp/dns/rr.hpp index c9d3dbf43..6bfda15d5 100644 --- a/llarp/dns/rr.hpp +++ b/llarp/dns/rr.hpp @@ -29,13 +29,8 @@ namespace llarp bool Decode(llarp_buffer_t* buf) override; - friend std::ostream& - operator<<(std::ostream& out, const ResourceRecord& rr) - { - return out << "[RR name=" << rr.rr_name << " type=" << rr.rr_type - << " class=" << rr.rr_class << " ttl=" << rr.ttl - << " rdata=[" << rr.rData.size() << " bytes]"; - } + std::ostream& + print(std::ostream& stream, int level, int spaces) const; Name_t rr_name; RRType_t rr_type; @@ -43,6 +38,12 @@ namespace llarp RR_TTL_t ttl; RR_RData_t rData; }; + + inline std::ostream& + operator<<(std::ostream& out, const ResourceRecord& rr) + { + return rr.print(out, -1, -1); + } } // namespace dns } // namespace llarp diff --git a/llarp/net/address_info.cpp b/llarp/net/address_info.cpp index f56897c6c..cb998ab65 100644 --- a/llarp/net/address_info.cpp +++ b/llarp/net/address_info.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include @@ -162,4 +163,17 @@ namespace llarp /** end */ return bencode_end(buff); } + + std::ostream & + AddressInfo::print(std::ostream &stream, int level, int spaces) const + { + char tmp[128] = {0}; + inet_ntop(AF_INET6, (void *)&ip, tmp, sizeof(tmp)); + + Printer printer(stream, level, spaces); + printer.printAttribute("ip", tmp); + printer.printAttribute("port", port); + + return stream; + } } // namespace llarp diff --git a/llarp/net/address_info.hpp b/llarp/net/address_info.hpp index 1776ed347..7c974b920 100644 --- a/llarp/net/address_info.hpp +++ b/llarp/net/address_info.hpp @@ -52,19 +52,8 @@ namespace llarp bool DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf) override; - friend std::ostream& - operator<<(std::ostream& out, const AddressInfo& a) - { - char tmp[128] = {0}; - inet_ntop(AF_INET6, (void*)&a.ip, tmp, sizeof(tmp)); - out << tmp << "."; -#if defined(ANDROID) || defined(RPI) - snprintf(tmp, sizeof(tmp), "%u", a.port); - return out << tmp; -#else - return out << std::to_string(a.port); -#endif - } + std::ostream& + print(std::ostream& stream, int level, int spaces) const; struct Hash { @@ -76,6 +65,12 @@ namespace llarp }; }; + inline std::ostream& + operator<<(std::ostream& out, const AddressInfo& a) + { + return a.print(out, -1, -1); + } + bool operator==(const AddressInfo& lhs, const AddressInfo& rhs); diff --git a/llarp/net/exit_info.cpp b/llarp/net/exit_info.cpp index 40a074313..8c04cf624 100644 --- a/llarp/net/exit_info.cpp +++ b/llarp/net/exit_info.cpp @@ -84,22 +84,27 @@ namespace llarp } std::ostream& - operator<<(std::ostream& out, const ExitInfo& xi) + ExitInfo::print(std::ostream& stream, int level, int spaces) const { + Printer printer(stream, level, spaces); + + std::ostringstream ss; char tmp[128] = {0}; - if(inet_ntop(AF_INET6, (void*)&xi.address, tmp, sizeof(tmp))) - out << std::string(tmp); + + if(inet_ntop(AF_INET6, (void*)&address, tmp, sizeof(tmp))) + ss << tmp; else - return out; - out << std::string("/"); + return stream; + ss << std::string("/"); #if defined(ANDROID) || defined(RPI) snprintf(tmp, sizeof(tmp), "%zu", - llarp::bits::count_array_bits(xi.netmask.s6_addr)); - return out << tmp; + llarp::bits::count_array_bits(netmask.s6_addr)); + ss << tmp; #else - return out << std::to_string( - llarp::bits::count_array_bits(xi.netmask.s6_addr)); + ss << std::to_string(llarp::bits::count_array_bits(netmask.s6_addr)); #endif + printer.printValue(ss.str()); + return stream; } } // namespace llarp diff --git a/llarp/net/exit_info.hpp b/llarp/net/exit_info.hpp index cef472dab..0277a4c71 100644 --- a/llarp/net/exit_info.hpp +++ b/llarp/net/exit_info.hpp @@ -53,10 +53,16 @@ namespace llarp ExitInfo & operator=(const ExitInfo &other); + + std::ostream & + print(std::ostream &stream, int level, int spaces) const; }; - std::ostream & - operator<<(std::ostream &out, const ExitInfo &xi); + inline std::ostream & + operator<<(std::ostream &out, const ExitInfo &xi) + { + return xi.print(out, -1, -1); + } } // namespace llarp #endif diff --git a/llarp/path/path.cpp b/llarp/path/path.cpp index b68d2ff14..c925de1ee 100644 --- a/llarp/path/path.cpp +++ b/llarp/path/path.cpp @@ -18,6 +18,18 @@ namespace llarp { namespace path { + std::ostream& + TransitHopInfo::print(std::ostream& stream, int level, int spaces) const + { + Printer printer(stream, level, spaces); + printer.printAttribute("tx", txID); + printer.printAttribute("rx", rxID); + printer.printAttribute("upstream", upstream); + printer.printAttribute("downstream", downstream); + + return stream; + } + PathContext::PathContext(AbstractRouter* router) : m_Router(router), m_AllowTransit(false) { @@ -350,6 +362,17 @@ namespace llarp RemovePathSet(ctx); } + std::ostream& + TransitHop::print(std::ostream& stream, int level, int spaces) const + { + Printer printer(stream, level, spaces); + printer.printAttribute("TransitHop", info); + printer.printAttribute("started", started); + printer.printAttribute("lifetime", lifetime); + + return stream; + } + PathHopConfig::PathHopConfig() { } diff --git a/llarp/path/path.hpp b/llarp/path/path.hpp index ec3f81f71..55c4ca03a 100644 --- a/llarp/path/path.hpp +++ b/llarp/path/path.hpp @@ -50,14 +50,8 @@ namespace llarp RouterID upstream; RouterID downstream; - friend std::ostream& - operator<<(std::ostream& out, const TransitHopInfo& info) - { - out << ""; - } + std::ostream& + print(std::ostream& stream, int level, int spaces) const; bool operator==(const TransitHopInfo& other) const @@ -102,6 +96,12 @@ namespace llarp }; }; + inline std::ostream& + operator<<(std::ostream& out, const TransitHopInfo& info) + { + return info.print(out, -1, -1); + } + struct IHopHandler { virtual ~IHopHandler(){}; @@ -172,12 +172,8 @@ namespace llarp return m_LastActivity; } - friend std::ostream& - operator<<(std::ostream& out, const TransitHop& h) - { - return out << "[TransitHop " << h.info << " started=" << h.started - << " lifetime=" << h.lifetime << "]"; - } + std::ostream& + print(std::ostream& stream, int level, int spaces) const; bool Expired(llarp_time_t now) const override; @@ -268,6 +264,12 @@ namespace llarp AbstractRouter* r) override; }; + inline std::ostream& + operator<<(std::ostream& out, const TransitHop& h) + { + return h.print(out, -1, -1); + } + /// configuration for a single hop when building a path struct PathHopConfig : public util::IStateful { diff --git a/llarp/pow.cpp b/llarp/pow.cpp index 48ce4e8e5..b42c25cfd 100644 --- a/llarp/pow.cpp +++ b/llarp/pow.cpp @@ -55,4 +55,16 @@ namespace llarp return true; } + std::ostream& + PoW::print(std::ostream& stream, int level, int spaces) const + { + Printer printer(stream, level, spaces); + + printer.printAttribute("pow timestamp", timestamp); + printer.printAttribute("lifetime", extendedLifetime); + printer.printAttribute("nonce", nonce); + + return stream; + } + } // namespace llarp diff --git a/llarp/pow.hpp b/llarp/pow.hpp index efc8f8290..c5ede182b 100644 --- a/llarp/pow.hpp +++ b/llarp/pow.hpp @@ -39,14 +39,15 @@ namespace llarp return !(*this == other); } - friend std::ostream& - operator<<(std::ostream& out, const PoW& p) - { - return out << "[pow timestamp=" << p.timestamp - << " lifetime=" << p.extendedLifetime << " nonce=" << p.nonce - << "]"; - } + std::ostream& + print(std::ostream& stream, int level, int spaces) const; }; + + inline std::ostream& + operator<<(std::ostream& out, const PoW& p) + { + return p.print(out, -1, -1); + } } // namespace llarp #endif diff --git a/llarp/router_contact.cpp b/llarp/router_contact.cpp index 60bc626ed..6353c44c8 100644 --- a/llarp/router_contact.cpp +++ b/llarp/router_contact.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -382,4 +383,20 @@ namespace llarp return *this; } + std::ostream & + RouterContact::print(std::ostream &stream, int level, int spaces) const + { + Printer printer(stream, level, spaces); + printer.printAttribute("k", pubkey); + printer.printAttribute("updated", last_updated); + printer.printAttribute("netid", netID); + printer.printAttribute("v", version); + printer.printAttribute("ai", addrs); + printer.printAttribute("xi", exits); + printer.printAttribute("e", enckey); + printer.printAttribute("z", signature); + + return stream; + } + } // namespace llarp diff --git a/llarp/router_contact.hpp b/llarp/router_contact.hpp index 65a758039..d7d4d50e0 100644 --- a/llarp/router_contact.hpp +++ b/llarp/router_contact.hpp @@ -40,10 +40,12 @@ namespace llarp return !(*this == other); } - friend std::ostream & - operator<<(std::ostream &out, const NetID &id) + std::ostream & + print(std::ostream &stream, int level, int spaces) const { - return out << id.ToString(); + Printer printer(stream, level, spaces); + printer.printValue(ToString()); + return stream; } std::string @@ -56,6 +58,12 @@ namespace llarp BEncode(llarp_buffer_t *buf) const; }; + inline std::ostream & + operator<<(std::ostream &out, const NetID &id) + { + return id.print(out, -1, -1); + } + /// RouterContact struct RouterContact final : public IBEncodeMessage, public util::IStateful { @@ -167,21 +175,8 @@ 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 << " ]"; - } + std::ostream & + print(std::ostream &stream, int level, int spaces) const; bool Read(const char *fname); @@ -194,6 +189,12 @@ namespace llarp VerifySignature(llarp::Crypto *crypto) const; }; + inline std::ostream & + operator<<(std::ostream &out, const RouterContact &rc) + { + return rc.print(out, -1, -1); + } + using RouterLookupHandler = std::function< void(const std::vector< RouterContact > &) >; } // namespace llarp diff --git a/llarp/service/Info.hpp b/llarp/service/Info.hpp index 871453cc5..bcac130d0 100644 --- a/llarp/service/Info.hpp +++ b/llarp/service/Info.hpp @@ -105,12 +105,8 @@ namespace llarp return Addr() < other.Addr(); } - friend std::ostream& - operator<<(std::ostream& out, const ServiceInfo& i) - { - return out << "[e=" << i.enckey << " s=" << i.signkey - << " v=" << i.version << " x=" << i.vanity << "]"; - } + std::ostream& + print(std::ostream& stream, int level, int spaces) const; /// .loki address std::string @@ -145,6 +141,12 @@ namespace llarp private: Address m_CachedAddr; }; + + inline std::ostream& + operator<<(std::ostream& out, const ServiceInfo& i) + { + return i.print(out, -1, -1); + } } // namespace service } // namespace llarp diff --git a/llarp/service/Intro.cpp b/llarp/service/Intro.cpp index 6304c6ce2..770f035da 100644 --- a/llarp/service/Intro.cpp +++ b/llarp/service/Intro.cpp @@ -65,5 +65,17 @@ namespace llarp latency = 0; expiresAt = 0; } + + std::ostream& + Introduction::print(std::ostream& stream, int level, int spaces) const + { + Printer printer(stream, level, spaces); + printer.printAttribute("k", router); + printer.printAttribute("p", pathID); + printer.printAttribute("v", version); + printer.printAttribute("x", expiresAt); + + return stream; + } } // namespace service } // namespace llarp diff --git a/llarp/service/Intro.hpp b/llarp/service/Intro.hpp index 52da874a0..9dd180bdb 100644 --- a/llarp/service/Intro.hpp +++ b/llarp/service/Intro.hpp @@ -50,12 +50,8 @@ namespace llarp ~Introduction(); - friend std::ostream& - operator<<(std::ostream& out, const Introduction& i) - { - return out << "k=" << i.router << " p=" << i.pathID - << " v=" << i.version << " x=" << i.expiresAt; - } + std::ostream& + print(std::ostream& stream, int level, int spaces) const; bool BEncode(llarp_buffer_t* buf) const override; @@ -95,6 +91,12 @@ namespace llarp } }; }; + + inline std::ostream& + operator<<(std::ostream& out, const Introduction& i) + { + return i.print(out, -1, -1); + } } // namespace service } // namespace llarp diff --git a/llarp/service/IntroSet.cpp b/llarp/service/IntroSet.cpp index 7ed83b9f3..ab2273ea6 100644 --- a/llarp/service/IntroSet.cpp +++ b/llarp/service/IntroSet.cpp @@ -179,5 +179,32 @@ namespace llarp t = std::max(intro.expiresAt, t); return t; } + + std::ostream& + IntroSet::print(std::ostream& stream, int level, int spaces) const + { + Printer printer(stream, level, spaces); + printer.printAttribute("A", A); + printer.printAttribute("I", I); + printer.printAttribute("K", K); + + std::string _topic = topic.ToString(); + + if(!_topic.empty()) + { + printer.printAttribute("topic", _topic); + } + else + { + printer.printAttribute("topic", topic); + } + + printer.printAttribute("T", T); + printer.printAttribute("W", W); + printer.printAttribute("V", version); + printer.printAttribute("Z", Z); + + return stream; + } } // namespace service } // namespace llarp diff --git a/llarp/service/IntroSet.hpp b/llarp/service/IntroSet.hpp index a8c9ae53d..87ac0e82f 100644 --- a/llarp/service/IntroSet.hpp +++ b/llarp/service/IntroSet.hpp @@ -114,32 +114,8 @@ namespace llarp return T < other.T; } - friend std::ostream& - operator<<(std::ostream& out, const IntroSet& i) - { - out << "A=[" << i.A << "] I=["; - for(const auto& intro : i.I) - { - out << intro << ", "; - } - out << "]"; - out << "K=" << i.K; - auto topic = i.topic.ToString(); - if(topic.size()) - { - out << " topic=" << topic; - } - else - { - out << " topic=" << i.topic; - } - out << " T=" << i.T; - if(i.W) - { - out << " W=" << *i.W; - } - return out << " V=" << i.version << " Z=" << i.Z; - } + std::ostream& + print(std::ostream& stream, int level, int spaces) const; llarp_time_t GetNewestIntroExpiration() const; @@ -160,6 +136,12 @@ namespace llarp Verify(llarp::Crypto* crypto, llarp_time_t now) const; }; + inline std::ostream& + operator<<(std::ostream& out, const IntroSet& i) + { + return i.print(out, -1, -1); + } + using IntroSetLookupHandler = std::function< void(const std::vector< IntroSet >&) >; diff --git a/llarp/service/info.cpp b/llarp/service/info.cpp index 08b0361b6..8005a02c4 100644 --- a/llarp/service/info.cpp +++ b/llarp/service/info.cpp @@ -82,5 +82,17 @@ namespace llarp return CalculateAddress(m_CachedAddr.as_array()); } + std::ostream& + ServiceInfo::print(std::ostream& stream, int level, int spaces) const + { + Printer printer(stream, level, spaces); + printer.printAttribute("e", enckey); + printer.printAttribute("s", signkey); + printer.printAttribute("v", version); + printer.printAttribute("x", vanity); + + return stream; + } + } // namespace service } // namespace llarp diff --git a/llarp/util/aligned.hpp b/llarp/util/aligned.hpp index 2d8f1fee0..da23cfe5b 100644 --- a/llarp/util/aligned.hpp +++ b/llarp/util/aligned.hpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include #include @@ -257,6 +259,15 @@ namespace llarp return std::string(HexEncode(*this, strbuf)); } + std::ostream& + print(std::ostream& stream, int level, int spaces) const + { + Printer printer(stream, level, spaces); + printer.printValue(ToHex()); + + return stream; + } + struct Hash { size_t @@ -273,7 +284,6 @@ namespace llarp type; // why did we align to the nearest double-precision float AlignedStorage val; }; - } // namespace llarp #endif