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

Utilities/Zeroconf.app: Use dns-sd instead of avahi-browse #57

Open
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

MalloryA
Copy link

Hi!

First of all: I did all this work on a macOS desktop. I suspect everything will work on FreeBSD/hello (FreeBSD's mDNSResponder package comes from Apple), but I don't have a spare machine to experiment with right now.

Anyway, here's a PR that removes one dependency on Avahi from hello. Avahi depends on D-Bus, and D-Bus is unwelcome on hello.

avahi-browse outputs all the relevant information when provided one set of flags. However the dns-sd tool in the mDNSResponder package requires several invocations to provide the same invocation so the Zeroconf.app tool has become a little more complex.

Keeping with the spirit of hello, the whole application is still in one file. However I did add a couple of unit tests in separate files (Resources/CommandReader_test.py and Resources/ZeroconfService_test.py).

Testing

Install and setup mDNSResponder:

  • sudo pkg install mDNSResponder mDNSResponder_nss
  • sudo vi /etc/nsswitch.conf
    • Add mdns to the end of the hosts: line
  • sudo sysrc mdnsd_enable=YES
  • sudo service mdnsd start

You can run the application with the Utilities/Zeroconf.app/Zeroconf script, or run python3 Utilities/Zeroconf.app/Resources/zeroconf.py.

You can advertise a new service with this command (and take away the new service with Ctrl-C):

dns-sd -R "My Test" _http._tcp . 80 path=/path-to-page.html

Hope this is useful. Thanks!

- This will make it easier to write unit tests later.
- This is consistent with the example set in Utilities/Calculator.app.
- ./Zeroconf is now just a shell script pointing to the Python
  application.
In Python 2, there were old-style classes (`class Foo:`) and new-style
classes (`class Foo(object):`). This is not necessary in Python 3.
This means that the self.app.aboutToQuit signal will be processed, and
the application can clean up after itself.
A simple class representing a service, with some unused fields left out,
and with no handle() method.
This will be useful when we want to remove services from the list or
avoid adding duplicates.
It runs a command using QProcess and emits each line of the output
through a pyqtSignal.
This class discovers services on the local network by running `dns-sd`.
This means the tool no longer leaves several `dns-sd` processes running
in the background.
The recently added/modified classes behave differently from the old ones
- so hook things up.
I don't know how to remove a PyQt5 widget entirely, so I used
setHidden(true).
- self.services changed slightly in the previous commit, so the method
  has been updated to handle that.
- DRY.
@probonopd
Copy link
Member

Thank you very much @MalloryA. Indeed we would prefer to use mDNSResponder rather than Avahi, but I am not sure whether we still need Avahi on the system for things like gvfs-mount which is currently used by Filer.

There is an ongoing discussion on whether we should replace gvfs, too...

@MalloryA
Copy link
Author

I am not sure whether we still need Avahi on the system for things like gvfs-mount which is currently used by Filer.

Preferences/Sharing.app also depends on Avahi because it uses x11vnc:

command = 'x11vnc'
args = ["-localhost", "-avahi", "-ncache", "10", "-ncache_cr", "-display", os.getenv("DISPLAY")]

Though, if net/x11vnc is built from the ports collection, there's an AVAHI config option that could be turned off. And Sharing.app could be modified to register the service over Zeroconf using dns-sd instead.

I'll also note that avahi and mDNSResponder can be installed at the same time. So if Avahi needs to be part of hello for the immediate future, individual components of hello can still be migrated to use mDNSResponder/dns-sd in the meantime.

@probonopd
Copy link
Member

probonopd commented Mar 28, 2021

I'll also note that avahi and mDNSResponder can be installed at the same time. So if Avahi needs to be part of hello for the immediate future, individual components of hello can still be migrated to use mDNSResponder/dns-sd in the meantime.

This is a very interesting aspect which I did not know before, I was assuming that it is an either-or.

At least Avahi had given me some messages to that end back when I was also experimenting with mDNSResponder/dns-sd, with the result that gvfs did not show network shares in Filer anymore.

I will need to do more testing (using Live systems so that I don't break my installed system).


Do you think you could implement something like #1 (comment)?

@probonopd
Copy link
Member

probonopd commented Jun 25, 2023

Note to self: Consider rewrite in Qt/C++ with https://github.com/nitroshare/qmdnsengine. This one is known to work even though avahi is running on the system (but does not need it).

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

Successfully merging this pull request may close these issues.

2 participants