| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
## Copyright (C) 2010, 2011, 2012, 2013, 2014 Red Hat, Inc. |
| 4 |
## Authors: |
| 5 |
## Tim Waugh <[email protected]> |
| 6 |
|
| 7 |
## This program is free software; you can redistribute it and/or modify |
| 8 |
## it under the terms of the GNU General Public License as published by |
| 9 |
## the Free Software Foundation; either version 2 of the License, or |
| 10 |
## (at your option) any later version. |
| 11 |
|
| 12 |
## This program is distributed in the hope that it will be useful, |
| 13 |
## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
## GNU General Public License for more details. |
| 16 |
|
| 17 |
## You should have received a copy of the GNU General Public License |
| 18 |
## along with this program; if not, write to the Free Software |
| 19 |
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 |
|
| 21 |
import dbus, re |
| 22 |
import urllib.parse |
| 23 |
from debug import * |
| 24 |
|
| 25 |
class DNSSDHostNamesResolver: |
| 26 |
def __init__ (self, devices): |
| 27 |
self._devices = devices |
| 28 |
self._unresolved = len (devices) |
| 29 |
self._device_uri_by_name = {} |
| 30 |
debugprint ("+%s" % self) |
| 31 |
|
| 32 |
def __del__ (self): |
| 33 |
debugprint ("-%s" % self) |
| 34 |
|
| 35 |
def resolve (self, reply_handler): |
| 36 |
|
| 37 |
self._reply_handler = reply_handler |
| 38 |
|
| 39 |
bus = dbus.SystemBus () |
| 40 |
if not bus: |
| 41 |
reply_handler ([]) |
| 42 |
del self._devices |
| 43 |
del self._reply_handler |
| 44 |
return |
| 45 |
|
| 46 |
for uri, device in self._devices.items (): |
| 47 |
if not uri.startswith ("dnssd://"): |
| 48 |
self._unresolved -= 1 |
| 49 |
continue |
| 50 |
|
| 51 |
# We need to resolve the DNS-SD hostname in order to |
| 52 |
# compare with other network devices. |
| 53 |
result = urllib.parse.urlparse (uri) |
| 54 |
hostname = result.netloc |
| 55 |
elements = hostname.rsplit (".", 3) |
| 56 |
if len (elements) != 4: |
| 57 |
self._resolved () |
| 58 |
continue |
| 59 |
|
| 60 |
name, stype, protocol, domain = elements |
| 61 |
name = urllib.parse.unquote (name) |
| 62 |
stype += "." + protocol # e.g. _printer._tcp |
| 63 |
|
| 64 |
try: |
| 65 |
obj = bus.get_object ("org.freedesktop.Avahi", "/") |
| 66 |
server = dbus.Interface (obj, |
| 67 |
"org.freedesktop.Avahi.Server") |
| 68 |
self._device_uri_by_name[(name, stype, domain)] = uri |
| 69 |
debugprint ("Resolving address for %s" % hostname) |
| 70 |
server.ResolveService (-1, -1, |
| 71 |
name, stype, domain, |
| 72 |
-1, 0, |
| 73 |
reply_handler=self._reply, |
| 74 |
error_handler=lambda e: |
| 75 |
self._error (uri, e)) |
| 76 |
except dbus.DBusException as e: |
| 77 |
debugprint ("Failed to resolve address: %s" % repr (e)) |
| 78 |
self._resolved () |
| 79 |
|
| 80 |
def _resolved (self): |
| 81 |
self._unresolved -= 1 |
| 82 |
if self._unresolved == 0: |
| 83 |
debugprint ("All addresses resolved") |
| 84 |
self._reply_handler (self._devices) |
| 85 |
del self._devices |
| 86 |
del self._reply_handler |
| 87 |
|
| 88 |
def _reply (self, interface, protocol, name, stype, domain, |
| 89 |
host, aprotocol, address, port, txt, flags): |
| 90 |
uri = self._device_uri_by_name[(name, stype, domain)] |
| 91 |
self._devices[uri].address = address |
| 92 |
hostname = host |
| 93 |
p = hostname.find(".") |
| 94 |
if p != -1: |
| 95 |
hostname = hostname[:p] |
| 96 |
debugprint ("%s is at %s (%s)" % (uri, address, hostname)) |
| 97 |
self._devices[uri].hostname = hostname |
| 98 |
self._resolved () |
| 99 |
|
| 100 |
def _error (self, uri, error): |
| 101 |
debugprint ("Error resolving %s: %s" % (uri, repr (error))) |
| 102 |
self._resolved () |
| 103 |
|
| 104 |
if __name__ == '__main__': |
| 105 |
class Device: |
| 106 |
def __repr__ (self): |
| 107 |
try: |
| 108 |
return "<Device @ %s>" % self.address |
| 109 |
except: |
| 110 |
return "<Device>" |
| 111 |
|
| 112 |
devices = {"dnssd://dlk-08E206-P1._printer._tcp.local/": Device(), |
| 113 |
"dnssd://foo._printer._tcp.local/": Device()} |
| 114 |
from dbus.glib import DBusGMainLoop |
| 115 |
DBusGMainLoop (set_as_default=True) |
| 116 |
|
| 117 |
class Test: |
| 118 |
def __init__ (self, loop, devices): |
| 119 |
self._loop = loop |
| 120 |
self._devices = devices |
| 121 |
|
| 122 |
def run (self): |
| 123 |
r = DNSSDHostNamesResolver (self._devices) |
| 124 |
r.resolve (reply_handler=self.reply) |
| 125 |
return False |
| 126 |
|
| 127 |
def reply (self, *args): |
| 128 |
print(args) |
| 129 |
self._loop.quit () |
| 130 |
|
| 131 |
from gi.repository import GObject |
| 132 |
from gi.repository import GLib |
| 133 |
loop = GObject.MainLoop () |
| 134 |
set_debugging (True) |
| 135 |
GLib.idle_add (Test (loop, devices).run) |
| 136 |
loop.run () |