Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to scan all mDNS in the network #8

Closed
guyluz11 opened this issue Jan 20, 2022 · 13 comments
Closed

Is there a way to scan all mDNS in the network #8

guyluz11 opened this issue Jan 20, 2022 · 13 comments
Labels
enhancement New feature or request

Comments

@guyluz11
Copy link

I am interested to scan for all NSD / DNS-SD / Bonjour / mDNS devices in the network.

I saw that the default serviceType is _http._tcp but it does not find all the mDNS devices at home I guess because they have different service types.

Is there a way to scan all the types at once?.

@sebastianhaberey
Copy link
Owner

To my understanding, there's no wildcard in the DNS-SD specification.

On the plus side, chapter 9 about service type enumeration states that if you use the special type _services._dns-sd._udp, you should get an enumeration of service types. You could then start a query for each service type received, and get all the devices on your network.

Right now the plugin doesn't support this special type (the string doesn't pass the validation regex) but I will have a look at this because it could definitely be useful.

@guyluz11
Copy link
Author

guyluz11 commented Jan 20, 2022

As I wrote on osociety/vernet#6 if there is an option to search for all services that start with TCP or search a list of known services this could also be a good start.

@sebastianhaberey
Copy link
Owner

search for all services that start with TCP or search a list of known services

I believe that both of these options are not supported by the DNS-SD specification. Consequently, the platform APIs (NsdManager on Android, NetService on iOS / macOS) do not support this. Do correct me if I'm wrong.

@guyluz11
Copy link
Author

guyluz11 commented Jan 20, 2022

Not that I know of.
But now I am curious how avahi-browse --all and mdns-scan do that on Linux.

And how does other apps on android do that like Network Scanner

@sebastianhaberey
Copy link
Owner

I haven't looked into this very deeply but my guess is, they'd have to use the method described above:

  1. Do discovery using special service type _services._dns-sd._udp
  2. Receive list of all service types in network
  3. Do discovery for each service type

That would give you a complete list of all service types and services in the network.

sebastianhaberey added a commit that referenced this issue Jan 21, 2022
- added exception for special type _services._dns-sd._udp in service type validity check
- added integration test for service type enumeration
- added unit test for validity check
@sebastianhaberey sebastianhaberey added the enhancement New feature or request label Jan 21, 2022
@sebastianhaberey
Copy link
Owner

sebastianhaberey commented Jan 21, 2022

I've implemented the above approach and it passes the integration test on Android / macOS / iOS. So in the new version (1.2.0) you can start a discovery like this:

final discovery = await startDiscovery('_services._dns-sd._udp', autoResolve: false);

The autoResolve flag is important because the results are not real services and cannot be resolved. The discovery.services list will then be populated with the answers. The answers look like this:

[MethodChannelNsdPlatform] [2022-01-21 12:19:56.941821] [calls] Callback: onServiceDiscovered {service.type: _tcp.local, service.host: null, service.name: _foo, handle: a353ff28-40dd-425d-a5a0-9966eea0c708}
[MethodChannelNsdPlatform] [2022-01-21 12:19:56.944118] [calls] Callback: onServiceDiscovered {service.type: _tcp.local, service.host: null, service.name: _bar, handle: a353ff28-40dd-425d-a5a0-9966eea0c708}

The first component of the service type (e.g. _foo) is contained in the service name attribute, the second component of the service type (e.g. _tcp) is contained in the service type attribute.

Even though using a service structure to represent a service type feels like a hack, it seems to be consistent on Android / macOS / iOS platform APIs. Since they are both doing it, I chose to adapt the same behavior for the plugin.

@guyluz11
Copy link
Author

guyluz11 commented Jan 22, 2022

Testing it now looks good.

have a couple of questions

  • Host and Port are unknown. Is it a bug? is it a work in progress?.
  • Using the default service _googlecast._tcp the discover finds my MiBox, but I can't see it when using the new discover all that you just implemented. Have I found a bug?.
    Not sure if this is related, MIBOX show as Type _googlecast._tcp and using the new method the devices are shown as _tcp.local.
  • I think it will be good to change the default search in the example to search for all services
    final discovery = await startDiscovery(serviceType);

@sebastianhaberey
Copy link
Owner

sebastianhaberey commented Jan 22, 2022

  1. A discovery of _services._dns-sd._udp does not find services, it finds service types. They don't have ports or hosts.
  2. The new feature is not a discover all. It finds service types. It should find the service type _googlecast._tcp in your case. You can then use the service type to find all services of the type.
  3. Good idea about updating the example, will put this on my backlog.

@sebastianhaberey
Copy link
Owner

You can enable call logging using

enableLogging(LogTopic.calls);

If you post the log here, I'll have a look at it.

@guyluz11
Copy link
Author

guyluz11 commented Jan 22, 2022

it finds service types.

Totally forgot about that, yes it finds _googlecast.tcp.local nice.

Thanks for solving that, can I get the device IP from the result by pinging by name or hostname?

@sebastianhaberey
Copy link
Owner

sebastianhaberey commented Jan 22, 2022

IPs are not part of the plugin right now (see #7). So yes, for now you'd have to lookup the host name received using something like InternetAddress.lookup.

@guyluz11
Copy link
Author

guyluz11 commented Jan 22, 2022

Ok thanks, will try InternetAddress.lookup for IP.

@oguzhaninal
Copy link

oguzhaninal commented Jun 9, 2023

_services._dns-sd._udp

Hello, i'm trying to find service type by using '_services._dns-sd._udp' but i got Callback: onDiscoveryStartFailed {handle: 06ac4af5-1301-4b78-9460-6dc6c16078d0}. I added info.plist

<key>NSBonjourServices</key>
<array>
<string>_http._tcp</string>
<string>_services._dns-sd._udp</string>
<string>_googlecast._tcp</string>
</array>

Can you please help me? @sebastianhaberey

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants