node.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # Copyright (C) 2001-2007, 2009-2011 Nominum, Inc.
  2. #
  3. # Permission to use, copy, modify, and distribute this software and its
  4. # documentation for any purpose with or without fee is hereby granted,
  5. # provided that the above copyright notice and this permission notice
  6. # appear in all copies.
  7. #
  8. # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
  9. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
  11. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  14. # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. """DNS nodes. A node is a set of rdatasets."""
  16. from io import StringIO
  17. import dns.rdataset
  18. import dns.rdatatype
  19. import dns.renderer
  20. class Node(object):
  21. """A DNS node.
  22. A node is a set of rdatasets
  23. @ivar rdatasets: the node's rdatasets
  24. @type rdatasets: list of dns.rdataset.Rdataset objects"""
  25. __slots__ = ['rdatasets']
  26. def __init__(self):
  27. """Initialize a DNS node.
  28. """
  29. self.rdatasets = []
  30. def to_text(self, name, **kw):
  31. """Convert a node to text format.
  32. Each rdataset at the node is printed. Any keyword arguments
  33. to this method are passed on to the rdataset's to_text() method.
  34. @param name: the owner name of the rdatasets
  35. @type name: dns.name.Name object
  36. @rtype: string
  37. """
  38. s = StringIO()
  39. for rds in self.rdatasets:
  40. if len(rds) > 0:
  41. s.write(rds.to_text(name, **kw))
  42. s.write(u'\n')
  43. return s.getvalue()[:-1]
  44. def __repr__(self):
  45. return '<DNS node ' + str(id(self)) + '>'
  46. def __eq__(self, other):
  47. """Two nodes are equal if they have the same rdatasets.
  48. @rtype: bool
  49. """
  50. #
  51. # This is inefficient. Good thing we don't need to do it much.
  52. #
  53. for rd in self.rdatasets:
  54. if rd not in other.rdatasets:
  55. return False
  56. for rd in other.rdatasets:
  57. if rd not in self.rdatasets:
  58. return False
  59. return True
  60. def __ne__(self, other):
  61. return not self.__eq__(other)
  62. def __len__(self):
  63. return len(self.rdatasets)
  64. def __iter__(self):
  65. return iter(self.rdatasets)
  66. def find_rdataset(self, rdclass, rdtype, covers=dns.rdatatype.NONE,
  67. create=False):
  68. """Find an rdataset matching the specified properties in the
  69. current node.
  70. @param rdclass: The class of the rdataset
  71. @type rdclass: int
  72. @param rdtype: The type of the rdataset
  73. @type rdtype: int
  74. @param covers: The covered type. Usually this value is
  75. dns.rdatatype.NONE, but if the rdtype is dns.rdatatype.SIG or
  76. dns.rdatatype.RRSIG, then the covers value will be the rdata
  77. type the SIG/RRSIG covers. The library treats the SIG and RRSIG
  78. types as if they were a family of
  79. types, e.g. RRSIG(A), RRSIG(NS), RRSIG(SOA). This makes RRSIGs much
  80. easier to work with than if RRSIGs covering different rdata
  81. types were aggregated into a single RRSIG rdataset.
  82. @type covers: int
  83. @param create: If True, create the rdataset if it is not found.
  84. @type create: bool
  85. @raises KeyError: An rdataset of the desired type and class does
  86. not exist and I{create} is not True.
  87. @rtype: dns.rdataset.Rdataset object
  88. """
  89. for rds in self.rdatasets:
  90. if rds.match(rdclass, rdtype, covers):
  91. return rds
  92. if not create:
  93. raise KeyError
  94. rds = dns.rdataset.Rdataset(rdclass, rdtype)
  95. self.rdatasets.append(rds)
  96. return rds
  97. def get_rdataset(self, rdclass, rdtype, covers=dns.rdatatype.NONE,
  98. create=False):
  99. """Get an rdataset matching the specified properties in the
  100. current node.
  101. None is returned if an rdataset of the specified type and
  102. class does not exist and I{create} is not True.
  103. @param rdclass: The class of the rdataset
  104. @type rdclass: int
  105. @param rdtype: The type of the rdataset
  106. @type rdtype: int
  107. @param covers: The covered type.
  108. @type covers: int
  109. @param create: If True, create the rdataset if it is not found.
  110. @type create: bool
  111. @rtype: dns.rdataset.Rdataset object or None
  112. """
  113. try:
  114. rds = self.find_rdataset(rdclass, rdtype, covers, create)
  115. except KeyError:
  116. rds = None
  117. return rds
  118. def delete_rdataset(self, rdclass, rdtype, covers=dns.rdatatype.NONE):
  119. """Delete the rdataset matching the specified properties in the
  120. current node.
  121. If a matching rdataset does not exist, it is not an error.
  122. @param rdclass: The class of the rdataset
  123. @type rdclass: int
  124. @param rdtype: The type of the rdataset
  125. @type rdtype: int
  126. @param covers: The covered type.
  127. @type covers: int
  128. """
  129. rds = self.get_rdataset(rdclass, rdtype, covers)
  130. if rds is not None:
  131. self.rdatasets.remove(rds)
  132. def replace_rdataset(self, replacement):
  133. """Replace an rdataset.
  134. It is not an error if there is no rdataset matching I{replacement}.
  135. Ownership of the I{replacement} object is transferred to the node;
  136. in other words, this method does not store a copy of I{replacement}
  137. at the node, it stores I{replacement} itself.
  138. """
  139. if not isinstance(replacement, dns.rdataset.Rdataset):
  140. raise ValueError('replacement is not an rdataset')
  141. self.delete_rdataset(replacement.rdclass, replacement.rdtype,
  142. replacement.covers)
  143. self.rdatasets.append(replacement)