You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.7 KiB

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