Convert most of llarp to use Printer

pull/349/head
Michael 5 years ago
parent e6e19369e9
commit e4cf1f245c
No known key found for this signature in database
GPG Key ID: 2D51757B47E2434C

@ -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
{

@ -4,6 +4,7 @@
#include <util/buffer.hpp>
#include <util/endian.hpp>
#include <util/logger.hpp>
#include <util/printer.hpp>
#include <array>
@ -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

@ -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

@ -1,6 +1,7 @@
#include <dns/question.hpp>
#include <util/logger.hpp>
#include <util/printer.hpp>
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

@ -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

@ -1,6 +1,7 @@
#include <dns/rr.hpp>
#include <util/logger.hpp>
#include <util/printer.hpp>
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

@ -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

@ -6,6 +6,7 @@
#include <net/net.hpp>
#include <util/bencode.h>
#include <util/mem.h>
#include <util/printer.hpp>
#include <string.h>
@ -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

@ -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);

@ -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

@ -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

@ -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()
{
}

@ -50,14 +50,8 @@ namespace llarp
RouterID upstream;
RouterID downstream;
friend std::ostream&
operator<<(std::ostream& out, const TransitHopInfo& info)
{
out << "<tx=" << info.txID << " rx=" << info.rxID;
out << " upstream=" << info.upstream
<< " downstream=" << info.downstream;
return 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
{

@ -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

@ -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

@ -7,6 +7,7 @@
#include <util/buffer.hpp>
#include <util/logger.hpp>
#include <util/mem.hpp>
#include <util/printer.hpp>
#include <util/time.hpp>
#include <fstream>
@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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 >&) >;

@ -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

@ -4,6 +4,8 @@
#include <util/bencode.h>
#include <util/encode.hpp>
#include <util/logger.hpp>
#include <util/printer.hpp>
#include <util/traits.hpp>
#include <array>
#include <cstddef>
@ -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

Loading…
Cancel
Save