Skip to content

multicast_dns 0.2.0 not detecting mDNS packets #79772

@biagiopietro

Description

@biagiopietro

I'm trying to do a lookup of a specific service using mDNS but the client in Flutter returns nothing albeit the host (Android Device) received the correct packet from mDNS server (see Wireshark section).

Steps to Reproduce

  1. Create mDNS server (you can use the following code in Go. So you need to put the Go code in a file called main.go and run go get -u github.com/hashicorp/mdns && go run main.go, please make sure you're running that command in the same folder of your main.go. To make sure that the server is running, you can run curl -X GET 'http://localhost:8081/' and then you should receive an ok (see screenshot)
    image
package main

import (
	"fmt"
	"github.com/hashicorp/mdns"
	"os"
  "net/http"
)

func health(w http.ResponseWriter, r *http.Request) {
  fmt.Fprintf(w, "ok")
}

func main() {

	hostname, err := os.Hostname()

	if err != nil {
		panic(fmt.Sprintf("Error getting current hostname, description: %s", err.Error()))
	}

	info := []string{"mDNS get server"}

	service, err := mdns.NewMDNSService(hostname, "_test._tcp", "", "", 8080, nil, info)

	if err != nil {
		panic(fmt.Sprintf("Error while exporting the service, description: %s", err.Error()))
	}

	server, err := mdns.NewServer(&mdns.Config{Zone: service})

	if err != nil {
		panic(fmt.Sprintf("Error while setting the discover server up, description: %s", err.Error()))
	}

	defer server.Shutdown()
	
	http.HandleFunc("/", health)
	http.ListenAndServe(":8081",nil)
}
  1. To check if mDNS server is working you can run (OSX): dns-sd -B _test._tcp (Linux: sudo apt-get install avahi-deamonavahi-utils && avahi-browse -rt _test._tcp)
  2. Regarding flutter you can use:
import 'package:multicast_dns/multicast_dns.dart';
import 'dart:io' show Platform;

Future<void> main() async {
  const String name = '_test._tcp.local';
  MDnsClient client;
  if (Platform.isAndroid) {
     var factory = (dynamic host, int port,
         {bool reuseAddress, bool reusePort, int ttl}) {
       print("Hostname $host");
       print("Port ${port.toString()}");
       return RawDatagramSocket.bind(host, port, reuseAddress: true, reusePort: false, ttl: ttl);
     };
     client = MDnsClient(rawDatagramSocketFactory: factory);
   } else {
     client = MDnsClient();
  }
  // Start the client with default options.
  await client.start();

  // Get the PTR record for the service.
  await for (final PtrResourceRecord ptr in client
      .lookup<PtrResourceRecord>(ResourceRecordQuery.serverPointer(name))) {
    // Use the domainName from the PTR record to get the SRV record,
    // which will have the port and local hostname.
    // Note that duplicate messages may come through, especially if any
    // other mDNS queries are running elsewhere on the machine.
    await for (final SrvResourceRecord srv in client.lookup<SrvResourceRecord>(
        ResourceRecordQuery.service(ptr.domainName))) {
      // Domain name will be something like "[email protected]._dartobservatory._tcp.local"
      final String bundleId =
          ptr.domainName; //.substring(0, ptr.domainName.indexOf('@'));
      print('Dart observatory instance found at '
          '${srv.target}:${srv.port} for "$bundleId".');
    }
  }
  client.stop();

  print('Done.');
}
  1. Run flutter run -d <android-device>.

Expected results:
Something like Dart observatory instance found at...

Actual results:

I/ViewRootImpl(28364): jank_removeInvalidNode all the node in jank list is out of time
W/InputMethodManager(28364): startInputReason = 1
I/flutter (28364): Hostname 0.0.0.0
I/flutter (28364): Port 5353
V/AudioManager(28364): playSoundEffect   effectType: 0
V/AudioManager(28364): querySoundEffectsEnabled...
I/flutter (28364): Hostname InternetAddress('127.0.0.1', IPv4)
I/flutter (28364): Port 5353
I/flutter (28364): Hostname InternetAddress('192.168.2.1', IPv4)
I/flutter (28364): Port 5353
I/flutter (28364): Done.
Logs

flutter doctor -v

[✓] Flutter (Channel stable, 2.0.4, on Linux, locale en_US.UTF-8)
    • Flutter version 2.0.4 at /home/puma/snap/flutter/common/flutter
    • Framework revision b1395592de (4 days ago), 2021-04-01 14:25:01 -0700
    • Engine revision 2dce47073a
    • Dart version 2.12.2

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /mnt/hd/Android/android-sdk-linux
    • Platform android-30, build-tools 29.0.2
    • Java binary at: /snap/android-studio/current/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✓] Android Studio
    • Android Studio at /snap/android-studio/current/android-studio/
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • android-studio-dir = /snap/android-studio/current/android-studio/
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[✓] Connected device (2 available)
    • ANE LX1 (mobile) • testAndroid • android-arm64  • Android 9 (API 28)
    • Chrome (web)     • chrome           • web-javascript • Google Chrome 89.0.4389.114

• No issues found!

flutter attach --verbose

[ +148 ms] executing: [/home/puma/snap/flutter/common/flutter/] git -c log.showSignature=false log -n 1 --pretty=format:%H
[  +72 ms] Exit code 0 from: git -c log.showSignature=false log -n 1 --pretty=format:%H
[        ] b1395592de68cc8ac4522094ae59956dd21a91db
[   +1 ms] executing: [/home/puma/snap/flutter/common/flutter/] git tag --points-at b1395592de68cc8ac4522094ae59956dd21a91db
[  +24 ms] Exit code 0 from: git tag --points-at b1395592de68cc8ac4522094ae59956dd21a91db
[        ] 2.0.4
[  +79 ms] executing: [/home/puma/snap/flutter/common/flutter/] git rev-parse --abbrev-ref --symbolic @{u}
[   +8 ms] Exit code 0 from: git rev-parse --abbrev-ref --symbolic @{u}
[        ] origin/stable
[        ] executing: [/home/puma/snap/flutter/common/flutter/] git ls-remote --get-url origin
[   +9 ms] Exit code 0 from: git ls-remote --get-url origin
[        ] https://github.com/flutter/flutter.git
[ +114 ms] executing: [/home/puma/snap/flutter/common/flutter/] git rev-parse --abbrev-ref HEAD
[  +10 ms] Exit code 0 from: git rev-parse --abbrev-ref HEAD
[        ] stable
[ +151 ms] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[   +3 ms] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterWebSdk' is not required, skipping update.
[  +12 ms] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[ +154 ms] Artifact Instance of 'MaterialFonts' is not required, skipping update.
[        ] Artifact Instance of 'GradleWrapper' is not required, skipping update.
[        ] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[   +1 ms] Artifact Instance of 'FlutterWebSdk' is not required, skipping update.
[        ] Artifact Instance of 'FlutterSdk' is not required, skipping update.
[        ] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[   +1 ms] Artifact Instance of 'FontSubsetArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'PubDependencies' is not required, skipping update.
[ +123 ms] executing: /mnt/hd/Android/android-sdk-linux/platform-tools/adb devices -l
[  +68 ms] List of devices attached
           testAndroid       device usb:2-1.1 product:ANE-LX1 model:ANE_LX1 device:HWANE transport_id:1
[ +102 ms] /mnt/hd/Android/android-sdk-linux/platform-tools/adb -s testAndroid shell getprop
[ +163 ms] Waiting for a connection from Flutter on ANE LX1...
[ +134 ms] Observatory URL on device: http://127.0.0.1:36421/LB5gpdiwoUU=/
[   +2 ms] executing: /mnt/hd/Android/android-sdk-linux/platform-tools/adb -s testAndroid forward tcp:0 tcp:36421
[  +15 ms] 45003
[   +1 ms] Forwarded host port 45003 to device port 36421 for Observatory
[   +9 ms] Connecting to service protocol: http://127.0.0.1:45003/LB5gpdiwoUU=/
[ +186 ms] Launching a Dart Developer Service (DDS) instance at http://127.0.0.1:0, connecting to VM service at http://127.0.0.1:45003/LB5gpdiwoUU=/.
[ +404 ms] Warning: Failed to start DDS: JSON-RPC error 100: Feature is disabled data: {details: A DDS instance is already connected at http://127.0.0.1:32821/rrJgFvQe3xQ=/., request: {jsonrpc: 2.0, method: _yieldControlToDDS, id: 8, params: {uri:
http://127.0.0.1:37321/2bciKM125Ng=/}}}
[  +55 ms] Successfully connected to service protocol: http://127.0.0.1:45003/LB5gpdiwoUU=/
[   +2 ms] executing: /mnt/hd/Android/android-sdk-linux/platform-tools/adb -s testAndroid shell -x logcat -v time -t 1
[  +84 ms] --------- beginning of main
           04-05 14:15:50.212 I/TrafficMonitor( 1273): gettimer:interval=2000
[ +955 ms] DevFS: Creating new filesystem on the device (null)
[  +18 ms] DevFS: Creating failed. Destroying and trying again
[   +1 ms] DevFS: Deleting filesystem on the device (null)
[  +34 ms] DevFS: Deleted filesystem on the device (null)
[  +15 ms] DevFS: Created new filesystem on the device (file:///data/user/0/com.example.test_mdns_client/code_cache/test_mdns_clientJAQOEZ/test_mdns_client/)
[  +10 ms] Updating assets
[ +299 ms] Syncing files to device ANE LX1...
[   +4 ms] <- reset
[        ] Compiling dart to kernel with 0 updated files
[  +17 ms] /home/puma/snap/flutter/common/flutter/bin/cache/dart-sdk/bin/dart --disable-dart-dev /home/puma/snap/flutter/common/flutter/bin/cache/artifacts/engine/linux-x64/frontend_server.dart.snapshot --sdk-root
/home/puma/snap/flutter/common/flutter/bin/cache/artifacts/engine/common/flutter_patched_sdk/ --incremental --target=flutter --debugger-module-names --experimental-emit-debug-metadata --output-dill /tmp/flutter_tools.FDFYPD/flutter_tool.JBRCDK/app.dill
--packages /home/puma/StudioProjects/test_mdns_client/.dart_tool/package_config.json -Ddart.vm.profile=false -Ddart.vm.product=false --enable-asserts --track-widget-creation --filesystem-scheme org-dartlang-root --initialize-from-dill
build/cache.dill.track.dill
[  +12 ms] <- compile package:test_mdns_client/main.dart

pubspec.yaml

name: test_mdns_client
description: A new Flutter project.

publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 1.0.0+1

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  intl: ^0.15.7
  multicast_dns: any

  cupertino_icons: ^1.0.2

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:
  uses-material-design: true

Wireshark

Source	        Destination	Protocol	Length	Info
192.168.2.1	224.0.0.251	MDNS		76	Standard query 0x0000 PTR _test._tcp.local, "QM" question
192.168.2.4	192.168.2.1	MDNS		180	Standard query response 0x0000 PTR puma._test._tcp.local SRV 10 1 8080 puma A 127.0.1.1 TXT

Where:

  • 192.168.2.1 is testAndroid device;
  • 192.168.2.4 is the device where mDNS server is running on.

Thanks in advance for your help!!!

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listfound in release: 2.0Found to occur in 2.0has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: multicast_dnsThe mdns packagepackageflutter/packages repository. See also p: labels.team-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions