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/ds/logger.h"
6#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 struct 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 auto resolver = new uv_getaddrinfo_t;
33 resolver->data = ud;
34
35 std::string host =
36 (host_.starts_with("[") && host_.ends_with("]") ?
37 host_.substr(1, host_.size() - 2) :
38 host_);
39
40 int rc;
41
42 if (async)
43 {
44 {
45 std::unique_lock<ccf::pal::Mutex> guard(pending_resolve_requests_mtx);
46 pending_resolve_requests.insert(resolver);
47 }
48
49 if (
50 (rc = uv_getaddrinfo(
51 uv_default_loop(),
52 resolver,
53 cb,
54 host.c_str(),
55 service.c_str(),
56 &hints)) < 0)
57 {
59 "uv_getaddrinfo for host:service [{}:{}] failed (async) with error "
60 "{}",
61 host,
62 service,
63 uv_strerror(rc));
64 {
65 std::unique_lock<ccf::pal::Mutex> guard(
66 pending_resolve_requests_mtx);
67 pending_resolve_requests.erase(resolver);
68 }
69 delete resolver;
70 return false;
71 }
72 }
73 else
74 {
75 if (
76 (rc = uv_getaddrinfo(
77 uv_default_loop(),
78 resolver,
79 nullptr,
80 host.c_str(),
81 service.c_str(),
82 &hints)) < 0)
83 {
85 "uv_getaddrinfo for host:service [{}:{}] failed with error {}",
86 host,
87 service,
88 uv_strerror(rc));
89 delete resolver;
90 return false;
91 }
92
93 cb(resolver, rc, &hints);
94 }
95
96 return true;
97 }
98 };
99}
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 logger.h:363
Definition after_io.h:8
std::mutex Mutex
Definition locking.h:12
Definition configuration.h:14