@@ -267,6 +267,22 @@ PublisherMDnsSd::~PublisherMDnsSd(void)
267267otbrError PublisherMDnsSd::Start (void )
268268{
269269 mState = State::kReady ;
270+
271+ for (auto kv : mServiceRegistrations )
272+ {
273+ static_cast <DnssdServiceRegistration &>(*kv.second ).Register ();
274+ }
275+
276+ for (auto kv : mHostRegistrations )
277+ {
278+ static_cast <DnssdHostRegistration &>(*kv.second ).Register ();
279+ }
280+
281+ for (auto kv : mKeyRegistrations )
282+ {
283+ static_cast <DnssdKeyRegistration &>(*kv.second ).Register ();
284+ }
285+
270286 mStateCallback (State::kReady );
271287 return OTBR_ERROR_NONE;
272288}
@@ -289,18 +305,32 @@ void PublisherMDnsSd::Stop(StopMode aStopMode)
289305 switch (aStopMode)
290306 {
291307 case kNormalStop :
308+ mServiceRegistrations .clear ();
309+ mHostRegistrations .clear ();
310+ mKeyRegistrations .clear ();
311+ DeallocateHostsRef ();
292312 break ;
293313
294314 case kStopOnServiceNotRunningError :
315+ for (auto kv : mServiceRegistrations )
316+ {
317+ static_cast <DnssdServiceRegistration &>(*kv.second ).Unregister ();
318+ }
319+
320+ for (auto kv : mHostRegistrations )
321+ {
322+ static_cast <DnssdHostRegistration &>(*kv.second ).Unregister ();
323+ }
324+
325+ for (auto kv : mKeyRegistrations )
326+ {
327+ static_cast <DnssdKeyRegistration &>(*kv.second ).Unregister ();
328+ }
329+
295330 DeallocateHostsRef ();
296331 break ;
297332 }
298333
299- mServiceRegistrations .clear ();
300- mHostRegistrations .clear ();
301- mKeyRegistrations .clear ();
302- DeallocateHostsRef ();
303-
304334 mSubscribedServices .clear ();
305335 mSubscribedHosts .clear ();
306336
@@ -340,7 +370,7 @@ void PublisherMDnsSd::Update(MainloopContext &aMainloop)
340370{
341371 mTaskRunner .Update (aMainloop);
342372
343- for (auto & kv : mServiceRegistrations )
373+ for (auto kv : mServiceRegistrations )
344374 {
345375 auto &serviceReg = static_cast <DnssdServiceRegistration &>(*kv.second );
346376
@@ -351,20 +381,29 @@ void PublisherMDnsSd::Update(MainloopContext &aMainloop)
351381 {
352382 int fd = DNSServiceRefSockFD (mHostsRef );
353383
354- assert (fd != -1 );
384+ if (fd == -1 )
385+ {
386+ otbrLogWarning (" mDNS connection socket is invalid, reconnecting..." );
387+ Stop (kStopOnServiceNotRunningError );
388+ Start ();
389+ ExitNow ();
390+ }
355391
356392 aMainloop.AddFdToReadSet (fd);
357393 }
358394
359- for (const auto & service : mSubscribedServices )
395+ for (auto service : mSubscribedServices )
360396 {
361397 service->UpdateAll (aMainloop);
362398 }
363399
364- for (const auto & host : mSubscribedHosts )
400+ for (auto host : mSubscribedHosts )
365401 {
366402 host->Update (aMainloop);
367403 }
404+
405+ exit:
406+ return ;
368407}
369408
370409void PublisherMDnsSd::Process (const MainloopContext &aMainloop)
@@ -373,7 +412,7 @@ void PublisherMDnsSd::Process(const MainloopContext &aMainloop)
373412
374413 mTaskRunner .Process (aMainloop);
375414
376- for (auto & kv : mServiceRegistrations )
415+ for (auto kv : mServiceRegistrations )
377416 {
378417 auto &serviceReg = static_cast <DnssdServiceRegistration &>(*kv.second );
379418
@@ -384,18 +423,26 @@ void PublisherMDnsSd::Process(const MainloopContext &aMainloop)
384423 {
385424 int fd = DNSServiceRefSockFD (mHostsRef );
386425
426+ if (fd == -1 )
427+ {
428+ otbrLogWarning (" mDNS connection socket is invalid, reconnecting..." );
429+ Stop (kStopOnServiceNotRunningError );
430+ Start ();
431+ ExitNow ();
432+ }
433+
387434 if (FD_ISSET (fd, &aMainloop.mReadFdSet ))
388435 {
389436 mServiceRefsToProcess .push_back (mHostsRef );
390437 }
391438 }
392439
393- for (const auto & service : mSubscribedServices )
440+ for (auto service : mSubscribedServices )
394441 {
395442 service->ProcessAll (aMainloop, mServiceRefsToProcess );
396443 }
397444
398- for (const auto & host : mSubscribedHosts )
445+ for (auto host : mSubscribedHosts )
399446 {
400447 host->Process (aMainloop, mServiceRefsToProcess );
401448 }
@@ -428,14 +475,15 @@ void PublisherMDnsSd::Process(const MainloopContext &aMainloop)
428475 otbrLog (logLevel, OTBR_LOG_TAG, " DNSServiceProcessResult failed: %s (serviceRef = %p)" ,
429476 DNSErrorToString (error), serviceRef);
430477 }
431- if (error == kDNSServiceErr_ServiceNotRunning )
478+ if (IsRetryableError ( error) )
432479 {
433- otbrLogWarning (" Need to reconnect to mdnsd" );
480+ otbrLogWarning (" mDNS error %d, need to reconnect to mdnsd" , error );
434481 Stop (kStopOnServiceNotRunningError );
435482 Start ();
436483 ExitNow ();
437484 }
438485 }
486+
439487exit:
440488 return ;
441489}
@@ -458,7 +506,13 @@ void PublisherMDnsSd::DnssdServiceRegistration::Update(MainloopContext &aMainloo
458506 VerifyOrExit (mServiceRef != nullptr );
459507
460508 fd = DNSServiceRefSockFD (mServiceRef );
461- VerifyOrExit (fd != -1 );
509+ if (fd == -1 )
510+ {
511+ otbrLogWarning (" mDNS connection socket is invalid, reconnecting..." );
512+ GetPublisher ().Stop (kStopOnServiceNotRunningError );
513+ GetPublisher ().Start ();
514+ ExitNow ();
515+ }
462516
463517 aMainloop.AddFdToReadSet (fd);
464518
@@ -474,7 +528,13 @@ void PublisherMDnsSd::DnssdServiceRegistration::Process(const MainloopContext
474528 VerifyOrExit (mServiceRef != nullptr );
475529
476530 fd = DNSServiceRefSockFD (mServiceRef );
477- VerifyOrExit (fd != -1 );
531+ if (fd == -1 )
532+ {
533+ otbrLogWarning (" mDNS connection socket is invalid, reconnecting..." );
534+ GetPublisher ().Stop (kStopOnServiceNotRunningError );
535+ GetPublisher ().Start ();
536+ ExitNow ();
537+ }
478538
479539 VerifyOrExit (FD_ISSET (fd, &aMainloop.mReadFdSet ));
480540 aReadyServices.push_back (mServiceRef );
@@ -1120,7 +1180,13 @@ void PublisherMDnsSd::ServiceRef::Update(MainloopContext &aMainloop) const
11201180 VerifyOrExit (mServiceRef != nullptr );
11211181
11221182 fd = DNSServiceRefSockFD (mServiceRef );
1123- assert (fd != -1 );
1183+ if (fd == -1 )
1184+ {
1185+ otbrLogWarning (" mDNS connection socket is invalid, reconnecting..." );
1186+ mPublisher .Stop (kStopOnServiceNotRunningError );
1187+ mPublisher .Start ();
1188+ ExitNow ();
1189+ }
11241190 aMainloop.AddFdToReadSet (fd);
11251191exit:
11261192 return ;
@@ -1134,7 +1200,13 @@ void PublisherMDnsSd::ServiceRef::Process(const MainloopContext &aMainloop,
11341200 VerifyOrExit (mServiceRef != nullptr );
11351201
11361202 fd = DNSServiceRefSockFD (mServiceRef );
1137- assert (fd != -1 );
1203+ if (fd == -1 )
1204+ {
1205+ otbrLogWarning (" mDNS connection socket is invalid, reconnecting..." );
1206+ mPublisher .Stop (kStopOnServiceNotRunningError );
1207+ mPublisher .Start ();
1208+ ExitNow ();
1209+ }
11381210 if (FD_ISSET (fd, &aMainloop.mReadFdSet ))
11391211 {
11401212 aReadyServices.push_back (mServiceRef );
@@ -1251,7 +1323,7 @@ void PublisherMDnsSd::ServiceSubscription::UpdateAll(MainloopContext &aMainloop)
12511323{
12521324 Update (aMainloop);
12531325
1254- for (const auto & instance : mResolvingInstances )
1326+ for (auto instance : mResolvingInstances )
12551327 {
12561328 instance->Update (aMainloop);
12571329 }
@@ -1262,7 +1334,7 @@ void PublisherMDnsSd::ServiceSubscription::ProcessAll(const MainloopContext
12621334{
12631335 Process (aMainloop, aReadyServices);
12641336
1265- for (const auto & instance : mResolvingInstances )
1337+ for (auto instance : mResolvingInstances )
12661338 {
12671339 instance->Process (aMainloop, aReadyServices);
12681340 }
0 commit comments