• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef MDNS_MANAGER_H
17 #define MDNS_MANAGER_H
18 
19 #include <any>
20 #include <list>
21 #include <set>
22 #include <string>
23 #include <queue>
24 
25 #include "mdns_common.h"
26 #include "mdns_packet_parser.h"
27 #include "mdns_socket_listener.h"
28 
29 namespace OHOS {
30 namespace NetManagerStandard {
31 
32 struct MDnsConfig {
33     bool ipv6Support = false;
34     int configAllIface = true;
35     int configLo = true;
36     std::string topDomain = MDNS_TOP_DOMAIN_DEFAULT;
37     std::string hostname;
38 };
39 
40 class MDnsProtocolImpl {
41 public:
42     MDnsProtocolImpl();
43     ~MDnsProtocolImpl() = default;
44 
45     enum ResultType {
46         UNKNOWN,
47         SERVICE_STARTED,
48         SERVICE_STOPED,
49         SERVICE_FOUND,
50         SERVICE_LOST,
51         INSTANCE_RESOLVED,
52         DOMAIN_RESOLVED
53     };
54 
55     using TxtRecord = std::map<std::string, std::vector<uint8_t>>;
56     using Task = std::function<bool()>;
57 
58     struct Result {
59         ResultType type;
60         std::string serviceName;
61         std::string serviceType;
62         std::string domain;
63         int port = -1;
64         bool ipv6 = false;
65         std::string addr;
66         TxtRecordEncoded txt;
67         std::string iface;
68     };
69 
70     using Handler = std::function<void(const Result &, int32_t)>;
71 
72     void SetConfig(const MDnsConfig &config);
73     const MDnsConfig &GetConfig() const;
74 
75     void SetHandler(const Handler &handler);
76     int32_t Register(const Result &info);
77     int32_t Discovery(const std::string &serviceType);
78     int32_t ResolveInstance(const std::string &instance);
79     int32_t Resolve(const std::string &domain);
80 
81     int32_t UnRegister(const std::string &key);
82     int32_t StopDiscovery(const std::string &key);
83     int32_t StopResolveInstance(const std::string &key);
84     int32_t StopResolve(const std::string &key);
85     int32_t Stop(const std::string &key);
86 
87     void RunTaskLater(const Task& task, bool atonce = true);
88 
89 private:
90     void Init();
91     int32_t Announce(const Result &info, bool off);
92     void ReceivePacket(int sock, const MDnsPayload &payload);
93     void OnRefresh(int sock);
94     void ProcessQuestion(int sock, const MDnsMessage &msg);
95     void ProcessQuestionRecord(const std::any &anyAddr, const DNSProto::RRType &anyAddrType,
96                                const DNSProto::Question &qu, int &phase, MDnsMessage &response);
97     void ProcessAnswer(int sock, const MDnsMessage &msg);
98     void ProcessAnswerRecord(bool v6, const DNSProto::ResourceRecord &rr, std::vector<Result> *matches,
99                              std::set<std::string> &changed);
100     void ProcessPtrRecord(bool v6, const DNSProto::ResourceRecord &rr, std::vector<Result> *matches);
101     void AppendRecord(std::vector<DNSProto::ResourceRecord> &rrlist, DNSProto::RRType type, const std::string &name,
102                       const std::any &rdata);
103     void HandleResolveInstanceLater(const Result& result);
104     bool ResolveInstanceFromCache(const std::string& name);
105     bool ResolveInstanceFromNet(const std::string &name);
106     bool ResolveFromCache(const std::string &domain);
107     bool ResolveFromNet(const std::string &domain);
108 
109     std::string ExtractInstance(const Result &info) const;
110     std::string Decorated(const std::string &name) const;
111     std::string Dotted(const std::string &name) const;
112     std::string UnDotted(const std::string &name) const;
113     std::string GetHostDomain();
114 
115     MDnsConfig config_;
116     MDnsSocketListener listener_;
117     Handler handler_;
118     std::map<std::string, Result> srvMap_;
119     std::map<std::string, int> reqMap_;
120     std::map<std::string, Result> cacheMap_;
121     std::recursive_mutex mutex_;
122     std::queue<Task> taskQueue_;
123 };
124 
125 } // namespace NetManagerStandard
126 } // namespace OHOS
127 #endif
128