Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Bitmap.from_rdtypes() #906

Merged
merged 5 commits into from
Mar 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions dns/rdtypes/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import collections
import random
import struct
from typing import Any, List

import dns.exception
import dns.ipv4
Expand Down Expand Up @@ -132,7 +133,7 @@ def __init__(self, windows=None):
if len(bitmap) == 0 or len(bitmap) > 32:
raise ValueError(f"bad {self.type_name} octets")

def to_text(self):
def to_text(self) -> str:
text = ""
for (window, bitmap) in self.windows:
bits = []
Expand All @@ -145,14 +146,18 @@ def to_text(self):
return text

@classmethod
def from_text(cls, tok):
def from_text(cls, tok: "dns.tokenizer.Tokenizer") -> "Bitmap":
rdtypes = []
for token in tok.get_remaining():
rdtype = dns.rdatatype.from_text(token.unescape().value)
if rdtype == 0:
raise dns.exception.SyntaxError(f"{cls.type_name} with bit 0")
rdtypes.append(rdtype)
rdtypes.sort()
return cls.from_rdtypes(rdtypes)

@classmethod
def from_rdtypes(cls, rdtypes: List[dns.rdatatype.RdataType]) -> "Bitmap":
rdtypes = sorted(rdtypes)
window = 0
octets = 0
prior_rdtype = 0
Expand All @@ -177,13 +182,13 @@ def from_text(cls, tok):
windows.append((window, bytes(bitmap[0:octets])))
return cls(windows)

def to_wire(self, file):
def to_wire(self, file: Any) -> None:
for (window, bitmap) in self.windows:
file.write(struct.pack("!BB", window, len(bitmap)))
file.write(bitmap)

@classmethod
def from_wire_parser(cls, parser):
def from_wire_parser(cls, parser: "dns.wire.Parser") -> "Bitmap":
windows = []
while parser.remaining() > 0:
window = parser.get_uint8()
Expand Down