Debian Codegraph @

dns/rrset.py

raw
1 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
2
3 # Copyright (C) 2003-2017 Nominum, Inc.
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 """DNS RRsets (an RRset is a named rdataset)"""
19
20 from typing import Any, Collection, Dict, cast
21
22 import dns.name
23 import dns.rdata
24 import dns.rdataclass
25 import dns.rdataset
26 import dns.rdatatype
27 import dns.renderer
28
29
30 class RRset(dns.rdataset.Rdataset):
31 """A DNS RRset (named rdataset).
32
33 RRset inherits from Rdataset, and RRsets can be treated as
34 Rdatasets in most cases. There are, however, a few notable
35 exceptions. RRsets have different to_wire() and to_text() method
36 arguments, reflecting the fact that RRsets always have an owner
37 name.
38 """
39
40 __slots__ = ["name", "deleting"]
41
42 def __init__(
43 self,
44 name: dns.name.Name,
45 rdclass: dns.rdataclass.RdataClass,
46 rdtype: dns.rdatatype.RdataType,
47 covers: dns.rdatatype.RdataType = dns.rdatatype.NONE,
48 deleting: dns.rdataclass.RdataClass | None = None,
49 ):
50 """Create a new RRset."""
51
52 super().__init__(rdclass, rdtype, covers)
53 self.name = name
54 self.deleting = deleting
55
56 def _clone(self):
57 obj = cast(RRset, super()._clone())
58 obj.name = self.name
59 obj.deleting = self.deleting
60 return obj
61
62 def __repr__(self):
63 if self.covers == 0:
64 ctext = ""
65 else:
66 ctext = "(" + dns.rdatatype.to_text(self.covers) + ")"
67 if self.deleting is not None:
68 dtext = " delete=" + dns.rdataclass.to_text(self.deleting)
69 else:
70 dtext = ""
71 return (
72 "<DNS "
73 + str(self.name)
74 + " "
75 + dns.rdataclass.to_text(self.rdclass)
76 + " "
77 + dns.rdatatype.to_text(self.rdtype)
78 + ctext
79 + dtext
80 + " RRset: "
81 + self._rdata_repr()
82 + ">"
83 )
84
85 def __str__(self):
86 return self.to_text()
87
88 def __eq__(self, other):
89 if isinstance(other, RRset):
90 if self.name != other.name:
91 return False
92 elif not isinstance(other, dns.rdataset.Rdataset):
93 return False
94 return super().__eq__(other)
95
96 def match(self, *args: Any, **kwargs: Any) -> bool: # type: ignore[override]
97 """Does this rrset match the specified attributes?
98
99 Behaves as :py:func:`full_match()` if the first argument is a
100 ``dns.name.Name``, and as :py:func:`dns.rdataset.Rdataset.match()`
101 otherwise.
102
103 (This behavior fixes a design mistake where the signature of this
104 method became incompatible with that of its superclass. The fix
105 makes RRsets matchable as Rdatasets while preserving backwards
106 compatibility.)
107 """
108 if isinstance(args[0], dns.name.Name):
109 return self.full_match(*args, **kwargs) # type: ignore[arg-type]
110 else:
111 return super().match(*args, **kwargs) # type: ignore[arg-type]
112
113 def full_match(
114 self,
115 name: dns.name.Name,
116 rdclass: dns.rdataclass.RdataClass,
117 rdtype: dns.rdatatype.RdataType,
118 covers: dns.rdatatype.RdataType,
119 deleting: dns.rdataclass.RdataClass | None = None,
120 ) -> bool:
121 """Returns ``True`` if this rrset matches the specified name, class,
122 type, covers, and deletion state.
123 """
124 if not super().match(rdclass, rdtype, covers):
125 return False
126 if self.name != name or self.deleting != deleting:
127 return False
128 return True
129
130 # pylint: disable=arguments-differ
131
132 def to_text( # type: ignore[override]
133 self,
134 origin: dns.name.Name | None = None,
135 relativize: bool = True,
136 **kw: Dict[str, Any],
137 ) -> str:
138 """Convert the RRset into DNS zone file format.
139
140 See ``dns.name.Name.choose_relativity`` for more information
141 on how *origin* and *relativize* determine the way names
142 are emitted.
143
144 Any additional keyword arguments are passed on to the rdata
145 ``to_text()`` method.
146
147 *origin*, a ``dns.name.Name`` or ``None``, the origin for relative
148 names.
149
150 *relativize*, a ``bool``. If ``True``, names will be relativized
151 to *origin*.
152 """
153
154 return super().to_text(
155 self.name, origin, relativize, self.deleting, **kw # type: ignore
156 )
157
158 def to_wire( # type: ignore[override]
159 self,
160 file: Any,
161 compress: dns.name.CompressType | None = None, # type: ignore
162 origin: dns.name.Name | None = None,
163 **kw: Dict[str, Any],
164 ) -> int:
165 """Convert the RRset to wire format.
166
167 All keyword arguments are passed to ``dns.rdataset.to_wire()``; see
168 that function for details.
169
170 Returns an ``int``, the number of records emitted.
171 """
172
173 return super().to_wire(
174 self.name, file, compress, origin, self.deleting, **kw # type:ignore
175 )
176
177 # pylint: enable=arguments-differ
178
179 def to_rdataset(self) -> dns.rdataset.Rdataset:
180 """Convert an RRset into an Rdataset.
181
182 Returns a ``dns.rdataset.Rdataset``.
183 """
184 return dns.rdataset.from_rdata_list(self.ttl, list(self))
185
186
187 def from_text_list(
188 name: dns.name.Name | str,
189 ttl: int,
190 rdclass: dns.rdataclass.RdataClass | str,
191 rdtype: dns.rdatatype.RdataType | str,
192 text_rdatas: Collection[str],
193 idna_codec: dns.name.IDNACodec | None = None,
194 origin: dns.name.Name | None = None,
195 relativize: bool = True,
196 relativize_to: dns.name.Name | None = None,
197 ) -> RRset:
198 """Create an RRset with the specified name, TTL, class, and type, and with
199 the specified list of rdatas in text format.
200
201 *idna_codec*, a ``dns.name.IDNACodec``, specifies the IDNA
202 encoder/decoder to use; if ``None``, the default IDNA 2003
203 encoder/decoder is used.
204
205 *origin*, a ``dns.name.Name`` (or ``None``), the
206 origin to use for relative names.
207
208 *relativize*, a ``bool``. If true, name will be relativized.
209
210 *relativize_to*, a ``dns.name.Name`` (or ``None``), the origin to use
211 when relativizing names. If not set, the *origin* value will be used.
212
213 Returns a ``dns.rrset.RRset`` object.
214 """
215
216 if isinstance(name, str):
217 name = dns.name.from_text(name, None, idna_codec=idna_codec)
218 rdclass = dns.rdataclass.RdataClass.make(rdclass)
219 rdtype = dns.rdatatype.RdataType.make(rdtype)
220 r = RRset(name, rdclass, rdtype)
221 r.update_ttl(ttl)
222 for t in text_rdatas:
223 rd = dns.rdata.from_text(
224 r.rdclass, r.rdtype, t, origin, relativize, relativize_to, idna_codec
225 )
226 r.add(rd)
227 return r
228
229
230 def from_text(
231 name: dns.name.Name | str,
232 ttl: int,
233 rdclass: dns.rdataclass.RdataClass | str,
234 rdtype: dns.rdatatype.RdataType | str,
235 *text_rdatas: Any,
236 ) -> RRset:
237 """Create an RRset with the specified name, TTL, class, and type and with
238 the specified rdatas in text format.
239
240 Returns a ``dns.rrset.RRset`` object.
241 """
242
243 return from_text_list(
244 name, ttl, rdclass, rdtype, cast(Collection[str], text_rdatas)
245 )
246
247
248 def from_rdata_list(
249 name: dns.name.Name | str,
250 ttl: int,
251 rdatas: Collection[dns.rdata.Rdata],
252 idna_codec: dns.name.IDNACodec | None = None,
253 ) -> RRset:
254 """Create an RRset with the specified name and TTL, and with
255 the specified list of rdata objects.
256
257 *idna_codec*, a ``dns.name.IDNACodec``, specifies the IDNA
258 encoder/decoder to use; if ``None``, the default IDNA 2003
259 encoder/decoder is used.
260
261 Returns a ``dns.rrset.RRset`` object.
262
263 """
264
265 if isinstance(name, str):
266 name = dns.name.from_text(name, None, idna_codec=idna_codec)
267
268 if len(rdatas) == 0:
269 raise ValueError("rdata list must not be empty")
270 r = None
271 for rd in rdatas:
272 if r is None:
273 r = RRset(name, rd.rdclass, rd.rdtype)
274 r.update_ttl(ttl)
275 r.add(rd)
276 assert r is not None
277 return r
278
279
280 def from_rdata(name: dns.name.Name | str, ttl: int, *rdatas: Any) -> RRset:
281 """Create an RRset with the specified name and TTL, and with
282 the specified rdata objects.
283
284 Returns a ``dns.rrset.RRset`` object.
285 """
286
287 return from_rdata_list(name, ttl, cast(Collection[dns.rdata.Rdata], rdatas))