xref: /aosp_15_r20/external/openthread/src/cli/cli_mdns.cpp (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker /*
2*cfb92d14SAndroid Build Coastguard Worker  *  Copyright (c) 2024, The OpenThread Authors.
3*cfb92d14SAndroid Build Coastguard Worker  *  All rights reserved.
4*cfb92d14SAndroid Build Coastguard Worker  *
5*cfb92d14SAndroid Build Coastguard Worker  *  Redistribution and use in source and binary forms, with or without
6*cfb92d14SAndroid Build Coastguard Worker  *  modification, are permitted provided that the following conditions are met:
7*cfb92d14SAndroid Build Coastguard Worker  *  1. Redistributions of source code must retain the above copyright
8*cfb92d14SAndroid Build Coastguard Worker  *     notice, this list of conditions and the following disclaimer.
9*cfb92d14SAndroid Build Coastguard Worker  *  2. Redistributions in binary form must reproduce the above copyright
10*cfb92d14SAndroid Build Coastguard Worker  *     notice, this list of conditions and the following disclaimer in the
11*cfb92d14SAndroid Build Coastguard Worker  *     documentation and/or other materials provided with the distribution.
12*cfb92d14SAndroid Build Coastguard Worker  *  3. Neither the name of the copyright holder nor the
13*cfb92d14SAndroid Build Coastguard Worker  *     names of its contributors may be used to endorse or promote products
14*cfb92d14SAndroid Build Coastguard Worker  *     derived from this software without specific prior written permission.
15*cfb92d14SAndroid Build Coastguard Worker  *
16*cfb92d14SAndroid Build Coastguard Worker  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*cfb92d14SAndroid Build Coastguard Worker  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*cfb92d14SAndroid Build Coastguard Worker  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*cfb92d14SAndroid Build Coastguard Worker  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*cfb92d14SAndroid Build Coastguard Worker  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*cfb92d14SAndroid Build Coastguard Worker  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*cfb92d14SAndroid Build Coastguard Worker  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*cfb92d14SAndroid Build Coastguard Worker  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*cfb92d14SAndroid Build Coastguard Worker  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*cfb92d14SAndroid Build Coastguard Worker  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*cfb92d14SAndroid Build Coastguard Worker  *  POSSIBILITY OF SUCH DAMAGE.
27*cfb92d14SAndroid Build Coastguard Worker  */
28*cfb92d14SAndroid Build Coastguard Worker 
29*cfb92d14SAndroid Build Coastguard Worker /**
30*cfb92d14SAndroid Build Coastguard Worker  * @file
31*cfb92d14SAndroid Build Coastguard Worker  *   This file implements CLI for mDNS.
32*cfb92d14SAndroid Build Coastguard Worker  */
33*cfb92d14SAndroid Build Coastguard Worker 
34*cfb92d14SAndroid Build Coastguard Worker #include <string.h>
35*cfb92d14SAndroid Build Coastguard Worker 
36*cfb92d14SAndroid Build Coastguard Worker #include "cli_mdns.hpp"
37*cfb92d14SAndroid Build Coastguard Worker 
38*cfb92d14SAndroid Build Coastguard Worker #include <openthread/nat64.h>
39*cfb92d14SAndroid Build Coastguard Worker #include "cli/cli.hpp"
40*cfb92d14SAndroid Build Coastguard Worker 
41*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_MULTICAST_DNS_ENABLE && OPENTHREAD_CONFIG_MULTICAST_DNS_PUBLIC_API_ENABLE
42*cfb92d14SAndroid Build Coastguard Worker 
43*cfb92d14SAndroid Build Coastguard Worker namespace ot {
44*cfb92d14SAndroid Build Coastguard Worker namespace Cli {
45*cfb92d14SAndroid Build Coastguard Worker 
Process(Arg aArgs[])46*cfb92d14SAndroid Build Coastguard Worker template <> otError Mdns::Process<Cmd("enable")>(Arg aArgs[])
47*cfb92d14SAndroid Build Coastguard Worker {
48*cfb92d14SAndroid Build Coastguard Worker     otError  error;
49*cfb92d14SAndroid Build Coastguard Worker     uint32_t infraIfIndex;
50*cfb92d14SAndroid Build Coastguard Worker 
51*cfb92d14SAndroid Build Coastguard Worker     SuccessOrExit(error = aArgs[0].ParseAsUint32(infraIfIndex));
52*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS);
53*cfb92d14SAndroid Build Coastguard Worker 
54*cfb92d14SAndroid Build Coastguard Worker     SuccessOrExit(error = otMdnsSetEnabled(GetInstancePtr(), true, infraIfIndex));
55*cfb92d14SAndroid Build Coastguard Worker 
56*cfb92d14SAndroid Build Coastguard Worker     mInfraIfIndex = infraIfIndex;
57*cfb92d14SAndroid Build Coastguard Worker 
58*cfb92d14SAndroid Build Coastguard Worker exit:
59*cfb92d14SAndroid Build Coastguard Worker     return error;
60*cfb92d14SAndroid Build Coastguard Worker }
61*cfb92d14SAndroid Build Coastguard Worker 
Process(Arg aArgs[])62*cfb92d14SAndroid Build Coastguard Worker template <> otError Mdns::Process<Cmd("disable")>(Arg aArgs[])
63*cfb92d14SAndroid Build Coastguard Worker {
64*cfb92d14SAndroid Build Coastguard Worker     otError error = OT_ERROR_NONE;
65*cfb92d14SAndroid Build Coastguard Worker 
66*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS);
67*cfb92d14SAndroid Build Coastguard Worker     error = otMdnsSetEnabled(GetInstancePtr(), false, /* aInfraIfIndex */ 0);
68*cfb92d14SAndroid Build Coastguard Worker 
69*cfb92d14SAndroid Build Coastguard Worker exit:
70*cfb92d14SAndroid Build Coastguard Worker     return error;
71*cfb92d14SAndroid Build Coastguard Worker }
72*cfb92d14SAndroid Build Coastguard Worker 
Process(Arg aArgs[])73*cfb92d14SAndroid Build Coastguard Worker template <> otError Mdns::Process<Cmd("state")>(Arg aArgs[])
74*cfb92d14SAndroid Build Coastguard Worker {
75*cfb92d14SAndroid Build Coastguard Worker     otError error = OT_ERROR_NONE;
76*cfb92d14SAndroid Build Coastguard Worker 
77*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS);
78*cfb92d14SAndroid Build Coastguard Worker     OutputEnabledDisabledStatus(otMdnsIsEnabled(GetInstancePtr()));
79*cfb92d14SAndroid Build Coastguard Worker 
80*cfb92d14SAndroid Build Coastguard Worker exit:
81*cfb92d14SAndroid Build Coastguard Worker     return error;
82*cfb92d14SAndroid Build Coastguard Worker }
83*cfb92d14SAndroid Build Coastguard Worker 
Process(Arg aArgs[])84*cfb92d14SAndroid Build Coastguard Worker template <> otError Mdns::Process<Cmd("unicastquestion")>(Arg aArgs[])
85*cfb92d14SAndroid Build Coastguard Worker {
86*cfb92d14SAndroid Build Coastguard Worker     return ProcessEnableDisable(aArgs, otMdnsIsQuestionUnicastAllowed, otMdnsSetQuestionUnicastAllowed);
87*cfb92d14SAndroid Build Coastguard Worker }
88*cfb92d14SAndroid Build Coastguard Worker 
OutputHost(const otMdnsHost & aHost)89*cfb92d14SAndroid Build Coastguard Worker void Mdns::OutputHost(const otMdnsHost &aHost)
90*cfb92d14SAndroid Build Coastguard Worker {
91*cfb92d14SAndroid Build Coastguard Worker     OutputLine("Host %s", aHost.mHostName);
92*cfb92d14SAndroid Build Coastguard Worker     OutputLine(kIndentSize, "%u address:", aHost.mAddressesLength);
93*cfb92d14SAndroid Build Coastguard Worker 
94*cfb92d14SAndroid Build Coastguard Worker     for (uint16_t index = 0; index < aHost.mAddressesLength; index++)
95*cfb92d14SAndroid Build Coastguard Worker     {
96*cfb92d14SAndroid Build Coastguard Worker         OutputFormat(kIndentSize, "  ");
97*cfb92d14SAndroid Build Coastguard Worker         OutputIp6AddressLine(aHost.mAddresses[index]);
98*cfb92d14SAndroid Build Coastguard Worker     }
99*cfb92d14SAndroid Build Coastguard Worker 
100*cfb92d14SAndroid Build Coastguard Worker     OutputLine(kIndentSize, "ttl: %lu", ToUlong(aHost.mTtl));
101*cfb92d14SAndroid Build Coastguard Worker }
102*cfb92d14SAndroid Build Coastguard Worker 
OutputService(const otMdnsService & aService)103*cfb92d14SAndroid Build Coastguard Worker void Mdns::OutputService(const otMdnsService &aService)
104*cfb92d14SAndroid Build Coastguard Worker {
105*cfb92d14SAndroid Build Coastguard Worker     OutputLine("Service %s for %s", aService.mServiceInstance, aService.mServiceType);
106*cfb92d14SAndroid Build Coastguard Worker     OutputLine(kIndentSize, "host: %s", aService.mHostName);
107*cfb92d14SAndroid Build Coastguard Worker 
108*cfb92d14SAndroid Build Coastguard Worker     if (aService.mSubTypeLabelsLength > 0)
109*cfb92d14SAndroid Build Coastguard Worker     {
110*cfb92d14SAndroid Build Coastguard Worker         OutputLine(kIndentSize, "%u sub-type:", aService.mSubTypeLabelsLength);
111*cfb92d14SAndroid Build Coastguard Worker 
112*cfb92d14SAndroid Build Coastguard Worker         for (uint16_t index = 0; index < aService.mSubTypeLabelsLength; index++)
113*cfb92d14SAndroid Build Coastguard Worker         {
114*cfb92d14SAndroid Build Coastguard Worker             OutputLine(kIndentSize * 2, "%s", aService.mSubTypeLabels[index]);
115*cfb92d14SAndroid Build Coastguard Worker         }
116*cfb92d14SAndroid Build Coastguard Worker     }
117*cfb92d14SAndroid Build Coastguard Worker 
118*cfb92d14SAndroid Build Coastguard Worker     OutputLine(kIndentSize, "port: %u", aService.mPort);
119*cfb92d14SAndroid Build Coastguard Worker     OutputLine(kIndentSize, "priority: %u", aService.mPriority);
120*cfb92d14SAndroid Build Coastguard Worker     OutputLine(kIndentSize, "weight: %u", aService.mWeight);
121*cfb92d14SAndroid Build Coastguard Worker     OutputLine(kIndentSize, "ttl: %lu", ToUlong(aService.mTtl));
122*cfb92d14SAndroid Build Coastguard Worker 
123*cfb92d14SAndroid Build Coastguard Worker     if ((aService.mTxtData == nullptr) || (aService.mTxtDataLength == 0))
124*cfb92d14SAndroid Build Coastguard Worker     {
125*cfb92d14SAndroid Build Coastguard Worker         OutputLine(kIndentSize, "txt-data: (empty)");
126*cfb92d14SAndroid Build Coastguard Worker     }
127*cfb92d14SAndroid Build Coastguard Worker     else
128*cfb92d14SAndroid Build Coastguard Worker     {
129*cfb92d14SAndroid Build Coastguard Worker         OutputFormat(kIndentSize, "txt-data: ");
130*cfb92d14SAndroid Build Coastguard Worker         OutputBytesLine(aService.mTxtData, aService.mTxtDataLength);
131*cfb92d14SAndroid Build Coastguard Worker     }
132*cfb92d14SAndroid Build Coastguard Worker }
133*cfb92d14SAndroid Build Coastguard Worker 
OutputKey(const otMdnsKey & aKey)134*cfb92d14SAndroid Build Coastguard Worker void Mdns::OutputKey(const otMdnsKey &aKey)
135*cfb92d14SAndroid Build Coastguard Worker {
136*cfb92d14SAndroid Build Coastguard Worker     if (aKey.mServiceType != nullptr)
137*cfb92d14SAndroid Build Coastguard Worker     {
138*cfb92d14SAndroid Build Coastguard Worker         OutputLine("Key %s for %s (service)", aKey.mName, aKey.mServiceType);
139*cfb92d14SAndroid Build Coastguard Worker     }
140*cfb92d14SAndroid Build Coastguard Worker     else
141*cfb92d14SAndroid Build Coastguard Worker     {
142*cfb92d14SAndroid Build Coastguard Worker         OutputLine("Key %s (host)", aKey.mName);
143*cfb92d14SAndroid Build Coastguard Worker     }
144*cfb92d14SAndroid Build Coastguard Worker 
145*cfb92d14SAndroid Build Coastguard Worker     OutputFormat(kIndentSize, "key-data: ");
146*cfb92d14SAndroid Build Coastguard Worker     OutputBytesLine(aKey.mKeyData, aKey.mKeyDataLength);
147*cfb92d14SAndroid Build Coastguard Worker 
148*cfb92d14SAndroid Build Coastguard Worker     OutputLine(kIndentSize, "ttl: %lu", ToUlong(aKey.mTtl));
149*cfb92d14SAndroid Build Coastguard Worker }
150*cfb92d14SAndroid Build Coastguard Worker 
OutputState(otMdnsEntryState aState)151*cfb92d14SAndroid Build Coastguard Worker void Mdns::OutputState(otMdnsEntryState aState)
152*cfb92d14SAndroid Build Coastguard Worker {
153*cfb92d14SAndroid Build Coastguard Worker     const char *stateString = "";
154*cfb92d14SAndroid Build Coastguard Worker 
155*cfb92d14SAndroid Build Coastguard Worker     switch (aState)
156*cfb92d14SAndroid Build Coastguard Worker     {
157*cfb92d14SAndroid Build Coastguard Worker     case OT_MDNS_ENTRY_STATE_PROBING:
158*cfb92d14SAndroid Build Coastguard Worker         stateString = "probing";
159*cfb92d14SAndroid Build Coastguard Worker         break;
160*cfb92d14SAndroid Build Coastguard Worker     case OT_MDNS_ENTRY_STATE_REGISTERED:
161*cfb92d14SAndroid Build Coastguard Worker         stateString = "registered";
162*cfb92d14SAndroid Build Coastguard Worker         break;
163*cfb92d14SAndroid Build Coastguard Worker     case OT_MDNS_ENTRY_STATE_CONFLICT:
164*cfb92d14SAndroid Build Coastguard Worker         stateString = "conflict";
165*cfb92d14SAndroid Build Coastguard Worker         break;
166*cfb92d14SAndroid Build Coastguard Worker     case OT_MDNS_ENTRY_STATE_REMOVING:
167*cfb92d14SAndroid Build Coastguard Worker         stateString = "removing";
168*cfb92d14SAndroid Build Coastguard Worker         break;
169*cfb92d14SAndroid Build Coastguard Worker     }
170*cfb92d14SAndroid Build Coastguard Worker 
171*cfb92d14SAndroid Build Coastguard Worker     OutputLine(kIndentSize, "state: %s", stateString);
172*cfb92d14SAndroid Build Coastguard Worker }
173*cfb92d14SAndroid Build Coastguard Worker 
OutputCacheInfo(const otMdnsCacheInfo & aInfo)174*cfb92d14SAndroid Build Coastguard Worker void Mdns::OutputCacheInfo(const otMdnsCacheInfo &aInfo)
175*cfb92d14SAndroid Build Coastguard Worker {
176*cfb92d14SAndroid Build Coastguard Worker     OutputLine(kIndentSize, "active: %s", aInfo.mIsActive ? "yes" : "no");
177*cfb92d14SAndroid Build Coastguard Worker     OutputLine(kIndentSize, "cached-results: %s", aInfo.mHasCachedResults ? "yes" : "no");
178*cfb92d14SAndroid Build Coastguard Worker }
179*cfb92d14SAndroid Build Coastguard Worker 
Process(Arg aArgs[])180*cfb92d14SAndroid Build Coastguard Worker template <> otError Mdns::Process<Cmd("register")>(Arg aArgs[])
181*cfb92d14SAndroid Build Coastguard Worker {
182*cfb92d14SAndroid Build Coastguard Worker     // mdns [async] [host|service|key] <entry specific args>
183*cfb92d14SAndroid Build Coastguard Worker 
184*cfb92d14SAndroid Build Coastguard Worker     otError error   = OT_ERROR_NONE;
185*cfb92d14SAndroid Build Coastguard Worker     bool    isAsync = false;
186*cfb92d14SAndroid Build Coastguard Worker 
187*cfb92d14SAndroid Build Coastguard Worker     if (aArgs[0] == "async")
188*cfb92d14SAndroid Build Coastguard Worker     {
189*cfb92d14SAndroid Build Coastguard Worker         isAsync = true;
190*cfb92d14SAndroid Build Coastguard Worker         aArgs++;
191*cfb92d14SAndroid Build Coastguard Worker     }
192*cfb92d14SAndroid Build Coastguard Worker 
193*cfb92d14SAndroid Build Coastguard Worker     if (aArgs[0] == "host")
194*cfb92d14SAndroid Build Coastguard Worker     {
195*cfb92d14SAndroid Build Coastguard Worker         SuccessOrExit(error = ProcessRegisterHost(aArgs + 1));
196*cfb92d14SAndroid Build Coastguard Worker     }
197*cfb92d14SAndroid Build Coastguard Worker     else if (aArgs[0] == "service")
198*cfb92d14SAndroid Build Coastguard Worker     {
199*cfb92d14SAndroid Build Coastguard Worker         SuccessOrExit(error = ProcessRegisterService(aArgs + 1));
200*cfb92d14SAndroid Build Coastguard Worker     }
201*cfb92d14SAndroid Build Coastguard Worker     else if (aArgs[0] == "key")
202*cfb92d14SAndroid Build Coastguard Worker     {
203*cfb92d14SAndroid Build Coastguard Worker         SuccessOrExit(error = ProcessRegisterKey(aArgs + 1));
204*cfb92d14SAndroid Build Coastguard Worker     }
205*cfb92d14SAndroid Build Coastguard Worker     else
206*cfb92d14SAndroid Build Coastguard Worker     {
207*cfb92d14SAndroid Build Coastguard Worker         ExitNow(error = OT_ERROR_INVALID_ARGS);
208*cfb92d14SAndroid Build Coastguard Worker     }
209*cfb92d14SAndroid Build Coastguard Worker 
210*cfb92d14SAndroid Build Coastguard Worker     if (isAsync)
211*cfb92d14SAndroid Build Coastguard Worker     {
212*cfb92d14SAndroid Build Coastguard Worker         OutputLine("mDNS request id: %lu", ToUlong(mRequestId));
213*cfb92d14SAndroid Build Coastguard Worker     }
214*cfb92d14SAndroid Build Coastguard Worker     else
215*cfb92d14SAndroid Build Coastguard Worker     {
216*cfb92d14SAndroid Build Coastguard Worker         error               = OT_ERROR_PENDING;
217*cfb92d14SAndroid Build Coastguard Worker         mWaitingForCallback = true;
218*cfb92d14SAndroid Build Coastguard Worker     }
219*cfb92d14SAndroid Build Coastguard Worker 
220*cfb92d14SAndroid Build Coastguard Worker exit:
221*cfb92d14SAndroid Build Coastguard Worker     return error;
222*cfb92d14SAndroid Build Coastguard Worker }
223*cfb92d14SAndroid Build Coastguard Worker 
ProcessRegisterHost(Arg aArgs[])224*cfb92d14SAndroid Build Coastguard Worker otError Mdns::ProcessRegisterHost(Arg aArgs[])
225*cfb92d14SAndroid Build Coastguard Worker {
226*cfb92d14SAndroid Build Coastguard Worker     // register host <name> [<zero or more addresses>] [<ttl>]
227*cfb92d14SAndroid Build Coastguard Worker 
228*cfb92d14SAndroid Build Coastguard Worker     otError      error = OT_ERROR_NONE;
229*cfb92d14SAndroid Build Coastguard Worker     otMdnsHost   host;
230*cfb92d14SAndroid Build Coastguard Worker     otIp6Address addresses[kMaxAddresses];
231*cfb92d14SAndroid Build Coastguard Worker 
232*cfb92d14SAndroid Build Coastguard Worker     memset(&host, 0, sizeof(host));
233*cfb92d14SAndroid Build Coastguard Worker 
234*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(!aArgs->IsEmpty(), error = OT_ERROR_INVALID_ARGS);
235*cfb92d14SAndroid Build Coastguard Worker     host.mHostName = aArgs->GetCString();
236*cfb92d14SAndroid Build Coastguard Worker     aArgs++;
237*cfb92d14SAndroid Build Coastguard Worker 
238*cfb92d14SAndroid Build Coastguard Worker     host.mAddresses = addresses;
239*cfb92d14SAndroid Build Coastguard Worker 
240*cfb92d14SAndroid Build Coastguard Worker     for (; !aArgs->IsEmpty(); aArgs++)
241*cfb92d14SAndroid Build Coastguard Worker     {
242*cfb92d14SAndroid Build Coastguard Worker         otIp6Address address;
243*cfb92d14SAndroid Build Coastguard Worker         uint32_t     ttl;
244*cfb92d14SAndroid Build Coastguard Worker 
245*cfb92d14SAndroid Build Coastguard Worker         if (aArgs->ParseAsIp6Address(address) == OT_ERROR_NONE)
246*cfb92d14SAndroid Build Coastguard Worker         {
247*cfb92d14SAndroid Build Coastguard Worker             VerifyOrExit(host.mAddressesLength < kMaxAddresses, error = OT_ERROR_NO_BUFS);
248*cfb92d14SAndroid Build Coastguard Worker             addresses[host.mAddressesLength] = address;
249*cfb92d14SAndroid Build Coastguard Worker             host.mAddressesLength++;
250*cfb92d14SAndroid Build Coastguard Worker         }
251*cfb92d14SAndroid Build Coastguard Worker         else if (aArgs->ParseAsUint32(ttl) == OT_ERROR_NONE)
252*cfb92d14SAndroid Build Coastguard Worker         {
253*cfb92d14SAndroid Build Coastguard Worker             host.mTtl = ttl;
254*cfb92d14SAndroid Build Coastguard Worker             VerifyOrExit(aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS);
255*cfb92d14SAndroid Build Coastguard Worker         }
256*cfb92d14SAndroid Build Coastguard Worker         else
257*cfb92d14SAndroid Build Coastguard Worker         {
258*cfb92d14SAndroid Build Coastguard Worker             ExitNow(error = OT_ERROR_INVALID_ARGS);
259*cfb92d14SAndroid Build Coastguard Worker         }
260*cfb92d14SAndroid Build Coastguard Worker     }
261*cfb92d14SAndroid Build Coastguard Worker 
262*cfb92d14SAndroid Build Coastguard Worker     OutputHost(host);
263*cfb92d14SAndroid Build Coastguard Worker 
264*cfb92d14SAndroid Build Coastguard Worker     mRequestId++;
265*cfb92d14SAndroid Build Coastguard Worker     error = otMdnsRegisterHost(GetInstancePtr(), &host, mRequestId, HandleRegisterationDone);
266*cfb92d14SAndroid Build Coastguard Worker 
267*cfb92d14SAndroid Build Coastguard Worker exit:
268*cfb92d14SAndroid Build Coastguard Worker     return error;
269*cfb92d14SAndroid Build Coastguard Worker }
270*cfb92d14SAndroid Build Coastguard Worker 
ProcessRegisterService(Arg aArgs[])271*cfb92d14SAndroid Build Coastguard Worker otError Mdns::ProcessRegisterService(Arg aArgs[])
272*cfb92d14SAndroid Build Coastguard Worker {
273*cfb92d14SAndroid Build Coastguard Worker     otError       error;
274*cfb92d14SAndroid Build Coastguard Worker     otMdnsService service;
275*cfb92d14SAndroid Build Coastguard Worker     Buffers       buffers;
276*cfb92d14SAndroid Build Coastguard Worker 
277*cfb92d14SAndroid Build Coastguard Worker     SuccessOrExit(error = ParseServiceArgs(aArgs, service, buffers));
278*cfb92d14SAndroid Build Coastguard Worker 
279*cfb92d14SAndroid Build Coastguard Worker     OutputService(service);
280*cfb92d14SAndroid Build Coastguard Worker 
281*cfb92d14SAndroid Build Coastguard Worker     mRequestId++;
282*cfb92d14SAndroid Build Coastguard Worker     error = otMdnsRegisterService(GetInstancePtr(), &service, mRequestId, HandleRegisterationDone);
283*cfb92d14SAndroid Build Coastguard Worker 
284*cfb92d14SAndroid Build Coastguard Worker exit:
285*cfb92d14SAndroid Build Coastguard Worker     return error;
286*cfb92d14SAndroid Build Coastguard Worker }
287*cfb92d14SAndroid Build Coastguard Worker 
ParseServiceArgs(Arg aArgs[],otMdnsService & aService,Buffers & aBuffers)288*cfb92d14SAndroid Build Coastguard Worker otError Mdns::ParseServiceArgs(Arg aArgs[], otMdnsService &aService, Buffers &aBuffers)
289*cfb92d14SAndroid Build Coastguard Worker {
290*cfb92d14SAndroid Build Coastguard Worker     // mdns register service <instance-label> <service-type,sub_types> <host-name> <port> [<prio>] [<weight>] [<ttl>]
291*cfb92d14SAndroid Build Coastguard Worker     // [<txtdata>]
292*cfb92d14SAndroid Build Coastguard Worker 
293*cfb92d14SAndroid Build Coastguard Worker     otError  error = OT_ERROR_INVALID_ARGS;
294*cfb92d14SAndroid Build Coastguard Worker     char    *label;
295*cfb92d14SAndroid Build Coastguard Worker     uint16_t len;
296*cfb92d14SAndroid Build Coastguard Worker 
297*cfb92d14SAndroid Build Coastguard Worker     memset(&aService, 0, sizeof(aService));
298*cfb92d14SAndroid Build Coastguard Worker 
299*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(!aArgs->IsEmpty());
300*cfb92d14SAndroid Build Coastguard Worker     aService.mServiceInstance = aArgs->GetCString();
301*cfb92d14SAndroid Build Coastguard Worker     aArgs++;
302*cfb92d14SAndroid Build Coastguard Worker 
303*cfb92d14SAndroid Build Coastguard Worker     // Copy service type into `aBuffer.mString`, then search for
304*cfb92d14SAndroid Build Coastguard Worker     // `,` in the string to parse the list of sub-types (if any).
305*cfb92d14SAndroid Build Coastguard Worker 
306*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(!aArgs->IsEmpty());
307*cfb92d14SAndroid Build Coastguard Worker     len = aArgs->GetLength();
308*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(len + 1 < kStringSize, error = OT_ERROR_NO_BUFS);
309*cfb92d14SAndroid Build Coastguard Worker     memcpy(aBuffers.mString, aArgs->GetCString(), len + 1);
310*cfb92d14SAndroid Build Coastguard Worker 
311*cfb92d14SAndroid Build Coastguard Worker     aService.mServiceType   = aBuffers.mString;
312*cfb92d14SAndroid Build Coastguard Worker     aService.mSubTypeLabels = aBuffers.mSubTypeLabels;
313*cfb92d14SAndroid Build Coastguard Worker 
314*cfb92d14SAndroid Build Coastguard Worker     label = strchr(aBuffers.mString, ',');
315*cfb92d14SAndroid Build Coastguard Worker 
316*cfb92d14SAndroid Build Coastguard Worker     if (label != nullptr)
317*cfb92d14SAndroid Build Coastguard Worker     {
318*cfb92d14SAndroid Build Coastguard Worker         while (true)
319*cfb92d14SAndroid Build Coastguard Worker         {
320*cfb92d14SAndroid Build Coastguard Worker             *label++ = '\0';
321*cfb92d14SAndroid Build Coastguard Worker 
322*cfb92d14SAndroid Build Coastguard Worker             VerifyOrExit(aService.mSubTypeLabelsLength < kMaxSubTypes, error = OT_ERROR_NO_BUFS);
323*cfb92d14SAndroid Build Coastguard Worker             aBuffers.mSubTypeLabels[aService.mSubTypeLabelsLength] = label;
324*cfb92d14SAndroid Build Coastguard Worker             aService.mSubTypeLabelsLength++;
325*cfb92d14SAndroid Build Coastguard Worker 
326*cfb92d14SAndroid Build Coastguard Worker             label = strchr(label, ',');
327*cfb92d14SAndroid Build Coastguard Worker 
328*cfb92d14SAndroid Build Coastguard Worker             if (label == nullptr)
329*cfb92d14SAndroid Build Coastguard Worker             {
330*cfb92d14SAndroid Build Coastguard Worker                 break;
331*cfb92d14SAndroid Build Coastguard Worker             }
332*cfb92d14SAndroid Build Coastguard Worker         }
333*cfb92d14SAndroid Build Coastguard Worker     }
334*cfb92d14SAndroid Build Coastguard Worker 
335*cfb92d14SAndroid Build Coastguard Worker     aArgs++;
336*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(!aArgs->IsEmpty());
337*cfb92d14SAndroid Build Coastguard Worker     aService.mHostName = aArgs->GetCString();
338*cfb92d14SAndroid Build Coastguard Worker 
339*cfb92d14SAndroid Build Coastguard Worker     aArgs++;
340*cfb92d14SAndroid Build Coastguard Worker     SuccessOrExit(aArgs->ParseAsUint16(aService.mPort));
341*cfb92d14SAndroid Build Coastguard Worker 
342*cfb92d14SAndroid Build Coastguard Worker     // The rest of `Args` are optional.
343*cfb92d14SAndroid Build Coastguard Worker 
344*cfb92d14SAndroid Build Coastguard Worker     error = OT_ERROR_NONE;
345*cfb92d14SAndroid Build Coastguard Worker 
346*cfb92d14SAndroid Build Coastguard Worker     aArgs++;
347*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(!aArgs->IsEmpty());
348*cfb92d14SAndroid Build Coastguard Worker     SuccessOrExit(error = aArgs->ParseAsUint16(aService.mPriority));
349*cfb92d14SAndroid Build Coastguard Worker 
350*cfb92d14SAndroid Build Coastguard Worker     aArgs++;
351*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(!aArgs->IsEmpty());
352*cfb92d14SAndroid Build Coastguard Worker     SuccessOrExit(error = aArgs->ParseAsUint16(aService.mWeight));
353*cfb92d14SAndroid Build Coastguard Worker 
354*cfb92d14SAndroid Build Coastguard Worker     aArgs++;
355*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(!aArgs->IsEmpty());
356*cfb92d14SAndroid Build Coastguard Worker     SuccessOrExit(error = aArgs->ParseAsUint32(aService.mTtl));
357*cfb92d14SAndroid Build Coastguard Worker 
358*cfb92d14SAndroid Build Coastguard Worker     aArgs++;
359*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(!aArgs->IsEmpty());
360*cfb92d14SAndroid Build Coastguard Worker     len = kMaxTxtDataSize;
361*cfb92d14SAndroid Build Coastguard Worker     SuccessOrExit(error = aArgs->ParseAsHexString(len, aBuffers.mTxtData));
362*cfb92d14SAndroid Build Coastguard Worker     aService.mTxtData       = aBuffers.mTxtData;
363*cfb92d14SAndroid Build Coastguard Worker     aService.mTxtDataLength = len;
364*cfb92d14SAndroid Build Coastguard Worker 
365*cfb92d14SAndroid Build Coastguard Worker     aArgs++;
366*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(aArgs->IsEmpty(), error = OT_ERROR_INVALID_ARGS);
367*cfb92d14SAndroid Build Coastguard Worker 
368*cfb92d14SAndroid Build Coastguard Worker exit:
369*cfb92d14SAndroid Build Coastguard Worker     return error;
370*cfb92d14SAndroid Build Coastguard Worker }
371*cfb92d14SAndroid Build Coastguard Worker 
ProcessRegisterKey(Arg aArgs[])372*cfb92d14SAndroid Build Coastguard Worker otError Mdns::ProcessRegisterKey(Arg aArgs[])
373*cfb92d14SAndroid Build Coastguard Worker {
374*cfb92d14SAndroid Build Coastguard Worker     otError   error = OT_ERROR_INVALID_ARGS;
375*cfb92d14SAndroid Build Coastguard Worker     otMdnsKey key;
376*cfb92d14SAndroid Build Coastguard Worker     uint16_t  len;
377*cfb92d14SAndroid Build Coastguard Worker     uint8_t   data[kMaxKeyDataSize];
378*cfb92d14SAndroid Build Coastguard Worker 
379*cfb92d14SAndroid Build Coastguard Worker     memset(&key, 0, sizeof(key));
380*cfb92d14SAndroid Build Coastguard Worker 
381*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(!aArgs->IsEmpty());
382*cfb92d14SAndroid Build Coastguard Worker     key.mName = aArgs->GetCString();
383*cfb92d14SAndroid Build Coastguard Worker 
384*cfb92d14SAndroid Build Coastguard Worker     aArgs++;
385*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(!aArgs->IsEmpty());
386*cfb92d14SAndroid Build Coastguard Worker 
387*cfb92d14SAndroid Build Coastguard Worker     if (aArgs->GetCString()[0] == '_')
388*cfb92d14SAndroid Build Coastguard Worker     {
389*cfb92d14SAndroid Build Coastguard Worker         key.mServiceType = aArgs->GetCString();
390*cfb92d14SAndroid Build Coastguard Worker         aArgs++;
391*cfb92d14SAndroid Build Coastguard Worker         VerifyOrExit(!aArgs->IsEmpty());
392*cfb92d14SAndroid Build Coastguard Worker     }
393*cfb92d14SAndroid Build Coastguard Worker 
394*cfb92d14SAndroid Build Coastguard Worker     len = kMaxKeyDataSize;
395*cfb92d14SAndroid Build Coastguard Worker     SuccessOrExit(error = aArgs->ParseAsHexString(len, data));
396*cfb92d14SAndroid Build Coastguard Worker 
397*cfb92d14SAndroid Build Coastguard Worker     key.mKeyData       = data;
398*cfb92d14SAndroid Build Coastguard Worker     key.mKeyDataLength = len;
399*cfb92d14SAndroid Build Coastguard Worker 
400*cfb92d14SAndroid Build Coastguard Worker     // ttl is optional
401*cfb92d14SAndroid Build Coastguard Worker 
402*cfb92d14SAndroid Build Coastguard Worker     aArgs++;
403*cfb92d14SAndroid Build Coastguard Worker 
404*cfb92d14SAndroid Build Coastguard Worker     if (!aArgs->IsEmpty())
405*cfb92d14SAndroid Build Coastguard Worker     {
406*cfb92d14SAndroid Build Coastguard Worker         SuccessOrExit(error = aArgs->ParseAsUint32(key.mTtl));
407*cfb92d14SAndroid Build Coastguard Worker         aArgs++;
408*cfb92d14SAndroid Build Coastguard Worker         VerifyOrExit(aArgs->IsEmpty(), error = kErrorInvalidArgs);
409*cfb92d14SAndroid Build Coastguard Worker     }
410*cfb92d14SAndroid Build Coastguard Worker 
411*cfb92d14SAndroid Build Coastguard Worker     OutputKey(key);
412*cfb92d14SAndroid Build Coastguard Worker 
413*cfb92d14SAndroid Build Coastguard Worker     mRequestId++;
414*cfb92d14SAndroid Build Coastguard Worker     error = otMdnsRegisterKey(GetInstancePtr(), &key, mRequestId, HandleRegisterationDone);
415*cfb92d14SAndroid Build Coastguard Worker 
416*cfb92d14SAndroid Build Coastguard Worker exit:
417*cfb92d14SAndroid Build Coastguard Worker     return error;
418*cfb92d14SAndroid Build Coastguard Worker }
419*cfb92d14SAndroid Build Coastguard Worker 
HandleRegisterationDone(otInstance * aInstance,otMdnsRequestId aRequestId,otError aError)420*cfb92d14SAndroid Build Coastguard Worker void Mdns::HandleRegisterationDone(otInstance *aInstance, otMdnsRequestId aRequestId, otError aError)
421*cfb92d14SAndroid Build Coastguard Worker {
422*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aInstance);
423*cfb92d14SAndroid Build Coastguard Worker 
424*cfb92d14SAndroid Build Coastguard Worker     Interpreter::GetInterpreter().mMdns.HandleRegisterationDone(aRequestId, aError);
425*cfb92d14SAndroid Build Coastguard Worker }
426*cfb92d14SAndroid Build Coastguard Worker 
HandleRegisterationDone(otMdnsRequestId aRequestId,otError aError)427*cfb92d14SAndroid Build Coastguard Worker void Mdns::HandleRegisterationDone(otMdnsRequestId aRequestId, otError aError)
428*cfb92d14SAndroid Build Coastguard Worker {
429*cfb92d14SAndroid Build Coastguard Worker     if (mWaitingForCallback && (aRequestId == mRequestId))
430*cfb92d14SAndroid Build Coastguard Worker     {
431*cfb92d14SAndroid Build Coastguard Worker         mWaitingForCallback = false;
432*cfb92d14SAndroid Build Coastguard Worker         Interpreter::GetInterpreter().OutputResult(aError);
433*cfb92d14SAndroid Build Coastguard Worker     }
434*cfb92d14SAndroid Build Coastguard Worker     else
435*cfb92d14SAndroid Build Coastguard Worker     {
436*cfb92d14SAndroid Build Coastguard Worker         OutputLine("mDNS registration for request id %lu outcome: %s", ToUlong(aRequestId),
437*cfb92d14SAndroid Build Coastguard Worker                    otThreadErrorToString(aError));
438*cfb92d14SAndroid Build Coastguard Worker     }
439*cfb92d14SAndroid Build Coastguard Worker }
440*cfb92d14SAndroid Build Coastguard Worker 
Process(Arg aArgs[])441*cfb92d14SAndroid Build Coastguard Worker template <> otError Mdns::Process<Cmd("unregister")>(Arg aArgs[])
442*cfb92d14SAndroid Build Coastguard Worker {
443*cfb92d14SAndroid Build Coastguard Worker     otError error = OT_ERROR_INVALID_ARGS;
444*cfb92d14SAndroid Build Coastguard Worker 
445*cfb92d14SAndroid Build Coastguard Worker     if (aArgs[0] == "host")
446*cfb92d14SAndroid Build Coastguard Worker     {
447*cfb92d14SAndroid Build Coastguard Worker         otMdnsHost host;
448*cfb92d14SAndroid Build Coastguard Worker 
449*cfb92d14SAndroid Build Coastguard Worker         memset(&host, 0, sizeof(host));
450*cfb92d14SAndroid Build Coastguard Worker         VerifyOrExit(!aArgs[1].IsEmpty());
451*cfb92d14SAndroid Build Coastguard Worker         host.mHostName = aArgs[1].GetCString();
452*cfb92d14SAndroid Build Coastguard Worker         VerifyOrExit(aArgs[2].IsEmpty());
453*cfb92d14SAndroid Build Coastguard Worker 
454*cfb92d14SAndroid Build Coastguard Worker         error = otMdnsUnregisterHost(GetInstancePtr(), &host);
455*cfb92d14SAndroid Build Coastguard Worker     }
456*cfb92d14SAndroid Build Coastguard Worker     else if (aArgs[0] == "service")
457*cfb92d14SAndroid Build Coastguard Worker     {
458*cfb92d14SAndroid Build Coastguard Worker         otMdnsService service;
459*cfb92d14SAndroid Build Coastguard Worker 
460*cfb92d14SAndroid Build Coastguard Worker         memset(&service, 0, sizeof(service));
461*cfb92d14SAndroid Build Coastguard Worker         VerifyOrExit(!aArgs[1].IsEmpty());
462*cfb92d14SAndroid Build Coastguard Worker         service.mServiceInstance = aArgs[1].GetCString();
463*cfb92d14SAndroid Build Coastguard Worker         VerifyOrExit(!aArgs[2].IsEmpty());
464*cfb92d14SAndroid Build Coastguard Worker         service.mServiceType = aArgs[2].GetCString();
465*cfb92d14SAndroid Build Coastguard Worker         VerifyOrExit(aArgs[3].IsEmpty());
466*cfb92d14SAndroid Build Coastguard Worker 
467*cfb92d14SAndroid Build Coastguard Worker         error = otMdnsUnregisterService(GetInstancePtr(), &service);
468*cfb92d14SAndroid Build Coastguard Worker     }
469*cfb92d14SAndroid Build Coastguard Worker     else if (aArgs[0] == "key")
470*cfb92d14SAndroid Build Coastguard Worker     {
471*cfb92d14SAndroid Build Coastguard Worker         otMdnsKey key;
472*cfb92d14SAndroid Build Coastguard Worker 
473*cfb92d14SAndroid Build Coastguard Worker         memset(&key, 0, sizeof(key));
474*cfb92d14SAndroid Build Coastguard Worker         VerifyOrExit(!aArgs[1].IsEmpty());
475*cfb92d14SAndroid Build Coastguard Worker         key.mName = aArgs[1].GetCString();
476*cfb92d14SAndroid Build Coastguard Worker 
477*cfb92d14SAndroid Build Coastguard Worker         if (!aArgs[2].IsEmpty())
478*cfb92d14SAndroid Build Coastguard Worker         {
479*cfb92d14SAndroid Build Coastguard Worker             key.mServiceType = aArgs[2].GetCString();
480*cfb92d14SAndroid Build Coastguard Worker             VerifyOrExit(aArgs[3].IsEmpty());
481*cfb92d14SAndroid Build Coastguard Worker         }
482*cfb92d14SAndroid Build Coastguard Worker 
483*cfb92d14SAndroid Build Coastguard Worker         error = otMdnsUnregisterKey(GetInstancePtr(), &key);
484*cfb92d14SAndroid Build Coastguard Worker     }
485*cfb92d14SAndroid Build Coastguard Worker 
486*cfb92d14SAndroid Build Coastguard Worker exit:
487*cfb92d14SAndroid Build Coastguard Worker     return error;
488*cfb92d14SAndroid Build Coastguard Worker }
489*cfb92d14SAndroid Build Coastguard Worker 
490*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_MULTICAST_DNS_ENTRY_ITERATION_API_ENABLE
491*cfb92d14SAndroid Build Coastguard Worker 
Process(Arg aArgs[])492*cfb92d14SAndroid Build Coastguard Worker template <> otError Mdns::Process<Cmd("hosts")>(Arg aArgs[])
493*cfb92d14SAndroid Build Coastguard Worker {
494*cfb92d14SAndroid Build Coastguard Worker     otError          error    = OT_ERROR_NONE;
495*cfb92d14SAndroid Build Coastguard Worker     otMdnsIterator  *iterator = nullptr;
496*cfb92d14SAndroid Build Coastguard Worker     otMdnsHost       host;
497*cfb92d14SAndroid Build Coastguard Worker     otMdnsEntryState state;
498*cfb92d14SAndroid Build Coastguard Worker 
499*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS);
500*cfb92d14SAndroid Build Coastguard Worker 
501*cfb92d14SAndroid Build Coastguard Worker     iterator = otMdnsAllocateIterator(GetInstancePtr());
502*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(iterator != nullptr, error = OT_ERROR_NO_BUFS);
503*cfb92d14SAndroid Build Coastguard Worker 
504*cfb92d14SAndroid Build Coastguard Worker     while (true)
505*cfb92d14SAndroid Build Coastguard Worker     {
506*cfb92d14SAndroid Build Coastguard Worker         error = otMdnsGetNextHost(GetInstancePtr(), iterator, &host, &state);
507*cfb92d14SAndroid Build Coastguard Worker 
508*cfb92d14SAndroid Build Coastguard Worker         if (error == OT_ERROR_NOT_FOUND)
509*cfb92d14SAndroid Build Coastguard Worker         {
510*cfb92d14SAndroid Build Coastguard Worker             error = OT_ERROR_NONE;
511*cfb92d14SAndroid Build Coastguard Worker             ExitNow();
512*cfb92d14SAndroid Build Coastguard Worker         }
513*cfb92d14SAndroid Build Coastguard Worker 
514*cfb92d14SAndroid Build Coastguard Worker         SuccessOrExit(error);
515*cfb92d14SAndroid Build Coastguard Worker 
516*cfb92d14SAndroid Build Coastguard Worker         OutputHost(host);
517*cfb92d14SAndroid Build Coastguard Worker         OutputState(state);
518*cfb92d14SAndroid Build Coastguard Worker     }
519*cfb92d14SAndroid Build Coastguard Worker 
520*cfb92d14SAndroid Build Coastguard Worker exit:
521*cfb92d14SAndroid Build Coastguard Worker     if (iterator != nullptr)
522*cfb92d14SAndroid Build Coastguard Worker     {
523*cfb92d14SAndroid Build Coastguard Worker         otMdnsFreeIterator(GetInstancePtr(), iterator);
524*cfb92d14SAndroid Build Coastguard Worker     }
525*cfb92d14SAndroid Build Coastguard Worker 
526*cfb92d14SAndroid Build Coastguard Worker     return error;
527*cfb92d14SAndroid Build Coastguard Worker }
528*cfb92d14SAndroid Build Coastguard Worker 
Process(Arg aArgs[])529*cfb92d14SAndroid Build Coastguard Worker template <> otError Mdns::Process<Cmd("services")>(Arg aArgs[])
530*cfb92d14SAndroid Build Coastguard Worker {
531*cfb92d14SAndroid Build Coastguard Worker     otError          error    = OT_ERROR_NONE;
532*cfb92d14SAndroid Build Coastguard Worker     otMdnsIterator  *iterator = nullptr;
533*cfb92d14SAndroid Build Coastguard Worker     otMdnsService    service;
534*cfb92d14SAndroid Build Coastguard Worker     otMdnsEntryState state;
535*cfb92d14SAndroid Build Coastguard Worker 
536*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS);
537*cfb92d14SAndroid Build Coastguard Worker 
538*cfb92d14SAndroid Build Coastguard Worker     iterator = otMdnsAllocateIterator(GetInstancePtr());
539*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(iterator != nullptr, error = OT_ERROR_NO_BUFS);
540*cfb92d14SAndroid Build Coastguard Worker 
541*cfb92d14SAndroid Build Coastguard Worker     while (true)
542*cfb92d14SAndroid Build Coastguard Worker     {
543*cfb92d14SAndroid Build Coastguard Worker         error = otMdnsGetNextService(GetInstancePtr(), iterator, &service, &state);
544*cfb92d14SAndroid Build Coastguard Worker 
545*cfb92d14SAndroid Build Coastguard Worker         if (error == OT_ERROR_NOT_FOUND)
546*cfb92d14SAndroid Build Coastguard Worker         {
547*cfb92d14SAndroid Build Coastguard Worker             error = OT_ERROR_NONE;
548*cfb92d14SAndroid Build Coastguard Worker             ExitNow();
549*cfb92d14SAndroid Build Coastguard Worker         }
550*cfb92d14SAndroid Build Coastguard Worker 
551*cfb92d14SAndroid Build Coastguard Worker         SuccessOrExit(error);
552*cfb92d14SAndroid Build Coastguard Worker 
553*cfb92d14SAndroid Build Coastguard Worker         OutputService(service);
554*cfb92d14SAndroid Build Coastguard Worker         OutputState(state);
555*cfb92d14SAndroid Build Coastguard Worker     }
556*cfb92d14SAndroid Build Coastguard Worker 
557*cfb92d14SAndroid Build Coastguard Worker exit:
558*cfb92d14SAndroid Build Coastguard Worker     if (iterator != nullptr)
559*cfb92d14SAndroid Build Coastguard Worker     {
560*cfb92d14SAndroid Build Coastguard Worker         otMdnsFreeIterator(GetInstancePtr(), iterator);
561*cfb92d14SAndroid Build Coastguard Worker     }
562*cfb92d14SAndroid Build Coastguard Worker 
563*cfb92d14SAndroid Build Coastguard Worker     return error;
564*cfb92d14SAndroid Build Coastguard Worker }
565*cfb92d14SAndroid Build Coastguard Worker 
Process(Arg aArgs[])566*cfb92d14SAndroid Build Coastguard Worker template <> otError Mdns::Process<Cmd("keys")>(Arg aArgs[])
567*cfb92d14SAndroid Build Coastguard Worker {
568*cfb92d14SAndroid Build Coastguard Worker     otError          error    = OT_ERROR_NONE;
569*cfb92d14SAndroid Build Coastguard Worker     otMdnsIterator  *iterator = nullptr;
570*cfb92d14SAndroid Build Coastguard Worker     otMdnsKey        key;
571*cfb92d14SAndroid Build Coastguard Worker     otMdnsEntryState state;
572*cfb92d14SAndroid Build Coastguard Worker 
573*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS);
574*cfb92d14SAndroid Build Coastguard Worker 
575*cfb92d14SAndroid Build Coastguard Worker     iterator = otMdnsAllocateIterator(GetInstancePtr());
576*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(iterator != nullptr, error = OT_ERROR_NO_BUFS);
577*cfb92d14SAndroid Build Coastguard Worker 
578*cfb92d14SAndroid Build Coastguard Worker     while (true)
579*cfb92d14SAndroid Build Coastguard Worker     {
580*cfb92d14SAndroid Build Coastguard Worker         error = otMdnsGetNextKey(GetInstancePtr(), iterator, &key, &state);
581*cfb92d14SAndroid Build Coastguard Worker 
582*cfb92d14SAndroid Build Coastguard Worker         if (error == OT_ERROR_NOT_FOUND)
583*cfb92d14SAndroid Build Coastguard Worker         {
584*cfb92d14SAndroid Build Coastguard Worker             error = OT_ERROR_NONE;
585*cfb92d14SAndroid Build Coastguard Worker             ExitNow();
586*cfb92d14SAndroid Build Coastguard Worker         }
587*cfb92d14SAndroid Build Coastguard Worker 
588*cfb92d14SAndroid Build Coastguard Worker         SuccessOrExit(error);
589*cfb92d14SAndroid Build Coastguard Worker 
590*cfb92d14SAndroid Build Coastguard Worker         OutputKey(key);
591*cfb92d14SAndroid Build Coastguard Worker         OutputState(state);
592*cfb92d14SAndroid Build Coastguard Worker     }
593*cfb92d14SAndroid Build Coastguard Worker 
594*cfb92d14SAndroid Build Coastguard Worker exit:
595*cfb92d14SAndroid Build Coastguard Worker     if (iterator != nullptr)
596*cfb92d14SAndroid Build Coastguard Worker     {
597*cfb92d14SAndroid Build Coastguard Worker         otMdnsFreeIterator(GetInstancePtr(), iterator);
598*cfb92d14SAndroid Build Coastguard Worker     }
599*cfb92d14SAndroid Build Coastguard Worker 
600*cfb92d14SAndroid Build Coastguard Worker     return error;
601*cfb92d14SAndroid Build Coastguard Worker }
602*cfb92d14SAndroid Build Coastguard Worker 
603*cfb92d14SAndroid Build Coastguard Worker #endif // OPENTHREAD_CONFIG_MULTICAST_DNS_ENTRY_ITERATION_API_ENABLE
604*cfb92d14SAndroid Build Coastguard Worker 
ParseStartOrStop(const Arg & aArg,bool & aIsStart)605*cfb92d14SAndroid Build Coastguard Worker otError Mdns::ParseStartOrStop(const Arg &aArg, bool &aIsStart)
606*cfb92d14SAndroid Build Coastguard Worker {
607*cfb92d14SAndroid Build Coastguard Worker     otError error = OT_ERROR_NONE;
608*cfb92d14SAndroid Build Coastguard Worker 
609*cfb92d14SAndroid Build Coastguard Worker     if (aArg == "start")
610*cfb92d14SAndroid Build Coastguard Worker     {
611*cfb92d14SAndroid Build Coastguard Worker         aIsStart = true;
612*cfb92d14SAndroid Build Coastguard Worker     }
613*cfb92d14SAndroid Build Coastguard Worker     else if (aArg == "stop")
614*cfb92d14SAndroid Build Coastguard Worker     {
615*cfb92d14SAndroid Build Coastguard Worker         aIsStart = false;
616*cfb92d14SAndroid Build Coastguard Worker     }
617*cfb92d14SAndroid Build Coastguard Worker     else
618*cfb92d14SAndroid Build Coastguard Worker     {
619*cfb92d14SAndroid Build Coastguard Worker         error = OT_ERROR_INVALID_ARGS;
620*cfb92d14SAndroid Build Coastguard Worker     }
621*cfb92d14SAndroid Build Coastguard Worker 
622*cfb92d14SAndroid Build Coastguard Worker     return error;
623*cfb92d14SAndroid Build Coastguard Worker }
624*cfb92d14SAndroid Build Coastguard Worker 
Process(Arg aArgs[])625*cfb92d14SAndroid Build Coastguard Worker template <> otError Mdns::Process<Cmd("browser")>(Arg aArgs[])
626*cfb92d14SAndroid Build Coastguard Worker {
627*cfb92d14SAndroid Build Coastguard Worker     // mdns browser start|stop <service-type> [<sub-type>]
628*cfb92d14SAndroid Build Coastguard Worker 
629*cfb92d14SAndroid Build Coastguard Worker     otError       error;
630*cfb92d14SAndroid Build Coastguard Worker     otMdnsBrowser browser;
631*cfb92d14SAndroid Build Coastguard Worker     bool          isStart;
632*cfb92d14SAndroid Build Coastguard Worker 
633*cfb92d14SAndroid Build Coastguard Worker     ClearAllBytes(browser);
634*cfb92d14SAndroid Build Coastguard Worker 
635*cfb92d14SAndroid Build Coastguard Worker     SuccessOrExit(error = ParseStartOrStop(aArgs[0], isStart));
636*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(!aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS);
637*cfb92d14SAndroid Build Coastguard Worker 
638*cfb92d14SAndroid Build Coastguard Worker     browser.mServiceType = aArgs[1].GetCString();
639*cfb92d14SAndroid Build Coastguard Worker 
640*cfb92d14SAndroid Build Coastguard Worker     if (!aArgs[2].IsEmpty())
641*cfb92d14SAndroid Build Coastguard Worker     {
642*cfb92d14SAndroid Build Coastguard Worker         browser.mSubTypeLabel = aArgs[2].GetCString();
643*cfb92d14SAndroid Build Coastguard Worker         VerifyOrExit(aArgs[3].IsEmpty(), error = OT_ERROR_INVALID_ARGS);
644*cfb92d14SAndroid Build Coastguard Worker     }
645*cfb92d14SAndroid Build Coastguard Worker 
646*cfb92d14SAndroid Build Coastguard Worker     browser.mInfraIfIndex = mInfraIfIndex;
647*cfb92d14SAndroid Build Coastguard Worker     browser.mCallback     = HandleBrowseResult;
648*cfb92d14SAndroid Build Coastguard Worker 
649*cfb92d14SAndroid Build Coastguard Worker     if (isStart)
650*cfb92d14SAndroid Build Coastguard Worker     {
651*cfb92d14SAndroid Build Coastguard Worker         error = otMdnsStartBrowser(GetInstancePtr(), &browser);
652*cfb92d14SAndroid Build Coastguard Worker     }
653*cfb92d14SAndroid Build Coastguard Worker     else
654*cfb92d14SAndroid Build Coastguard Worker     {
655*cfb92d14SAndroid Build Coastguard Worker         error = otMdnsStopBrowser(GetInstancePtr(), &browser);
656*cfb92d14SAndroid Build Coastguard Worker     }
657*cfb92d14SAndroid Build Coastguard Worker 
658*cfb92d14SAndroid Build Coastguard Worker exit:
659*cfb92d14SAndroid Build Coastguard Worker     return error;
660*cfb92d14SAndroid Build Coastguard Worker }
661*cfb92d14SAndroid Build Coastguard Worker 
HandleBrowseResult(otInstance * aInstance,const otMdnsBrowseResult * aResult)662*cfb92d14SAndroid Build Coastguard Worker void Mdns::HandleBrowseResult(otInstance *aInstance, const otMdnsBrowseResult *aResult)
663*cfb92d14SAndroid Build Coastguard Worker {
664*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aInstance);
665*cfb92d14SAndroid Build Coastguard Worker 
666*cfb92d14SAndroid Build Coastguard Worker     Interpreter::GetInterpreter().mMdns.HandleBrowseResult(*aResult);
667*cfb92d14SAndroid Build Coastguard Worker }
668*cfb92d14SAndroid Build Coastguard Worker 
HandleBrowseResult(const otMdnsBrowseResult & aResult)669*cfb92d14SAndroid Build Coastguard Worker void Mdns::HandleBrowseResult(const otMdnsBrowseResult &aResult)
670*cfb92d14SAndroid Build Coastguard Worker {
671*cfb92d14SAndroid Build Coastguard Worker     OutputFormat("mDNS browse result for %s", aResult.mServiceType);
672*cfb92d14SAndroid Build Coastguard Worker 
673*cfb92d14SAndroid Build Coastguard Worker     if (aResult.mSubTypeLabel)
674*cfb92d14SAndroid Build Coastguard Worker     {
675*cfb92d14SAndroid Build Coastguard Worker         OutputLine(" sub-type %s", aResult.mSubTypeLabel);
676*cfb92d14SAndroid Build Coastguard Worker     }
677*cfb92d14SAndroid Build Coastguard Worker     else
678*cfb92d14SAndroid Build Coastguard Worker     {
679*cfb92d14SAndroid Build Coastguard Worker         OutputNewLine();
680*cfb92d14SAndroid Build Coastguard Worker     }
681*cfb92d14SAndroid Build Coastguard Worker 
682*cfb92d14SAndroid Build Coastguard Worker     OutputLine(kIndentSize, "instance: %s", aResult.mServiceInstance);
683*cfb92d14SAndroid Build Coastguard Worker     OutputLine(kIndentSize, "ttl: %lu", ToUlong(aResult.mTtl));
684*cfb92d14SAndroid Build Coastguard Worker     OutputLine(kIndentSize, "if-index: %lu", ToUlong(aResult.mInfraIfIndex));
685*cfb92d14SAndroid Build Coastguard Worker }
686*cfb92d14SAndroid Build Coastguard Worker 
Process(Arg aArgs[])687*cfb92d14SAndroid Build Coastguard Worker template <> otError Mdns::Process<Cmd("srvresolver")>(Arg aArgs[])
688*cfb92d14SAndroid Build Coastguard Worker {
689*cfb92d14SAndroid Build Coastguard Worker     // mdns srvresolver start|stop <service-instance> <service-type>
690*cfb92d14SAndroid Build Coastguard Worker 
691*cfb92d14SAndroid Build Coastguard Worker     otError           error;
692*cfb92d14SAndroid Build Coastguard Worker     otMdnsSrvResolver resolver;
693*cfb92d14SAndroid Build Coastguard Worker     bool              isStart;
694*cfb92d14SAndroid Build Coastguard Worker 
695*cfb92d14SAndroid Build Coastguard Worker     ClearAllBytes(resolver);
696*cfb92d14SAndroid Build Coastguard Worker 
697*cfb92d14SAndroid Build Coastguard Worker     SuccessOrExit(error = ParseStartOrStop(aArgs[0], isStart));
698*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(!aArgs[2].IsEmpty(), error = OT_ERROR_INVALID_ARGS);
699*cfb92d14SAndroid Build Coastguard Worker 
700*cfb92d14SAndroid Build Coastguard Worker     resolver.mServiceInstance = aArgs[1].GetCString();
701*cfb92d14SAndroid Build Coastguard Worker     resolver.mServiceType     = aArgs[2].GetCString();
702*cfb92d14SAndroid Build Coastguard Worker     resolver.mInfraIfIndex    = mInfraIfIndex;
703*cfb92d14SAndroid Build Coastguard Worker     resolver.mCallback        = HandleSrvResult;
704*cfb92d14SAndroid Build Coastguard Worker 
705*cfb92d14SAndroid Build Coastguard Worker     if (isStart)
706*cfb92d14SAndroid Build Coastguard Worker     {
707*cfb92d14SAndroid Build Coastguard Worker         error = otMdnsStartSrvResolver(GetInstancePtr(), &resolver);
708*cfb92d14SAndroid Build Coastguard Worker     }
709*cfb92d14SAndroid Build Coastguard Worker     else
710*cfb92d14SAndroid Build Coastguard Worker     {
711*cfb92d14SAndroid Build Coastguard Worker         error = otMdnsStopSrvResolver(GetInstancePtr(), &resolver);
712*cfb92d14SAndroid Build Coastguard Worker     }
713*cfb92d14SAndroid Build Coastguard Worker 
714*cfb92d14SAndroid Build Coastguard Worker exit:
715*cfb92d14SAndroid Build Coastguard Worker     return error;
716*cfb92d14SAndroid Build Coastguard Worker }
717*cfb92d14SAndroid Build Coastguard Worker 
HandleSrvResult(otInstance * aInstance,const otMdnsSrvResult * aResult)718*cfb92d14SAndroid Build Coastguard Worker void Mdns::HandleSrvResult(otInstance *aInstance, const otMdnsSrvResult *aResult)
719*cfb92d14SAndroid Build Coastguard Worker {
720*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aInstance);
721*cfb92d14SAndroid Build Coastguard Worker 
722*cfb92d14SAndroid Build Coastguard Worker     Interpreter::GetInterpreter().mMdns.HandleSrvResult(*aResult);
723*cfb92d14SAndroid Build Coastguard Worker }
724*cfb92d14SAndroid Build Coastguard Worker 
HandleSrvResult(const otMdnsSrvResult & aResult)725*cfb92d14SAndroid Build Coastguard Worker void Mdns::HandleSrvResult(const otMdnsSrvResult &aResult)
726*cfb92d14SAndroid Build Coastguard Worker {
727*cfb92d14SAndroid Build Coastguard Worker     OutputLine("mDNS SRV result for %s for %s", aResult.mServiceInstance, aResult.mServiceType);
728*cfb92d14SAndroid Build Coastguard Worker 
729*cfb92d14SAndroid Build Coastguard Worker     if (aResult.mTtl != 0)
730*cfb92d14SAndroid Build Coastguard Worker     {
731*cfb92d14SAndroid Build Coastguard Worker         OutputLine(kIndentSize, "host: %s", aResult.mHostName);
732*cfb92d14SAndroid Build Coastguard Worker         OutputLine(kIndentSize, "port: %u", aResult.mPort);
733*cfb92d14SAndroid Build Coastguard Worker         OutputLine(kIndentSize, "priority: %u", aResult.mPriority);
734*cfb92d14SAndroid Build Coastguard Worker         OutputLine(kIndentSize, "weight: %u", aResult.mWeight);
735*cfb92d14SAndroid Build Coastguard Worker     }
736*cfb92d14SAndroid Build Coastguard Worker 
737*cfb92d14SAndroid Build Coastguard Worker     OutputLine(kIndentSize, "ttl: %lu", ToUlong(aResult.mTtl));
738*cfb92d14SAndroid Build Coastguard Worker     OutputLine(kIndentSize, "if-index: %lu", ToUlong(aResult.mInfraIfIndex));
739*cfb92d14SAndroid Build Coastguard Worker }
740*cfb92d14SAndroid Build Coastguard Worker 
Process(Arg aArgs[])741*cfb92d14SAndroid Build Coastguard Worker template <> otError Mdns::Process<Cmd("txtresolver")>(Arg aArgs[])
742*cfb92d14SAndroid Build Coastguard Worker {
743*cfb92d14SAndroid Build Coastguard Worker     // mdns txtresolver start|stop <service-instance> <service-type>
744*cfb92d14SAndroid Build Coastguard Worker 
745*cfb92d14SAndroid Build Coastguard Worker     otError           error;
746*cfb92d14SAndroid Build Coastguard Worker     otMdnsTxtResolver resolver;
747*cfb92d14SAndroid Build Coastguard Worker     bool              isStart;
748*cfb92d14SAndroid Build Coastguard Worker 
749*cfb92d14SAndroid Build Coastguard Worker     ClearAllBytes(resolver);
750*cfb92d14SAndroid Build Coastguard Worker 
751*cfb92d14SAndroid Build Coastguard Worker     SuccessOrExit(error = ParseStartOrStop(aArgs[0], isStart));
752*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(!aArgs[2].IsEmpty(), error = OT_ERROR_INVALID_ARGS);
753*cfb92d14SAndroid Build Coastguard Worker 
754*cfb92d14SAndroid Build Coastguard Worker     resolver.mServiceInstance = aArgs[1].GetCString();
755*cfb92d14SAndroid Build Coastguard Worker     resolver.mServiceType     = aArgs[2].GetCString();
756*cfb92d14SAndroid Build Coastguard Worker     resolver.mInfraIfIndex    = mInfraIfIndex;
757*cfb92d14SAndroid Build Coastguard Worker     resolver.mCallback        = HandleTxtResult;
758*cfb92d14SAndroid Build Coastguard Worker 
759*cfb92d14SAndroid Build Coastguard Worker     if (isStart)
760*cfb92d14SAndroid Build Coastguard Worker     {
761*cfb92d14SAndroid Build Coastguard Worker         error = otMdnsStartTxtResolver(GetInstancePtr(), &resolver);
762*cfb92d14SAndroid Build Coastguard Worker     }
763*cfb92d14SAndroid Build Coastguard Worker     else
764*cfb92d14SAndroid Build Coastguard Worker     {
765*cfb92d14SAndroid Build Coastguard Worker         error = otMdnsStopTxtResolver(GetInstancePtr(), &resolver);
766*cfb92d14SAndroid Build Coastguard Worker     }
767*cfb92d14SAndroid Build Coastguard Worker 
768*cfb92d14SAndroid Build Coastguard Worker exit:
769*cfb92d14SAndroid Build Coastguard Worker     return error;
770*cfb92d14SAndroid Build Coastguard Worker }
771*cfb92d14SAndroid Build Coastguard Worker 
HandleTxtResult(otInstance * aInstance,const otMdnsTxtResult * aResult)772*cfb92d14SAndroid Build Coastguard Worker void Mdns::HandleTxtResult(otInstance *aInstance, const otMdnsTxtResult *aResult)
773*cfb92d14SAndroid Build Coastguard Worker {
774*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aInstance);
775*cfb92d14SAndroid Build Coastguard Worker 
776*cfb92d14SAndroid Build Coastguard Worker     Interpreter::GetInterpreter().mMdns.HandleTxtResult(*aResult);
777*cfb92d14SAndroid Build Coastguard Worker }
778*cfb92d14SAndroid Build Coastguard Worker 
HandleTxtResult(const otMdnsTxtResult & aResult)779*cfb92d14SAndroid Build Coastguard Worker void Mdns::HandleTxtResult(const otMdnsTxtResult &aResult)
780*cfb92d14SAndroid Build Coastguard Worker {
781*cfb92d14SAndroid Build Coastguard Worker     OutputLine("mDNS TXT result for %s for %s", aResult.mServiceInstance, aResult.mServiceType);
782*cfb92d14SAndroid Build Coastguard Worker 
783*cfb92d14SAndroid Build Coastguard Worker     if (aResult.mTtl != 0)
784*cfb92d14SAndroid Build Coastguard Worker     {
785*cfb92d14SAndroid Build Coastguard Worker         OutputFormat(kIndentSize, "txt-data: ");
786*cfb92d14SAndroid Build Coastguard Worker         OutputBytesLine(aResult.mTxtData, aResult.mTxtDataLength);
787*cfb92d14SAndroid Build Coastguard Worker     }
788*cfb92d14SAndroid Build Coastguard Worker 
789*cfb92d14SAndroid Build Coastguard Worker     OutputLine(kIndentSize, "ttl: %lu", ToUlong(aResult.mTtl));
790*cfb92d14SAndroid Build Coastguard Worker     OutputLine(kIndentSize, "if-index: %lu", ToUlong(aResult.mInfraIfIndex));
791*cfb92d14SAndroid Build Coastguard Worker }
Process(Arg aArgs[])792*cfb92d14SAndroid Build Coastguard Worker template <> otError Mdns::Process<Cmd("ip6resolver")>(Arg aArgs[])
793*cfb92d14SAndroid Build Coastguard Worker {
794*cfb92d14SAndroid Build Coastguard Worker     // mdns ip6resolver start|stop <host-name>
795*cfb92d14SAndroid Build Coastguard Worker 
796*cfb92d14SAndroid Build Coastguard Worker     otError               error;
797*cfb92d14SAndroid Build Coastguard Worker     otMdnsAddressResolver resolver;
798*cfb92d14SAndroid Build Coastguard Worker     bool                  isStart;
799*cfb92d14SAndroid Build Coastguard Worker 
800*cfb92d14SAndroid Build Coastguard Worker     ClearAllBytes(resolver);
801*cfb92d14SAndroid Build Coastguard Worker 
802*cfb92d14SAndroid Build Coastguard Worker     SuccessOrExit(error = ParseStartOrStop(aArgs[0], isStart));
803*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(!aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS);
804*cfb92d14SAndroid Build Coastguard Worker 
805*cfb92d14SAndroid Build Coastguard Worker     resolver.mHostName     = aArgs[1].GetCString();
806*cfb92d14SAndroid Build Coastguard Worker     resolver.mInfraIfIndex = mInfraIfIndex;
807*cfb92d14SAndroid Build Coastguard Worker     resolver.mCallback     = HandleIp6AddressResult;
808*cfb92d14SAndroid Build Coastguard Worker 
809*cfb92d14SAndroid Build Coastguard Worker     if (isStart)
810*cfb92d14SAndroid Build Coastguard Worker     {
811*cfb92d14SAndroid Build Coastguard Worker         error = otMdnsStartIp6AddressResolver(GetInstancePtr(), &resolver);
812*cfb92d14SAndroid Build Coastguard Worker     }
813*cfb92d14SAndroid Build Coastguard Worker     else
814*cfb92d14SAndroid Build Coastguard Worker     {
815*cfb92d14SAndroid Build Coastguard Worker         error = otMdnsStopIp6AddressResolver(GetInstancePtr(), &resolver);
816*cfb92d14SAndroid Build Coastguard Worker     }
817*cfb92d14SAndroid Build Coastguard Worker 
818*cfb92d14SAndroid Build Coastguard Worker exit:
819*cfb92d14SAndroid Build Coastguard Worker     return error;
820*cfb92d14SAndroid Build Coastguard Worker }
821*cfb92d14SAndroid Build Coastguard Worker 
HandleIp6AddressResult(otInstance * aInstance,const otMdnsAddressResult * aResult)822*cfb92d14SAndroid Build Coastguard Worker void Mdns::HandleIp6AddressResult(otInstance *aInstance, const otMdnsAddressResult *aResult)
823*cfb92d14SAndroid Build Coastguard Worker {
824*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aInstance);
825*cfb92d14SAndroid Build Coastguard Worker 
826*cfb92d14SAndroid Build Coastguard Worker     Interpreter::GetInterpreter().mMdns.HandleAddressResult(*aResult, kIp6Address);
827*cfb92d14SAndroid Build Coastguard Worker }
828*cfb92d14SAndroid Build Coastguard Worker 
HandleAddressResult(const otMdnsAddressResult & aResult,IpAddressType aType)829*cfb92d14SAndroid Build Coastguard Worker void Mdns::HandleAddressResult(const otMdnsAddressResult &aResult, IpAddressType aType)
830*cfb92d14SAndroid Build Coastguard Worker {
831*cfb92d14SAndroid Build Coastguard Worker     OutputLine("mDNS %s address result for %s", aType == kIp6Address ? "IPv6" : "IPv4", aResult.mHostName);
832*cfb92d14SAndroid Build Coastguard Worker 
833*cfb92d14SAndroid Build Coastguard Worker     OutputLine(kIndentSize, "%u address:", aResult.mAddressesLength);
834*cfb92d14SAndroid Build Coastguard Worker 
835*cfb92d14SAndroid Build Coastguard Worker     for (uint16_t index = 0; index < aResult.mAddressesLength; index++)
836*cfb92d14SAndroid Build Coastguard Worker     {
837*cfb92d14SAndroid Build Coastguard Worker         OutputFormat(kIndentSize, "  ");
838*cfb92d14SAndroid Build Coastguard Worker         OutputIp6Address(aResult.mAddresses[index].mAddress);
839*cfb92d14SAndroid Build Coastguard Worker         OutputLine(" ttl:%lu", ToUlong(aResult.mAddresses[index].mTtl));
840*cfb92d14SAndroid Build Coastguard Worker     }
841*cfb92d14SAndroid Build Coastguard Worker 
842*cfb92d14SAndroid Build Coastguard Worker     OutputLine(kIndentSize, "if-index: %lu", ToUlong(aResult.mInfraIfIndex));
843*cfb92d14SAndroid Build Coastguard Worker }
844*cfb92d14SAndroid Build Coastguard Worker 
Process(Arg aArgs[])845*cfb92d14SAndroid Build Coastguard Worker template <> otError Mdns::Process<Cmd("ip4resolver")>(Arg aArgs[])
846*cfb92d14SAndroid Build Coastguard Worker {
847*cfb92d14SAndroid Build Coastguard Worker     // mdns ip4resolver start|stop <host-name>
848*cfb92d14SAndroid Build Coastguard Worker 
849*cfb92d14SAndroid Build Coastguard Worker     otError               error;
850*cfb92d14SAndroid Build Coastguard Worker     otMdnsAddressResolver resolver;
851*cfb92d14SAndroid Build Coastguard Worker     bool                  isStart;
852*cfb92d14SAndroid Build Coastguard Worker 
853*cfb92d14SAndroid Build Coastguard Worker     ClearAllBytes(resolver);
854*cfb92d14SAndroid Build Coastguard Worker 
855*cfb92d14SAndroid Build Coastguard Worker     SuccessOrExit(error = ParseStartOrStop(aArgs[0], isStart));
856*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(!aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS);
857*cfb92d14SAndroid Build Coastguard Worker 
858*cfb92d14SAndroid Build Coastguard Worker     resolver.mHostName     = aArgs[1].GetCString();
859*cfb92d14SAndroid Build Coastguard Worker     resolver.mInfraIfIndex = mInfraIfIndex;
860*cfb92d14SAndroid Build Coastguard Worker     resolver.mCallback     = HandleIp4AddressResult;
861*cfb92d14SAndroid Build Coastguard Worker 
862*cfb92d14SAndroid Build Coastguard Worker     if (isStart)
863*cfb92d14SAndroid Build Coastguard Worker     {
864*cfb92d14SAndroid Build Coastguard Worker         error = otMdnsStartIp4AddressResolver(GetInstancePtr(), &resolver);
865*cfb92d14SAndroid Build Coastguard Worker     }
866*cfb92d14SAndroid Build Coastguard Worker     else
867*cfb92d14SAndroid Build Coastguard Worker     {
868*cfb92d14SAndroid Build Coastguard Worker         error = otMdnsStopIp4AddressResolver(GetInstancePtr(), &resolver);
869*cfb92d14SAndroid Build Coastguard Worker     }
870*cfb92d14SAndroid Build Coastguard Worker 
871*cfb92d14SAndroid Build Coastguard Worker exit:
872*cfb92d14SAndroid Build Coastguard Worker     return error;
873*cfb92d14SAndroid Build Coastguard Worker }
874*cfb92d14SAndroid Build Coastguard Worker 
HandleIp4AddressResult(otInstance * aInstance,const otMdnsAddressResult * aResult)875*cfb92d14SAndroid Build Coastguard Worker void Mdns::HandleIp4AddressResult(otInstance *aInstance, const otMdnsAddressResult *aResult)
876*cfb92d14SAndroid Build Coastguard Worker {
877*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aInstance);
878*cfb92d14SAndroid Build Coastguard Worker 
879*cfb92d14SAndroid Build Coastguard Worker     Interpreter::GetInterpreter().mMdns.HandleAddressResult(*aResult, kIp4Address);
880*cfb92d14SAndroid Build Coastguard Worker }
881*cfb92d14SAndroid Build Coastguard Worker 
882*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_MULTICAST_DNS_ENTRY_ITERATION_API_ENABLE
883*cfb92d14SAndroid Build Coastguard Worker 
Process(Arg aArgs[])884*cfb92d14SAndroid Build Coastguard Worker template <> otError Mdns::Process<Cmd("browsers")>(Arg aArgs[])
885*cfb92d14SAndroid Build Coastguard Worker {
886*cfb92d14SAndroid Build Coastguard Worker     // mdns browsers
887*cfb92d14SAndroid Build Coastguard Worker 
888*cfb92d14SAndroid Build Coastguard Worker     otError         error;
889*cfb92d14SAndroid Build Coastguard Worker     otMdnsIterator *iterator = nullptr;
890*cfb92d14SAndroid Build Coastguard Worker     otMdnsCacheInfo info;
891*cfb92d14SAndroid Build Coastguard Worker     otMdnsBrowser   browser;
892*cfb92d14SAndroid Build Coastguard Worker 
893*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS);
894*cfb92d14SAndroid Build Coastguard Worker 
895*cfb92d14SAndroid Build Coastguard Worker     iterator = otMdnsAllocateIterator(GetInstancePtr());
896*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(iterator != nullptr, error = OT_ERROR_NO_BUFS);
897*cfb92d14SAndroid Build Coastguard Worker 
898*cfb92d14SAndroid Build Coastguard Worker     while (true)
899*cfb92d14SAndroid Build Coastguard Worker     {
900*cfb92d14SAndroid Build Coastguard Worker         error = otMdnsGetNextBrowser(GetInstancePtr(), iterator, &browser, &info);
901*cfb92d14SAndroid Build Coastguard Worker 
902*cfb92d14SAndroid Build Coastguard Worker         if (error == OT_ERROR_NOT_FOUND)
903*cfb92d14SAndroid Build Coastguard Worker         {
904*cfb92d14SAndroid Build Coastguard Worker             error = OT_ERROR_NONE;
905*cfb92d14SAndroid Build Coastguard Worker             ExitNow();
906*cfb92d14SAndroid Build Coastguard Worker         }
907*cfb92d14SAndroid Build Coastguard Worker 
908*cfb92d14SAndroid Build Coastguard Worker         SuccessOrExit(error);
909*cfb92d14SAndroid Build Coastguard Worker 
910*cfb92d14SAndroid Build Coastguard Worker         OutputFormat("Browser %s", browser.mServiceType);
911*cfb92d14SAndroid Build Coastguard Worker 
912*cfb92d14SAndroid Build Coastguard Worker         if (browser.mSubTypeLabel != nullptr)
913*cfb92d14SAndroid Build Coastguard Worker         {
914*cfb92d14SAndroid Build Coastguard Worker             OutputFormat(" for sub-type %s", browser.mSubTypeLabel);
915*cfb92d14SAndroid Build Coastguard Worker         }
916*cfb92d14SAndroid Build Coastguard Worker 
917*cfb92d14SAndroid Build Coastguard Worker         OutputNewLine();
918*cfb92d14SAndroid Build Coastguard Worker         OutputCacheInfo(info);
919*cfb92d14SAndroid Build Coastguard Worker     }
920*cfb92d14SAndroid Build Coastguard Worker 
921*cfb92d14SAndroid Build Coastguard Worker exit:
922*cfb92d14SAndroid Build Coastguard Worker     if (iterator != nullptr)
923*cfb92d14SAndroid Build Coastguard Worker     {
924*cfb92d14SAndroid Build Coastguard Worker         otMdnsFreeIterator(GetInstancePtr(), iterator);
925*cfb92d14SAndroid Build Coastguard Worker     }
926*cfb92d14SAndroid Build Coastguard Worker 
927*cfb92d14SAndroid Build Coastguard Worker     return error;
928*cfb92d14SAndroid Build Coastguard Worker }
929*cfb92d14SAndroid Build Coastguard Worker 
Process(Arg aArgs[])930*cfb92d14SAndroid Build Coastguard Worker template <> otError Mdns::Process<Cmd("srvresolvers")>(Arg aArgs[])
931*cfb92d14SAndroid Build Coastguard Worker {
932*cfb92d14SAndroid Build Coastguard Worker     // mdns srvresolvers
933*cfb92d14SAndroid Build Coastguard Worker 
934*cfb92d14SAndroid Build Coastguard Worker     otError           error;
935*cfb92d14SAndroid Build Coastguard Worker     otMdnsIterator   *iterator = nullptr;
936*cfb92d14SAndroid Build Coastguard Worker     otMdnsCacheInfo   info;
937*cfb92d14SAndroid Build Coastguard Worker     otMdnsSrvResolver resolver;
938*cfb92d14SAndroid Build Coastguard Worker 
939*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS);
940*cfb92d14SAndroid Build Coastguard Worker 
941*cfb92d14SAndroid Build Coastguard Worker     iterator = otMdnsAllocateIterator(GetInstancePtr());
942*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(iterator != nullptr, error = OT_ERROR_NO_BUFS);
943*cfb92d14SAndroid Build Coastguard Worker 
944*cfb92d14SAndroid Build Coastguard Worker     while (true)
945*cfb92d14SAndroid Build Coastguard Worker     {
946*cfb92d14SAndroid Build Coastguard Worker         error = otMdnsGetNextSrvResolver(GetInstancePtr(), iterator, &resolver, &info);
947*cfb92d14SAndroid Build Coastguard Worker 
948*cfb92d14SAndroid Build Coastguard Worker         if (error == OT_ERROR_NOT_FOUND)
949*cfb92d14SAndroid Build Coastguard Worker         {
950*cfb92d14SAndroid Build Coastguard Worker             error = OT_ERROR_NONE;
951*cfb92d14SAndroid Build Coastguard Worker             ExitNow();
952*cfb92d14SAndroid Build Coastguard Worker         }
953*cfb92d14SAndroid Build Coastguard Worker 
954*cfb92d14SAndroid Build Coastguard Worker         SuccessOrExit(error);
955*cfb92d14SAndroid Build Coastguard Worker 
956*cfb92d14SAndroid Build Coastguard Worker         OutputLine("SRV resolver %s for %s", resolver.mServiceInstance, resolver.mServiceType);
957*cfb92d14SAndroid Build Coastguard Worker         OutputCacheInfo(info);
958*cfb92d14SAndroid Build Coastguard Worker     }
959*cfb92d14SAndroid Build Coastguard Worker 
960*cfb92d14SAndroid Build Coastguard Worker exit:
961*cfb92d14SAndroid Build Coastguard Worker     if (iterator != nullptr)
962*cfb92d14SAndroid Build Coastguard Worker     {
963*cfb92d14SAndroid Build Coastguard Worker         otMdnsFreeIterator(GetInstancePtr(), iterator);
964*cfb92d14SAndroid Build Coastguard Worker     }
965*cfb92d14SAndroid Build Coastguard Worker 
966*cfb92d14SAndroid Build Coastguard Worker     return error;
967*cfb92d14SAndroid Build Coastguard Worker }
968*cfb92d14SAndroid Build Coastguard Worker 
Process(Arg aArgs[])969*cfb92d14SAndroid Build Coastguard Worker template <> otError Mdns::Process<Cmd("txtresolvers")>(Arg aArgs[])
970*cfb92d14SAndroid Build Coastguard Worker {
971*cfb92d14SAndroid Build Coastguard Worker     // mdns txtresolvers
972*cfb92d14SAndroid Build Coastguard Worker 
973*cfb92d14SAndroid Build Coastguard Worker     otError           error;
974*cfb92d14SAndroid Build Coastguard Worker     otMdnsIterator   *iterator = nullptr;
975*cfb92d14SAndroid Build Coastguard Worker     otMdnsCacheInfo   info;
976*cfb92d14SAndroid Build Coastguard Worker     otMdnsTxtResolver resolver;
977*cfb92d14SAndroid Build Coastguard Worker 
978*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS);
979*cfb92d14SAndroid Build Coastguard Worker 
980*cfb92d14SAndroid Build Coastguard Worker     iterator = otMdnsAllocateIterator(GetInstancePtr());
981*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(iterator != nullptr, error = OT_ERROR_NO_BUFS);
982*cfb92d14SAndroid Build Coastguard Worker 
983*cfb92d14SAndroid Build Coastguard Worker     while (true)
984*cfb92d14SAndroid Build Coastguard Worker     {
985*cfb92d14SAndroid Build Coastguard Worker         error = otMdnsGetNextTxtResolver(GetInstancePtr(), iterator, &resolver, &info);
986*cfb92d14SAndroid Build Coastguard Worker 
987*cfb92d14SAndroid Build Coastguard Worker         if (error == OT_ERROR_NOT_FOUND)
988*cfb92d14SAndroid Build Coastguard Worker         {
989*cfb92d14SAndroid Build Coastguard Worker             error = OT_ERROR_NONE;
990*cfb92d14SAndroid Build Coastguard Worker             ExitNow();
991*cfb92d14SAndroid Build Coastguard Worker         }
992*cfb92d14SAndroid Build Coastguard Worker 
993*cfb92d14SAndroid Build Coastguard Worker         SuccessOrExit(error);
994*cfb92d14SAndroid Build Coastguard Worker 
995*cfb92d14SAndroid Build Coastguard Worker         OutputLine("TXT resolver %s for %s", resolver.mServiceInstance, resolver.mServiceType);
996*cfb92d14SAndroid Build Coastguard Worker         OutputCacheInfo(info);
997*cfb92d14SAndroid Build Coastguard Worker     }
998*cfb92d14SAndroid Build Coastguard Worker 
999*cfb92d14SAndroid Build Coastguard Worker exit:
1000*cfb92d14SAndroid Build Coastguard Worker     if (iterator != nullptr)
1001*cfb92d14SAndroid Build Coastguard Worker     {
1002*cfb92d14SAndroid Build Coastguard Worker         otMdnsFreeIterator(GetInstancePtr(), iterator);
1003*cfb92d14SAndroid Build Coastguard Worker     }
1004*cfb92d14SAndroid Build Coastguard Worker 
1005*cfb92d14SAndroid Build Coastguard Worker     return error;
1006*cfb92d14SAndroid Build Coastguard Worker }
1007*cfb92d14SAndroid Build Coastguard Worker 
Process(Arg aArgs[])1008*cfb92d14SAndroid Build Coastguard Worker template <> otError Mdns::Process<Cmd("ip6resolvers")>(Arg aArgs[])
1009*cfb92d14SAndroid Build Coastguard Worker {
1010*cfb92d14SAndroid Build Coastguard Worker     // mdns ip6resolvers
1011*cfb92d14SAndroid Build Coastguard Worker 
1012*cfb92d14SAndroid Build Coastguard Worker     otError               error;
1013*cfb92d14SAndroid Build Coastguard Worker     otMdnsIterator       *iterator = nullptr;
1014*cfb92d14SAndroid Build Coastguard Worker     otMdnsCacheInfo       info;
1015*cfb92d14SAndroid Build Coastguard Worker     otMdnsAddressResolver resolver;
1016*cfb92d14SAndroid Build Coastguard Worker 
1017*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS);
1018*cfb92d14SAndroid Build Coastguard Worker 
1019*cfb92d14SAndroid Build Coastguard Worker     iterator = otMdnsAllocateIterator(GetInstancePtr());
1020*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(iterator != nullptr, error = OT_ERROR_NO_BUFS);
1021*cfb92d14SAndroid Build Coastguard Worker 
1022*cfb92d14SAndroid Build Coastguard Worker     while (true)
1023*cfb92d14SAndroid Build Coastguard Worker     {
1024*cfb92d14SAndroid Build Coastguard Worker         error = otMdnsGetNextIp6AddressResolver(GetInstancePtr(), iterator, &resolver, &info);
1025*cfb92d14SAndroid Build Coastguard Worker 
1026*cfb92d14SAndroid Build Coastguard Worker         if (error == OT_ERROR_NOT_FOUND)
1027*cfb92d14SAndroid Build Coastguard Worker         {
1028*cfb92d14SAndroid Build Coastguard Worker             error = OT_ERROR_NONE;
1029*cfb92d14SAndroid Build Coastguard Worker             ExitNow();
1030*cfb92d14SAndroid Build Coastguard Worker         }
1031*cfb92d14SAndroid Build Coastguard Worker 
1032*cfb92d14SAndroid Build Coastguard Worker         SuccessOrExit(error);
1033*cfb92d14SAndroid Build Coastguard Worker 
1034*cfb92d14SAndroid Build Coastguard Worker         OutputLine("IPv6 address resolver %s", resolver.mHostName);
1035*cfb92d14SAndroid Build Coastguard Worker         OutputCacheInfo(info);
1036*cfb92d14SAndroid Build Coastguard Worker     }
1037*cfb92d14SAndroid Build Coastguard Worker 
1038*cfb92d14SAndroid Build Coastguard Worker exit:
1039*cfb92d14SAndroid Build Coastguard Worker     if (iterator != nullptr)
1040*cfb92d14SAndroid Build Coastguard Worker     {
1041*cfb92d14SAndroid Build Coastguard Worker         otMdnsFreeIterator(GetInstancePtr(), iterator);
1042*cfb92d14SAndroid Build Coastguard Worker     }
1043*cfb92d14SAndroid Build Coastguard Worker 
1044*cfb92d14SAndroid Build Coastguard Worker     return error;
1045*cfb92d14SAndroid Build Coastguard Worker }
1046*cfb92d14SAndroid Build Coastguard Worker 
Process(Arg aArgs[])1047*cfb92d14SAndroid Build Coastguard Worker template <> otError Mdns::Process<Cmd("ip4resolvers")>(Arg aArgs[])
1048*cfb92d14SAndroid Build Coastguard Worker {
1049*cfb92d14SAndroid Build Coastguard Worker     // mdns ip4resolvers
1050*cfb92d14SAndroid Build Coastguard Worker 
1051*cfb92d14SAndroid Build Coastguard Worker     otError               error;
1052*cfb92d14SAndroid Build Coastguard Worker     otMdnsIterator       *iterator = nullptr;
1053*cfb92d14SAndroid Build Coastguard Worker     otMdnsCacheInfo       info;
1054*cfb92d14SAndroid Build Coastguard Worker     otMdnsAddressResolver resolver;
1055*cfb92d14SAndroid Build Coastguard Worker 
1056*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS);
1057*cfb92d14SAndroid Build Coastguard Worker 
1058*cfb92d14SAndroid Build Coastguard Worker     iterator = otMdnsAllocateIterator(GetInstancePtr());
1059*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(iterator != nullptr, error = OT_ERROR_NO_BUFS);
1060*cfb92d14SAndroid Build Coastguard Worker 
1061*cfb92d14SAndroid Build Coastguard Worker     while (true)
1062*cfb92d14SAndroid Build Coastguard Worker     {
1063*cfb92d14SAndroid Build Coastguard Worker         error = otMdnsGetNextIp4AddressResolver(GetInstancePtr(), iterator, &resolver, &info);
1064*cfb92d14SAndroid Build Coastguard Worker 
1065*cfb92d14SAndroid Build Coastguard Worker         if (error == OT_ERROR_NOT_FOUND)
1066*cfb92d14SAndroid Build Coastguard Worker         {
1067*cfb92d14SAndroid Build Coastguard Worker             error = OT_ERROR_NONE;
1068*cfb92d14SAndroid Build Coastguard Worker             ExitNow();
1069*cfb92d14SAndroid Build Coastguard Worker         }
1070*cfb92d14SAndroid Build Coastguard Worker 
1071*cfb92d14SAndroid Build Coastguard Worker         SuccessOrExit(error);
1072*cfb92d14SAndroid Build Coastguard Worker 
1073*cfb92d14SAndroid Build Coastguard Worker         OutputLine("IPv4 address resolver %s", resolver.mHostName);
1074*cfb92d14SAndroid Build Coastguard Worker         OutputCacheInfo(info);
1075*cfb92d14SAndroid Build Coastguard Worker     }
1076*cfb92d14SAndroid Build Coastguard Worker 
1077*cfb92d14SAndroid Build Coastguard Worker exit:
1078*cfb92d14SAndroid Build Coastguard Worker     if (iterator != nullptr)
1079*cfb92d14SAndroid Build Coastguard Worker     {
1080*cfb92d14SAndroid Build Coastguard Worker         otMdnsFreeIterator(GetInstancePtr(), iterator);
1081*cfb92d14SAndroid Build Coastguard Worker     }
1082*cfb92d14SAndroid Build Coastguard Worker 
1083*cfb92d14SAndroid Build Coastguard Worker     return error;
1084*cfb92d14SAndroid Build Coastguard Worker }
1085*cfb92d14SAndroid Build Coastguard Worker 
1086*cfb92d14SAndroid Build Coastguard Worker #endif // OPENTHREAD_CONFIG_MULTICAST_DNS_ENTRY_ITERATION_API_ENABLE
1087*cfb92d14SAndroid Build Coastguard Worker 
Process(Arg aArgs[])1088*cfb92d14SAndroid Build Coastguard Worker otError Mdns::Process(Arg aArgs[])
1089*cfb92d14SAndroid Build Coastguard Worker {
1090*cfb92d14SAndroid Build Coastguard Worker #define CmdEntry(aCommandString)                            \
1091*cfb92d14SAndroid Build Coastguard Worker     {                                                       \
1092*cfb92d14SAndroid Build Coastguard Worker         aCommandString, &Mdns::Process<Cmd(aCommandString)> \
1093*cfb92d14SAndroid Build Coastguard Worker     }
1094*cfb92d14SAndroid Build Coastguard Worker 
1095*cfb92d14SAndroid Build Coastguard Worker     static constexpr Command kCommands[] = {
1096*cfb92d14SAndroid Build Coastguard Worker         CmdEntry("browser"),
1097*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_MULTICAST_DNS_ENTRY_ITERATION_API_ENABLE
1098*cfb92d14SAndroid Build Coastguard Worker         CmdEntry("browsers"),
1099*cfb92d14SAndroid Build Coastguard Worker #endif
1100*cfb92d14SAndroid Build Coastguard Worker         CmdEntry("disable"),
1101*cfb92d14SAndroid Build Coastguard Worker         CmdEntry("enable"),
1102*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_MULTICAST_DNS_ENTRY_ITERATION_API_ENABLE
1103*cfb92d14SAndroid Build Coastguard Worker         CmdEntry("hosts"),
1104*cfb92d14SAndroid Build Coastguard Worker #endif
1105*cfb92d14SAndroid Build Coastguard Worker         CmdEntry("ip4resolver"),
1106*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_MULTICAST_DNS_ENTRY_ITERATION_API_ENABLE
1107*cfb92d14SAndroid Build Coastguard Worker         CmdEntry("ip4resolvers"),
1108*cfb92d14SAndroid Build Coastguard Worker #endif
1109*cfb92d14SAndroid Build Coastguard Worker         CmdEntry("ip6resolver"),
1110*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_MULTICAST_DNS_ENTRY_ITERATION_API_ENABLE
1111*cfb92d14SAndroid Build Coastguard Worker         CmdEntry("ip6resolvers"),
1112*cfb92d14SAndroid Build Coastguard Worker         CmdEntry("keys"),
1113*cfb92d14SAndroid Build Coastguard Worker #endif
1114*cfb92d14SAndroid Build Coastguard Worker         CmdEntry("register"),
1115*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_MULTICAST_DNS_ENTRY_ITERATION_API_ENABLE
1116*cfb92d14SAndroid Build Coastguard Worker         CmdEntry("services"),
1117*cfb92d14SAndroid Build Coastguard Worker #endif
1118*cfb92d14SAndroid Build Coastguard Worker         CmdEntry("srvresolver"),
1119*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_MULTICAST_DNS_ENTRY_ITERATION_API_ENABLE
1120*cfb92d14SAndroid Build Coastguard Worker         CmdEntry("srvresolvers"),
1121*cfb92d14SAndroid Build Coastguard Worker #endif
1122*cfb92d14SAndroid Build Coastguard Worker         CmdEntry("state"),
1123*cfb92d14SAndroid Build Coastguard Worker         CmdEntry("txtresolver"),
1124*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_MULTICAST_DNS_ENTRY_ITERATION_API_ENABLE
1125*cfb92d14SAndroid Build Coastguard Worker         CmdEntry("txtresolvers"),
1126*cfb92d14SAndroid Build Coastguard Worker #endif
1127*cfb92d14SAndroid Build Coastguard Worker         CmdEntry("unicastquestion"),
1128*cfb92d14SAndroid Build Coastguard Worker         CmdEntry("unregister"),
1129*cfb92d14SAndroid Build Coastguard Worker     };
1130*cfb92d14SAndroid Build Coastguard Worker 
1131*cfb92d14SAndroid Build Coastguard Worker #undef CmdEntry
1132*cfb92d14SAndroid Build Coastguard Worker 
1133*cfb92d14SAndroid Build Coastguard Worker     static_assert(BinarySearch::IsSorted(kCommands), "kCommands is not sorted");
1134*cfb92d14SAndroid Build Coastguard Worker 
1135*cfb92d14SAndroid Build Coastguard Worker     otError        error = OT_ERROR_INVALID_COMMAND;
1136*cfb92d14SAndroid Build Coastguard Worker     const Command *command;
1137*cfb92d14SAndroid Build Coastguard Worker 
1138*cfb92d14SAndroid Build Coastguard Worker     if (aArgs[0].IsEmpty() || (aArgs[0] == "help"))
1139*cfb92d14SAndroid Build Coastguard Worker     {
1140*cfb92d14SAndroid Build Coastguard Worker         OutputCommandTable(kCommands);
1141*cfb92d14SAndroid Build Coastguard Worker         ExitNow(error = aArgs[0].IsEmpty() ? error : OT_ERROR_NONE);
1142*cfb92d14SAndroid Build Coastguard Worker     }
1143*cfb92d14SAndroid Build Coastguard Worker 
1144*cfb92d14SAndroid Build Coastguard Worker     command = BinarySearch::Find(aArgs[0].GetCString(), kCommands);
1145*cfb92d14SAndroid Build Coastguard Worker     VerifyOrExit(command != nullptr);
1146*cfb92d14SAndroid Build Coastguard Worker 
1147*cfb92d14SAndroid Build Coastguard Worker     error = (this->*command->mHandler)(aArgs + 1);
1148*cfb92d14SAndroid Build Coastguard Worker 
1149*cfb92d14SAndroid Build Coastguard Worker exit:
1150*cfb92d14SAndroid Build Coastguard Worker     return error;
1151*cfb92d14SAndroid Build Coastguard Worker }
1152*cfb92d14SAndroid Build Coastguard Worker 
1153*cfb92d14SAndroid Build Coastguard Worker } // namespace Cli
1154*cfb92d14SAndroid Build Coastguard Worker } // namespace ot
1155*cfb92d14SAndroid Build Coastguard Worker 
1156*cfb92d14SAndroid Build Coastguard Worker #endif // OPENTHREAD_CONFIG_MULTICAST_DNS_ENABLE && OPENTHREAD_CONFIG_MULTICAST_DNS_PUBLIC_API_ENABLE
1157