2010-02-08 15:08:40 +01:00
|
|
|
## common/resolver.py
|
2006-02-03 13:17:34 +01:00
|
|
|
##
|
2006-03-30 18:10:50 +02:00
|
|
|
## Copyright (C) 2006 Dimitur Kirov <dkirov@gmail.com>
|
2006-02-03 13:17:34 +01:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
2006-02-03 13:17:34 +01:00
|
|
|
## it under the terms of the GNU General Public License as published
|
2007-10-22 13:13:13 +02:00
|
|
|
## by the Free Software Foundation; version 3 only.
|
2006-02-03 13:17:34 +01:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
2006-02-03 13:17:34 +01:00
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## You should have received a copy of the GNU General Public License
|
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
##
|
2006-02-03 13:17:34 +01:00
|
|
|
|
2006-03-30 18:10:50 +02:00
|
|
|
import sys
|
|
|
|
import os
|
2006-11-19 00:21:59 +01:00
|
|
|
import re
|
2009-01-11 18:54:41 +01:00
|
|
|
import logging
|
2016-10-02 15:01:12 +02:00
|
|
|
import functools
|
2009-01-11 18:54:41 +01:00
|
|
|
log = logging.getLogger('gajim.c.resolver')
|
2006-02-03 13:17:34 +01:00
|
|
|
|
2009-12-18 17:27:09 +01:00
|
|
|
if __name__ == '__main__':
|
2010-02-08 15:08:40 +01:00
|
|
|
sys.path.append('..')
|
|
|
|
from common import i18n
|
|
|
|
import common.configpaths
|
|
|
|
common.configpaths.gajimpaths.init(None)
|
2009-12-18 17:27:09 +01:00
|
|
|
|
2009-01-14 18:27:53 +01:00
|
|
|
from common import helpers
|
2012-12-09 21:37:51 +01:00
|
|
|
from nbxmpp.idlequeue import IdleCommand
|
2016-10-02 15:01:12 +02:00
|
|
|
from gi.repository import Gio, GLib
|
|
|
|
|
2006-03-30 18:10:50 +02:00
|
|
|
|
2006-02-03 13:17:34 +01:00
|
|
|
# it is good to check validity of arguments, when calling system commands
|
2006-11-19 00:21:59 +01:00
|
|
|
ns_type_pattern = re.compile('^[a-z]+$')
|
2006-02-03 13:17:34 +01:00
|
|
|
|
|
|
|
# match srv host_name
|
2006-11-19 00:21:59 +01:00
|
|
|
host_pattern = re.compile('^[a-z0-9\-._]*[a-z0-9]\.[a-z]{2,}$')
|
2006-02-03 13:17:34 +01:00
|
|
|
|
2008-09-01 01:40:06 +02:00
|
|
|
try:
|
2010-02-08 15:08:40 +01:00
|
|
|
#raise ImportError("Manually disabled libasync")
|
|
|
|
import libasyncns
|
|
|
|
USE_LIBASYNCNS = True
|
|
|
|
log.info("libasyncns-python loaded")
|
2008-09-01 01:40:06 +02:00
|
|
|
except ImportError:
|
2010-02-08 15:08:40 +01:00
|
|
|
USE_LIBASYNCNS = False
|
|
|
|
log.debug("Import of libasyncns-python failed, getaddrinfo will block", exc_info=True)
|
2008-09-01 01:40:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_resolver(idlequeue):
|
2010-02-08 15:08:40 +01:00
|
|
|
if USE_LIBASYNCNS:
|
2014-04-09 17:04:03 +02:00
|
|
|
log.info('Using LibAsyncNSResolver')
|
2010-02-08 15:08:40 +01:00
|
|
|
return LibAsyncNSResolver()
|
|
|
|
else:
|
2014-04-09 17:04:03 +02:00
|
|
|
if helpers.is_in_path('host'):
|
|
|
|
log.info('Using HostResolver')
|
|
|
|
return HostResolver(idlequeue)
|
|
|
|
log.info('Using NSLookupResolver')
|
2010-02-08 15:08:40 +01:00
|
|
|
return NSLookupResolver(idlequeue)
|
2008-09-01 01:40:06 +02:00
|
|
|
|
|
|
|
class CommonResolver():
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self):
|
|
|
|
# dict {"host+type" : list of records}
|
|
|
|
self.resolved_hosts = {}
|
|
|
|
# dict {"host+type" : list of callbacks}
|
|
|
|
self.handlers = {}
|
|
|
|
|
2014-04-09 17:04:03 +02:00
|
|
|
def resolve(self, host, on_ready, type_='srv'):
|
2012-06-19 19:19:24 +02:00
|
|
|
host = host.lower()
|
2014-04-09 17:04:03 +02:00
|
|
|
log.debug('resolve %s type=%s' % (host, type_))
|
|
|
|
assert(type_ in ['srv', 'txt'])
|
2010-02-08 15:08:40 +01:00
|
|
|
if not host:
|
|
|
|
# empty host, return empty list of srv records
|
|
|
|
on_ready([])
|
|
|
|
return
|
2014-04-09 17:04:03 +02:00
|
|
|
if host + type_ in self.resolved_hosts:
|
2010-02-08 15:08:40 +01:00
|
|
|
# host is already resolved, return cached values
|
2010-10-19 18:14:30 +02:00
|
|
|
log.debug('%s already resolved: %s' % (host,
|
2014-04-09 17:04:03 +02:00
|
|
|
self.resolved_hosts[host + type_]))
|
|
|
|
on_ready(host, self.resolved_hosts[host + type_])
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2014-04-09 17:04:03 +02:00
|
|
|
if host + type_ in self.handlers:
|
2010-02-08 15:08:40 +01:00
|
|
|
# host is about to be resolved by another connection,
|
|
|
|
# attach our callback
|
|
|
|
log.debug('already resolving %s' % host)
|
2014-04-09 17:04:03 +02:00
|
|
|
self.handlers[host + type_].append(on_ready)
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
|
|
|
# host has never been resolved, start now
|
|
|
|
log.debug('Starting to resolve %s using %s' % (host, self))
|
2014-04-09 17:04:03 +02:00
|
|
|
self.handlers[host + type_] = [on_ready]
|
|
|
|
self.start_resolve(host, type_)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2014-04-09 17:04:03 +02:00
|
|
|
def _on_ready(self, host, type_, result_list):
|
2010-02-08 15:08:40 +01:00
|
|
|
# practically it is impossible to be the opposite, but who knows :)
|
2012-06-19 19:19:24 +02:00
|
|
|
host = host.lower()
|
2010-02-08 15:08:40 +01:00
|
|
|
log.debug('Resolving result for %s: %s' % (host, result_list))
|
2014-04-09 17:04:03 +02:00
|
|
|
if host + type_ not in self.resolved_hosts:
|
|
|
|
self.resolved_hosts[host + type_] = result_list
|
|
|
|
if host + type_ in self.handlers:
|
|
|
|
for callback in self.handlers[host + type_]:
|
2010-02-08 15:08:40 +01:00
|
|
|
callback(host, result_list)
|
2014-04-09 17:04:03 +02:00
|
|
|
del(self.handlers[host + type_])
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2014-04-09 17:04:03 +02:00
|
|
|
def start_resolve(self, host, type_):
|
2010-02-08 15:08:40 +01:00
|
|
|
pass
|
2008-09-01 01:40:06 +02:00
|
|
|
|
2009-01-11 18:54:41 +01:00
|
|
|
# FIXME: API usage is not consistent! This one requires that process is called
|
2008-09-01 01:40:06 +02:00
|
|
|
class LibAsyncNSResolver(CommonResolver):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Asynchronous resolver using libasyncns-python. process() method has to be
|
|
|
|
called in order to proceed the pending requests. Based on patch submitted by
|
|
|
|
Damien Thebault.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.asyncns = libasyncns.Asyncns()
|
|
|
|
CommonResolver.__init__(self)
|
|
|
|
|
2014-04-09 17:04:03 +02:00
|
|
|
def start_resolve(self, host, type_):
|
|
|
|
type_ = libasyncns.ns_t_srv
|
|
|
|
if type_ == 'txt': type_ = libasyncns.ns_t_txt
|
|
|
|
resq = self.asyncns.res_query(host, libasyncns.ns_c_in, type_)
|
|
|
|
resq.userdata = {'host':host, 'type':type_}
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# getaddrinfo to be done
|
|
|
|
#def resolve_name(self, dname, callback):
|
|
|
|
#resq = self.asyncns.getaddrinfo(dname)
|
|
|
|
#resq.userdata = {'callback':callback, 'dname':dname}
|
|
|
|
|
2014-04-09 17:04:03 +02:00
|
|
|
def _on_ready(self, host, type_, result_list):
|
|
|
|
if type_ == libasyncns.ns_t_srv: type_ = 'srv'
|
|
|
|
elif type_ == libasyncns.ns_t_txt: type_ = 'txt'
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2014-04-09 17:04:03 +02:00
|
|
|
CommonResolver._on_ready(self, host, type_, result_list)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def process(self):
|
|
|
|
try:
|
|
|
|
self.asyncns.wait(False)
|
|
|
|
resq = self.asyncns.get_next()
|
|
|
|
except:
|
|
|
|
return True
|
|
|
|
if type(resq) == libasyncns.ResQuery:
|
|
|
|
# TXT or SRV result
|
|
|
|
while resq is not None:
|
|
|
|
try:
|
|
|
|
rl = resq.get_done()
|
|
|
|
except Exception:
|
|
|
|
rl = []
|
|
|
|
hosts = []
|
|
|
|
requested_type = resq.userdata['type']
|
|
|
|
requested_host = resq.userdata['host']
|
|
|
|
if rl:
|
|
|
|
for r in rl:
|
|
|
|
if r['type'] != requested_type:
|
|
|
|
# Answer doesn't contain valid SRV data
|
|
|
|
continue
|
|
|
|
r['prio'] = r['pref']
|
|
|
|
hosts.append(r)
|
2014-04-09 17:04:03 +02:00
|
|
|
self._on_ready(host=requested_host, type_=requested_type,
|
2010-02-08 15:08:40 +01:00
|
|
|
result_list=hosts)
|
|
|
|
try:
|
|
|
|
resq = self.asyncns.get_next()
|
|
|
|
except Exception:
|
|
|
|
resq = None
|
|
|
|
elif type(resq) == libasyncns.AddrInfoQuery:
|
|
|
|
# getaddrinfo result (A or AAAA)
|
|
|
|
rl = resq.get_done()
|
|
|
|
resq.userdata['callback'](resq.userdata['dname'], rl)
|
|
|
|
return True
|
2008-09-01 01:40:06 +02:00
|
|
|
|
2009-01-11 18:54:41 +01:00
|
|
|
|
2008-09-01 01:40:06 +02:00
|
|
|
class NSLookupResolver(CommonResolver):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Asynchronous DNS resolver calling nslookup. Processing of pending requests
|
|
|
|
is invoked from idlequeue which is watching file descriptor of pipe of
|
|
|
|
stdout of nslookup process.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, idlequeue):
|
|
|
|
self.idlequeue = idlequeue
|
|
|
|
self.process = False
|
|
|
|
CommonResolver.__init__(self)
|
|
|
|
|
|
|
|
def parse_srv_result(self, fqdn, result):
|
|
|
|
"""
|
|
|
|
Parse the output of nslookup command and return list of properties:
|
|
|
|
'host', 'port','weight', 'priority' corresponding to the found srv hosts
|
|
|
|
"""
|
|
|
|
if os.name == 'nt':
|
|
|
|
return self._parse_srv_result_nt(fqdn, result)
|
|
|
|
elif os.name == 'posix':
|
|
|
|
return self._parse_srv_result_posix(fqdn, result)
|
|
|
|
|
|
|
|
def _parse_srv_result_nt(self, fqdn, result):
|
|
|
|
# output from win32 nslookup command
|
|
|
|
if not result:
|
|
|
|
return []
|
|
|
|
hosts = []
|
2016-07-07 22:41:35 +02:00
|
|
|
result = result.decode(sys.stdout.encoding)
|
|
|
|
lines = result.replace('\r', '').split('\n')
|
2010-02-08 15:08:40 +01:00
|
|
|
current_host = None
|
|
|
|
for line in lines:
|
|
|
|
line = line.lstrip()
|
|
|
|
if line == '':
|
|
|
|
continue
|
|
|
|
if line.startswith(fqdn):
|
|
|
|
rest = line[len(fqdn):]
|
|
|
|
if rest.find('service') > -1:
|
|
|
|
current_host = {}
|
|
|
|
elif isinstance(current_host, dict):
|
|
|
|
res = line.strip().split('=')
|
|
|
|
if len(res) != 2:
|
|
|
|
if len(current_host) == 4:
|
|
|
|
hosts.append(current_host)
|
|
|
|
current_host = None
|
|
|
|
continue
|
|
|
|
prop_type = res[0].strip()
|
|
|
|
prop_value = res[1].strip()
|
|
|
|
if prop_type.find('prio') > -1:
|
|
|
|
try:
|
|
|
|
current_host['prio'] = int(prop_value)
|
|
|
|
except ValueError:
|
|
|
|
continue
|
|
|
|
elif prop_type.find('weight') > -1:
|
|
|
|
try:
|
|
|
|
current_host['weight'] = int(prop_value)
|
|
|
|
except ValueError:
|
|
|
|
continue
|
|
|
|
elif prop_type.find('port') > -1:
|
|
|
|
try:
|
|
|
|
current_host['port'] = int(prop_value)
|
|
|
|
except ValueError:
|
|
|
|
continue
|
|
|
|
elif prop_type.find('host') > -1:
|
|
|
|
# strip '.' at the end of hostname
|
|
|
|
if prop_value[-1] == '.':
|
|
|
|
prop_value = prop_value[:-1]
|
|
|
|
current_host['host'] = prop_value
|
|
|
|
if len(current_host) == 4:
|
|
|
|
hosts.append(current_host)
|
|
|
|
current_host = None
|
|
|
|
return hosts
|
|
|
|
|
|
|
|
def _parse_srv_result_posix(self, fqdn, result):
|
|
|
|
# typical output of bind-tools nslookup command:
|
|
|
|
# _xmpp-client._tcp.jabber.org service = 30 30 5222 jabber.org.
|
|
|
|
if not result:
|
|
|
|
return []
|
|
|
|
ufqdn = helpers.ascii_to_idn(fqdn) # Unicode domain name
|
|
|
|
hosts = []
|
|
|
|
lines = result.split('\n')
|
|
|
|
for line in lines:
|
|
|
|
if line == '':
|
|
|
|
continue
|
|
|
|
domain = None
|
|
|
|
if line.startswith(fqdn):
|
|
|
|
domain = fqdn # For nslookup 9.5
|
2013-01-02 17:53:42 +01:00
|
|
|
elif line.startswith(ufqdn):
|
2010-02-08 15:08:40 +01:00
|
|
|
domain = ufqdn # For nslookup 9.6
|
|
|
|
if domain:
|
|
|
|
rest = line[len(domain):].split('=')
|
|
|
|
if len(rest) != 2:
|
|
|
|
continue
|
|
|
|
answer_type, props_str = rest
|
|
|
|
if answer_type.strip() != 'service':
|
|
|
|
continue
|
|
|
|
props = props_str.strip().split(' ')
|
|
|
|
if len(props) < 4:
|
|
|
|
continue
|
|
|
|
prio, weight, port, host = props[-4:]
|
|
|
|
if host[-1] == '.':
|
|
|
|
host = host[:-1]
|
|
|
|
try:
|
|
|
|
prio = int(prio)
|
|
|
|
weight = int(weight)
|
|
|
|
port = int(port)
|
|
|
|
except ValueError:
|
|
|
|
continue
|
|
|
|
hosts.append({'host': host, 'port': port, 'weight': weight,
|
|
|
|
'prio': prio})
|
|
|
|
return hosts
|
|
|
|
|
2014-04-09 17:04:03 +02:00
|
|
|
def _on_ready(self, host, type_, result):
|
2010-02-08 15:08:40 +01:00
|
|
|
# nslookup finished, parse the result and call the handlers
|
|
|
|
result_list = self.parse_srv_result(host, result)
|
2014-04-09 17:04:03 +02:00
|
|
|
CommonResolver._on_ready(self, host, type_, result_list)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2014-04-09 17:04:03 +02:00
|
|
|
def start_resolve(self, host, type_):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Spawn new nslookup process and start waiting for results
|
|
|
|
"""
|
2014-04-09 17:04:03 +02:00
|
|
|
ns = NsLookup(self._on_ready, host, type_)
|
2010-02-08 15:08:40 +01:00
|
|
|
ns.set_idlequeue(self.idlequeue)
|
|
|
|
ns.commandtimeout = 20
|
|
|
|
ns.start()
|
2006-02-03 13:17:34 +01:00
|
|
|
|
2009-01-11 18:54:41 +01:00
|
|
|
|
2014-04-09 17:04:03 +02:00
|
|
|
class HostResolver(CommonResolver):
|
|
|
|
"""
|
|
|
|
Asynchronous DNS resolver calling host. Processing of pending requests
|
|
|
|
is invoked from idlequeue which is watching file descriptor of pipe of
|
|
|
|
stdout of host process.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, idlequeue):
|
|
|
|
self.idlequeue = idlequeue
|
|
|
|
self.process = False
|
|
|
|
CommonResolver.__init__(self)
|
|
|
|
|
|
|
|
def parse_srv_result(self, fqdn, result):
|
|
|
|
"""
|
|
|
|
Parse the output of host command and return list of properties:
|
|
|
|
'host', 'port','weight', 'priority' corresponding to the found srv hosts
|
|
|
|
"""
|
|
|
|
# typical output of host command:
|
|
|
|
# _xmpp-client._tcp.jabber.org has SRV record 30 30 5222 jabber.org.
|
|
|
|
if not result:
|
|
|
|
return []
|
|
|
|
ufqdn = helpers.ascii_to_idn(fqdn) # Unicode domain name
|
|
|
|
hosts = []
|
|
|
|
lines = result.split('\n')
|
|
|
|
for line in lines:
|
|
|
|
if line == '':
|
|
|
|
continue
|
|
|
|
domain = None
|
|
|
|
if line.startswith(fqdn):
|
|
|
|
domain = fqdn # For nslookup 9.5
|
2015-01-02 21:53:59 +01:00
|
|
|
elif line.startswith(ufqdn):
|
2014-04-09 17:04:03 +02:00
|
|
|
domain = ufqdn # For nslookup 9.6
|
|
|
|
if domain:
|
|
|
|
# add 4 for ' has' after domain name
|
2014-04-22 13:11:00 +02:00
|
|
|
rest = line[len(domain)+4:].split('record')
|
2014-04-09 17:04:03 +02:00
|
|
|
if len(rest) != 2:
|
|
|
|
continue
|
|
|
|
answer_type, props_str = rest
|
|
|
|
if answer_type.strip() != 'SRV':
|
|
|
|
continue
|
|
|
|
props = props_str.strip().split(' ')
|
|
|
|
if len(props) < 4:
|
|
|
|
continue
|
|
|
|
prio, weight, port, host = props[-4:]
|
|
|
|
if host[-1] == '.':
|
|
|
|
host = host[:-1]
|
|
|
|
try:
|
|
|
|
prio = int(prio)
|
|
|
|
weight = int(weight)
|
|
|
|
port = int(port)
|
|
|
|
except ValueError:
|
|
|
|
continue
|
|
|
|
hosts.append({'host': host, 'port': port, 'weight': weight,
|
|
|
|
'prio': prio})
|
|
|
|
return hosts
|
|
|
|
|
|
|
|
def _on_ready(self, host, type_, result):
|
|
|
|
# host finished, parse the result and call the handlers
|
|
|
|
result_list = self.parse_srv_result(host, result)
|
|
|
|
CommonResolver._on_ready(self, host, type_, result_list)
|
|
|
|
|
|
|
|
def start_resolve(self, host, type_):
|
|
|
|
"""
|
|
|
|
Spawn new nslookup process and start waiting for results
|
|
|
|
"""
|
|
|
|
ns = Host(self._on_ready, host, type_)
|
|
|
|
ns.set_idlequeue(self.idlequeue)
|
|
|
|
ns.commandtimeout = 20
|
|
|
|
ns.start()
|
|
|
|
|
2006-02-03 13:17:34 +01:00
|
|
|
class NsLookup(IdleCommand):
|
2014-04-09 17:04:03 +02:00
|
|
|
def __init__(self, on_result, host='_xmpp-client', type_='srv'):
|
2010-02-08 15:08:40 +01:00
|
|
|
IdleCommand.__init__(self, on_result)
|
|
|
|
self.commandtimeout = 10
|
|
|
|
self.host = host.lower()
|
2014-04-09 17:04:03 +02:00
|
|
|
self.type_ = type_.lower()
|
2010-02-08 15:08:40 +01:00
|
|
|
if not host_pattern.match(self.host):
|
|
|
|
# invalid host name
|
|
|
|
log.error('Invalid host: %s' % self.host)
|
|
|
|
self.canexecute = False
|
|
|
|
return
|
2012-08-22 12:55:57 +02:00
|
|
|
if not ns_type_pattern.match(self.type_):
|
|
|
|
log.error('Invalid querytype: %s' % self.type_)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.canexecute = False
|
|
|
|
return
|
|
|
|
|
|
|
|
def _compose_command_args(self):
|
2012-08-22 12:55:57 +02:00
|
|
|
return ['nslookup', '-type=' + self.type_, self.host]
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _return_result(self):
|
|
|
|
if self.result_handler:
|
2012-08-22 12:55:57 +02:00
|
|
|
self.result_handler(self.host, self.type_, self.result)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.result_handler = None
|
2009-06-15 21:00:14 +02:00
|
|
|
|
2014-04-09 17:04:03 +02:00
|
|
|
|
|
|
|
class Host(NsLookup):
|
|
|
|
def _compose_command_args(self):
|
|
|
|
return ['host', '-t', self.type_, self.host]
|
|
|
|
|
2016-10-02 15:01:12 +02:00
|
|
|
|
|
|
|
class GioResolver(CommonResolver):
|
|
|
|
"""
|
|
|
|
Asynchronous resolver using GIO. process() method has to be
|
|
|
|
called in order to proceed the pending requests.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.gio_resolver = Gio.Resolver.get_default()
|
|
|
|
|
|
|
|
def start_resolve(self, host, type_):
|
|
|
|
if type_ == 'txt':
|
|
|
|
# TXT record resolution isn't used anywhere at the moment so
|
|
|
|
# implementing it here isn't urgent
|
|
|
|
raise NotImplemented("Gio resolver does not currently implement TXT records")
|
|
|
|
else:
|
|
|
|
callback = functools.partial(self._on_ready_srv, host)
|
|
|
|
type_ = Gio.ResolverRecordType.SRV
|
|
|
|
|
|
|
|
resq = self.gio_resolver.lookup_records_async(host, type_, None, callback)
|
|
|
|
|
|
|
|
def _on_ready_srv(self, host, source_object, result):
|
|
|
|
try:
|
|
|
|
variant_results = source_object.lookup_records_finish(result)
|
|
|
|
except GLib.Error as e:
|
|
|
|
if e.domain == 'g-resolver-error-quark':
|
|
|
|
result_list = []
|
|
|
|
log.warning("Could not resolve host: %s", e.message)
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
result_list = [
|
|
|
|
{
|
|
|
|
'weight': weight,
|
|
|
|
'prio': prio,
|
|
|
|
'port': port,
|
|
|
|
'host': host,
|
|
|
|
}
|
|
|
|
for prio, weight, port, host
|
|
|
|
in variant_results
|
|
|
|
]
|
|
|
|
super()._on_ready(host, 'srv', result_list)
|
|
|
|
|
|
|
|
|
2006-03-30 18:10:50 +02:00
|
|
|
# below lines is on how to use API and assist in testing
|
2006-02-03 13:17:34 +01:00
|
|
|
if __name__ == '__main__':
|
2013-07-28 20:50:30 +02:00
|
|
|
from gi.repository import GLib
|
2012-12-23 16:23:43 +01:00
|
|
|
from gi.repository import Gtk
|
2012-12-09 21:37:51 +01:00
|
|
|
from nbxmpp import idlequeue
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
idlequeue = idlequeue.get_idlequeue()
|
|
|
|
resolver = get_resolver(idlequeue)
|
|
|
|
|
|
|
|
def clicked(widget):
|
|
|
|
global resolver
|
|
|
|
host = text_view.get_text()
|
|
|
|
def on_result(host, result_array):
|
2013-01-01 19:36:56 +01:00
|
|
|
print('Result:\n' + repr(result_array))
|
2010-02-08 15:08:40 +01:00
|
|
|
resolver.resolve(host, on_result)
|
2012-12-23 16:23:43 +01:00
|
|
|
win = Gtk.Window()
|
2010-02-08 15:08:40 +01:00
|
|
|
win.set_border_width(6)
|
2012-12-23 16:23:43 +01:00
|
|
|
text_view = Gtk.Entry()
|
2010-02-08 15:08:40 +01:00
|
|
|
text_view.set_text('_xmpp-client._tcp.jabber.org')
|
2012-12-23 16:23:43 +01:00
|
|
|
hbox = Gtk.HBox()
|
2010-02-08 15:08:40 +01:00
|
|
|
hbox.set_spacing(3)
|
2012-12-23 16:23:43 +01:00
|
|
|
but = Gtk.Button(' Lookup SRV ')
|
|
|
|
hbox.pack_start(text_view, 5, True, 0)
|
|
|
|
hbox.pack_start(but, 0, True, 0)
|
2010-02-08 15:08:40 +01:00
|
|
|
but.connect('clicked', clicked)
|
|
|
|
win.add(hbox)
|
|
|
|
win.show_all()
|
2013-07-28 20:50:30 +02:00
|
|
|
GLib.timeout_add(200, idlequeue.process)
|
2010-02-08 15:08:40 +01:00
|
|
|
if USE_LIBASYNCNS:
|
2013-07-28 20:50:30 +02:00
|
|
|
GLib.timeout_add(200, resolver.process)
|
2012-12-23 16:23:43 +01:00
|
|
|
Gtk.main()
|