xref: /aosp_15_r20/external/ot-br-posix/src/sdp_proxy/discovery_proxy.hpp (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
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/rcp_host.hpp"
52 
53 namespace otbr {
54 namespace Dnssd {
55 
56 /**
57  * This class implements the DNS-SD Discovery Proxy.
58  */
59 class DiscoveryProxy : 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(Ncp::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     enum : uint32_t
92     {
93         kServiceTtlCapLimit = 10, // TTL cap limit for Discovery Proxy (in seconds).
94     };
95 
96     static void        OnDiscoveryProxySubscribe(void *aContext, const char *aFullName);
97     void               OnDiscoveryProxySubscribe(const char *aSubscription);
98     static void        OnDiscoveryProxyUnsubscribe(void *aContext, const char *aFullName);
99     void               OnDiscoveryProxyUnsubscribe(const char *aSubscription);
100     int                GetServiceSubscriptionCount(const DnsNameInfo &aNameInfo) const;
101     static std::string TranslateDomain(const std::string &aName, const std::string &aTargetDomain);
102     void               OnServiceDiscovered(const std::string                             &aSubscription,
103                                            const Mdns::Publisher::DiscoveredInstanceInfo &aInstanceInfo);
104     void OnHostDiscovered(const std::string &aHostName, const Mdns::Publisher::DiscoveredHostInfo &aHostInfo);
105     static uint32_t CapTtl(uint32_t aTtl);
106 
107     void Start(void);
108     void Stop(void);
IsEnabled(void) const109     bool IsEnabled(void) const { return mIsEnabled; }
110 
111     Ncp::RcpHost    &mHost;
112     Mdns::Publisher &mMdnsPublisher;
113     bool             mIsEnabled;
114     uint64_t         mSubscriberId = 0;
115 };
116 
117 } // namespace Dnssd
118 } // namespace otbr
119 
120 #endif // OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
121 
122 #endif // OTBR_AGENT_DISCOVERY_PROXY_HPP_
123