aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/coroutine/dns/async.cpp
blob: be35135828c1a1c272777cbd3335892faea39e67 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "async.h"

#include <util/generic/singleton.h>
#include <util/generic/vector.h>

#include <contrib/libs/c-ares/include/ares.h>

using namespace NAsyncDns;

namespace {
    struct TAresError: public TDnsError {
        inline TAresError(int code) {
            (*this) << ares_strerror(code);
        }
    };

    struct TAresInit {
        inline TAresInit() {
            const int code = ares_library_init(ARES_LIB_INIT_ALL);

            if (code) {
                ythrow TAresError(code) << "can not init ares engine";
            }
        }

        inline ~TAresInit() {
            ares_library_cleanup();
        }

        static inline void InitOnce() {
            Singleton<TAresInit>();
        }
    };
}

class TAsyncDns::TImpl {
public:
    inline TImpl(IPoller* poller, const TOptions& o)
        : P_(poller)
    {
        TAresInit::InitOnce();

        ares_options opts;

        Zero(opts);

        int optflags = 0;

        optflags |= ARES_OPT_FLAGS;
        opts.flags = ARES_FLAG_STAYOPEN;

        optflags |= ARES_OPT_TIMEOUTMS;
        opts.timeout = o.TimeOut.MilliSeconds();

        optflags |= ARES_OPT_TRIES;
        opts.tries = o.Retries;

        optflags |= ARES_OPT_SOCK_STATE_CB;
        opts.sock_state_cb = (decltype(opts.sock_state_cb))StateCb;
        static_assert(sizeof(opts.sock_state_cb) == sizeof(&StateCb), "Inconsistent socket state size");
        opts.sock_state_cb_data = this;

        const int code = ares_init_options(&H_, &opts, optflags);

        if (code) {
            ythrow TAresError(code) << "can not init ares channel";
        }
    }

    inline ~TImpl() {
        ares_destroy(H_);
    }

    inline TDuration Timeout() {
        struct timeval tv;
        Zero(tv);

        ares_timeout(H_, nullptr, &tv);

        return TDuration(tv);
    }

    template <class T>
    inline void ProcessSocket(T s) {
        ares_process_fd(H_, s, s);
    }

    inline void ProcessNone() {
        ProcessSocket(ARES_SOCKET_BAD);
    }

    inline void AsyncResolve(const TNameRequest& req) {
        ares_gethostbyname(H_, req.Addr, req.Family, AsyncResolveHostCb, req.CB);
    }

private:
    static void StateCb(void* arg, int s, int read, int write) {
        ((TImpl*)arg)->P_->OnStateChange((SOCKET)s, (bool)read, (bool)write);
    }

    static void AsyncResolveHostCb(void* arg, int status, int timeouts, hostent* he) {
        const IHostResult::TResult res = {
            status, timeouts, he};

        ((IHostResult*)arg)->OnComplete(res);
    }

private:
    IPoller* P_;
    ares_channel H_;
};

void NAsyncDns::CheckAsyncStatus(int status) {
    if (status) {
        ythrow TAresError(status);
    }
}

void NAsyncDns::CheckPartialAsyncStatus(int status) {
    if (status == ARES_ENODATA) {
        return;
    }

    CheckAsyncStatus(status);
}

TAsyncDns::TAsyncDns(IPoller* poller, const TOptions& opts)
    : I_(new TImpl(poller, opts))
{
}

TAsyncDns::~TAsyncDns() {
}

void TAsyncDns::AsyncResolve(const TNameRequest& req) {
    I_->AsyncResolve(req);
}

TDuration TAsyncDns::Timeout() {
    return I_->Timeout();
}

void TAsyncDns::ProcessSocket(SOCKET s) {
    I_->ProcessSocket(s);
}

void TAsyncDns::ProcessNone() {
    I_->ProcessNone();
}