Line data Source code
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 : #pragma once
18 :
19 : #include <inet/IPPacketInfo.h>
20 : #include <lib/dnssd/minimal_mdns/Server.h>
21 :
22 : namespace chip {
23 : namespace Dnssd {
24 :
25 : /// Generic receive delegate for a MDNS packet
26 : class MdnsPacketDelegate
27 : {
28 : public:
29 66 : virtual ~MdnsPacketDelegate() {}
30 : virtual void OnMdnsPacketData(const mdns::Minimal::BytesRange & data, const chip::Inet::IPPacketInfo * info) = 0;
31 : };
32 :
33 : /// A global mdns::Minimal::Server wrapper
34 : /// used to share the same server between MDNS Advertiser and resolver
35 : /// as advertiser responds to 'onquery' and resolver expects 'onresponse'
36 : class GlobalMinimalMdnsServer : public mdns::Minimal::ServerDelegate
37 : {
38 : public:
39 : static constexpr size_t kMaxEndPoints = 30;
40 :
41 : using ServerType = mdns::Minimal::Server<kMaxEndPoints>;
42 :
43 : GlobalMinimalMdnsServer();
44 :
45 : static GlobalMinimalMdnsServer & Instance();
46 56 : static mdns::Minimal::ServerBase & Server()
47 : {
48 56 : if (Instance().mReplacementServer != nullptr)
49 : {
50 0 : return *Instance().mReplacementServer;
51 : }
52 :
53 56 : return Instance().mServer;
54 : }
55 :
56 : /// Calls Server().Listen() on all available interfaces
57 : CHIP_ERROR StartServer(chip::Inet::EndPointManager<chip::Inet::UDPEndPoint> * udpEndPointManager, uint16_t port);
58 : void ShutdownServer();
59 :
60 3 : void SetQueryDelegate(MdnsPacketDelegate * delegate) { mQueryDelegate = delegate; }
61 63 : void SetResponseDelegate(MdnsPacketDelegate * delegate) { mResponseDelegate = delegate; }
62 :
63 : // ServerDelegate implementation
64 24 : void OnQuery(const mdns::Minimal::BytesRange & data, const chip::Inet::IPPacketInfo * info) override
65 : {
66 24 : if (mQueryDelegate != nullptr)
67 : {
68 24 : mQueryDelegate->OnMdnsPacketData(data, info);
69 : }
70 24 : }
71 :
72 12 : void OnResponse(const mdns::Minimal::BytesRange & data, const chip::Inet::IPPacketInfo * info) override
73 : {
74 12 : if (mResponseDelegate != nullptr)
75 : {
76 12 : mResponseDelegate->OnMdnsPacketData(data, info);
77 : }
78 12 : }
79 :
80 : void SetReplacementServer(mdns::Minimal::ServerBase * server) { mReplacementServer = server; }
81 :
82 : private:
83 : ServerType mServer;
84 : mdns::Minimal::ServerBase * mReplacementServer = nullptr;
85 : MdnsPacketDelegate * mQueryDelegate = nullptr;
86 : MdnsPacketDelegate * mResponseDelegate = nullptr;
87 : };
88 :
89 : } // namespace Dnssd
90 : } // namespace chip
|