From 56fbe32b113b5e316a0cb9eeb2c202d34ae52c07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Sun, 27 May 2018 21:03:42 +0200 Subject: [PATCH] Fix zeroconf txtrecord encoding Fixes #9146 --- gajim/common/zeroconf/zeroconf_avahi.py | 40 +++++++++-------------- gajim/common/zeroconf/zeroconf_bonjour.py | 3 -- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/gajim/common/zeroconf/zeroconf_avahi.py b/gajim/common/zeroconf/zeroconf_avahi.py index 7b18bb1ff..6f97db5fd 100644 --- a/gajim/common/zeroconf/zeroconf_avahi.py +++ b/gajim/common/zeroconf/zeroconf_avahi.py @@ -130,25 +130,21 @@ class Zeroconf: def txt_array_to_dict(self, txt_array): txt_dict = {} - for els in txt_array: - key, val = '', None - for c in els: - c = chr(c) - if val is None: - if c == '=': - val = '' - else: - key += c + for array in txt_array: + item = bytes(array) + item = item.decode('utf-8') + item = item.split('=', 1) + + if item[0] and (item[0] not in txt_dict): + if len(item) == 1: + txt_dict[item[0]] = None else: - val += c - if val is None: # missing '=' - val = '' - txt_dict[key] = val + txt_dict[item[0]] = item[1] + return txt_dict @staticmethod def string_to_byte_array(s): - s = s.encode('utf-8') r = [] for c in s: @@ -157,18 +153,14 @@ class Zeroconf: return r def dict_to_txt_array(self, txt_dict): - l = [] + array = [] - for k,v in txt_dict.items(): - if isinstance(k, str): - k = k.encode('utf-8') + for k, v in txt_dict.items(): + item = '%s=%s' % (k, v) + item = item.encode('utf-8') + array.append(self.string_to_byte_array(item)) - if isinstance(v, str): - v = v.encode('utf-8') - - l.append(self.string_to_byte_array("%s=%s" % (k,v))) - - return l + return array def service_resolved_callback(self, interface, protocol, name, stype, domain, host, aprotocol, address, port, txt, flags): diff --git a/gajim/common/zeroconf/zeroconf_bonjour.py b/gajim/common/zeroconf/zeroconf_bonjour.py index a30e2e93a..ff39d5729 100644 --- a/gajim/common/zeroconf/zeroconf_bonjour.py +++ b/gajim/common/zeroconf/zeroconf_bonjour.py @@ -139,9 +139,6 @@ class Zeroconf: def query_txt_callback(self, sdRef, flags, interfaceIndex, errorCode, hosttarget, rrtype, rrclass, rdata, ttl): - # Callback from DNSServiceQueryRecord, it does not call decode() - # on rdata - rdata = rdata.decode() if errorCode != pybonjour.kDNSServiceErr_NoError: log.error('Error in query_record_callback: %s', str(errorCode))