| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| """DNS Message Flags.""" |
|
|
| import enum |
| from typing import Any |
|
|
| |
|
|
|
|
| class Flag(enum.IntFlag): |
| |
| QR = 0x8000 |
| |
| AA = 0x0400 |
| |
| TC = 0x0200 |
| |
| RD = 0x0100 |
| |
| RA = 0x0080 |
| |
| AD = 0x0020 |
| |
| CD = 0x0010 |
|
|
|
|
| |
|
|
|
|
| class EDNSFlag(enum.IntFlag): |
| |
| DO = 0x8000 |
|
|
|
|
| def _from_text(text: str, enum_class: Any) -> int: |
| flags = 0 |
| tokens = text.split() |
| for t in tokens: |
| flags |= enum_class[t.upper()] |
| return flags |
|
|
|
|
| def _to_text(flags: int, enum_class: Any) -> str: |
| text_flags = [] |
| for k, v in enum_class.__members__.items(): |
| if flags & v != 0: |
| text_flags.append(k) |
| return " ".join(text_flags) |
|
|
|
|
| def from_text(text: str) -> int: |
| """Convert a space-separated list of flag text values into a flags |
| value. |
| |
| Returns an ``int`` |
| """ |
|
|
| return _from_text(text, Flag) |
|
|
|
|
| def to_text(flags: int) -> str: |
| """Convert a flags value into a space-separated list of flag text |
| values. |
| |
| Returns a ``str``. |
| """ |
|
|
| return _to_text(flags, Flag) |
|
|
|
|
| def edns_from_text(text: str) -> int: |
| """Convert a space-separated list of EDNS flag text values into a EDNS |
| flags value. |
| |
| Returns an ``int`` |
| """ |
|
|
| return _from_text(text, EDNSFlag) |
|
|
|
|
| def edns_to_text(flags: int) -> str: |
| """Convert an EDNS flags value into a space-separated list of EDNS flag |
| text values. |
| |
| Returns a ``str``. |
| """ |
|
|
| return _to_text(flags, EDNSFlag) |
|
|
|
|
| |
|
|
| QR = Flag.QR |
| AA = Flag.AA |
| TC = Flag.TC |
| RD = Flag.RD |
| RA = Flag.RA |
| AD = Flag.AD |
| CD = Flag.CD |
|
|
| |
|
|
| |
|
|
| DO = EDNSFlag.DO |
|
|
| |
|
|