Skip to content

Commit

Permalink
Merge pull request #1439 from kedixa/dev
Browse files Browse the repository at this point in the history
EndpointParams::address_family, set family for domain name resolution
  • Loading branch information
Barenboim authored Dec 7, 2023
2 parents b876f85 + bf48d96 commit fb4c512
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
7 changes: 5 additions & 2 deletions src/manager/EndpointParams.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
#ifndef _ENDPOINTPARAMS_H_
#define _ENDPOINTPARAMS_H_

#include <stddef.h>
#include <sys/types.h>
#include <sys/socket.h>

/**
* @file EndpointParams.h
Expand All @@ -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;
Expand All @@ -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,
Expand Down
16 changes: 10 additions & 6 deletions src/manager/RouteManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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,
Expand Down
38 changes: 28 additions & 10 deletions src/nameservice/WFDnsResolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,33 @@ 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)
{
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_; }
Expand All @@ -92,6 +96,7 @@ class DnsInput
std::string host_;
unsigned short port_;
bool numeric_host_;
int family_;

friend class DnsRoutine;
};
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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>::
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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)
{
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit fb4c512

Please sign in to comment.