From d816bf7c8831b2eb43b2c1b2a29e169bed0d33c3 Mon Sep 17 00:00:00 2001 From: kedixa <1204837541@qq.com> Date: Thu, 7 Dec 2023 19:52:03 +0800 Subject: [PATCH 1/2] EndpointParams::address_family, set family for domain name resolution --- src/manager/EndpointParams.h | 7 ++++-- src/manager/RouteManager.cc | 14 +++++++----- src/nameservice/WFDnsResolver.cc | 38 +++++++++++++++++++++++--------- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/manager/EndpointParams.h b/src/manager/EndpointParams.h index 737430dc..c0febae1 100644 --- a/src/manager/EndpointParams.h +++ b/src/manager/EndpointParams.h @@ -19,7 +19,8 @@ #ifndef _ENDPOINTPARAMS_H_ #define _ENDPOINTPARAMS_H_ -#include +#include +#include /** * @file EndpointParams.h @@ -37,7 +38,8 @@ enum TransportType struct EndpointParams { - size_t max_connections; + int address_family; + unsigned 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, diff --git a/src/manager/RouteManager.cc b/src/manager/RouteManager.cc index 931f5cb6..4ddcf828 100644 --- a/src/manager/RouteManager.cc +++ b/src/manager/RouteManager.cc @@ -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, + (int)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, diff --git a/src/nameservice/WFDnsResolver.cc b/src/nameservice/WFDnsResolver.cc index b1089a70..fc807274 100644 --- a/src/nameservice/WFDnsResolver.cc +++ b/src/nameservice/WFDnsResolver.cc @@ -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:: @@ -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); } From bf48d96a7dcdafb35e5a300c86f09e424d6a1244 Mon Sep 17 00:00:00 2001 From: kedixa <1204837541@qq.com> Date: Thu, 7 Dec 2023 20:15:33 +0800 Subject: [PATCH 2/2] change max_connections type to int --- src/manager/EndpointParams.h | 2 +- src/manager/RouteManager.cc | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/manager/EndpointParams.h b/src/manager/EndpointParams.h index c0febae1..aec2cf1e 100644 --- a/src/manager/EndpointParams.h +++ b/src/manager/EndpointParams.h @@ -39,7 +39,7 @@ enum TransportType struct EndpointParams { int address_family; - unsigned int max_connections; + int max_connections; int connect_timeout; int response_timeout; int ssl_connect_timeout; diff --git a/src/manager/RouteManager.cc b/src/manager/RouteManager.cc index 4ddcf828..607d041d 100644 --- a/src/manager/RouteManager.cc +++ b/src/manager/RouteManager.cc @@ -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; @@ -410,7 +410,7 @@ static uint64_t __generate_key(enum TransportType type, int params[] = { ep_params->address_family, - (int)ep_params->max_connections, + ep_params->max_connections, ep_params->connect_timeout, ep_params->response_timeout };