Skip to content

Commit 535844a

Browse files
committed
[mdns] fix service loss on mDNSResponder daemon restart
1 parent 1f9573f commit 535844a

3 files changed

Lines changed: 106 additions & 35 deletions

File tree

src/host/ncp_spinel.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ void NcpSpinel::HandleValueInserted(spinel_prop_key_t aKey, const uint8_t *aBuff
667667
callbackDataCopy.assign(callbackData, callbackData + callbackDataSize);
668668

669669
mPublisher->PublishHost(host.mHostName, addressList, [this, requestId, callbackDataCopy](otbrError aError) {
670-
IgnoreError(SendDnssdResult(requestId, callbackDataCopy, OtbrErrorToOtError(aError)));
670+
OT_UNUSED_VARIABLE(SendDnssdResult(requestId, callbackDataCopy, OtbrErrorToOtError(aError)));
671671
});
672672
break;
673673
}
@@ -694,7 +694,7 @@ void NcpSpinel::HandleValueInserted(spinel_prop_key_t aKey, const uint8_t *aBuff
694694

695695
mPublisher->PublishService(service.mHostName, service.mServiceInstance, service.mServiceType, subTypeList,
696696
service.mPort, txtData, [this, requestId, callbackDataCopy](otbrError aError) {
697-
IgnoreError(
697+
OT_UNUSED_VARIABLE(
698698
SendDnssdResult(requestId, callbackDataCopy, OtbrErrorToOtError(aError)));
699699
});
700700
break;
@@ -713,7 +713,7 @@ void NcpSpinel::HandleValueInserted(spinel_prop_key_t aKey, const uint8_t *aBuff
713713
callbackDataCopy.assign(callbackData, callbackData + callbackDataSize);
714714

715715
mPublisher->PublishKey(KeyNameFor(key), keyData, [this, requestId, callbackDataCopy](otbrError aError) {
716-
IgnoreError(SendDnssdResult(requestId, callbackDataCopy, OtbrErrorToOtError(aError)));
716+
OT_UNUSED_VARIABLE(SendDnssdResult(requestId, callbackDataCopy, OtbrErrorToOtError(aError)));
717717
});
718718
break;
719719
}
@@ -732,7 +732,7 @@ void NcpSpinel::HandleValueInserted(spinel_prop_key_t aKey, const uint8_t *aBuff
732732
DnssdPlatform::Get().StartServiceBrowser(browser,
733733
std::make_shared<DnssdPlatform::StdBrowseCallback>(
734734
[this, callbackDataCopy](const otPlatDnssdBrowseResult &aResult) {
735-
IgnoreError(SendDnssdBrowseResult(aResult, callbackDataCopy));
735+
SendDnssdBrowseResult(aResult, callbackDataCopy);
736736
},
737737
mDiscoveryProxyId++));
738738
break;
@@ -780,7 +780,7 @@ void NcpSpinel::HandleValueRemoved(spinel_prop_key_t aKey, const uint8_t *aBuffe
780780
callbackDataCopy.assign(callbackData, callbackData + callbackDataSize);
781781

782782
mPublisher->UnpublishHost(host.mHostName, [this, requestId, callbackDataCopy](otbrError aError) {
783-
IgnoreError(SendDnssdResult(requestId, callbackDataCopy, OtbrErrorToOtError(aError)));
783+
OT_UNUSED_VARIABLE(SendDnssdResult(requestId, callbackDataCopy, OtbrErrorToOtError(aError)));
784784
});
785785
break;
786786
}
@@ -799,8 +799,8 @@ void NcpSpinel::HandleValueRemoved(spinel_prop_key_t aKey, const uint8_t *aBuffe
799799
callbackDataCopy.assign(callbackData, callbackData + callbackDataSize);
800800

801801
mPublisher->UnpublishService(
802-
service.mHostName, service.mServiceType, [this, requestId, callbackDataCopy](otbrError aError) {
803-
IgnoreError(SendDnssdResult(requestId, callbackDataCopy, OtbrErrorToOtError(aError)));
802+
service.mServiceInstance, service.mServiceType, [this, requestId, callbackDataCopy](otbrError aError) {
803+
OT_UNUSED_VARIABLE(SendDnssdResult(requestId, callbackDataCopy, OtbrErrorToOtError(aError)));
804804
});
805805
break;
806806
}
@@ -816,7 +816,7 @@ void NcpSpinel::HandleValueRemoved(spinel_prop_key_t aKey, const uint8_t *aBuffe
816816
callbackDataCopy.assign(callbackData, callbackData + callbackDataSize);
817817

818818
mPublisher->UnpublishKey(KeyNameFor(key), [this, requestId, callbackDataCopy](otbrError aError) {
819-
IgnoreError(SendDnssdResult(requestId, callbackDataCopy, OtbrErrorToOtError(aError)));
819+
OT_UNUSED_VARIABLE(SendDnssdResult(requestId, callbackDataCopy, OtbrErrorToOtError(aError)));
820820
});
821821
break;
822822
}

src/mdns/mdns_mdnssd.cpp

Lines changed: 92 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,22 @@ PublisherMDnsSd::~PublisherMDnsSd(void)
267267
otbrError 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

370409
void 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+
439487
exit:
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);
11251191
exit:
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
}

src/mdns/mdns_mdnssd.hpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ class PublisherMDnsSd : public MainloopProcessor, public Publisher
128128
void Update(MainloopContext &aMainloop) const;
129129
void Process(const MainloopContext &aMainloop, std::vector<DNSServiceRef> &aReadyServices) const;
130130
otbrError Register(void);
131+
void Unregister(void);
131132

132133
private:
133-
void Unregister(void);
134-
PublisherMDnsSd &GetPublisher(void) { return *static_cast<PublisherMDnsSd *>(mPublisher); }
134+
PublisherMDnsSd &GetPublisher(void) const { return *static_cast<PublisherMDnsSd *>(mPublisher); }
135135
void HandleRegisterResult(DNSServiceFlags aFlags, DNSServiceErrorType aError);
136136
static void HandleRegisterResult(DNSServiceRef aServiceRef,
137137
DNSServiceFlags aFlags,
@@ -153,10 +153,10 @@ class PublisherMDnsSd : public MainloopProcessor, public Publisher
153153
~DnssdHostRegistration(void) override { Unregister(); }
154154

155155
otbrError Register(void);
156+
void Unregister(void);
156157

157158
private:
158-
void Unregister(void);
159-
PublisherMDnsSd &GetPublisher(void) { return *static_cast<PublisherMDnsSd *>(mPublisher); }
159+
PublisherMDnsSd &GetPublisher(void) const { return *static_cast<PublisherMDnsSd *>(mPublisher); }
160160
void HandleRegisterResult(DNSRecordRef aRecordRef, DNSServiceErrorType aError);
161161
static void HandleRegisterResult(DNSServiceRef aServiceRef,
162162
DNSRecordRef aRecordRef,
@@ -178,10 +178,10 @@ class PublisherMDnsSd : public MainloopProcessor, public Publisher
178178
~DnssdKeyRegistration(void) override { Unregister(); }
179179

180180
otbrError Register(void);
181+
void Unregister(void);
181182

182183
private:
183-
void Unregister(void);
184-
PublisherMDnsSd &GetPublisher(void) { return *static_cast<PublisherMDnsSd *>(mPublisher); }
184+
PublisherMDnsSd &GetPublisher(void) const { return *static_cast<PublisherMDnsSd *>(mPublisher); }
185185
void HandleRegisterResult(DNSServiceErrorType aError);
186186
static void HandleRegisterResult(DNSServiceRef aServiceRef,
187187
DNSRecordRef aRecordRef,
@@ -361,7 +361,6 @@ class PublisherMDnsSd : public MainloopProcessor, public Publisher
361361

362362
static std::string MakeRegType(const std::string &aType, SubTypeList aSubTypeList);
363363

364-
void Stop(StopMode aStopMode);
365364
DNSServiceErrorType CreateSharedHostsRef(void);
366365
void DeallocateHostsRef(void);
367366
void HandleServiceRefDeallocating(const DNSServiceRef &aServiceRef);

0 commit comments

Comments
 (0)