Merge pull request #1439 from kedixa/dev

EndpointParams::address_family, set family for domain name resolution
This commit is contained in:
xiehan
2023-12-07 22:46:39 +08:00
committed by GitHub
3 changed files with 43 additions and 18 deletions
+5 -2
View File
@@ -19,7 +19,8 @@
#ifndef _ENDPOINTPARAMS_H_
#define _ENDPOINTPARAMS_H_
#include <stddef.h>
#include <sys/types.h>
#include <sys/socket.h>
/**
* @file EndpointParams.h
@@ -37,7 +38,8 @@ enum TransportType
struct EndpointParams
{
size_t max_connections;
int address_family;
int max_connections;
int connect_timeout;
int response_timeout;
int ssl_connect_timeout;
@@ -46,6 +48,7 @@ struct EndpointParams
static constexpr struct EndpointParams ENDPOINT_PARAMS_DEFAULT =
{
.address_family = AF_UNSPEC,
.max_connections = 200,
.connect_timeout = 10 * 1000,
.response_timeout = 10 * 1000,
+10 -6
View File
@@ -106,7 +106,7 @@ struct RouteParams
const struct addrinfo *addrinfo;
uint64_t key;
SSL_CTX *ssl_ctx;
unsigned int max_connections;
int max_connections;
int connect_timeout;
int response_timeout;
int ssl_connect_timeout;
@@ -404,14 +404,18 @@ static uint64_t __generate_key(enum TransportType type,
const std::string& hostname)
{
std::string buf((const char *)&type, sizeof (enum TransportType));
unsigned int max_conn = ep_params->max_connections;
if (!other_info.empty())
buf += other_info;
buf.append((const char *)&max_conn, sizeof (unsigned int));
buf.append((const char *)&ep_params->connect_timeout, sizeof (int));
buf.append((const char *)&ep_params->response_timeout, sizeof (int));
int params[] = {
ep_params->address_family,
ep_params->max_connections,
ep_params->connect_timeout,
ep_params->response_timeout
};
buf.append((const char *)params, sizeof params);
if (type == TT_TCP_SSL)
{
buf.append((const char *)&ep_params->ssl_connect_timeout, sizeof (int));
@@ -514,7 +518,7 @@ int RouteManager::get(enum TransportType type,
.addrinfo = addrinfo,
.key = key,
.ssl_ctx = ssl_ctx,
.max_connections = (unsigned int)ep_params->max_connections,
.max_connections = ep_params->max_connections,
.connect_timeout = ep_params->connect_timeout,
.response_timeout = ep_params->response_timeout,
.ssl_connect_timeout = ssl_connect_timeout,
+28 -10
View File
@@ -59,14 +59,16 @@ class DnsInput
public:
DnsInput() :
port_(0),
numeric_host_(false)
numeric_host_(false),
family_(AF_UNSPEC)
{}
DnsInput(const std::string& host, unsigned short port,
bool numeric_host) :
bool numeric_host, int family) :
host_(host),
port_(port),
numeric_host_(numeric_host)
numeric_host_(numeric_host),
family_(family)
{}
void reset(const std::string& host, unsigned short port)
@@ -74,14 +76,16 @@ public:
host_.assign(host);
port_ = port;
numeric_host_ = false;
family_ = AF_UNSPEC;
}
void reset(const std::string& host, unsigned short port,
bool numeric_host)
bool numeric_host, int family)
{
host_.assign(host);
port_ = port;
numeric_host_ = numeric_host;
family_ = family;
}
const std::string& get_host() const { return host_; }
@@ -92,6 +96,7 @@ protected:
std::string host_;
unsigned short port_;
bool numeric_host_;
int family_;
friend class DnsRoutine;
};
@@ -187,6 +192,7 @@ void DnsRoutine::run(const DnsInput *in, DnsOutput *out)
char port_str[PORT_STR_MAX + 1];
hints.ai_flags |= AI_NUMERICSERV;
hints.ai_family = in->family_;
if (in->is_numeric_host())
hints.ai_flags |= AI_NUMERICHOST;
@@ -337,6 +343,7 @@ static void __add_passive_flags(struct addrinfo *ai)
static ThreadDnsTask *__create_thread_dns_task(const std::string& host,
unsigned short port,
int family,
thread_dns_callback_t callback)
{
auto *task = WFThreadTaskFactory<DnsInput, DnsOutput>::
@@ -345,7 +352,7 @@ static ThreadDnsTask *__create_thread_dns_task(const std::string& host,
DnsRoutine::run,
std::move(callback));
task->get_input()->reset(host, port);
task->get_input()->reset(host, port, false, family);
return task;
}
@@ -409,7 +416,8 @@ void WFResolverTask::dispatch()
if (ret == 1)
{
DnsInput dns_in(hostname, port_, true); // 'true' means numeric host
// 'true' means numeric host
DnsInput dns_in(hostname, port_, true, AF_UNSPEC);
DnsOutput dns_out;
DnsRoutine::run(&dns_in, &dns_out);
@@ -424,7 +432,11 @@ void WFResolverTask::dispatch()
if (hosts)
{
struct addrinfo *ai;
int ret = __readaddrinfo(hosts, host_, port_, &__ai_hints, &ai);
struct addrinfo hints = __ai_hints;
int ret;
hints.ai_family = ep_params_.address_family;
ret = __readaddrinfo(hosts, host_, port_, &hints, &ai);
if (ret == 0)
{
@@ -440,9 +452,13 @@ void WFResolverTask::dispatch()
WFDnsClient *client = WFGlobal::get_dns_client();
if (client)
{
static int family = __default_family();
static int default_family = __default_family();
WFResourcePool *respool = WFGlobal::get_dns_respool();
int family = ep_params_.address_family;
if (family == AF_UNSPEC)
family = default_family;
if (family == AF_INET || family == AF_INET6)
{
auto&& cb = std::bind(&WFResolverTask::dns_single_callback,
@@ -492,11 +508,13 @@ void WFResolverTask::dispatch()
}
else
{
ThreadDnsTask *dns_task;
auto&& cb = std::bind(&WFResolverTask::thread_dns_callback,
this,
std::placeholders::_1);
ThreadDnsTask *dns_task = __create_thread_dns_task(hostname, port_,
std::move(cb));
dns_task = __create_thread_dns_task(hostname, port_,
ep_params_.address_family,
std::move(cb));
series_of(this)->push_front(dns_task);
}