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.

108 lines
3.9 KiB

4 years ago
  1. # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
  2. # Copyright (C) 2003-2017 Nominum, Inc.
  3. # Copyright (C) 2016 Coresec Systems AB
  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. # THE SOFTWARE IS PROVIDED "AS IS" AND CORESEC SYSTEMS AB DISCLAIMS ALL
  19. # WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  20. # WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL CORESEC
  21. # SYSTEMS AB BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  22. # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  23. # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  24. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  25. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  26. """DNS name dictionary"""
  27. import collections
  28. import dns.name
  29. from ._compat import xrange
  30. class NameDict(collections.MutableMapping):
  31. """A dictionary whose keys are dns.name.Name objects.
  32. In addition to being like a regular Python dictionary, this
  33. dictionary can also get the deepest match for a given key.
  34. """
  35. __slots__ = ["max_depth", "max_depth_items", "__store"]
  36. def __init__(self, *args, **kwargs):
  37. super(NameDict, self).__init__()
  38. self.__store = dict()
  39. #: the maximum depth of the keys that have ever been added
  40. self.max_depth = 0
  41. #: the number of items of maximum depth
  42. self.max_depth_items = 0
  43. self.update(dict(*args, **kwargs))
  44. def __update_max_depth(self, key):
  45. if len(key) == self.max_depth:
  46. self.max_depth_items = self.max_depth_items + 1
  47. elif len(key) > self.max_depth:
  48. self.max_depth = len(key)
  49. self.max_depth_items = 1
  50. def __getitem__(self, key):
  51. return self.__store[key]
  52. def __setitem__(self, key, value):
  53. if not isinstance(key, dns.name.Name):
  54. raise ValueError('NameDict key must be a name')
  55. self.__store[key] = value
  56. self.__update_max_depth(key)
  57. def __delitem__(self, key):
  58. value = self.__store.pop(key)
  59. if len(value) == self.max_depth:
  60. self.max_depth_items = self.max_depth_items - 1
  61. if self.max_depth_items == 0:
  62. self.max_depth = 0
  63. for k in self.__store:
  64. self.__update_max_depth(k)
  65. def __iter__(self):
  66. return iter(self.__store)
  67. def __len__(self):
  68. return len(self.__store)
  69. def has_key(self, key):
  70. return key in self.__store
  71. def get_deepest_match(self, name):
  72. """Find the deepest match to *fname* in the dictionary.
  73. The deepest match is the longest name in the dictionary which is
  74. a superdomain of *name*. Note that *superdomain* includes matching
  75. *name* itself.
  76. *name*, a ``dns.name.Name``, the name to find.
  77. Returns a ``(key, value)`` where *key* is the deepest
  78. ``dns.name.Name``, and *value* is the value associated with *key*.
  79. """
  80. depth = len(name)
  81. if depth > self.max_depth:
  82. depth = self.max_depth
  83. for i in xrange(-depth, 0):
  84. n = dns.name.Name(name[i:])
  85. if n in self:
  86. return (n, self[n])
  87. v = self[dns.name.empty]
  88. return (dns.name.empty, v)