From 78c1714e8ea0908186574565c5a76d40a70dedfb Mon Sep 17 00:00:00 2001 From: Vibhav Pant Date: Tue, 29 Aug 2023 16:46:24 +0530 Subject: [PATCH] ServiceBrowser: Ignore local services. --- .../platform/implementation/linux/avahi.cc | 21 +++++++++++++------ .../platform/implementation/linux/avahi.h | 9 ++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/internal/platform/implementation/linux/avahi.cc b/internal/platform/implementation/linux/avahi.cc index 8384e205..8538459b 100644 --- a/internal/platform/implementation/linux/avahi.cc +++ b/internal/platform/implementation/linux/avahi.cc @@ -17,6 +17,10 @@ void ServiceBrowser::onItemNew(const int32_t &interface, << protocol << ", name: '" << name << "', type: '" << type << "', domain: '" << domain << "', flags: " << flags; + if (flags & kAvahiLookupResultLocal) { + NEARBY_LOGS(VERBOSE) << __func__ << ": Ignoring local service."; + return; + } NsdServiceInfo info; try { @@ -45,9 +49,9 @@ void ServiceBrowser::onItemNew(const int32_t &interface, discovery_cb_.service_discovered_cb(std::move(info)); } -void ServiceBrowser::onItemRemove(const int32_t &interface, const int32_t &protocol, - const std::string &name, const std::string &type, - const std::string &domain, const uint32_t &flags) { +void ServiceBrowser::onItemRemove( + const int32_t &interface, const int32_t &protocol, const std::string &name, + const std::string &type, const std::string &domain, const uint32_t &flags) { // TODO: Can we even resolve removed items? NEARBY_LOGS(VERBOSE) << __func__ << ": " << getObjectPath() << ": Item removed through the ServiceBrowser: " @@ -55,6 +59,11 @@ void ServiceBrowser::onItemRemove(const int32_t &interface, const int32_t &proto << protocol << ", name: '" << name << "', type: '" << type << "', domain: '" << domain << "', flags: " << flags; + if (flags & kAvahiLookupResultLocal) { + NEARBY_LOGS(VERBOSE) << __func__ << ": Ignoring local service."; + return; + } + NsdServiceInfo info; try { auto [r_iface, r_protocol, r_name, r_type, r_domain, r_host, r_aprotocol, @@ -89,13 +98,13 @@ void ServiceBrowser::onFailure(const std::string &error) { void ServiceBrowser::onAllForNow() { NEARBY_LOGS(VERBOSE) << __func__ << ": " << getObjectPath() - << ": notified via ServiceBrowser that all records have " - "been added for now"; + << ": notified via ServiceBrowser that all records have " + "been added for now"; } void ServiceBrowser::onCacheExhausted() { NEARBY_LOGS(VERBOSE) << __func__ << ": " << getObjectPath() - << ": notified via ServiceBrowser of cache exhaustion"; + << ": notified via ServiceBrowser of cache exhaustion"; } } // namespace avahi diff --git a/internal/platform/implementation/linux/avahi.h b/internal/platform/implementation/linux/avahi.h index 51002bfe..e1ca2d06 100644 --- a/internal/platform/implementation/linux/avahi.h +++ b/internal/platform/implementation/linux/avahi.h @@ -91,6 +91,15 @@ protected: void onCacheExhausted() override; private: + enum LookupResultFlags { + kAvahiLookupResultFlagCached = 1, + kAvahiLookupResultFlagWideArea = 2, + kAvahiLookupResultFlagMulticast = 4, + kAvahiLookupResultLocal = 8, + kAvahiLookupResultOurOwn = 16, + kAvahiLookupResultStatic = 32, + }; + api::WifiLanMedium::DiscoveredServiceCallback discovery_cb_; std::shared_ptr server_; };