From 980d24b38ed4364443b22196347c00880364af64 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 23 Mar 2018 13:10:00 -0700 Subject: [PATCH] ircd::net: Simplify listener config object. --- include/ircd/net/acceptor.h | 2 +- include/ircd/net/listener.h | 4 +- ircd/m/m.cc | 82 +++++++++++++++---------------------- ircd/net.cc | 34 ++++++++++----- 4 files changed, 60 insertions(+), 62 deletions(-) diff --git a/include/ircd/net/acceptor.h b/include/ircd/net/acceptor.h index 191b026b4..de0caf10e 100644 --- a/include/ircd/net/acceptor.h +++ b/include/ircd/net/acceptor.h @@ -52,6 +52,6 @@ struct ircd::net::listener::acceptor bool interrupt() noexcept; void join() noexcept; - acceptor(const json::object &opts); + acceptor(const string_view &name, const json::object &opts); ~acceptor() noexcept; }; diff --git a/include/ircd/net/listener.h b/include/ircd/net/listener.h index cf07cb830..2b77a5990 100644 --- a/include/ircd/net/listener.h +++ b/include/ircd/net/listener.h @@ -26,7 +26,7 @@ struct ircd::net::listener std::shared_ptr acceptor; public: - explicit listener(const std::string &options); - listener(const json::object &options); + explicit listener(const string_view &name, const std::string &options); + listener(const string_view &name, const json::object &options); ~listener() noexcept; }; diff --git a/ircd/m/m.cc b/ircd/m/m.cc index a0ca138b0..863d868c8 100644 --- a/ircd/m/m.cc +++ b/ircd/m/m.cc @@ -167,8 +167,7 @@ ircd::m::init::modules() namespace ircd::m { - static void init_listener(const json::object &config, const json::object &opts, const string_view &bindaddr); - static void init_listener(const json::object &config, const json::object &opts); + static void init_listener(const json::object &config, const string_view &name, const json::object &conf); } void @@ -186,58 +185,33 @@ ircd::m::init::listeners() const json::array listeners { - config["listeners"] + config[{"ircd", "listen"}] }; - if(m::listeners.empty()) - init_listener(config, {}); - else - for(const json::object opts : listeners) - init_listener(config, opts); + for(const auto &name : listeners) try + { + const json::object &opts + { + config.at({"listen", unquote(name)}) + }; + + init_listener(config, name, opts); + } + catch(const json::not_found &e) + { + throw ircd::user_error + { + "Failed to find configuration block for listener %s", name + }; + } } static void ircd::m::init_listener(const json::object &config, + const string_view &name, const json::object &opts) { - const json::array binds - { - opts["bind_addresses"] - }; - - if(binds.empty()) - init_listener(config, opts, "0.0.0.0"); - else - for(const auto &bindaddr : binds) - init_listener(config, opts, unquote(bindaddr)); -} - -static void -ircd::m::init_listener(const json::object &config, - const json::object &opts, - const string_view &host) -{ - const json::array resources - { - opts["resources"] - }; - - // resources has multiple names with different configs which are being - // ignored :-/ - const std::string name{"Matrix"s}; - - // Translate synapse options to our options (which reflect asio::ssl) - const json::strung options{json::members - { - { "name", name }, - { "host", host }, - { "port", opts.get("port", 8448L) }, - { "ssl_certificate_file_pem", config["tls_certificate_path"] }, - { "ssl_private_key_file_pem", config["tls_private_key_path"] }, - { "ssl_tmp_dh_file", config["tls_dh_params_path"] }, - }}; - - m::listeners.emplace_back(options); + m::listeners.emplace_back(name, opts); } void @@ -470,14 +444,24 @@ noexcept void ircd::m::keys::init::certificate() { + const string_view origin + { + unquote(this->config.at({"ircd", "origin"})) + }; + + const json::object config + { + this->config.at({"origin", unquote(origin)}) + }; + const std::string private_key_file { - unquote(config.at("tls_private_key_path")) + unquote(config.at("ssl_private_key_file_pem")) }; const std::string public_key_file { - unquote(config.get("tls_public_key_path", private_key_file + ".pub")) + unquote(config.get("ssl_public_key_file_pem", private_key_file + ".pub")) }; if(!fs::exists(private_key_file)) @@ -488,7 +472,7 @@ ircd::m::keys::init::certificate() const std::string cert_file { - unquote(config.at("tls_certificate_path")) + unquote(config.at("ssl_certificate_file_pem")) }; if(!fs::exists(cert_file)) diff --git a/ircd/net.cc b/ircd/net.cc index 86e64a16c..440266fe9 100644 --- a/ircd/net.cc +++ b/ircd/net.cc @@ -783,13 +783,21 @@ ircd::net::blocking(const socket &socket) // net/listener.h // -ircd::net::listener::listener(const std::string &opts) -:listener{json::object{opts}} +ircd::net::listener::listener(const string_view &name, + const std::string &opts) +:listener +{ + name, json::object{opts} +} { } -ircd::net::listener::listener(const json::object &opts) -:acceptor{std::make_shared(opts)} +ircd::net::listener::listener(const string_view &name, + const json::object &opts) +:acceptor +{ + std::make_shared(name, opts) +} { // Starts the first asynchronous accept. This has to be done out here after // the acceptor's shared object is constructed. @@ -824,16 +832,18 @@ ircd::net::listener::acceptor::timeout { "default", 5000L }, }; -ircd::net::listener::acceptor::acceptor(const json::object &opts) +ircd::net::listener::acceptor::acceptor(const string_view &name, + const json::object &opts) try :name { - unquote(opts.get("name", "IRCd (ssl)"s)) + name } ,backlog { + //TODO: XXX //boost::asio::ip::tcp::socket::max_connections <-- linkage failed? - opts.get("backlog", SOMAXCONN) //TODO: XXX + std::min(opts.get("backlog", SOMAXCONN), uint(SOMAXCONN)) } ,ssl { @@ -842,7 +852,7 @@ try ,ep { ip::address::from_string(unquote(opts.get("host", "127.0.0.1"s))), - opts.at("port") + opts.get("port", 8448L) } ,a { @@ -851,8 +861,9 @@ try { static const auto &max_connections { + //TODO: XXX //boost::asio::ip::tcp::socket::max_connections <-- linkage failed? - SOMAXCONN //TODO: XXX + std::min(opts.get("max_connections", SOMAXCONN), uint(SOMAXCONN)) }; static const ip::tcp::acceptor::reuse_address reuse_address @@ -882,7 +893,10 @@ try } catch(const boost::system::system_error &e) { - throw error("listener: %s", e.what()); + throw error + { + "listener: %s", e.what() + }; } ircd::net::listener::acceptor::~acceptor()