3 * Copyright (c) 2021 Project CHIP Authors
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <lib/mdns/platform/Mdns.h>
28 enum class ContextType
40 DNSServiceRef serviceRef;
43 struct RegisterContext : public GenericContext
45 RegisterContext(void * cbContext)
47 type = ContextType::Register;
52 struct BrowseContext : public GenericContext
54 MdnsBrowseCallback callback;
55 std::vector<MdnsService> services;
56 MdnsServiceProtocol protocol;
58 BrowseContext(void * cbContext, MdnsBrowseCallback cb, MdnsServiceProtocol cbContextProtocol)
60 type = ContextType::Browse;
63 protocol = cbContextProtocol;
67 struct ResolveContext : public GenericContext
69 MdnsResolveCallback callback;
71 ResolveContext(void * cbContext, MdnsResolveCallback cb)
73 type = ContextType::Resolve;
79 struct GetAddrInfoContext : public GenericContext
81 MdnsResolveCallback callback;
82 std::vector<TextEntry> textEntries;
85 GetAddrInfoContext(void * cbContext, MdnsResolveCallback cb, uint16_t cbContextPort)
87 type = ContextType::GetAddrInfo;
97 MdnsContexts(const MdnsContexts &) = delete;
98 MdnsContexts & operator=(const MdnsContexts &) = delete;
100 static MdnsContexts & GetInstance() { return sInstance; }
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);
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);
110 void SetHostname(const char * name) { mHostname = name; }
111 const char * GetHostname() { return mHostname.c_str(); }
115 static MdnsContexts sInstance;
116 std::string mHostname;
118 void Delete(GenericContext * context);
119 std::vector<GenericContext *> mContexts;