| 1 |
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license |
| 2 |
|
| 3 |
# Copyright (C) 2006-2017 Nominum, Inc. |
| 4 |
# |
| 5 |
# Permission to use, copy, modify, and distribute this software and its |
| 6 |
# documentation for any purpose with or without fee is hereby granted, |
| 7 |
# provided that the above copyright notice and this permission notice |
| 8 |
# appear in all copies. |
| 9 |
# |
| 10 |
# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES |
| 11 |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR |
| 13 |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 16 |
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 |
|
| 18 |
"""DNS E.164 helpers.""" |
| 19 |
|
| 20 |
from typing import Iterable |
| 21 |
|
| 22 |
import dns.exception |
| 23 |
import dns.name |
| 24 |
import dns.resolver |
| 25 |
|
| 26 |
#: The public E.164 domain. |
| 27 |
public_enum_domain = dns.name.from_text("e164.arpa.") |
| 28 |
|
| 29 |
|
| 30 |
def from_e164( |
| 31 |
text: str, origin: dns.name.Name | None = public_enum_domain |
| 32 |
) -> dns.name.Name: |
| 33 |
"""Convert an E.164 number in textual form into a Name object whose |
| 34 |
value is the ENUM domain name for that number. |
| 35 |
|
| 36 |
Non-digits in the text are ignored, i.e. "16505551212", |
| 37 |
"+1.650.555.1212" and "1 (650) 555-1212" are all the same. |
| 38 |
|
| 39 |
*text*, a ``str``, is an E.164 number in textual form. |
| 40 |
|
| 41 |
*origin*, a ``dns.name.Name``, the domain in which the number |
| 42 |
should be constructed. The default is ``e164.arpa.``. |
| 43 |
|
| 44 |
Returns a ``dns.name.Name``. |
| 45 |
""" |
| 46 |
|
| 47 |
parts = [d for d in text if d.isdigit()] |
| 48 |
parts.reverse() |
| 49 |
return dns.name.from_text(".".join(parts), origin=origin) |
| 50 |
|
| 51 |
|
| 52 |
def to_e164( |
| 53 |
name: dns.name.Name, |
| 54 |
origin: dns.name.Name | None = public_enum_domain, |
| 55 |
want_plus_prefix: bool = True, |
| 56 |
) -> str: |
| 57 |
"""Convert an ENUM domain name into an E.164 number. |
| 58 |
|
| 59 |
Note that dnspython does not have any information about preferred |
| 60 |
number formats within national numbering plans, so all numbers are |
| 61 |
emitted as a simple string of digits, prefixed by a '+' (unless |
| 62 |
*want_plus_prefix* is ``False``). |
| 63 |
|
| 64 |
*name* is a ``dns.name.Name``, the ENUM domain name. |
| 65 |
|
| 66 |
*origin* is a ``dns.name.Name``, a domain containing the ENUM |
| 67 |
domain name. The name is relativized to this domain before being |
| 68 |
converted to text. If ``None``, no relativization is done. |
| 69 |
|
| 70 |
*want_plus_prefix* is a ``bool``. If True, add a '+' to the beginning of |
| 71 |
the returned number. |
| 72 |
|
| 73 |
Returns a ``str``. |
| 74 |
|
| 75 |
""" |
| 76 |
if origin is not None: |
| 77 |
name = name.relativize(origin) |
| 78 |
dlabels = [d for d in name.labels if d.isdigit() and len(d) == 1] |
| 79 |
if len(dlabels) != len(name.labels): |
| 80 |
raise dns.exception.SyntaxError("non-digit labels in ENUM domain name") |
| 81 |
dlabels.reverse() |
| 82 |
text = b"".join(dlabels) |
| 83 |
if want_plus_prefix: |
| 84 |
text = b"+" + text |
| 85 |
return text.decode() |
| 86 |
|
| 87 |
|
| 88 |
def query( |
| 89 |
number: str, |
| 90 |
domains: Iterable[dns.name.Name | str], |
| 91 |
resolver: dns.resolver.Resolver | None = None, |
| 92 |
) -> dns.resolver.Answer: |
| 93 |
"""Look for NAPTR RRs for the specified number in the specified domains. |
| 94 |
|
| 95 |
e.g. lookup('16505551212', ['e164.dnspython.org.', 'e164.arpa.']) |
| 96 |
|
| 97 |
*number*, a ``str`` is the number to look for. |
| 98 |
|
| 99 |
*domains* is an iterable containing ``dns.name.Name`` values. |
| 100 |
|
| 101 |
*resolver*, a ``dns.resolver.Resolver``, is the resolver to use. If |
| 102 |
``None``, the default resolver is used. |
| 103 |
""" |
| 104 |
|
| 105 |
if resolver is None: |
| 106 |
resolver = dns.resolver.get_default_resolver() |
| 107 |
e_nx = dns.resolver.NXDOMAIN() |
| 108 |
for domain in domains: |
| 109 |
if isinstance(domain, str): |
| 110 |
domain = dns.name.from_text(domain) |
| 111 |
qname = from_e164(number, domain) |
| 112 |
try: |
| 113 |
return resolver.resolve(qname, "NAPTR") |
| 114 |
except dns.resolver.NXDOMAIN as e: |
| 115 |
e_nx += e |
| 116 |
raise e_nx |