cbrand / micropython-mdns

MDNS implementation for MicroPython with Service Discovery
MIT License
53 stars 12 forks source link

Possibility to specify different service name and hostname #12

Closed ondrej1024 closed 1 year ago

ondrej1024 commented 1 year ago

There are some devices which announce a service name which is different from the hostname. I have here a Tradfri gateway which shows up in avahi-browse as follows:

= wlp3s0 IPv4 gw-b072bf27b113                               _coap._udp           local
   hostname = [TRADFRI-Gateway-b072bf27b113.local]
   address = [192.168.1.60]
   port = [5684]

So the service name is gw-b072bf27b113 but the host name is TRADFRI-Gateway-b072bf27b113.local.

Can we do the same with the micropython-mdns library?

In the Responder class we can specify the host parameter which seems to be used for both service name and hostname? Is it possible to specify different names in some way?

Thanks.

ondrej1024 commented 1 year ago

I think the root cause of this issue is in function host_fqdn() in responder.py:

return ".".join((host, "local"))

The FQDN is generated by just adding .local to the specifed hostname. If we wanted something else here we would need an additional parameter to be passed to the Responder class at initialization.

cbrand commented 1 year ago

After taking a look at the code base I would like to understand your use case a bit more:

If you want to send another hostname for a service on the same machine it is possible to do it with relative easy adjustments of the code base, though I would make it accessible in the advertise function so you can set an own host name per service, but I cannot quite wrap my head around right now why you would like to have this feature in this case.

So could you give me some more information about your intended use case?

Thank you :)

ondrej1024 commented 1 year ago

Basically I need to mimic the behaviour of the Raspberry Pi, which, for some reason, advertises the service like this (hostname + mac address):

raspberrypi [12:34:56:78:9a:bc]

but the actual address is this (without the MAC address):

raspberrypi.local

Actually I don't know why some devices announce a service name different from the host name (I guess it has to do with how the client deals with the discovery of a specific device) but MDNS seems to allow this and it would be nice to have this possibility also on a Micropython device.

cbrand commented 1 year ago

Okay I hacked something small together. I am curently not in the area of an esp32 so would ask you to test it. Also I am not 100% sure if this is the behavior which helps you.

https://github.com/cbrand/micropython-mdns/pull/13

You can set the service hostname via a new parameter in the advertise function like this:

responder.advertise("_myawesomeservice", "_tcp", port=12345, data={"some": "metadata", "for": ["my", "service"]}, host="myoverwrittenhost")

For easier testing here is a micropython 1.20 build for the ESP32 which you can flash: firmware.mp.1.20.bin.zip

ondrej1024 commented 1 year ago

Great, looks exactly like the feature I need. Will test it next week and report back. Thanks

ondrej1024 commented 1 year ago

I ran a quick test today. If I pass the new host parameter to the advertise() function, the service is discoverd but the host name cannot be resolved:

$ avahi-browse -a -r
+ wlp3s0 IPv4 my-pico                                       Workstation          local
Failed to resolve service 'my-pico' of type '_workstation._tcp' in domain 'local': Timeout reached

Without specifying the host parameter, it works as before.

cbrand commented 1 year ago

Alright I got my hands on an ESP32 and could test it myself. The PR now works for me and I am building a release with the new functionality right now.

cbrand commented 1 year ago

Autoclosed after merging. The new functionality has one additional user change: The parameter is renamed to service_host_name.

It is available in version 1.3.0 and works with the ESP32 i had here and avahi-browse -a -r resolves it now as expected.

https://github.com/cbrand/micropython-mdns/releases/tag/1.3.0

ondrej1024 commented 1 year ago

I had the chance to test this now. Works perfect. Thanks.