Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / Darwin / MdnsImpl.h
1 /*
2  *
3  *    Copyright (c) 2021 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 <dns_sd.h>
21 #include <lib/mdns/platform/Mdns.h>
22 #include <string>
23 #include <vector>
24
25 namespace chip {
26 namespace Mdns {
27
28 enum class ContextType
29 {
30     Register,
31     Browse,
32     Resolve,
33     GetAddrInfo,
34 };
35
36 struct GenericContext
37 {
38     ContextType type;
39     void * context;
40     DNSServiceRef serviceRef;
41 };
42
43 struct RegisterContext : public GenericContext
44 {
45     RegisterContext(void * cbContext)
46     {
47         type    = ContextType::Register;
48         context = cbContext;
49     }
50 };
51
52 struct BrowseContext : public GenericContext
53 {
54     MdnsBrowseCallback callback;
55     std::vector<MdnsService> services;
56     MdnsServiceProtocol protocol;
57
58     BrowseContext(void * cbContext, MdnsBrowseCallback cb, MdnsServiceProtocol cbContextProtocol)
59     {
60         type     = ContextType::Browse;
61         context  = cbContext;
62         callback = cb;
63         protocol = cbContextProtocol;
64     }
65 };
66
67 struct ResolveContext : public GenericContext
68 {
69     MdnsResolveCallback callback;
70
71     ResolveContext(void * cbContext, MdnsResolveCallback cb)
72     {
73         type     = ContextType::Resolve;
74         context  = cbContext;
75         callback = cb;
76     }
77 };
78
79 struct GetAddrInfoContext : public GenericContext
80 {
81     MdnsResolveCallback callback;
82     std::vector<TextEntry> textEntries;
83     uint16_t port;
84
85     GetAddrInfoContext(void * cbContext, MdnsResolveCallback cb, uint16_t cbContextPort)
86     {
87         type     = ContextType::GetAddrInfo;
88         context  = cbContext;
89         callback = cb;
90         port     = cbContextPort;
91     }
92 };
93
94 class MdnsContexts
95 {
96 public:
97     MdnsContexts(const MdnsContexts &) = delete;
98     MdnsContexts & operator=(const MdnsContexts &) = delete;
99     ~MdnsContexts();
100     static MdnsContexts & GetInstance() { return sInstance; }
101
102     void PrepareSelect(fd_set & readFdSet, fd_set & writeFdSet, fd_set & errorFdSet, int & maxFd, timeval & timeout);
103     void HandleSelectResult(fd_set & readFdSet, fd_set & writeFdSet, fd_set & errorFdSet);
104
105     CHIP_ERROR Add(GenericContext * context, DNSServiceRef sdRef);
106     CHIP_ERROR Remove(GenericContext * context);
107     CHIP_ERROR Removes(ContextType type);
108     CHIP_ERROR Get(ContextType type, GenericContext * context);
109
110     void SetHostname(const char * name) { mHostname = name; }
111     const char * GetHostname() { return mHostname.c_str(); }
112
113 private:
114     MdnsContexts(){};
115     static MdnsContexts sInstance;
116     std::string mHostname;
117
118     void Delete(GenericContext * context);
119     std::vector<GenericContext *> mContexts;
120 };
121
122 } // namespace Mdns
123 } // namespace chip