home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / pyxmpp / resolver.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  3.1 KB  |  119 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. __revision__ = '$Id: resolver.py 714 2010-04-05 10:20:10Z jajcus $'
  5. __docformat__ = 'restructuredtext en'
  6. import re
  7. import socket
  8. import dns.resolver as dns
  9. import dns.name as dns
  10. import dns.exception as dns
  11. import random
  12. from encodings import idna
  13. service_aliases = {
  14.     'xmpp-server': ('jabber-server', 'jabber') }
  15. ip_re = re.compile('\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}')
  16.  
  17. def shuffle_srv(records):
  18.     if not records:
  19.         return []
  20.     ret = []
  21.     while len(records) > 1:
  22.         weight_sum = 0
  23.         for rr in records:
  24.             weight_sum += rr.weight + 0.1
  25.         
  26.         thres = random.random() * weight_sum
  27.         weight_sum = 0
  28.         for rr in records:
  29.             weight_sum += rr.weight + 0.1
  30.             if thres < weight_sum:
  31.                 records.remove(rr)
  32.                 ret.append(rr)
  33.                 break
  34.                 continue
  35.             records
  36.         
  37.     ret.append(records[0])
  38.     return ret
  39.  
  40.  
  41. def reorder_srv(records):
  42.     records = list(records)
  43.     records.sort()
  44.     ret = []
  45.     tmp = []
  46.     for rr in records:
  47.         if not tmp or rr.priority == tmp[0].priority:
  48.             tmp.append(rr)
  49.             continue
  50.         
  51.         ret += shuffle_srv(tmp)
  52.     
  53.     if tmp:
  54.         ret += shuffle_srv(tmp)
  55.     
  56.     return ret
  57.  
  58.  
  59. def resolve_srv(domain, service, proto = 'tcp'):
  60.     names_to_try = [
  61.         u'_%s._%s.%s' % (service, proto, domain)]
  62.     if service_aliases.has_key(service):
  63.         for a in service_aliases[service]:
  64.             names_to_try.append(u'_%s._%s.%s' % (a, proto, domain))
  65.         
  66.     
  67.     for name in names_to_try:
  68.         name = idna.ToASCII(name)
  69.         
  70.         try:
  71.             r = dns.resolver.query(name, 'SRV')
  72.         except dns.exception.DNSException:
  73.             continue
  74.  
  75.         if not r:
  76.             continue
  77.         
  78.         return [ (rr.target.to_text(), rr.port) for rr in reorder_srv(r) ]
  79.     
  80.  
  81.  
  82. def getaddrinfo(host, port, family = 0, socktype = socket.SOCK_STREAM, proto = 0, allow_cname = True):
  83.     ret = []
  84.     if proto == 0:
  85.         proto = socket.getprotobyname('tcp')
  86.     elif type(proto) != int:
  87.         proto = socket.getprotobyname(proto)
  88.     
  89.     if type(port) != int:
  90.         port = socket.getservbyname(port, proto)
  91.     
  92.     if family not in (0, socket.AF_INET):
  93.         raise NotImplementedError, 'Protocol family other than AF_INET not supported, yet'
  94.     family not in (0, socket.AF_INET)
  95.     if ip_re.match(host):
  96.         return [
  97.             (socket.AF_INET, socktype, proto, host, (host, port))]
  98.     host = idna.ToASCII(host)
  99.     
  100.     try:
  101.         r = dns.resolver.query(host, 'A')
  102.     except dns.exception.DNSException:
  103.         ip_re.match(host)
  104.         ip_re.match(host)
  105.         r = dns.resolver.query(host + '.', 'A')
  106.     except:
  107.         ip_re.match(host)
  108.  
  109.     if not allow_cname and r.rrset.name != dns.name.from_text(host):
  110.         raise ValueError, 'Unexpected CNAME record found for %s' % (host,)
  111.     r.rrset.name != dns.name.from_text(host)
  112.     if r:
  113.         for rr in r:
  114.             ret.append((socket.AF_INET, socktype, proto, r.rrset.name, (rr.to_text(), port)))
  115.         
  116.     
  117.     return ret
  118.  
  119.