• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *    Copyright (c) 2020, 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 definitions for Advertising Proxy.
32  */
33 
34 #ifndef OTBR_SRP_ADVERTISING_PROXY_HPP_
35 #define OTBR_SRP_ADVERTISING_PROXY_HPP_
36 
37 #include "openthread-br/config.h"
38 
39 #if OTBR_ENABLE_SRP_ADVERTISING_PROXY
40 
41 #include <stdint.h>
42 
43 #include <openthread/instance.h>
44 #include <openthread/srp_server.h>
45 
46 #include "common/code_utils.hpp"
47 #include "mdns/mdns.hpp"
48 #include "ncp/ncp_openthread.hpp"
49 
50 namespace otbr {
51 
52 /**
53  * This class implements the Advertising Proxy.
54  *
55  */
56 class AdvertisingProxy : private NonCopyable
57 {
58 public:
59     /**
60      * This constructor initializes the Advertising Proxy object.
61      *
62      * @param[in] aNcp        A reference to the NCP controller.
63      * @param[in] aPublisher  A reference to the mDNS publisher.
64      *
65      */
66     explicit AdvertisingProxy(Ncp::ControllerOpenThread &aNcp, Mdns::Publisher &aPublisher);
67 
68     /**
69      * This method enables/disables the Advertising Proxy.
70      *
71      * @param[in] aIsEnabled  Whether to enable the Advertising Proxy.
72      *
73      */
74     void SetEnabled(bool aIsEnabled);
75 
76     /**
77      * This method publishes all registered hosts and services.
78      *
79      */
80     void PublishAllHostsAndServices(void);
81 
82     /**
83      * This method handles mDNS publisher's state changes.
84      *
85      * @param[in] aState  The state of mDNS publisher.
86      *
87      */
88     void HandleMdnsState(Mdns::Publisher::State aState);
89 
90 private:
91     struct OutstandingUpdate
92     {
93         otSrpServerServiceUpdateId mId;                // The ID of the SRP service update transaction.
94         std::string                mHostName;          // The host name.
95         uint32_t                   mCallbackCount = 0; // The number of callbacks which we are waiting for.
96     };
97 
98     static void AdvertisingHandler(otSrpServerServiceUpdateId aId,
99                                    const otSrpServerHost     *aHost,
100                                    uint32_t                   aTimeout,
101                                    void                      *aContext);
102     void        AdvertisingHandler(otSrpServerServiceUpdateId aId, const otSrpServerHost *aHost, uint32_t aTimeout);
103 
104     static Mdns::Publisher::TxtData     MakeTxtData(const otSrpServerService *aSrpService);
105     static Mdns::Publisher::SubTypeList MakeSubTypeList(const otSrpServerService *aSrpService);
106     void                                OnMdnsPublishResult(otSrpServerServiceUpdateId aUpdateId, otbrError aError);
107 
108     std::vector<Ip6Address> GetEligibleAddresses(const otIp6Address *aHostAddresses, uint8_t aHostAddressNum);
109 
110     void Start(void);
111     void Stop(void);
IsEnabled(void) const112     bool IsEnabled(void) const { return mIsEnabled; }
113 
114     /**
115      * This method publishes a specified host and its services.
116      *
117      * It also makes a OutstandingUpdate object when needed.
118      *
119      * @param[in]  aHost         A pointer to the host.
120      * @param[in]  aUpdate       A pointer to the output OutstandingUpdate object. When it's not null, the method will
121      *                           fill its fields, otherwise it's ignored.
122      *
123      * @retval  OTBR_ERROR_NONE  Successfully published the host and its services.
124      * @retval  ...              Failed to publish the host and/or its services.
125      *
126      */
127     otbrError PublishHostAndItsServices(const otSrpServerHost *aHost, OutstandingUpdate *aUpdate);
128 
GetInstance(void)129     otInstance *GetInstance(void) { return mNcp.GetInstance(); }
130 
131     // A reference to the NCP controller, has no ownership.
132     Ncp::ControllerOpenThread &mNcp;
133 
134     // A reference to the mDNS publisher, has no ownership.
135     Mdns::Publisher &mPublisher;
136 
137     bool mIsEnabled;
138 
139     // A vector that tracks outstanding updates.
140     std::vector<OutstandingUpdate> mOutstandingUpdates;
141 };
142 
143 } // namespace otbr
144 
145 #endif // OTBR_ENABLE_SRP_ADVERTISING_PROXY
146 
147 #endif // OTBR_SRP_ADVERTISING_PROXY_HPP_
148