windy1 / zeroconf-rs

zeroconf is a cross-platform library that wraps underlying ZeroConf/mDNS implementations such as Bonjour or Avahi, providing an easy and idiomatic way to both register and browse services.
MIT License
77 stars 25 forks source link

Fix service discovery on macOS #3

Closed oltoko closed 3 years ago

oltoko commented 3 years ago

Problem

I have some problems with the service discovery using version 0.6.2 on macOS. It can be reproduced by running the browser example on macOS. I changed the search term to _googlecast._tcp (because I have one) and got the following result.

Service discovered: ServiceDiscovery { name: "Chromecast-d17b7612bf302156649b47d71759c75d", kind: "_googlecast._tcp.", domain: "local", host_name: "d17b7612-bf30-2156-649b-47d71759c75d.local.", address: "100.113.246.228", port: 18719, txt: Some(BonjourTxtRecord(ManagedTXTRecordRef)) }

The resulting IP and Port are just wrong, and the IP even change from execution to execution. The correct and expected values are address: "192.168.178.51", port: 8009.

Environment

Solution in the PR

I debuged the code and found out that for the u32, which represents the IP, and the u16, which represent the Port, the endianess is messed up. The IP seem to use host byte order which is the reason why inet_ntoa is returning the wrong result. The Port is also just using the wrong byte order and a simple conversion to big endian is doing the trick.

As this solution is working for me I'm still not sure why this is happening. It seems like the bonjour implementation isn't compatible with libc and also the conversion from the u16 port value into rust isn't working. I also wasn't able to find something about it somewhere in the internet what gives this solution a bit of uncertainty.

oltoko commented 3 years ago

I noticed that it doesn't make any sense to add linux implementations into the macos/browser.rs 😅

oltoko commented 3 years ago

Thank you for merging my PR and thank you for the good work with this library 👍🏼

windy1 commented 3 years ago

Thanks!