aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/interconnect/interconnect_resolve.cpp
blob: d638ff830cd0c339f982139d5f721967b8956c0b (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "interconnect.h"
#include "interconnect_address.h"
#include "events_local.h"
#include "logging.h"

#include <library/cpp/actors/core/actor_bootstrapped.h>
#include <library/cpp/actors/core/hfunc.h>
#include <library/cpp/actors/dnsresolver/dnsresolver.h>

namespace NActors {

    using namespace NActors::NDnsResolver;

    class TInterconnectResolveActor
        : public TActorBootstrapped<TInterconnectResolveActor>
        , public TInterconnectLoggingBase
    {
    public:
        TInterconnectResolveActor(
                const TString& host, ui16 port, ui32 nodeId, const TString& defaultAddress,
                const TActorId& replyTo, const TActorId& replyFrom, TInstant deadline)
            : Host(host)
            , NodeId(nodeId)
            , Port(port)
            , DefaultAddress(defaultAddress)
            , ReplyTo(replyTo)
            , ReplyFrom(replyFrom)
            , Deadline(deadline)
        { }

        TInterconnectResolveActor(
                const TString& host, ui16 port,
                const TActorId& replyTo, const TActorId& replyFrom, TInstant deadline)
            : Host(host)
            , Port(port)
            , ReplyTo(replyTo)
            , ReplyFrom(replyFrom)
            , Deadline(deadline)
        { }

        static constexpr EActivityType ActorActivityType() {
            return NAMESERVICE;
        }

        void Bootstrap() {
            TMaybe<TString> errorText;
            if (auto addr = ExtractDefaultAddr(errorText)) {
                LOG_TRACE_IC("ICR01", "Host: %s, CACHED address: %s", Host.c_str(), DefaultAddress.c_str());
                if (NodeId) {
                    return SendLocalNodeInfoAndDie({{*addr}});
                } else {
                    return SendAddressInfoAndDie(std::move(addr));
                }
            }

            if (errorText) {
                SendErrorAndDie(*errorText);
            }

            auto now = TActivationContext::Now();
            if (Deadline < now) {
                SendErrorAndDie("Deadline");
                return;
            }

            LOG_DEBUG_IC("ICR02", "Host: %s, RESOLVING address ...", Host.c_str());
            Send(MakeDnsResolverActorId(),
                NodeId
                    ? static_cast<IEventBase*>(new TEvDns::TEvGetHostByName(Host, AF_UNSPEC))
                    : static_cast<IEventBase*>(new TEvDns::TEvGetAddr(Host, AF_UNSPEC)),
                IEventHandle::FlagTrackDelivery);

            if (Deadline != TInstant::Max()) {
                Schedule(Deadline, new TEvents::TEvWakeup);
            }

            Become(&TThis::StateWork);
        }

        STRICT_STFUNC(StateWork, {
            sFunc(TEvents::TEvWakeup, HandleTimeout);
            sFunc(TEvents::TEvUndelivered, HandleUndelivered);
            hFunc(TEvDns::TEvGetAddrResult, Handle);
            hFunc(TEvDns::TEvGetHostByNameResult, Handle);
        });

        void HandleTimeout() {
            SendErrorAndDie("Deadline");
        }

        void HandleUndelivered() {
            SendErrorAndDie("Dns resolver is unavailable");
        }

        void Handle(TEvDns::TEvGetAddrResult::TPtr& ev) {
            if (auto addr = ExtractAddr(ev->Get())) {
                SendAddressInfoAndDie(std::move(addr));
            } else {
                SendErrorAndDie(ev->Get()->ErrorText);
            }
        }

        void Handle(TEvDns::TEvGetHostByNameResult::TPtr& ev) {
            auto& msg = *ev->Get();
            if (msg.Status) {
                SendErrorAndDie(msg.ErrorText);
            } else {
                std::vector<NInterconnect::TAddress> addresses;
                for (const auto& ipv6 : msg.AddrsV6) {
                    addresses.emplace_back(ipv6, Port);
                }
                for (const auto& ipv4 : msg.AddrsV4) {
                    addresses.emplace_back(ipv4, Port);
                }
                SendLocalNodeInfoAndDie(std::move(addresses));
            }
        }

        void SendAddressInfoAndDie(NAddr::IRemoteAddrPtr addr) {
            LOG_DEBUG_IC("ICR03", "Host: %s, RESOLVED address", Host.c_str());
            auto reply = new TEvAddressInfo;
            reply->Address = std::move(addr);
            TActivationContext::Send(new IEventHandle(ReplyTo, ReplyFrom, reply));
            PassAway();
        }

        void SendLocalNodeInfoAndDie(std::vector<NInterconnect::TAddress> addresses) {
            LOG_DEBUG_IC("ICR04", "Host: %s, RESOLVED address", Host.c_str());
            auto reply = std::make_unique<TEvLocalNodeInfo>();
            reply->NodeId = *NodeId;
            reply->Addresses = std::move(addresses);
            TActivationContext::Send(new IEventHandle(ReplyTo, ReplyFrom, reply.release()));
            PassAway();
        }

        void SendErrorAndDie(const TString& errorText) {
            LOG_DEBUG_IC("ICR05", "Host: %s, ERROR resolving: %s", Host.c_str(), errorText.c_str());
            auto *event = new TEvResolveError;
            event->Explain = errorText;
            event->Host = Host;
            TActivationContext::Send(new IEventHandle(ReplyTo, ReplyFrom, event));
            PassAway();
        }

        NAddr::IRemoteAddrPtr ExtractAddr(TEvDns::TEvGetAddrResult* msg) {
            if (msg->Status == 0) {
                if (msg->IsV6()) {
                    struct sockaddr_in6 sin6;
                    Zero(sin6);
                    sin6.sin6_family = AF_INET6;
                    sin6.sin6_addr = msg->GetAddrV6();
                    sin6.sin6_port = HostToInet(Port);
                    return MakeHolder<NAddr::TIPv6Addr>(sin6);
                }

                if (msg->IsV4()) {
                    return MakeHolder<NAddr::TIPv4Addr>(TIpAddress(msg->GetAddrV4().s_addr, Port));
                }

                Y_FAIL("Unexpected result address family");
            }

            return nullptr;
        }

        NAddr::IRemoteAddrPtr ExtractDefaultAddr(TMaybe<TString>& errorText) {
            if (DefaultAddress) {
                NInterconnect::TAddress address(DefaultAddress.data(), Port);

                switch (address.GetFamily()) {
                case AF_INET:
                    return MakeHolder<NAddr::TIPv4Addr>(*(sockaddr_in*)address.SockAddr());
                case AF_INET6:
                    return MakeHolder<NAddr::TIPv6Addr>(*(sockaddr_in6*)address.SockAddr());
                default:
                    errorText = "Unsupported default address: " + DefaultAddress;
                    break;
                }
            }

            return nullptr;
        }

    private:
        const TString Host;
        const std::optional<ui32> NodeId;
        const ui16 Port;
        const TString DefaultAddress;
        const TActorId ReplyTo;
        const TActorId ReplyFrom;
        const TInstant Deadline;
    };

    IActor* CreateResolveActor(
        const TString& host, ui16 port, ui32 nodeId, const TString& defaultAddress,
        const TActorId& replyTo, const TActorId& replyFrom, TInstant deadline)
    {
        return new TInterconnectResolveActor(host, port, nodeId, defaultAddress, replyTo, replyFrom, deadline);
    }

    IActor* CreateResolveActor(
        const TString& host, ui16 port,
        const TActorId& replyTo, const TActorId& replyFrom, TInstant deadline)
    {
        return new TInterconnectResolveActor(host, port, replyTo, replyFrom, deadline);
    }

} // namespace NActors