0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-17 07:20:55 +01:00

modules/s_dns: Simplify/breakdown callback stack related.

This commit is contained in:
Jason Volk 2018-10-03 14:18:27 -07:00
parent 66ab3d85a5
commit 0df3eafea2
3 changed files with 98 additions and 76 deletions

View file

@ -2909,7 +2909,7 @@ ircd::net::dns::resolve(const hostport &hp,
const opts &op, const opts &op,
callback_ipport_one cb) callback_ipport_one cb)
{ {
using prototype = void (const hostport &, const opts &, callback_ipport_one); using prototype = void (const hostport &, opts, callback_ipport_one);
static mods::import<prototype> function static mods::import<prototype> function
{ {
@ -2926,7 +2926,7 @@ ircd::net::dns::resolve(const hostport &hp,
const opts &op, const opts &op,
callback_SRV_one cb) callback_SRV_one cb)
{ {
using prototype = void (const hostport &, const opts &, callback_SRV_one); using prototype = void (const hostport &, opts, callback_SRV_one);
static mods::import<prototype> function static mods::import<prototype> function
{ {
@ -2943,7 +2943,7 @@ ircd::net::dns::resolve(const hostport &hp,
const opts &op, const opts &op,
callback_A_one cb) callback_A_one cb)
{ {
using prototype = void (const hostport &, const opts &, callback_A_one); using prototype = void (const hostport &, opts, callback_A_one);
static mods::import<prototype> function static mods::import<prototype> function
{ {

View file

@ -29,60 +29,58 @@ IRCD_MODULE
/// intermediate results. /// intermediate results.
void void
ircd::net::dns::_resolve_ipport(const hostport &hp, ircd::net::dns::_resolve_ipport(const hostport &hp,
const opts &opts, opts opts,
callback_ipport_one callback) callback_ipport_one callback)
{ {
//TODO: ip6 auto handler
auto calluser{[callback(std::move(callback))]
(std::exception_ptr eptr, const hostport &hp, const uint32_t &ip)
{ {
if(eptr) std::bind(&handle_ipport__A, std::move(callback), ph::_1, ph::_2, ph::_3)
return callback(std::move(eptr), hp, {});
if(!ip)
{
static const net::not_found no_record
{
"Host has no A record"
}; };
return callback(std::make_exception_ptr(no_record), hp, {});
}
const ipport ipport{ip, port(hp)};
callback(std::move(eptr), hp, ipport);
}};
if(!hp.service) if(!hp.service)
return _resolve__A(hp, opts, [calluser(std::move(calluser))] return _resolve__A(hp, opts, std::move(handler));
(std::exception_ptr eptr, const hostport &hp, const rfc1035::record::A &record)
{
calluser(std::move(eptr), hp, record.ip4);
});
auto srv_opts{opts}; opts.nxdomain_exceptions = false;
srv_opts.nxdomain_exceptions = false; _resolve__SRV(hp, opts, [opts(opts), handler(std::move(handler))]
_resolve__SRV(hp, srv_opts, [opts(opts), calluser(std::move(calluser))]
(std::exception_ptr eptr, hostport hp, const rfc1035::record::SRV &record) (std::exception_ptr eptr, hostport hp, const rfc1035::record::SRV &record)
mutable mutable
{ {
if(eptr) if(eptr)
return calluser(std::move(eptr), hp, 0);
if(record.port != 0)
hp.port = record.port;
hp.host = record.tgt?: unmake_SRV_key(hp.host);
opts.qtype = 0;
_resolve__A(hp, opts, [calluser(std::move(calluser))]
(std::exception_ptr eptr, const hostport &hp, const rfc1035::record::A &record)
{ {
calluser(std::move(eptr), hp, record.ip4); static const rfc1035::record::A empty;
}); return handler(std::move(eptr), hp, empty);
}
opts.qtype = 0;
opts.nxdomain_exceptions = true;
hp.host = record.tgt?: unmake_SRV_key(hp.host);
hp.port = record.port? record.port : hp.port;
_resolve__A(hp, opts, std::move(handler));
}); });
} }
void
ircd::net::dns::handle_ipport__A(callback_ipport_one callback,
std::exception_ptr eptr,
const hostport &hp,
const rfc1035::record::A &record)
{
static const ircd::net::not_found no_record
{
"Host has no A record"
};
if(!eptr && !record.ip4)
eptr = std::make_exception_ptr(no_record);
const ipport ipport
{
record.ip4, port(hp)
};
callback(std::move(eptr), hp, ipport);
}
/// Convenience callback with a single SRV record which was selected from /// Convenience callback with a single SRV record which was selected from
/// the vector with stochastic respect for weighting and priority. /// the vector with stochastic respect for weighting and priority.
void void
@ -106,8 +104,19 @@ ircd::net::dns::_resolve__SRV(const hostport &hp,
if(!opts.qtype) if(!opts.qtype)
opts.qtype = qtype; opts.qtype = qtype;
_resolve__(hp, opts, [callback(std::move(callback))] auto handler
(std::exception_ptr eptr, const hostport &hp, const vector_view<const rfc1035::record *> rrs) {
std::bind(&handle__SRV, std::move(callback), ph::_1, ph::_2, ph::_3)
};
_resolve__(hp, opts, std::move(handler));
}
void
ircd::net::dns::handle__SRV(callback_SRV_one callback,
std::exception_ptr eptr,
const hostport &hp,
const records &rrs)
{ {
static const rfc1035::record::SRV empty; static const rfc1035::record::SRV empty;
@ -126,7 +135,6 @@ ircd::net::dns::_resolve__SRV(const hostport &hp,
} }
return callback(std::move(eptr), hp, empty); return callback(std::move(eptr), hp, empty);
});
} }
/// Convenience callback with a single A record which was selected from /// Convenience callback with a single A record which was selected from
@ -152,8 +160,19 @@ ircd::net::dns::_resolve__A(const hostport &hp,
if(!opts.qtype) if(!opts.qtype)
opts.qtype = qtype; opts.qtype = qtype;
_resolve__(hp, opts, [callback(std::move(callback))] auto handler
(std::exception_ptr eptr, const hostport &hp, const vector_view<const rfc1035::record *> &rrs) {
std::bind(&handle__A, std::move(callback), ph::_1, ph::_2, ph::_3)
};
_resolve__(hp, opts, std::move(handler));
}
void
ircd::net::dns::handle__A(callback_A_one callback,
std::exception_ptr eptr,
const hostport &hp,
const records &rrs)
{ {
static const rfc1035::record::A empty; static const rfc1035::record::A empty;
@ -172,7 +191,6 @@ ircd::net::dns::_resolve__A(const hostport &hp,
} }
return callback(std::move(eptr), hp, empty); return callback(std::move(eptr), hp, empty);
});
} }
/// Fundamental callback with a vector of abstract resource records. /// Fundamental callback with a vector of abstract resource records.

View file

@ -16,10 +16,14 @@ namespace ircd::net::dns
// Maximum number of records we present in result vector to any closure // Maximum number of records we present in result vector to any closure
constexpr const size_t MAX_COUNT {64}; constexpr const size_t MAX_COUNT {64};
static void handle__A(callback_A_one, std::exception_ptr, const hostport &, const records &);
static void handle__SRV(callback_SRV_one, std::exception_ptr, const hostport &, const records &);
static void handle_ipport__A(callback_ipport_one, std::exception_ptr, const hostport &, const rfc1035::record::A &);
extern "C" void _resolve__(const hostport &, const opts &, callback); extern "C" void _resolve__(const hostport &, const opts &, callback);
extern "C" void _resolve__A(const hostport &, opts, callback_A_one); extern "C" void _resolve__A(const hostport &, opts, callback_A_one);
extern "C" void _resolve__SRV(const hostport &, opts, callback_SRV_one); extern "C" void _resolve__SRV(const hostport &, opts, callback_SRV_one);
extern "C" void _resolve_ipport(const hostport &, const opts &, callback_ipport_one); extern "C" void _resolve_ipport(const hostport &, opts, callback_ipport_one);
} }
// //