From: Chris Hofstaedtler Date: Tue, 27 Mar 2018 10:14:30 +0000 (+0200) Subject: dnsdist: avoid LuaWrapper assert on errors in newServer() X-Git-Tag: dnsdist-1.3.0~13^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a5526b57790b651fcfa86d6e415ec5730c3267db;p=pdns dnsdist: avoid LuaWrapper assert on errors in newServer() --- diff --git a/pdns/dnsdist-lua.cc b/pdns/dnsdist-lua.cc index 1c9f18c60..6f46af20a 100644 --- a/pdns/dnsdist-lua.cc +++ b/pdns/dnsdist-lua.cc @@ -110,66 +110,48 @@ void setupLuaConfig(bool client) }); g_lua.writeFunction("newServer", - [client](boost::variant pvars, boost::optional qps) { - setLuaSideEffect(); - if(client) { - return std::make_shared(ComboAddress()); - } - ComboAddress sourceAddr; - unsigned int sourceItf = 0; - size_t numberOfSockets = 1; - std::set cpus; - if(auto addressStr = boost::get(&pvars)) { - std::shared_ptr ret; - try { - ComboAddress address(*addressStr, 53); - if(IsAnyAddress(address)) { - g_outputBuffer="Error creating new server: invalid address for a downstream server."; - errlog("Error creating new server: %s is not a valid address for a downstream server", *addressStr); - return ret; - } - ret=std::make_shared(address); - } - catch(const PDNSException& e) { - g_outputBuffer="Error creating new server: "+string(e.reason); - errlog("Error creating new server with address %s: %s", addressStr, e.reason); - return ret; - } - catch(std::exception& e) { - g_outputBuffer="Error creating new server: "+string(e.what()); - errlog("Error creating new server with address %s: %s", addressStr, e.what()); - return ret; - } + [client](boost::variant pvars, boost::optional qps) { + setLuaSideEffect(); - if(qps) { - ret->qps=QPSLimiter(*qps, *qps); - } - g_dstates.modify([ret](servers_t& servers) { - servers.push_back(ret); - std::stable_sort(servers.begin(), servers.end(), [](const decltype(ret)& a, const decltype(ret)& b) { - return a->order < b->order; - }); + std::shared_ptr ret = std::make_shared(ComboAddress()); + newserver_t vars; - }); + ComboAddress serverAddr; + std::string serverAddressStr; + if(auto addrStr = boost::get(&pvars)) { + serverAddressStr = *addrStr; + if(qps) { + vars["qps"] = std::to_string(*qps); + } + } else { + vars = boost::get(pvars); + serverAddressStr = boost::get(vars["address"]); + } - auto localPools = g_pools.getCopy(); - addServerToPool(localPools, "", ret); - g_pools.setState(localPools); + try { + serverAddr = ComboAddress(serverAddressStr, 53); + } + catch(const PDNSException& e) { + g_outputBuffer="Error creating new server: "+string(e.reason); + errlog("Error creating new server with address %s: %s", serverAddressStr, e.reason); + return ret; + } + catch(std::exception& e) { + g_outputBuffer="Error creating new server: "+string(e.what()); + errlog("Error creating new server with address %s: %s", serverAddressStr, e.what()); + return ret; + } - if (ret->connected) { - if(g_launchWork) { - g_launchWork->push_back([ret]() { - ret->tid = thread(responderThread, ret); - }); - } - else { - ret->tid = thread(responderThread, ret); - } - } + if(IsAnyAddress(serverAddr)) { + g_outputBuffer="Error creating new server: invalid address for a downstream server."; + errlog("Error creating new server: %s is not a valid address for a downstream server", serverAddressStr); + return ret; + } - return ret; - } - auto vars=boost::get(pvars); + ComboAddress sourceAddr; + unsigned int sourceItf = 0; + size_t numberOfSockets = 1; + std::set cpus; if(vars.count("source")) { /* handle source in the following forms: @@ -225,26 +207,11 @@ void setupLuaConfig(bool client) } } - std::shared_ptr ret; - try { - ComboAddress address(boost::get(vars["address"]), 53); - if(IsAnyAddress(address)) { - g_outputBuffer="Error creating new server: invalid address for a downstream server."; - errlog("Error creating new server: %s is not a valid address for a downstream server", boost::get(vars["address"])); - return ret; - } - ret=std::make_shared(address, sourceAddr, sourceItf, numberOfSockets); - } - catch(const PDNSException& e) { - g_outputBuffer="Error creating new server: "+string(e.reason); - errlog("Error creating new server with address %s: %s", boost::get(vars["address"]), e.reason); - return ret; - } - catch(std::exception& e) { - g_outputBuffer="Error creating new server: "+string(e.what()); - errlog("Error creating new server with address %s: %s", boost::get(vars["address"]), e.what()); - return ret; - } + if(client) { + // do not construct DownstreamState now, it would try binding sockets. + return ret; + } + ret=std::make_shared(serverAddr, sourceAddr, sourceItf, numberOfSockets); if(vars.count("qps")) { int qpsVal=std::stoi(boost::get(vars["qps"]));