• 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 
24 #include "imdns_service.h"
25 #include "mdns_common.h"
26 #include "mdns_packet_parser.h"
27 #include "mdns_socket_listener.h"
28 #include "common_event_subscriber.h"
29 #include "common_event_support.h"
30 #include "common_event_manager.h"
31 #include "iservice_registry.h"
32 
33 namespace OHOS {
34 namespace NetManagerStandard {
35 
36 struct MDnsConfig {
37     bool ipv6Support = false;
38     int configAllIface = true;
39     int configLo = true;
40     std::string topDomain = MDNS_TOP_DOMAIN_DEFAULT;
41     std::string hostname;
42 };
43 
44 class MDnsProtocolImpl {
45 public:
46     MDnsProtocolImpl();
47     ~MDnsProtocolImpl() = default;
48 
49     struct Result;
50     using TxtRecord = std::map<std::string, std::vector<uint8_t>>;
51     using Task = std::function<bool()>;
52 
53     enum class State {
54         DEAD,
55         ADD,
56         LIVE,
57         REFRESH,
58         REMOVE,
59     };
60 
61     struct Result {
62         std::string serviceName;
63         std::string serviceType;
64         std::string domain;
65         int port = -1;
66         bool ipv6 = false;
67         std::string addr;
68         TxtRecordEncoded txt;
69         State state = State::DEAD;
70         uint32_t ttl = 0;
71         int64_t refrehTime = -1;
72         int32_t err = NETMANAGER_EXT_SUCCESS;
73     };
74 
75     static void SetScreenState(bool isOn);
76     void SetConfig(const MDnsConfig &config);
77     bool Browse();
78     const MDnsConfig &GetConfig() const;
79 
80     int32_t Register(const Result &info);
81     int32_t Discovery(const std::string &serviceType, const sptr<IDiscoveryCallback> &cb);
82     int32_t ResolveInstance(const std::string &instance, const sptr<IResolveCallback> &cb);
83 
84     int32_t UnRegister(const std::string &key);
85     int32_t StopCbMap(const std::string &key);
86 
87     void AddTask(const Task &task, bool atonce = true);
88     void AddEvent(const std::string &key, const Task &task);
89 
90     void Init();
91     int32_t Announce(const Result &info, bool off);
92     void ReceivePacket(int sock, const MDnsPayload &payload);
93     void RunTaskQueue(std::list<Task> &queue);
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::set<std::string> &changed);
99     void UpdatePtr(bool v6, const DNSProto::ResourceRecord &rr, std::set<std::string> &changed);
100     void UpdateSrv(bool v6, const DNSProto::ResourceRecord &rr, std::set<std::string> &changed);
101     void UpdateTxt(bool v6, const DNSProto::ResourceRecord &rr, std::set<std::string> &changed);
102     void UpdateAddr(bool v6, const DNSProto::ResourceRecord &rr, std::set<std::string> &changed);
103     void AppendRecord(std::vector<DNSProto::ResourceRecord> &rrlist, DNSProto::RRType type, const std::string &name,
104                       const std::any &rdata);
105 
106     bool ResolveInstanceFromCache(const std::string &name, const sptr<IResolveCallback> &cb);
107     bool ResolveInstanceFromNet(const std::string &name, const sptr<IResolveCallback> &cb);
108     bool ResolveFromCache(const std::string &domain, const sptr<IResolveCallback> &cb);
109     bool ResolveFromNet(const std::string &domain, const sptr<IResolveCallback> &cb);
110     bool DiscoveryFromCache(const std::string &serviceType, const sptr<IDiscoveryCallback> &cb);
111     bool DiscoveryFromNet(const std::string &serviceType, const sptr<IDiscoveryCallback> &cb);
112     bool IsCacheAvailable(const std::string &key);
113     bool IsDomainCacheAvailable(const std::string &key);
114     bool IsInstanceCacheAvailable(const std::string &key);
115     bool IsBrowserAvailable(const std::string &key);
116     void KillCache(const std::string &key);
117     MDnsServiceInfo ConvertResultToInfo(const Result &result);
118 
119     std::string Decorated(const std::string &name) const;
120     std::string Dotted(const std::string &name) const;
121     std::string UnDotted(const std::string &name) const;
122     std::string GetHostDomain();
123 
124 private:
125     class MdnsSubscriber : public OHOS::EventFwk::CommonEventSubscriber {
126     public:
MdnsSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)127         explicit MdnsSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo)
128             : OHOS::EventFwk::CommonEventSubscriber(subscribeInfo) {};
129         virtual void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override;
130     };
131     void SubscribeCes();
132     void handleOfflineService(const std::string &key, std::vector<Result> &res);
133     void KillBrowseCache(const std::string &key, std::vector<Result>::iterator &it);
134 
135     int32_t ConnectControl(int32_t sockfd, sockaddr* serverAddr);
136     bool IsConnectivity(const std::string &ip, int32_t port);
137 
138 public:
139     std::map<std::string, Result> srvMap_;
140 
141 private:
142     int64_t lastRunTime = {-1};
143     MDnsConfig config_;
144     MDnsSocketListener listener_;
145     std::map<std::string, std::vector<Result>> browserMap_;
146     std::map<std::string, Result> cacheMap_;
147     std::recursive_mutex mutex_;
148     std::list<Task> taskQueue_;
149     std::map<std::string, std::list<Task>> taskOnChange_;
150     std::map<std::string, sptr<IDiscoveryCallback>> nameCbMap_;
151     std::shared_ptr<MdnsSubscriber> subscriber_ = nullptr;
152 };
153 } // namespace NetManagerStandard
154 } // namespace OHOS
155 #endif
156