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