namedict.py 3.6 KB

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