1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """DNS E.164 helpers."""
19
20 import dns.exception
21 import dns.name
22 import dns.resolver
23 from ._compat import string_types, maybe_decode
24
25
26 public_enum_domain = dns.name.from_text('e164.arpa.')
27
28
30 """Convert an E.164 number in textual form into a Name object whose
31 value is the ENUM domain name for that number.
32
33 Non-digits in the text are ignored, i.e. "16505551212",
34 "+1.650.555.1212" and "1 (650) 555-1212" are all the same.
35
36 *text*, a ``text``, is an E.164 number in textual form.
37
38 *origin*, a ``dns.name.Name``, the domain in which the number
39 should be constructed. The default is ``e164.arpa.``.
40
41 Returns a ``dns.name.Name``.
42 """
43
44 parts = [d for d in text if d.isdigit()]
45 parts.reverse()
46 return dns.name.from_text('.'.join(parts), origin=origin)
47
48
50 """Convert an ENUM domain name into an E.164 number.
51
52 Note that dnspython does not have any information about preferred
53 number formats within national numbering plans, so all numbers are
54 emitted as a simple string of digits, prefixed by a '+' (unless
55 *want_plus_prefix* is ``False``).
56
57 *name* is a ``dns.name.Name``, the ENUM domain name.
58
59 *origin* is a ``dns.name.Name``, a domain containing the ENUM
60 domain name. The name is relativized to this domain before being
61 converted to text. If ``None``, no relativization is done.
62
63 *want_plus_prefix* is a ``bool``. If True, add a '+' to the beginning of
64 the returned number.
65
66 Returns a ``text``.
67
68 """
69 if origin is not None:
70 name = name.relativize(origin)
71 dlabels = [d for d in name.labels if d.isdigit() and len(d) == 1]
72 if len(dlabels) != len(name.labels):
73 raise dns.exception.SyntaxError('non-digit labels in ENUM domain name')
74 dlabels.reverse()
75 text = b''.join(dlabels)
76 if want_plus_prefix:
77 text = b'+' + text
78 return maybe_decode(text)
79
80
81 -def query(number, domains, resolver=None):
82 """Look for NAPTR RRs for the specified number in the specified domains.
83
84 e.g. lookup('16505551212', ['e164.dnspython.org.', 'e164.arpa.'])
85
86 *number*, a ``text`` is the number to look for.
87
88 *domains* is an iterable containing ``dns.name.Name`` values.
89
90 *resolver*, a ``dns.resolver.Resolver``, is the resolver to use. If
91 ``None``, the default resolver is used.
92 """
93
94 if resolver is None:
95 resolver = dns.resolver.get_default_resolver()
96 e_nx = dns.resolver.NXDOMAIN()
97 for domain in domains:
98 if isinstance(domain, string_types):
99 domain = dns.name.from_text(domain)
100 qname = dns.e164.from_e164(number, domain)
101 try:
102 return resolver.query(qname, 'NAPTR')
103 except dns.resolver.NXDOMAIN as e:
104 e_nx += e
105 raise e_nx
106