From 04430ab9ab68fdc7c2632ba6c5a91ce435ab505f Mon Sep 17 00:00:00 2001 From: Xie Han <63350856@qq.com> Date: Fri, 14 Feb 2025 21:53:56 +0800 Subject: [PATCH] Simplify DNS resolving when resolving both v4 and v6. --- src/nameservice/WFDnsResolver.cc | 52 ++++++++++++-------------------- 1 file changed, 19 insertions(+), 33 deletions(-) diff --git a/src/nameservice/WFDnsResolver.cc b/src/nameservice/WFDnsResolver.cc index 98a4374e7b..8ba963b092 100644 --- a/src/nameservice/WFDnsResolver.cc +++ b/src/nameservice/WFDnsResolver.cc @@ -211,10 +211,8 @@ using thread_dns_callback_t = std::function; struct DnsContext { - int state; - int error; - int eai_error; unsigned short port; + int eai_error; struct addrinfo *ai; }; @@ -662,10 +660,9 @@ void WFResolverTask::dns_partial_callback(void *net_dns_task) WFGlobal::get_dns_respool()->post(NULL); struct DnsContext *ctx = (struct DnsContext *)dns_task->user_data; + ctx->ai = NULL; - ctx->state = dns_task->get_state(); - ctx->error = dns_task->get_error(); - if (ctx->state == WFT_STATE_SUCCESS) + if (dns_task->get_state() == WFT_STATE_SUCCESS) { protocol::DnsResponse *resp = dns_task->get_resp(); ctx->eai_error = protocol::DnsUtil::getaddrinfo(resp, ctx->port, @@ -678,46 +675,35 @@ void WFResolverTask::dns_partial_callback(void *net_dns_task) void WFResolverTask::dns_parallel_callback(const void *parallel) { const ParallelWork *pwork = (const ParallelWork *)parallel; - struct DnsContext *c4 = (struct DnsContext *)(pwork->get_context()); + struct DnsContext *c4 = (struct DnsContext *)pwork->get_context(); struct DnsContext *c6 = c4 + 1; - DnsOutput out; - if (c4->state != WFT_STATE_SUCCESS && c6->state != WFT_STATE_SUCCESS) - { - this->state = WFT_STATE_DNS_ERROR; - this->error = EAI_AGAIN; - } - else if (c4->eai_error != 0 && c6->eai_error != 0) + if (c4->eai_error == 0 || c6->eai_error == 0) { - int eai_error = c4->eai_error; + struct addrinfo *ai = NULL; + struct addrinfo **pai = &ai; + DnsOutput out; - if (c6->eai_error == EAI_AGAIN) - eai_error = EAI_AGAIN; + *pai = c4->ai; + while (*pai) + pai = &(*pai)->ai_next; - DnsRoutine::create(&out, eai_error, NULL); + *pai = c6->ai; + DnsRoutine::create(&out, 0, ai); dns_callback_internal(&out, dns_ttl_default_, dns_ttl_min_); } else { - struct addrinfo *ai = NULL; - struct addrinfo **pai = &ai; - - if (c4->ai != NULL) - { - *pai = c4->ai; - while (*pai) - pai = &(*pai)->ai_next; - } + int eai_error = c4->eai_error; - if (c6->ai != NULL) - *pai = c6->ai; + if (c6->eai_error == EAI_AGAIN) + eai_error = EAI_AGAIN; - DnsRoutine::create(&out, 0, ai); - dns_callback_internal(&out, dns_ttl_default_, dns_ttl_min_); + this->state = WFT_STATE_DNS_ERROR; + this->error = eai_error; } - delete[] c4; - + delete []c4; task_callback(); }