From a46bf7dcbe66f0743aa2428ce2c4a4aa05ba31f6 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 5 Mar 2020 09:48:17 -0800 Subject: [PATCH] ircd::net: Eliminate the legacy canon_port/canon_service defaults. ircd::m: Move canon_port/canon_service into matrix lib. --- include/ircd/m/m.h | 3 +++ include/ircd/net/hostport.h | 36 +++++++++++++++++++++--------------- ircd/net.cc | 34 +++------------------------------- ircd/net_dns_resolver.cc | 4 +++- ircd/server.cc | 10 +++++----- matrix/homeserver.cc | 8 ++++---- matrix/matrix.cc | 12 ++++++++++++ 7 files changed, 51 insertions(+), 56 deletions(-) diff --git a/include/ircd/m/m.h b/include/ircd/m/m.h index e202f4334..a53062ec5 100644 --- a/include/ircd/m/m.h +++ b/include/ircd/m/m.h @@ -39,6 +39,9 @@ namespace ircd::m IRCD_OVERLOAD(generate) + extern const uint16_t canon_port; + extern const string_view canon_service; + extern struct log::log log; } diff --git a/include/ircd/net/hostport.h b/include/ircd/net/hostport.h index 11673d1ca..69c843d12 100644 --- a/include/ircd/net/hostport.h +++ b/include/ircd/net/hostport.h @@ -25,11 +25,10 @@ namespace ircd::net uint16_t &port(hostport &); string_view &host(hostport &); + string_view string(const mutable_buffer &out, const hostport &); - - string_view canonize(const mutable_buffer &out, const hostport &, const uint16_t &port = canon_port); - std::string canonize(const hostport &, const uint16_t &port = canon_port); - + string_view canonize(const mutable_buffer &out, const hostport &, const uint16_t &port = 0); + std::string canonize(const hostport &, const uint16_t &port = 0); std::ostream &operator<<(std::ostream &, const hostport &); } @@ -56,14 +55,14 @@ struct ircd::net::hostport { IRCD_OVERLOAD(verbatim) - string_view host {"0.0.0.0"}; - string_view service {canon_service}; - uint16_t port {canon_port}; + string_view host; + string_view service; + uint16_t port {0}; explicit operator bool() const; bool operator!() const; - hostport(const string_view &host, const string_view &service, const uint16_t &port = canon_port); + hostport(const string_view &host, const string_view &service, const uint16_t &port); hostport(const string_view &host, const uint16_t &port); hostport(const string_view &amalgam); hostport(const string_view &amalgam, verbatim_t); @@ -72,16 +71,23 @@ struct ircd::net::hostport /// Creates a host:service pair from a hostname and a service name string. /// When passed to net::dns() this will indicate SRV resolution. If no -/// SRV record is found -/// TODO: todo: the servce is translated into its proper port. -/// TODO: now: the port 8448 is used with the hostname. +/// SRV record is found. inline ircd::net::hostport::hostport(const string_view &host, const string_view &service, const uint16_t &port) -:host{rfc3986::host(host)} -,service{service} -,port{port} +:host +{ + rfc3986::host(host) +} +,service +{ + service +} +,port +{ + port +} {} /// Creates a host:port pair from a hostname and a port number. When @@ -102,7 +108,7 @@ ircd::net::hostport::operator bool() const { - return net::host(*this) != net::host(hostport{}); + return bool(host); } inline bool diff --git a/ircd/net.cc b/ircd/net.cc index 706bf4047..9be6b33d0 100644 --- a/ircd/net.cc +++ b/ircd/net.cc @@ -4380,18 +4380,6 @@ const // net/hostport.h // -decltype(ircd::net::canon_port) -ircd::net::canon_port -{ - 8448 -}; - -decltype(ircd::net::canon_service) -ircd::net::canon_service -{ - "matrix" -}; - /// Creates a host:service or host:port pair from the single string literally /// containing the colon deliminated values. If the suffix is a port number /// then the behavior for the port number constructor applies; if a service @@ -4407,28 +4395,12 @@ ircd::net::hostport::hostport(const string_view &amalgam) rfc3986::port(amalgam) } { - // When the amalgam has no port - if(amalgam == host) - { - // set the port to the canon_port; port=0 is bad - port = port?: canon_port; - return; - } - - // or a valid integer port - if(port) + // When the amalgam has no port || a valid integer port + if(amalgam == host || port) return; // When the port is actually a service string - const auto service - { - rsplit(amalgam, ':').second - }; - - if(service) - this->service = service; - else - this->port = canon_port; + this->service = rsplit(amalgam, ':').second; } ircd::net::hostport::hostport(const string_view &amalgam, diff --git a/ircd/net_dns_resolver.cc b/ircd/net_dns_resolver.cc index ecc0d41f5..d0c0f19bd 100644 --- a/ircd/net_dns_resolver.cc +++ b/ircd/net_dns_resolver.cc @@ -936,7 +936,9 @@ try const auto &port { - net::port(hp) != canon_port? net::port(hp) : uint16_t(53) + net::port(hp)? + net::port(hp): + uint16_t(53) }; const ipport ipp diff --git a/ircd/server.cc b/ircd/server.cc index 6fc04bcc7..e2295cf1c 100644 --- a/ircd/server.cc +++ b/ircd/server.cc @@ -579,11 +579,11 @@ ircd::server::peer::peer(const net::hostport &hostport, } ,service { - // hostport arguments with a port of 0 or net::canon_port (8448) are - // normal; if the port is such, and if we are supplied a service string - // an SRV query will be performed. For other port numbers, we ignore any - // service string and SRV won't be resolved. - !net::port(hostport) || net::port(hostport) == net::canon_port? + // hostport arguments with a port of 0 are normal; if the port is such, + // and if we are supplied a service string an SRV query will be performed. + // For other port numbers, we ignore any service string and SRV won't be + // resolved. + !net::port(hostport)? net::service(hostport): string_view{} } diff --git a/matrix/homeserver.cc b/matrix/homeserver.cc index 0ac8412f8..f36bfa348 100644 --- a/matrix/homeserver.cc +++ b/matrix/homeserver.cc @@ -949,7 +949,7 @@ ircd::m::self::my_host(const string_view &name) bool ircd::m::self::host(const string_view &other) { - assert(net::canon_port == 8448); + assert(m::canon_port == 8448); const net::hostport other_host{other}; for(const auto &[my_network, hs_p] : homeserver::map) if(match(my_network, other)) @@ -966,17 +966,17 @@ noexcept // port() is 0 when the origin has no port (and implies 8448) const auto my_port { - port(a)?: 8448 + port(a)?: m::canon_port }; // If my_host has a non-canonical port number, then the argument must // also have the same port number, or there is no possible match. - if(my_port != net::canon_port) + if(my_port != m::canon_port) return my_port == port(b) && host(a) == host(b); // Since my host is on the canonical port, if other host has some // different port number, there is no possible match. - if(port(b) != net::canon_port) + if(port(b) != m::canon_port) return false; // Both myself and input are using 8448; now the name has to match. diff --git a/matrix/matrix.cc b/matrix/matrix.cc index 05bc164b9..3f457879d 100644 --- a/matrix/matrix.cc +++ b/matrix/matrix.cc @@ -27,6 +27,18 @@ ircd::m::log "m", 'm' }; +decltype(ircd::m::canon_port) +ircd::m::canon_port +{ + 8448 +}; + +decltype(ircd::m::canon_service) +ircd::m::canon_service +{ + "matrix" +}; + /// This is an ordered list for loading and unloading modules. This is not the /// solution I really want at all so consider it temporary. Modules are loaded /// in the order of the lines and unloaded in reverse order.