@@ -32,12 +32,8 @@ using namespace chip::Dnssd::Internal;
32
32
33
33
namespace {
34
34
35
- constexpr char kLocalDot [] = " local." ;
36
-
37
- constexpr char kSrpDot [] = " default.service.arpa." ;
38
-
39
- // The extra time in milliseconds that we will wait for the resolution on the srp domain to complete.
40
- constexpr uint16_t kSrpTimeoutInMsec = 250 ;
35
+ // The extra time in milliseconds that we will wait for the resolution on the open thread domain to complete.
36
+ constexpr uint16_t kOpenThreadTimeoutInMsec = 250 ;
41
37
42
38
constexpr DNSServiceFlags kRegisterFlags = kDNSServiceFlagsNoAutoRename ;
43
39
constexpr DNSServiceFlags kBrowseFlags = kDNSServiceFlagsShareConnection ;
@@ -148,57 +144,59 @@ std::string GetDomainFromHostName(const char * hostnameWithDomain)
148
144
{
149
145
std::string hostname = std::string (hostnameWithDomain);
150
146
151
- // Find the first occurence of '.'
152
- size_t first_pos = hostname.find (" ." );
153
-
154
- // if not found, return empty string
155
- VerifyOrReturnValue (first_pos != std::string::npos, std::string ());
147
+ // Find the last occurence of '.'
148
+ size_t last_pos = hostname.find_last_of (" ." );
149
+ if (last_pos != std::string::npos)
150
+ {
151
+ // Get a substring without last '.'
152
+ std::string substring = hostname.substr (0 , last_pos);
156
153
157
- // Get a substring after the first occurence of '.' to the end of the string
158
- return hostname.substr (first_pos + 1 , hostname.size ());
154
+ // Find the last occurence of '.' in the substring created above.
155
+ size_t pos = substring.find_last_of (" ." );
156
+ if (pos != std::string::npos)
157
+ {
158
+ // Return the domain name between the last 2 occurences of '.' including the trailing dot'.'.
159
+ return std::string (hostname.substr (pos + 1 , last_pos));
160
+ }
161
+ }
162
+ return std::string ();
159
163
}
160
164
165
+ Global<MdnsContexts> MdnsContexts::sInstance ;
166
+
167
+ namespace {
168
+
161
169
/* *
162
- * @brief Callback that is called when the timeout for resolving on the kSrpDot domain has expired.
170
+ * @brief Callback that is called when the timeout for resolving on the kOpenThreadDot domain has expired.
163
171
*
164
172
* @param[in] systemLayer The system layer.
165
173
* @param[in] callbackContext The context passed to the timer callback.
166
174
*/
167
- void SrpTimerExpiredCallback (System::Layer * systemLayer, void * callbackContext)
175
+ void OpenThreadTimerExpiredCallback (System::Layer * systemLayer, void * callbackContext)
168
176
{
169
- ChipLogProgress (Discovery, " Mdns: Timer expired for resolve to complete on the srp domain." );
177
+ ChipLogProgress (Discovery, " Mdns: Timer expired for resolve to complete on the open thread domain." );
170
178
auto sdCtx = static_cast <ResolveContext *>(callbackContext);
171
179
VerifyOrDie (sdCtx != nullptr );
172
- sdCtx->Finalize ();
173
- }
174
180
175
- /* *
176
- * @brief Starts a timer to wait for the resolution on the kSrpDot domain to happen.
177
- *
178
- * @param[in] timeoutSeconds The timeout in seconds.
179
- * @param[in] ResolveContext The resolve context.
180
- */
181
- CHIP_ERROR StartSrpTimer (uint16_t timeoutInMSecs, ResolveContext * ctx)
182
- {
183
- VerifyOrReturnValue (ctx != nullptr , CHIP_ERROR_INCORRECT_STATE);
184
- return DeviceLayer::SystemLayer ().StartTimer (System::Clock::Milliseconds16 (timeoutInMSecs), SrpTimerExpiredCallback,
185
- reinterpret_cast <void *>(ctx));
181
+ if (sdCtx->hasOpenThreadTimerStarted )
182
+ {
183
+ sdCtx->Finalize ();
184
+ }
186
185
}
187
186
188
187
/* *
189
- * @brief Cancels the timer that was started to wait for the resolution on the kSrpDot domain to happen.
188
+ * @brief Starts a timer to wait for the resolution on the kOpenThreadDot domain to happen.
190
189
*
190
+ * @param[in] timeoutSeconds The timeout in seconds.
191
191
* @param[in] ResolveContext The resolve context.
192
192
*/
193
- void CancelSrpTimer ( ResolveContext * ctx)
193
+ void StartOpenThreadTimer ( uint16_t timeoutInMSecs, ResolveContext * ctx)
194
194
{
195
- DeviceLayer::SystemLayer ().CancelTimer (SrpTimerExpiredCallback, reinterpret_cast <void *>(ctx));
195
+ VerifyOrReturn (ctx != nullptr , ChipLogError (Discovery, " Can't schedule open thread timer since context is null" ));
196
+ DeviceLayer::SystemLayer ().StartTimer (System::Clock::Milliseconds16 (timeoutInMSecs), OpenThreadTimerExpiredCallback,
197
+ reinterpret_cast <void *>(ctx));
196
198
}
197
199
198
- Global<MdnsContexts> MdnsContexts::sInstance ;
199
-
200
- namespace {
201
-
202
200
static void OnRegister (DNSServiceRef sdRef, DNSServiceFlags flags, DNSServiceErrorType err, const char * name, const char * type,
203
201
const char * domain, void * context)
204
202
{
@@ -250,17 +248,17 @@ CHIP_ERROR Browse(BrowseHandler * sdCtx, uint32_t interfaceId, const char * type
250
248
auto err = DNSServiceCreateConnection (&sdCtx->serviceRef );
251
249
VerifyOrReturnError (kDNSServiceErr_NoError == err, sdCtx->Finalize (err));
252
250
253
- // We will browse on both the local domain and the srp domain.
251
+ // We will browse on both the local domain and the open thread domain.
254
252
ChipLogProgress (Discovery, " Browsing for: %s on domain %s" , StringOrNullMarker (type), kLocalDot );
255
253
256
254
auto sdRefLocal = sdCtx->serviceRef ; // Mandatory copy because of kDNSServiceFlagsShareConnection
257
255
err = DNSServiceBrowse (&sdRefLocal, kBrowseFlags , interfaceId, type, kLocalDot , OnBrowse, sdCtx);
258
256
VerifyOrReturnError (kDNSServiceErr_NoError == err, sdCtx->Finalize (err));
259
257
260
- ChipLogProgress (Discovery, " Browsing for: %s on domain %s" , StringOrNullMarker (type), kSrpDot );
258
+ ChipLogProgress (Discovery, " Browsing for: %s on domain %s" , StringOrNullMarker (type), kOpenThreadDot );
261
259
262
- auto sdRefSrp = sdCtx->serviceRef ; // Mandatory copy because of kDNSServiceFlagsShareConnection
263
- err = DNSServiceBrowse (&sdRefSrp , kBrowseFlags , interfaceId, type, kSrpDot , OnBrowse, sdCtx);
260
+ DNSServiceRef sdRefOpenThread = sdCtx->serviceRef ; // Mandatory copy because of kDNSServiceFlagsShareConnection
261
+ err = DNSServiceBrowse (&sdRefOpenThread , kBrowseFlags , interfaceId, type, kOpenThreadDot , OnBrowse, sdCtx);
264
262
VerifyOrReturnError (kDNSServiceErr_NoError == err, sdCtx->Finalize (err));
265
263
266
264
return MdnsContexts::GetInstance ().Add (sdCtx, sdCtx->serviceRef );
@@ -309,37 +307,25 @@ static void OnGetAddrInfo(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t i
309
307
{
310
308
VerifyOrReturn (sdCtx->HasAddress (), sdCtx->Finalize (kDNSServiceErr_BadState ));
311
309
312
- if (domainName.compare (kSrpDot ) == 0 )
310
+ if (domainName.compare (kOpenThreadDot ) == 0 )
313
311
{
314
- ChipLogProgress (Discovery, " Mdns: Resolve completed on the srp domain." );
315
-
316
- // Cancel the timer if one has been started
317
- if (sdCtx->hasSrpTimerStarted )
318
- {
319
- CancelSrpTimer (sdCtx);
320
- }
312
+ ChipLogProgress (Discovery, " Mdns: Resolve completed on the open thread domain." );
321
313
sdCtx->Finalize ();
322
314
}
323
315
else if (domainName.compare (kLocalDot ) == 0 )
324
316
{
325
- ChipLogProgress (Discovery,
326
- " Mdns: Resolve completed on the local domain. Starting a timer for the srp resolve to come back" );
317
+ ChipLogProgress (
318
+ Discovery,
319
+ " Mdns: Resolve completed on the local domain. Starting a timer for the open thread resolve to come back" );
327
320
328
- // Usually the resolution on the local domain is quicker than on the srp domain. We would like to give the
329
- // resolution on the srp domain around 250 millisecs more to give it a chance to resolve before finalizing
321
+ // Usually the resolution on the local domain is quicker than on the open thread domain. We would like to give the
322
+ // resolution on the open thread domain around 250 millisecs more to give it a chance to resolve before finalizing
330
323
// the resolution.
331
- if (!sdCtx->hasSrpTimerStarted )
324
+ if (!sdCtx->hasOpenThreadTimerStarted )
332
325
{
333
- // Schedule a timer to allow the resolve on Srp domain to complete.
334
- CHIP_ERROR error = StartSrpTimer (kSrpTimeoutInMsec , sdCtx);
335
-
336
- // If the timer fails to start, finalize the context and return.
337
- if (error != CHIP_NO_ERROR)
338
- {
339
- sdCtx->Finalize ();
340
- return ;
341
- }
342
- sdCtx->hasSrpTimerStarted = true ;
326
+ // Schedule a timer to allow the resolve on OpenThread domain to complete.
327
+ StartOpenThreadTimer (kOpenThreadTimeoutInMsec , sdCtx);
328
+ sdCtx->hasOpenThreadTimerStarted = true ;
343
329
}
344
330
}
345
331
}
@@ -381,7 +367,8 @@ static void OnResolve(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t inter
381
367
if (!sdCtx->isResolveRequested )
382
368
{
383
369
GetAddrInfo (sdCtx);
384
- sdCtx->isResolveRequested = true ;
370
+ sdCtx->isResolveRequested = true ;
371
+ sdCtx->hasOpenThreadTimerStarted = false ;
385
372
}
386
373
}
387
374
}
@@ -395,13 +382,13 @@ static CHIP_ERROR Resolve(ResolveContext * sdCtx, uint32_t interfaceId, chip::In
395
382
auto err = DNSServiceCreateConnection (&sdCtx->serviceRef );
396
383
VerifyOrReturnError (kDNSServiceErr_NoError == err, sdCtx->Finalize (err));
397
384
398
- // Similar to browse, will try to resolve using both the local domain and the srp domain.
385
+ // Similar to browse, will try to resolve using both the local domain and the open thread domain.
399
386
auto sdRefLocal = sdCtx->serviceRef ; // Mandatory copy because of kDNSServiceFlagsShareConnection
400
387
err = DNSServiceResolve (&sdRefLocal, kResolveFlags , interfaceId, name, type, kLocalDot , OnResolve, sdCtx);
401
388
VerifyOrReturnError (kDNSServiceErr_NoError == err, sdCtx->Finalize (err));
402
389
403
- auto sdRefSrp = sdCtx->serviceRef ; // Mandatory copy because of kDNSServiceFlagsShareConnection
404
- err = DNSServiceResolve (&sdRefSrp , kResolveFlags , interfaceId, name, type, kSrpDot , OnResolve, sdCtx);
390
+ auto sdRefOpenThread = sdCtx->serviceRef ; // Mandatory copy because of kDNSServiceFlagsShareConnection
391
+ err = DNSServiceResolve (&sdRefOpenThread , kResolveFlags , interfaceId, name, type, kOpenThreadDot , OnResolve, sdCtx);
405
392
VerifyOrReturnError (kDNSServiceErr_NoError == err, sdCtx->Finalize (err));
406
393
407
394
auto retval = MdnsContexts::GetInstance ().Add (sdCtx, sdCtx->serviceRef );
0 commit comments