From 76e16e4d80c0e6888f2e2b2666e1f623753485ac Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 10 Jan 2018 01:20:16 -0800 Subject: [PATCH] ircd::net: Improve remote construction; hostport/ipport related. --- include/ircd/net/hostport.h | 10 ++++++++++ include/ircd/net/ipport.h | 1 + include/ircd/net/remote.h | 6 +++++- ircd/net.cc | 22 ++-------------------- 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/include/ircd/net/hostport.h b/include/ircd/net/hostport.h index dffa24265..9b1a1d97a 100644 --- a/include/ircd/net/hostport.h +++ b/include/ircd/net/hostport.h @@ -26,6 +26,8 @@ namespace ircd::net { struct hostport; + uint16_t port(const hostport &); + const auto &host(const hostport &); auto &host(hostport &); @@ -83,3 +85,11 @@ ircd::net::host(const hostport &hp) { return hp.host; } + +inline uint16_t +ircd::net::port(const hostport &hp) +{ + return hp.portnum? hp.portnum: + hp.port? lex_cast(hp.port): + 0; +} diff --git a/include/ircd/net/ipport.h b/include/ircd/net/ipport.h index 733e7a730..386ccc4d5 100644 --- a/include/ircd/net/ipport.h +++ b/include/ircd/net/ipport.h @@ -96,6 +96,7 @@ ircd::net::ipport::ipport(const uint32_t &ip, const uint16_t &p) { std::get(*this) = false; + host6(*this) = 0; host4(*this) = ip; port(*this) = p; } diff --git a/include/ircd/net/remote.h b/include/ircd/net/remote.h index 8ad197128..15352a291 100644 --- a/include/ircd/net/remote.h +++ b/include/ircd/net/remote.h @@ -53,7 +53,11 @@ struct ircd::net::remote :ipport{ipp} {} - remote(const hostport &hp); + explicit remote(const hostport &hostport) + :ipport{uint32_t(0), net::port(hostport)} + ,hostname{net::host(hostport)} + {} + remote() = default; friend std::ostream &operator<<(std::ostream &, const remote &); diff --git a/ircd/net.cc b/ircd/net.cc index 4a7f6b5de..afa12d5d5 100644 --- a/ircd/net.cc +++ b/ircd/net.cc @@ -2254,22 +2254,6 @@ ircd::net::string(const mutable_buffer &buf, } } -// -// remote -// - -ircd::net::remote::remote(const hostport &hostport) -:ipport -{ - resolve(hostport) -} -,hostname -{ - hostport.host -} -{ -} - /////////////////////////////////////////////////////////////////////////////// // // net/ipport.h @@ -2421,9 +2405,7 @@ ircd::net::string(const mutable_buffer &buf, { fmt::sprintf { - buf, "%s:%s", - hp.host, - hp.portnum? lex_cast(hp.portnum) : hp.port + buf, "%s:%u", host(hp), port(hp) } }; @@ -2444,7 +2426,7 @@ ircd::net::string(const ip::address &addr) std::string ircd::net::string(const ip::tcp::endpoint &ep) { - std::string ret(256, char{}); + std::string ret(128, char{}); const auto addr{string(net::addr(ep))}; const auto data{const_cast(ret.data())}; ret.resize(snprintf(data, ret.size(), "%s:%u", addr.c_str(), port(ep)));