Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / Linux / MdnsImpl.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
5  *    Licensed under the Apache License, Version 2.0 (the "License");
6  *    you may not use this file except in compliance with the License.
7  *    You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *    Unless required by applicable law or agreed to in writing, software
12  *    distributed under the License is distributed on an "AS IS" BASIS,
13  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *    See the License for the specific language governing permissions and
15  *    limitations under the License.
16  */
17
18 #pragma once
19
20 #include <sys/select.h>
21 #include <unistd.h>
22
23 #include <chrono>
24 #include <map>
25 #include <memory>
26 #include <set>
27 #include <vector>
28
29 #include <avahi-client/client.h>
30 #include <avahi-client/lookup.h>
31 #include <avahi-client/publish.h>
32 #include <avahi-common/domain.h>
33 #include <avahi-common/error.h>
34 #include <avahi-common/watch.h>
35
36 #include "lib/mdns/platform/Mdns.h"
37
38 struct AvahiWatch
39 {
40     int mFd;                      ///< The file descriptor to watch.
41     AvahiWatchEvent mWatchEvents; ///< The interested events.
42     int mHappenedEvents;          ///< The events happened.
43     AvahiWatchCallback mCallback; ///< The function to be called when interested events happened on mFd.
44     void * mContext;              ///< A pointer to application-specific context.
45     void * mPoller;               ///< The poller created this watch.
46 };
47
48 struct AvahiTimeout
49 {
50     std::chrono::steady_clock::time_point mAbsTimeout; ///< Absolute time when this timer timeout.
51     AvahiTimeoutCallback mCallback;                    ///< The function to be called when timeout.
52     bool mEnabled;                                     ///< Whether the timeout is enabled.
53     void * mContext;                                   ///< The pointer to application-specific context.
54     void * mPoller;                                    ///< The poller created this timer.
55 };
56
57 namespace chip {
58 namespace Mdns {
59
60 class Poller
61 {
62 public:
63     Poller(void);
64
65     void UpdateFdSet(fd_set & readFdSet, fd_set & writeFdSet, fd_set & errorFdSet, int & maxFd, timeval & timeout);
66
67     void Process(const fd_set & readFdSet, const fd_set & writeFdSet, const fd_set & errorFdSet);
68
69     const AvahiPoll * GetAvahiPoll(void) const { return &mAvahiPoller; }
70
71 private:
72     static AvahiWatch * WatchNew(const struct AvahiPoll * poller, int fd, AvahiWatchEvent event, AvahiWatchCallback callback,
73                                  void * context);
74     AvahiWatch * WatchNew(int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void * context);
75
76     static void WatchUpdate(AvahiWatch * watch, AvahiWatchEvent event);
77
78     static AvahiWatchEvent WatchGetEvents(AvahiWatch * watch);
79
80     static void WatchFree(AvahiWatch * watch);
81     void WatchFree(AvahiWatch & watch);
82
83     static AvahiTimeout * TimeoutNew(const AvahiPoll * poller, const struct timeval * timeout, AvahiTimeoutCallback callback,
84                                      void * context);
85     AvahiTimeout * TimeoutNew(const struct timeval * timeout, AvahiTimeoutCallback callback, void * context);
86
87     static void TimeoutUpdate(AvahiTimeout * timer, const struct timeval * timeout);
88
89     static void TimeoutFree(AvahiTimeout * timer);
90     void TimeoutFree(AvahiTimeout & timer);
91
92     std::vector<std::unique_ptr<AvahiWatch>> mWatches;
93     std::vector<std::unique_ptr<AvahiTimeout>> mTimers;
94     AvahiPoll mAvahiPoller;
95 };
96
97 class MdnsAvahi
98 {
99 public:
100     MdnsAvahi(const MdnsAvahi &) = delete;
101     MdnsAvahi & operator=(const MdnsAvahi &) = delete;
102
103     CHIP_ERROR Init(MdnsAsyncReturnCallback initCallback, MdnsAsyncReturnCallback errorCallback, void * context);
104     CHIP_ERROR SetHostname(const char * hostname);
105     CHIP_ERROR PublishService(const MdnsService & service);
106     CHIP_ERROR StopPublish();
107     CHIP_ERROR Browse(const char * type, MdnsServiceProtocol protocol, chip::Inet::IPAddressType addressType,
108                       chip::Inet::InterfaceId interface, MdnsBrowseCallback callback, void * context);
109     CHIP_ERROR Resolve(const char * name, const char * type, MdnsServiceProtocol protocol, chip::Inet::IPAddressType addressType,
110                        chip::Inet::InterfaceId interface, MdnsResolveCallback callback, void * context);
111
112     Poller & GetPoller() { return mPoller; }
113
114     static MdnsAvahi & GetInstance() { return sInstance; }
115
116     ~MdnsAvahi();
117
118 private:
119     struct BrowseContext
120     {
121         MdnsAvahi * mInstance;
122         MdnsBrowseCallback mCallback;
123         void * mContext;
124         std::vector<MdnsService> mServices;
125     };
126
127     struct ResolveContext
128     {
129         MdnsAvahi * mInstance;
130         MdnsResolveCallback mCallback;
131         void * mContext;
132     };
133
134     MdnsAvahi() : mClient(nullptr), mGroup(nullptr) {}
135     static MdnsAvahi sInstance;
136
137     static void HandleClientState(AvahiClient * client, AvahiClientState state, void * context);
138     void HandleClientState(AvahiClient * client, AvahiClientState state);
139
140     static void HandleGroupState(AvahiEntryGroup * group, AvahiEntryGroupState state, void * context);
141     void HandleGroupState(AvahiEntryGroup * group, AvahiEntryGroupState state);
142
143     static void HandleBrowse(AvahiServiceBrowser * broswer, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event,
144                              const char * name, const char * type, const char * domain, AvahiLookupResultFlags flags,
145                              void * userdata);
146     static void HandleResolve(AvahiServiceResolver * resolver, AvahiIfIndex interface, AvahiProtocol protocol,
147                               AvahiResolverEvent event, const char * name, const char * type, const char * domain,
148                               const char * host_name, const AvahiAddress * address, uint16_t port, AvahiStringList * txt,
149                               AvahiLookupResultFlags flags, void * userdata);
150
151     MdnsAsyncReturnCallback mInitCallback;
152     MdnsAsyncReturnCallback mErrorCallback;
153     void * mAsyncReturnContext;
154
155     std::set<std::string> mPublishedServices;
156     AvahiClient * mClient;
157     AvahiEntryGroup * mGroup;
158     Poller mPoller;
159 };
160
161 } // namespace Mdns
162 } // namespace chip