# # (C) Copyright 2003-2010 Jacek Konieczny # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License Version # 2.1 as published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # """DNS resolever with SRV record support. Normative reference: - `RFC 1035 `__ - `RFC 2782 `__ """ __docformat__="restructuredtext en" import re import socket from socket import AF_UNSPEC, AF_INET, AF_INET6 import dns.resolver import dns.name import dns.exception import random import logging from .exceptions import DNSError, UnexpectedCNAMEError logger = logging.getLogger("pyxmpp.resolver") # check IPv6 support try: socket.socket(AF_INET6) except socket.error: default_address_family = AF_INET else: default_address_family = AF_UNSPEC def set_default_address_family(family): """Select default address family. :Parameters: - `family`: `AF_INET` for IPv4, `AF_INET6` for IPv6 and `AF_UNSPEC` for dual stack.""" global default_address_family default_address_family = family service_aliases={"xmpp-server": ("jabber-server","jabber")} # should match all valid IP addresses, but can pass some false-positives, # which are not valid domain names ipv4_re=re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$") ipv6_re=re.compile(r"^[0-9a-f]{0,4}:[0-9a-f:]{0,29}:([0-9a-f]{0,4}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$") def shuffle_srv(records): """Randomly reorder SRV records using their weights. :Parameters: - `records`: SRV records to shuffle. :Types: - `records`: sequence of `dns.rdtypes.IN.SRV` :return: reordered records. :returntype: `list` of `dns.rdtypes.IN.SRV`""" if not records: return [] ret=[] while len(records)>1: weight_sum=0 for rr in records: weight_sum+=rr.weight+0.1 thres=random.random()*weight_sum weight_sum=0 for rr in records: weight_sum+=rr.weight+0.1 if thres