CCF
Loading...
Searching...
No Matches
dns.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#include "ccf/pal/locking.h"
7
8#include <unordered_set>
9#include <uv.h>
10
11namespace asynchost
12{
13 static std::unordered_set<uv_getaddrinfo_t*> pending_resolve_requests;
14 static ccf::pal::Mutex pending_resolve_requests_mtx;
15
16 class DNS
17 {
18 public:
19 static bool resolve(
20 const std::string& host_,
21 const std::string& service,
22 void* ud,
23 uv_getaddrinfo_cb cb,
24 bool async)
25 {
26 addrinfo hints{};
27 hints.ai_family = AF_UNSPEC;
28 hints.ai_socktype = SOCK_STREAM;
29 hints.ai_protocol = IPPROTO_TCP;
30 hints.ai_flags = 0;
31
32 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
33 auto* resolver = new uv_getaddrinfo_t;
34 resolver->data = ud;
35
36 std::string host =
37 (host_.starts_with("[") && host_.ends_with("]") ?
38 host_.substr(1, host_.size() - 2) :
39 host_);
40
41 int rc = 0;
42
43 if (async)
44 {
45 {
46 std::unique_lock<ccf::pal::Mutex> guard(pending_resolve_requests_mtx);
47 pending_resolve_requests.insert(resolver);
48 }
49
50 if (
51 (rc = uv_getaddrinfo(
52 uv_default_loop(),
53 resolver,
54 cb,
55 host.c_str(),
56 service.c_str(),
57 &hints)) < 0)
58 {
60 "uv_getaddrinfo for host:service [{}:{}] failed (async) with error "
61 "{}",
62 host,
63 service,
64 uv_strerror(rc));
65 {
66 std::unique_lock<ccf::pal::Mutex> guard(
67 pending_resolve_requests_mtx);
68 pending_resolve_requests.erase(resolver);
69 }
70 delete resolver; // NOLINT(cppcoreguidelines-owning-memory)
71 return false;
72 }
73 }
74 else
75 {
76 if (
77 (rc = uv_getaddrinfo(
78 uv_default_loop(),
79 resolver,
80 nullptr,
81 host.c_str(),
82 service.c_str(),
83 &hints)) < 0)
84 {
86 "uv_getaddrinfo for host:service [{}:{}] failed with error {}",
87 host,
88 service,
89 uv_strerror(rc));
90 delete resolver; // NOLINT(cppcoreguidelines-owning-memory)
91 return false;
92 }
93
94 cb(resolver, rc, &hints);
95 }
96
97 return true;
98 }
99 };
100}
Definition dns.h:17
static bool resolve(const std::string &host_, const std::string &service, void *ud, uv_getaddrinfo_cb cb, bool async)
Definition dns.h:19
#define LOG_FAIL_FMT
Definition internal_logger.h:16
Definition after_io.h:8
std::mutex Mutex
Definition locking.h:12
Definition configuration.h:14