• 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 "mdns/mdns.hpp"
51 #include "ncp/ncp_openthread.hpp"
52 
53 namespace otbr {
54 namespace Dnssd {
55 
56 /**
57  * This class implements the DNS-SD Discovery Proxy.
58  *
59  */
60 class DiscoveryProxy : private NonCopyable
61 {
62 public:
63     /**
64      * This constructor initializes the Discovery Proxy instance.
65      *
66      * @param[in] aNcp        A reference to the OpenThread Controller instance.
67      * @param[in] aPublisher  A reference to the mDNS Publisher.
68      *
69      */
70     explicit DiscoveryProxy(Ncp::ControllerOpenThread &aNcp, Mdns::Publisher &aPublisher);
71 
72     /**
73      * This method enables/disables the Discovery Proxy.
74      *
75      * @param[in] aIsEnabled  Whether to enable the Discovery Proxy.
76      *
77      */
78     void SetEnabled(bool aIsEnabled);
79 
80     /**
81      * This method handles mDNS publisher's state changes.
82      *
83      * @param[in] aState  The state of mDNS publisher.
84      *
85      */
HandleMdnsState(Mdns::Publisher::State aState)86     void HandleMdnsState(Mdns::Publisher::State aState)
87     {
88         VerifyOrExit(IsEnabled());
89         OTBR_UNUSED_VARIABLE(aState);
90     exit:
91         return;
92     }
93 
94 private:
95     enum : uint32_t
96     {
97         kServiceTtlCapLimit = 10, // TTL cap limit for Discovery Proxy (in seconds).
98     };
99 
100     static void        OnDiscoveryProxySubscribe(void *aContext, const char *aFullName);
101     void               OnDiscoveryProxySubscribe(const char *aSubscription);
102     static void        OnDiscoveryProxyUnsubscribe(void *aContext, const char *aFullName);
103     void               OnDiscoveryProxyUnsubscribe(const char *aSubscription);
104     int                GetServiceSubscriptionCount(const DnsNameInfo &aNameInfo) const;
105     static std::string TranslateDomain(const std::string &aName, const std::string &aTargetDomain);
106     void               OnServiceDiscovered(const std::string                             &aSubscription,
107                                            const Mdns::Publisher::DiscoveredInstanceInfo &aInstanceInfo);
108     void OnHostDiscovered(const std::string &aHostName, const Mdns::Publisher::DiscoveredHostInfo &aHostInfo);
109     static uint32_t CapTtl(uint32_t aTtl);
110 
111     void Start(void);
112     void Stop(void);
IsEnabled(void) const113     bool IsEnabled(void) const { return mIsEnabled; }
114 
115     Ncp::ControllerOpenThread &mNcp;
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