From 49a78ae8ed999ee8866ea570746b9c1b61dd0984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Fri, 16 Jun 2017 23:40:42 +0200 Subject: [PATCH] Support IPV6 and IPV4 domainparts https://tools.ietf.org/html/rfc7622#section-3.2 --- src/common/helpers.py | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/src/common/helpers.py b/src/common/helpers.py index 0d8f4cf70..17a1fb9d0 100644 --- a/src/common/helpers.py +++ b/src/common/helpers.py @@ -173,6 +173,35 @@ def prep(user, server, resource): """ # This function comes from #http://svn.twistedmatrix.com/cvs/trunk/twisted/words/protocols/jabber/jid.py + + ip_address = False + + try: + socket.inet_aton(server) + ip_address = True + except socket.error: + pass + + if not ip_address and hasattr(socket, 'inet_pton'): + try: + socket.inet_pton(socket.AF_INET6, server.strip('[]')) + server = '[%s]' % server.strip('[]') + ip_address = True + except (socket.error, ValueError): + pass + + if not ip_address: + if server is not None: + if len(server) < 1 or len(server) > 1023: + raise InvalidFormat(_('Server must be between 1 and 1023 chars')) + try: + from nbxmpp.stringprepare import nameprep + server = nameprep.prepare(server) + except UnicodeError: + raise InvalidFormat(_('Invalid character in hostname.')) + else: + raise InvalidFormat(_('Server address required.')) + if user is not None: if len(user) < 1 or len(user) > 1023: raise InvalidFormat(_('Username must be between 1 and 1023 chars')) @@ -184,17 +213,6 @@ def prep(user, server, resource): else: user = None - if server is not None: - if len(server) < 1 or len(server) > 1023: - raise InvalidFormat(_('Server must be between 1 and 1023 chars')) - try: - from nbxmpp.stringprepare import nameprep - server = nameprep.prepare(server) - except UnicodeError: - raise InvalidFormat(_('Invalid character in hostname.')) - else: - raise InvalidFormat(_('Server address required.')) - if resource is not None: if len(resource) < 1 or len(resource) > 1023: raise InvalidFormat(_('Resource must be between 1 and 1023 chars'))