• 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 <string>
22 #include <queue>
23 
24 #include "mdns_common.h"
25 #include "mdns_packet_parser.h"
26 #include "mdns_socket_listener.h"
27 
28 namespace OHOS {
29 namespace NetManagerStandard {
30 
31 struct MDnsConfig {
32     bool ipv6Support = false;
33     int configAllIface = true;
34     int configLo = true;
35     std::string topDomain = MDNS_TOP_DOMAIN_DEFAULT;
36     std::string hostname;
37 };
38 
39 class MDnsProtocolImpl {
40 public:
41     MDnsProtocolImpl();
42     ~MDnsProtocolImpl() = default;
43 
44     enum ResultType {
45         UNKNOWN,
46         SERVICE_STARTED,
47         SERVICE_STOPED,
48         SERVICE_FOUND,
49         SERVICE_LOST,
50         INSTANCE_RESOLVED,
51         DOMAIN_RESOLVED
52     };
53 
54     using TxtRecord = std::map<std::string, std::vector<uint8_t>>;
55     using Task = std::function<void()>;
56 
57     struct Result {
58         ResultType type;
59         std::string serviceName;
60         std::string serviceType;
61         std::string domain;
62         int port = -1;
63         bool ipv6 = false;
64         std::string addr;
65         TxtRecordEncoded txt;
66         std::string iface;
67     };
68 
69     using Handler = std::function<void(const Result &, int32_t)>;
70 
71     void SetConfig(const MDnsConfig &config);
72     const MDnsConfig &GetConfig() const;
73 
74     void SetHandler(const Handler &handler);
75     int32_t Register(const Result &info);
76     int32_t Discovery(const std::string &serviceType);
77     int32_t ResolveInstance(const std::string &instance);
78     int32_t Resolve(const std::string &domain);
79 
80     int32_t UnRegister(const std::string &key);
81     int32_t StopDiscovery(const std::string &key);
82     int32_t StopResolveInstance(const std::string &key);
83     int32_t StopResolve(const std::string &key);
84     int32_t Stop(const std::string &key);
85 
86     void RunTaskLater(const Task& task);
87 
88 private:
89     void Init();
90     int32_t Announce(const Result &info, bool off);
91     void ReceivePacket(int sock, const MDnsPayload &payload);
92     void OnRefresh(int sock);
93     void ProcessQuestion(int sock, const MDnsMessage &msg);
94     void ProcessQuestionRecord(const std::any &anyAddr, const DNSProto::RRType &anyAddrType,
95                                const DNSProto::Question &qu, int &phase, MDnsMessage &response);
96     void ProcessAnswer(int sock, const MDnsMessage &msg);
97     void ProcessAnswerRecord(bool v6, const DNSProto::ResourceRecord &rr, std::vector<Result> &matches,
98                              std::map<std::string, Result> &results, std::map<std::string, std::string> &needMore);
99     void AppendRecord(std::vector<DNSProto::ResourceRecord> &rrlist, DNSProto::RRType type, const std::string &name,
100                       const std::any &rdata);
101 
102     std::string ExtractInstance(const Result &info) const;
103     std::string Decorated(const std::string &name) const;
104     std::string Dotted(const std::string &name) const;
105     std::string UnDotted(const std::string &name) const;
106     std::string GetHostDomain();
107 
108     MDnsConfig config_;
109     MDnsSocketListener listener_;
110     Handler handler_;
111     std::map<std::string, Result> srvMap_;
112     std::map<std::string, int> reqMap_;
113     std::atomic_int reqCount_ = 0;
114     std::mutex mutex_;
115     std::queue<Task> taskQueue_;
116 };
117 
118 } // namespace NetManagerStandard
119 } // namespace OHOS
120 #endif
121