• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *    Copyright (c) 2021, The OpenThread Authors.
3  *    All rights reserved.
4  *
5  *    Redistribution and use in source and binary forms, with or without
6  *    modification, are permitted provided that the following conditions are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *    3. Neither the name of the copyright holder nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *    POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  *   This file includes definition for DNS-SD Discovery Proxy.
32  */
33 
34 #ifndef OTBR_AGENT_DISCOVERY_PROXY_HPP_
35 #define OTBR_AGENT_DISCOVERY_PROXY_HPP_
36 
37 #include "openthread-br/config.h"
38 
39 #if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
40 
41 #include <set>
42 #include <utility>
43 
44 #include <stdint.h>
45 
46 #include <openthread/dnssd_server.h>
47 #include <openthread/instance.h>
48 
49 #include "common/dns_utils.hpp"
50 #include "host/rcp_host.hpp"
51 #include "mdns/mdns.hpp"
52 
53 namespace otbr {
54 namespace Dnssd {
55 
56 /**
57  * This class implements the DNS-SD Discovery Proxy.
58  */
59 class DiscoveryProxy : public Mdns::StateObserver, private NonCopyable
60 {
61 public:
62     /**
63      * This constructor initializes the Discovery Proxy instance.
64      *
65      * @param[in] aHost       A reference to the OpenThread Controller instance.
66      * @param[in] aPublisher  A reference to the mDNS Publisher.
67      */
68     explicit DiscoveryProxy(Host::RcpHost &aHost, Mdns::Publisher &aPublisher);
69 
70     /**
71      * This method enables/disables the Discovery Proxy.
72      *
73      * @param[in] aIsEnabled  Whether to enable the Discovery Proxy.
74      */
75     void SetEnabled(bool aIsEnabled);
76 
77     /**
78      * This method handles mDNS publisher's state changes.
79      *
80      * @param[in] aState  The state of mDNS publisher.
81      */
HandleMdnsState(Mdns::Publisher::State aState)82     void HandleMdnsState(Mdns::Publisher::State aState)
83     {
84         VerifyOrExit(IsEnabled());
85         OTBR_UNUSED_VARIABLE(aState);
86     exit:
87         return;
88     }
89 
90 private:
91     using AddressList = Mdns::Publisher::AddressList;
92 
93     enum : uint32_t
94     {
95         kServiceTtlCapLimit = 10, // TTL cap limit for Discovery Proxy (in seconds).
96     };
97 
98     static void        OnDiscoveryProxySubscribe(void *aContext, const char *aFullName);
99     void               OnDiscoveryProxySubscribe(const char *aSubscription);
100     static void        OnDiscoveryProxyUnsubscribe(void *aContext, const char *aFullName);
101     void               OnDiscoveryProxyUnsubscribe(const char *aSubscription);
102     int                GetServiceSubscriptionCount(const DnsNameInfo &aNameInfo) const;
103     static std::string TranslateDomain(const std::string &aName, const std::string &aTargetDomain);
104     void               OnServiceDiscovered(const std::string                             &aSubscription,
105                                            const Mdns::Publisher::DiscoveredInstanceInfo &aInstanceInfo);
106     void OnHostDiscovered(const std::string &aHostName, const Mdns::Publisher::DiscoveredHostInfo &aHostInfo);
107     static uint32_t CapTtl(uint32_t aTtl);
108 
109     static void FilterLinkLocalAddresses(const AddressList &aAddrList, AddressList &aFilteredList);
110 
111     void Start(void);
112     void Stop(void);
IsEnabled(void) const113     bool IsEnabled(void) const { return mIsEnabled; }
114 
115     Host::RcpHost   &mHost;
116     Mdns::Publisher &mMdnsPublisher;
117     bool             mIsEnabled;
118     uint64_t         mSubscriberId = 0;
119 };
120 
121 } // namespace Dnssd
122 } // namespace otbr
123 
124 #endif // OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
125 
126 #endif // OTBR_AGENT_DISCOVERY_PROXY_HPP_
127