ttl.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
  2. # Copyright (C) 2003-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 TTL conversion."""
  17. from typing import Union
  18. import dns.exception
  19. # Technically TTLs are supposed to be between 0 and 2**31 - 1, with values
  20. # greater than that interpreted as 0, but we do not impose this policy here
  21. # as values > 2**31 - 1 occur in real world data.
  22. #
  23. # We leave it to applications to impose tighter bounds if desired.
  24. MAX_TTL = 2**32 - 1
  25. class BadTTL(dns.exception.SyntaxError):
  26. """DNS TTL value is not well-formed."""
  27. def from_text(text: str) -> int:
  28. """Convert the text form of a TTL to an integer.
  29. The BIND 8 units syntax for TTLs (e.g. '1w6d4h3m10s') is supported.
  30. *text*, a ``str``, the textual TTL.
  31. Raises ``dns.ttl.BadTTL`` if the TTL is not well-formed.
  32. Returns an ``int``.
  33. """
  34. if text.isdigit():
  35. total = int(text)
  36. elif len(text) == 0:
  37. raise BadTTL
  38. else:
  39. total = 0
  40. current = 0
  41. need_digit = True
  42. for c in text:
  43. if c.isdigit():
  44. current *= 10
  45. current += int(c)
  46. need_digit = False
  47. else:
  48. if need_digit:
  49. raise BadTTL
  50. c = c.lower()
  51. if c == "w":
  52. total += current * 604800
  53. elif c == "d":
  54. total += current * 86400
  55. elif c == "h":
  56. total += current * 3600
  57. elif c == "m":
  58. total += current * 60
  59. elif c == "s":
  60. total += current
  61. else:
  62. raise BadTTL(f"unknown unit '{c}'")
  63. current = 0
  64. need_digit = True
  65. if not current == 0:
  66. raise BadTTL("trailing integer")
  67. if total < 0 or total > MAX_TTL:
  68. raise BadTTL("TTL should be between 0 and 2**32 - 1 (inclusive)")
  69. return total
  70. def make(value: Union[int, str]) -> int:
  71. if isinstance(value, int):
  72. return value
  73. elif isinstance(value, str):
  74. return dns.ttl.from_text(value)
  75. else:
  76. raise ValueError("cannot convert value to TTL")