2018-10-01 04:01:06 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
2019-09-25 21:48:00 +02:00
|
|
|
namespace ircd::net::dns
|
|
|
|
{
|
|
|
|
template<class T> static rfc1035::record *new_record(mutable_buffer &, const rfc1035::answer &);
|
|
|
|
static void handle_resolved(std::exception_ptr, const tag &, const answers &);
|
|
|
|
|
2020-03-05 23:15:14 +01:00
|
|
|
static void handle_resolve_A_ipport(const hostport &, const json::object &rr, opts, callback_ipport);
|
2019-09-25 21:48:00 +02:00
|
|
|
static void handle_resolve_SRV_ipport(const hostport &, const json::object &rr, opts, callback_ipport);
|
|
|
|
static void handle_resolve_one(const hostport &, const json::array &rr, callback_one);
|
|
|
|
}
|
|
|
|
|
2019-10-06 02:02:50 +02:00
|
|
|
decltype(ircd::net::dns::log)
|
|
|
|
ircd::net::dns::log
|
2018-10-01 04:01:06 +02:00
|
|
|
{
|
2019-10-06 02:02:50 +02:00
|
|
|
"net.dns"
|
2018-10-01 04:01:06 +02:00
|
|
|
};
|
|
|
|
|
2019-10-06 02:02:50 +02:00
|
|
|
decltype(ircd::net::dns::opts_default)
|
|
|
|
ircd::net::dns::opts_default;
|
|
|
|
|
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::net::dns::init::init()
|
2019-08-16 09:27:09 +02:00
|
|
|
{
|
2020-03-08 23:05:21 +01:00
|
|
|
#ifdef HAVE_NETDB_H
|
|
|
|
static const int stay_open {true};
|
2020-03-12 18:00:09 +01:00
|
|
|
::setservent(stay_open);
|
2020-03-08 23:05:21 +01:00
|
|
|
#endif
|
|
|
|
|
2019-10-06 02:02:50 +02:00
|
|
|
assert(!resolver_instance);
|
|
|
|
resolver_instance = new resolver
|
|
|
|
{
|
|
|
|
handle_resolved
|
|
|
|
};
|
2019-08-16 09:27:09 +02:00
|
|
|
}
|
|
|
|
|
2019-10-06 02:02:50 +02:00
|
|
|
ircd::net::dns::init::~init()
|
|
|
|
noexcept
|
2019-08-16 09:27:09 +02:00
|
|
|
{
|
2019-10-06 02:02:50 +02:00
|
|
|
delete resolver_instance;
|
|
|
|
resolver_instance = nullptr;
|
2020-03-08 23:05:21 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_NETDB_H
|
2020-03-12 18:00:09 +01:00
|
|
|
::endservent();
|
2020-03-08 23:05:21 +01:00
|
|
|
#endif
|
2019-08-16 09:27:09 +02:00
|
|
|
}
|
|
|
|
|
2019-10-06 02:02:50 +02:00
|
|
|
//
|
|
|
|
// net/dns.h
|
|
|
|
//
|
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
void
|
|
|
|
ircd::net::dns::resolve(const hostport &hp,
|
|
|
|
const opts &opts_,
|
|
|
|
callback_ipport callback)
|
|
|
|
{
|
|
|
|
if(unlikely(!port(hp) && !hp.service))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"Port or service is required for this query"
|
|
|
|
};
|
|
|
|
|
|
|
|
dns::opts opts(opts_);
|
2019-03-23 10:32:20 +01:00
|
|
|
opts.qtype = opts_.qtype?: 33; // default to SRV
|
2019-03-24 21:20:40 +01:00
|
|
|
|
|
|
|
if(opts.qtype == 33)
|
2018-10-01 21:46:35 +02:00
|
|
|
{
|
2019-03-24 21:20:40 +01:00
|
|
|
opts.nxdomain_exceptions = false;
|
|
|
|
net::dns::callback_one handler
|
|
|
|
{
|
|
|
|
std::bind(&handle_resolve_SRV_ipport, ph::_1, ph::_2, opts, std::move(callback))
|
|
|
|
};
|
2019-03-22 02:24:36 +01:00
|
|
|
|
2019-03-24 21:20:40 +01:00
|
|
|
resolve(hp, opts, std::move(handler));
|
|
|
|
}
|
|
|
|
else if(opts.qtype == 1 || opts.qtype == 28)
|
|
|
|
{
|
|
|
|
net::dns::callback_one handler
|
|
|
|
{
|
2020-03-05 23:15:14 +01:00
|
|
|
std::bind(&handle_resolve_A_ipport, ph::_1, ph::_2, opts, std::move(callback))
|
2019-03-24 21:20:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
resolve(hp, opts, std::move(handler));
|
|
|
|
}
|
|
|
|
else throw error
|
|
|
|
{
|
|
|
|
"Query type:%u not valid for ipport result callback.", opts.qtype
|
|
|
|
};
|
2018-10-01 21:46:35 +02:00
|
|
|
}
|
|
|
|
|
2018-10-03 23:18:27 +02:00
|
|
|
void
|
2019-03-22 02:24:36 +01:00
|
|
|
ircd::net::dns::resolve(const hostport &hp,
|
|
|
|
const opts &opts,
|
|
|
|
callback_one callback)
|
2018-10-03 23:18:27 +02:00
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
if(unlikely(!opts.qtype))
|
|
|
|
throw error
|
|
|
|
{
|
2020-03-05 22:58:21 +01:00
|
|
|
"Query type is required; not specified; cannot be deduced here."
|
2019-03-22 02:24:36 +01:00
|
|
|
};
|
2018-10-03 23:18:27 +02:00
|
|
|
|
2019-03-23 08:59:25 +01:00
|
|
|
dns::callback handler
|
2018-10-03 23:18:27 +02:00
|
|
|
{
|
2019-03-23 08:59:25 +01:00
|
|
|
std::bind(&handle_resolve_one, ph::_1, ph::_2, std::move(callback))
|
|
|
|
};
|
|
|
|
|
|
|
|
resolve(hp, opts, std::move(handler));
|
2019-03-22 02:24:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-03-06 20:41:09 +01:00
|
|
|
ircd::net::dns::resolve(const hostport &hp_,
|
2019-03-22 02:24:36 +01:00
|
|
|
const opts &opts,
|
|
|
|
callback cb)
|
|
|
|
{
|
2020-03-06 20:41:09 +01:00
|
|
|
hostport hp(hp_);
|
2019-03-22 02:24:36 +01:00
|
|
|
if(unlikely(!opts.qtype))
|
|
|
|
throw error
|
|
|
|
{
|
2020-03-05 22:58:21 +01:00
|
|
|
"Query type is required; not specified; cannot be deduced here."
|
2019-03-22 02:24:36 +01:00
|
|
|
};
|
|
|
|
|
2020-03-06 20:41:09 +01:00
|
|
|
// Make any necessary attempt to translate a service name into a portnum.
|
|
|
|
if(likely(opts.service_port))
|
|
|
|
if(!port(hp) && service(hp))
|
2020-03-11 20:30:31 +01:00
|
|
|
port(hp) = service_port(service(hp), opts.proto);
|
2020-03-06 20:41:09 +01:00
|
|
|
|
2019-09-11 21:11:01 +02:00
|
|
|
// Try to satisfy from the cache first. This requires a ctx.
|
|
|
|
if(likely(ctx::current && opts.cache_check))
|
2019-03-22 02:24:36 +01:00
|
|
|
if(cache::get(hp, opts, cb))
|
|
|
|
return;
|
2018-10-03 23:18:27 +02:00
|
|
|
|
2019-09-11 21:11:01 +02:00
|
|
|
// Remote query will be made; register this callback as waiting for reply
|
2019-09-12 05:09:09 +02:00
|
|
|
assert(cb);
|
2020-04-25 08:50:44 +02:00
|
|
|
const ctx::critical_assertion ca;
|
2019-09-12 05:09:09 +02:00
|
|
|
cache::waiting.emplace_back(hp, opts, std::move(cb));
|
2019-03-25 22:06:10 +01:00
|
|
|
|
2019-09-11 21:11:01 +02:00
|
|
|
// Check if there is already someone else waiting on the same query
|
2020-04-25 08:50:44 +02:00
|
|
|
const auto existing
|
2019-09-11 21:11:01 +02:00
|
|
|
{
|
2020-04-25 08:50:44 +02:00
|
|
|
std::find_if(begin(cache::waiting), end(cache::waiting), []
|
2019-09-11 21:11:01 +02:00
|
|
|
(const auto &a)
|
|
|
|
{
|
2019-09-12 19:58:55 +02:00
|
|
|
return a == cache::waiting.back();
|
2019-09-11 21:11:01 +02:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
// When nobody else is already waiting on this query we have to submit it.
|
2020-04-25 08:50:44 +02:00
|
|
|
assert(existing != end(cache::waiting));
|
|
|
|
if(*existing == cache::waiting.back())
|
2019-09-11 21:11:01 +02:00
|
|
|
resolver_call(hp, opts);
|
2019-03-23 08:59:25 +01:00
|
|
|
}
|
|
|
|
|
2019-10-06 02:02:50 +02:00
|
|
|
/// Really assumptional and hacky right now. We're just assuming the SRV
|
|
|
|
/// key is the first two elements of a dot-delimited string which start
|
|
|
|
/// with underscores. If that isn't good enough in the future this will rot
|
|
|
|
/// and become a regression hazard.
|
|
|
|
ircd::string_view
|
|
|
|
ircd::net::dns::unmake_SRV_key(const string_view &key)
|
|
|
|
{
|
|
|
|
if(token_count(key, '.') < 3)
|
|
|
|
return key;
|
|
|
|
|
|
|
|
if(!startswith(token(key, '.', 0), '_'))
|
|
|
|
return key;
|
|
|
|
|
|
|
|
if(!startswith(token(key, '.', 1), '_'))
|
|
|
|
return key;
|
|
|
|
|
|
|
|
return tokens_after(key, '.', 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::net::dns::make_SRV_key(const mutable_buffer &out,
|
|
|
|
const hostport &hp,
|
|
|
|
const opts &opts)
|
|
|
|
{
|
|
|
|
thread_local char tlbuf[2][rfc1035::NAME_BUFSIZE];
|
2020-03-05 20:05:10 +01:00
|
|
|
if(unlikely(!service(hp) && !opts.srv))
|
|
|
|
throw error
|
2019-10-06 02:02:50 +02:00
|
|
|
{
|
2020-03-05 20:05:10 +01:00
|
|
|
"Service name or query string option is required for SRV lookup."
|
2019-10-06 02:02:50 +02:00
|
|
|
};
|
2020-03-05 20:05:10 +01:00
|
|
|
|
|
|
|
assert(host(hp));
|
|
|
|
if(!service(hp))
|
|
|
|
{
|
|
|
|
assert(opts.srv);
|
2019-10-06 02:02:50 +02:00
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
out, "%s%s",
|
|
|
|
opts.srv,
|
|
|
|
tolower(tlbuf[1], host(hp))
|
|
|
|
};
|
2020-03-05 20:05:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(service(hp));
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
out, "_%s._%s.%s",
|
|
|
|
tolower(tlbuf[0], service(hp)),
|
|
|
|
opts.proto,
|
|
|
|
tolower(tlbuf[1], host(hp)),
|
|
|
|
};
|
2019-10-06 02:02:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::object
|
|
|
|
ircd::net::dns::random_choice(const json::array &rrs)
|
|
|
|
{
|
|
|
|
const size_t &count
|
|
|
|
{
|
|
|
|
rrs.size()
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!count)
|
|
|
|
return json::object{};
|
|
|
|
|
|
|
|
const auto choice
|
|
|
|
{
|
|
|
|
rand::integer(0, count - 1)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(choice < count);
|
|
|
|
const json::object &rr
|
|
|
|
{
|
|
|
|
rrs[choice]
|
|
|
|
};
|
|
|
|
|
|
|
|
return rr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::net::dns::expired(const json::object &rr,
|
|
|
|
const time_t &rr_ts)
|
|
|
|
{
|
|
|
|
const seconds &min_seconds
|
|
|
|
{
|
|
|
|
cache::min_ttl
|
|
|
|
};
|
|
|
|
|
|
|
|
const seconds &err_seconds
|
|
|
|
{
|
|
|
|
cache::error_ttl
|
|
|
|
};
|
|
|
|
|
|
|
|
const time_t &min
|
|
|
|
{
|
|
|
|
is_error(rr)?
|
|
|
|
err_seconds.count():
|
|
|
|
min_seconds.count()
|
|
|
|
};
|
|
|
|
|
|
|
|
return expired(rr, rr_ts, min);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::net::dns::expired(const json::object &rr,
|
|
|
|
const time_t &rr_ts,
|
|
|
|
const time_t &min_ttl)
|
|
|
|
{
|
|
|
|
const auto &ttl
|
|
|
|
{
|
|
|
|
get_ttl(rr)
|
|
|
|
};
|
|
|
|
|
|
|
|
return rr_ts + std::max(ttl, min_ttl) < ircd::time();
|
|
|
|
}
|
|
|
|
|
|
|
|
time_t
|
|
|
|
ircd::net::dns::get_ttl(const json::object &rr)
|
|
|
|
{
|
|
|
|
return rr.get<time_t>("ttl", 0L);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::net::dns::is_empty(const json::array &rrs)
|
|
|
|
{
|
|
|
|
return std::all_of(begin(rrs), end(rrs), []
|
|
|
|
(const json::object &rr)
|
|
|
|
{
|
|
|
|
return is_empty(rr);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::net::dns::is_empty(const json::object &rr)
|
|
|
|
{
|
|
|
|
return empty(rr) || (rr.has("ttl") && size(rr) == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::net::dns::is_error(const json::array &rrs)
|
|
|
|
{
|
|
|
|
return !std::none_of(begin(rrs), end(rrs), []
|
|
|
|
(const json::object &rr)
|
|
|
|
{
|
|
|
|
return is_error(rr);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::net::dns::is_error(const json::object &rr)
|
|
|
|
{
|
|
|
|
return rr.has("error");
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// internal
|
|
|
|
//
|
|
|
|
|
2019-03-23 08:59:25 +01:00
|
|
|
void
|
|
|
|
ircd::net::dns::handle_resolve_one(const hostport &hp,
|
|
|
|
const json::array &rrs,
|
|
|
|
callback_one callback)
|
|
|
|
{
|
|
|
|
const json::object &rr
|
2019-03-22 02:24:36 +01:00
|
|
|
{
|
2019-03-24 22:50:26 +01:00
|
|
|
random_choice(rrs)
|
2019-03-23 08:59:25 +01:00
|
|
|
};
|
2019-03-22 02:24:36 +01:00
|
|
|
|
2019-03-23 08:59:25 +01:00
|
|
|
callback(hp, rr);
|
2018-10-03 23:18:27 +02:00
|
|
|
}
|
|
|
|
|
2018-10-01 21:46:35 +02:00
|
|
|
void
|
2019-03-23 08:59:25 +01:00
|
|
|
ircd::net::dns::handle_resolve_SRV_ipport(const hostport &hp,
|
|
|
|
const json::object &rr,
|
|
|
|
opts opts,
|
|
|
|
callback_ipport callback)
|
2018-10-01 21:46:35 +02:00
|
|
|
{
|
2019-03-23 08:59:25 +01:00
|
|
|
const json::string &error
|
2018-10-03 07:38:08 +02:00
|
|
|
{
|
2019-03-23 08:59:25 +01:00
|
|
|
rr.get("error")
|
2018-10-03 07:38:08 +02:00
|
|
|
};
|
|
|
|
|
2019-03-23 08:59:25 +01:00
|
|
|
const hostport &target
|
|
|
|
{
|
|
|
|
rr.has("tgt")?
|
|
|
|
rstrip(unquote(rr.at("tgt")), '.'):
|
|
|
|
host(hp),
|
2018-10-03 07:38:08 +02:00
|
|
|
|
2019-03-23 08:59:25 +01:00
|
|
|
rr.has("port")?
|
|
|
|
rr.get<uint16_t>("port"):
|
|
|
|
!error?
|
|
|
|
port(hp):
|
|
|
|
uint16_t(0)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(error)
|
2019-03-22 02:24:36 +01:00
|
|
|
{
|
2019-03-23 08:59:25 +01:00
|
|
|
static const ipport empty;
|
2019-08-26 20:03:15 +02:00
|
|
|
const auto eptr
|
|
|
|
{
|
|
|
|
make_exception_ptr<rfc1035::error>(exception::hide_name, "%s", error)
|
|
|
|
};
|
|
|
|
|
2019-03-23 08:59:25 +01:00
|
|
|
return callback(eptr, target, empty);
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.qtype = 1;
|
|
|
|
opts.nxdomain_exceptions = true;
|
|
|
|
net::dns::callback_one handler
|
|
|
|
{
|
2020-03-05 23:15:14 +01:00
|
|
|
std::bind(&handle_resolve_A_ipport, ph::_1, ph::_2, opts, std::move(callback))
|
2019-03-22 02:24:36 +01:00
|
|
|
};
|
|
|
|
|
2019-03-23 08:59:25 +01:00
|
|
|
resolve(target, opts, std::move(handler));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::net::dns::handle_resolve_A_ipport(const hostport &hp,
|
|
|
|
const json::object &rr,
|
|
|
|
const opts opts,
|
|
|
|
const callback_ipport callback)
|
|
|
|
{
|
|
|
|
const json::string &error
|
2019-03-22 02:24:36 +01:00
|
|
|
{
|
2019-03-23 08:59:25 +01:00
|
|
|
rr.get("error")
|
2019-03-22 02:24:36 +01:00
|
|
|
};
|
2018-10-03 07:38:08 +02:00
|
|
|
|
2019-03-23 08:59:25 +01:00
|
|
|
const json::string &ip
|
2018-10-01 21:46:35 +02:00
|
|
|
{
|
2019-03-24 21:20:40 +01:00
|
|
|
opts.qtype == 28?
|
|
|
|
rr.get("ip", ":::0"_sv):
|
|
|
|
rr.get("ip", "0.0.0.0"_sv)
|
2018-10-03 23:18:27 +02:00
|
|
|
};
|
2018-10-01 21:46:35 +02:00
|
|
|
|
2019-03-23 08:59:25 +01:00
|
|
|
const ipport &ipport
|
2019-03-22 02:24:36 +01:00
|
|
|
{
|
2020-03-05 23:15:14 +01:00
|
|
|
ip, port(hp)
|
2019-03-23 08:59:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto eptr
|
2019-03-22 02:24:36 +01:00
|
|
|
{
|
2019-03-23 08:59:25 +01:00
|
|
|
!empty(error)?
|
2019-08-26 20:03:15 +02:00
|
|
|
make_exception_ptr<rfc1035::error>(exception::hide_name, "%s", error):
|
2019-03-23 08:59:25 +01:00
|
|
|
!ipport?
|
|
|
|
make_exception_ptr<net::error>("Host has no A record."):
|
|
|
|
std::exception_ptr{}
|
2019-03-22 02:24:36 +01:00
|
|
|
};
|
2019-03-23 08:59:25 +01:00
|
|
|
|
2020-03-05 23:15:14 +01:00
|
|
|
callback(eptr, hp, ipport);
|
2018-10-03 23:18:27 +02:00
|
|
|
}
|
2018-10-01 21:46:35 +02:00
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
/// Called back from the dns::resolver with a vector of answers to the
|
|
|
|
/// question (we get the whole tag here).
|
|
|
|
///
|
|
|
|
/// This is being invoked on the dns::resolver's receiver context stack
|
|
|
|
/// under lock preventing any other activity with the resolver.
|
|
|
|
///
|
|
|
|
/// We process these results and insert them into our cache. The cache
|
|
|
|
/// insertion involves sending a message to the DNS room. Matrix hooks
|
|
|
|
/// on that room will catch this message for the user(s) which initiated
|
|
|
|
/// this query; we don't callback or deal with said users here.
|
|
|
|
///
|
2018-10-03 23:18:27 +02:00
|
|
|
void
|
2019-03-22 02:24:36 +01:00
|
|
|
ircd::net::dns::handle_resolved(std::exception_ptr eptr,
|
|
|
|
const tag &tag,
|
|
|
|
const answers &an)
|
|
|
|
try
|
2018-10-03 23:18:27 +02:00
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
static const size_t recsz(1024);
|
|
|
|
thread_local char recbuf[recsz * MAX_COUNT];
|
|
|
|
thread_local std::array<const rfc1035::record *, MAX_COUNT> record;
|
|
|
|
|
|
|
|
size_t i(0);
|
|
|
|
mutable_buffer buf{recbuf};
|
|
|
|
for(; i < an.size(); ++i) switch(an.at(i).qtype)
|
2018-11-14 08:19:57 +01:00
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
case 1:
|
|
|
|
record.at(i) = new_record<rfc1035::record::A>(buf, an.at(i));
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
record.at(i) = new_record<rfc1035::record::CNAME>(buf, an.at(i));
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 28:
|
|
|
|
record.at(i) = new_record<rfc1035::record::AAAA>(buf, an.at(i));
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 33:
|
|
|
|
record.at(i) = new_record<rfc1035::record::SRV>(buf, an.at(i));
|
|
|
|
continue;
|
2018-10-01 04:01:06 +02:00
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
default:
|
|
|
|
record.at(i) = new_record<rfc1035::record>(buf, an.at(i));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the records by type so we can create smaller vectors to send to the
|
|
|
|
// cache. nulls from running out of space should be pushed to the back.
|
|
|
|
std::sort(begin(record), begin(record) + an.size(), []
|
|
|
|
(const auto *const &a, const auto *const &b)
|
|
|
|
{
|
|
|
|
if(!a)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!b)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return a->type < b->type;
|
|
|
|
});
|
|
|
|
|
|
|
|
//TODO: don't send cache ephemeral rcodes
|
|
|
|
// Bail on error here; send the cache the message
|
2018-10-03 23:18:27 +02:00
|
|
|
if(eptr)
|
2019-03-22 02:24:36 +01:00
|
|
|
{
|
|
|
|
cache::put(tag.hp, tag.opts, tag.rcode, what(eptr));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Branch on no records with no error
|
|
|
|
if(!i)
|
|
|
|
{
|
|
|
|
static const records empty;
|
|
|
|
cache::put(tag.hp, tag.opts, empty);
|
|
|
|
return;
|
|
|
|
}
|
2018-10-03 23:18:27 +02:00
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
// Iterate the record vector which was sorted by type;
|
|
|
|
// send the cache an individual view of each type since
|
|
|
|
// the cache is organized by record type.
|
|
|
|
size_t s(0), e(0);
|
|
|
|
auto last(record.at(e)->type);
|
|
|
|
for(++e; e <= i; ++e)
|
2018-10-03 23:18:27 +02:00
|
|
|
{
|
2019-03-22 02:24:36 +01:00
|
|
|
if(e < i && record.at(e)->type == last)
|
2018-10-03 23:18:27 +02:00
|
|
|
continue;
|
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
const vector_view<const rfc1035::record *> records
|
|
|
|
{
|
|
|
|
record.data() + s, record.data() + e
|
|
|
|
};
|
|
|
|
|
2019-03-26 03:21:22 +01:00
|
|
|
assert(!empty(records));
|
2019-03-22 02:24:36 +01:00
|
|
|
cache::put(tag.hp, tag.opts, records);
|
2019-03-26 03:21:22 +01:00
|
|
|
s = e;
|
2019-03-22 02:24:36 +01:00
|
|
|
if(e < i)
|
|
|
|
last = record.at(e)->type;
|
2018-10-03 23:18:27 +02:00
|
|
|
}
|
2019-03-22 21:30:55 +01:00
|
|
|
|
|
|
|
// We have to send something to the cache with the same type
|
|
|
|
// as the query, otherwise our user will never get a response
|
|
|
|
// to what they're waiting for.
|
|
|
|
bool has_tag_qtype{false};
|
|
|
|
for(size_t i(0); i < an.size() && !has_tag_qtype; ++i)
|
|
|
|
has_tag_qtype = an.at(i).qtype == tag.opts.qtype;
|
|
|
|
|
|
|
|
if(!has_tag_qtype)
|
|
|
|
{
|
|
|
|
static const records empty;
|
|
|
|
cache::put(tag.hp, tag.opts, empty);
|
|
|
|
}
|
2019-03-22 02:24:36 +01:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "handle resolved: tag[%u] :%s",
|
|
|
|
tag.id,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-10-03 23:18:27 +02:00
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
throw;
|
2018-10-01 21:46:35 +02:00
|
|
|
}
|
|
|
|
|
2019-03-22 02:24:36 +01:00
|
|
|
template<class type>
|
|
|
|
ircd::rfc1035::record *
|
|
|
|
ircd::net::dns::new_record(mutable_buffer &buf,
|
|
|
|
const rfc1035::answer &answer)
|
|
|
|
{
|
|
|
|
if(unlikely(sizeof(type) > size(buf)))
|
|
|
|
return nullptr;
|
|
|
|
|
2019-09-07 22:22:36 +02:00
|
|
|
void *const pos(data(buf));
|
2019-03-22 02:24:36 +01:00
|
|
|
consume(buf, sizeof(type));
|
2019-09-04 19:21:02 +02:00
|
|
|
return new (pos) type(answer);
|
2019-03-22 02:24:36 +01:00
|
|
|
}
|