Module Contents

The module exports following elements.

Enums

Following enumerations provides all valid values for some DNS packet and record fields.

OpCode

class anubi.mdns.OpCode

DNS packet OpCodes.

QUERY = 0

Query

IQUERY = 1

Inverse Query (Obsolete)

STATUS = 2

Status

NOTIFY = 4

Notify

UPDATE = 5

Update

DSO = 6

DNS Stateful Operations

RCode

class anubi.mdns.RCode

DNS packet RCODEs.

NOERROR = 0

No error

FORMERR = 1

Format error

SERVFAIL = 2

Server failure

NXDOMAIN = 3

Non-Existent domain

NOTIMP = 4

Not implemented

REFUSED = 5

Query refused

YXDOMAIN = 6

Name exist when it should not

YXRRSET = 7

RR Set exist when it should not

NXRRSET = 8

RR Set that should exist does not

NOTAUTH = 9

Server not authoritative for zone/Not authorized

NOTZONE = 10

Name not contained in zone

DSOTYPENI = 11

DSO-TYPE not implemented

BADVERS = 16

Bad OPT version

BADSIG = 16

TSIG signature failure

BADKEY = 17

Key not recognized

BADTIME = 18

Signature out of time window

BADMODE = 19

Bad TKEY mode

BADNAME = 20

Duplicate key name

BADALG = 21

Algorithm not supported

BADTRUNC = 22

Bad truncation

BADCOOKIE = 23

Bad/Missing server cookie

DnsType

class anubi.mdns.DnsType

DNS record types.

A = 1

Host address (IPv4)

NS = 2

Authoritative name server

MD = 3

Mail destination (Obsolete, use MX)

MF = 4

Mail forwarder (Obsolete, use MX)

CNAME = 5

Canonical name for alias

SOA = 6

Mark starts of a zone of authority

MB = 7

Mailbox domain name (Experimental)

MG = 8

Mail group member (Experimental)

MR = 9

Mail rename domain name (Experimental)

NULL = 10

Null (Experimental)

WKS = 11

Well known service description

PTR = 12

Domain name pointer

HINFO = 13

Host information

MINFO = 14

Mailbox or mail list information

MX = 15

Mail exchange

TXT = 16

Text string

RP = 17

Responsible person

AFSDB = 18

AFS database location

X25 = 19

X.25 PSDN address

ISDN = 20

ISDN address

RT = 21

Route through

NSAP = 22

NSAP address (NSAP stype A record)

NSAP_PTR = 23

Domain name pointer, NSAP style

SIG = 24

Security signature

KEY = 25

Security key

PX = 26

X.400 mail mapping information

GPOS = 27

Geographical position

AAAA = 28

Host address (IPv6)

LOC = 29

Location information

NXT = 30

Next domain (Obsolete)

EID = 31

Endpoint identifier

NIMLOC = 32

Nimrod locator

SRV = 33

Server selection

ATMA = 34

ATM address

NAPTR = 35

Naming authority pointer

KX = 36

Key exchanger

CERT = 37

CERT

A6 = 38

Host address (IPv6 - Obsolete, use AAAA)

DNAME = 39

DNAME

SINK = 40

SINK

OPT = 41

OPT

APL = 42

APL

DS = 43

Delegation singer

SSHFP = 44

SSH key fingerprint

IPSECKEY = 45

IPSECKEY

RRSIG = 46

RRSIG

NSEC = 47

NSEC

DNSKEY = 48

DNSKEY

DHCID = 49

DHCID

NSEC3 = 50

NSEC3

NSEC3PARAM = 51

NSEC3PARAM

TLSA = 52

TLSA

SIMMEA = 53

S/MIME cer association

HIP = 55

Host identity protocol

NINFO = 56

NINFO

RKEY = 57

RKEY

Trust anchor link

CDS = 59

Child DS

CDNSKEY = 60

DNSKEY(s) the child wants reflected in DS

OPENPGPKEY = 61

OpenPGP key

CSYNC = 62

Child-To-Parent syncronization

ZONEMD = 63

Message digest for DNS zone

SPF = 99

SPF

UINFO = 100

UINFO (IANA reservedd)

UID = 101

UID (IANA reservedd)

GID = 102

GID (IANA reservedd)

UNSPEC = 103

UNSPEC (IANA reservedd)

NID = 104

NID

L32 = 105

L32

L64 = 106

L64

LP = 107

LP

EUI48 = 108

EUI-48 address

EUI64 = 109

EUI-64 address

TKEY = 249

Transaction key

TSIG = 250

Transaction signature

IXFR = 251

Incremental transfer

AXFR = 252

Transfer of an entire zone

MAILB = 253

Mailbox related resource record (MB, MG or MR)

MAILA = 254

Mail agent (Obsolete, see MX)

ANY = 255

Any

URI = 256

URI

CAA = 257

Certification authority restriction

AVC = 258

Application visibility and control

DOA = 259

Digital object architecture

AMTRELAY = 260

Automatic multicast tunnelling relay

TA = 32769

DNSSEC trust authorities

DLV = 32769

DNSSEC lookaside validation (Obsolete)

contains_value = <bound method DnsType.contains_value of <enum 'DnsType'>>
is_obsolete = <bound method DnsType.is_obsolete of <enum 'DnsType'>>

DnsClass

class anubi.mdns.DnsClass

DNS class types.

IN = 1

Internet

CS = 2

CSNET (Obsolete)

CH = 3

Chaos

HS = 4

Hesoid

NONE = 254

None

ANY = 255

Any

contains_value = <bound method DnsClass.contains_value of <enum 'DnsClass'>>

DNS Classes

Following classes can be used to manipulate DNS packets and records.

mDNS

class anubi.mdns.mDNS

Implements the multicast DNS logic. Once started it spawn a new thread that reply to mDNS queries when there is at least one answer to the query question in the internal record database.

add_record(record)
Parameters:record (DnsRRecord) – The record that has to be added to the internal database.
Returns:True if the record has been added, False otherwise.

Add the given record to the internal class database. This method can be called any time, even after the instance has been started by the start() method.

start()

Start the mDSN instance. Once started the instance reply to mDNS queries if the internal database contains at least one record that reply to query questions. This method cannot be called on a stopped instance.

stop()

Stop a running instance of mDNS that do not reply to queries anymore. Once stopped an mDNS instance cannot be started again.

DnsPacket

class anubi.mdns.DnsPacket(is_query)
Parameters:is_query (bool) – A flag that specify if the packes is a query packet or not.

Represent a DNS packet and exposes API to control packet content.

classmethod decode(data)
Parameters:data (bytearray) – Array of bytes that has to be decoded.
Returns:A new instance of DnsPacket built from given data.

Decodes the given data into a DnsPacket instance.

encode(compress_names=True)
Parameters:compress_names (bool) – A flag that indicates if the domain names in the packed has to be compressed or not. Default to True
Returns:A bytearray that represent the DnsPacket instance.

Encode the intance to a bytearray compressing domain names according with the given flag.

AA

Flags that indicates if the packet is an authoritative answer (True) or not (False).

AD

Flags that indicates if the data in answer and authority sections has been authenticated (True) or not (False).

CD

Flags that indicates if server that reply to the quesry has to authenticate the data in answer and authority sections (True) or not (False).

OpCode

The packet OpCode.

QR

Flag that indicates if the packet represent a query (True) or not (False).

RA

Flags that indicates if the name resolution recursion is available (True) or not (False) on the server that send the reply.

RCODE

The packet RCode (response code).

RD

Flags that indicates if the name resolution recursion for the query is desired (True) or not (False).

TC

Flags that indicates if the packet is truncated (True) or not (False).

additional_records

The list of additional records in the packet.

answer_records

The list of answer records in the packet.

authority_records

The list of authority records in the packet.

id

The query identifier of the packet.

question_records

The list of question records in the packet.

DnsQRecord

class anubi.mdns.DnsQRecord(name, type_, class_)
Parameters:
  • name (str) – The record domain name.
  • type (int) – The record DnsType.
  • class (int) – The record DnsClass.

Represent a DNS record and allows to maipulate its properties.

classmethod decode(data, idx)
Parameters:
  • data (bytearray) – Array of bytes that has to be decoded.
  • idx (Index) – The index from where the record data begins in the data array.

Decode the given data starting from the given index into a DnsQRecord instance.

encode(data, name_compression_dictionary)
Parameters:
  • data (bytearray) – The bytearray to which the instance data will be appended.
  • name_compression_dictionary (dict[str, int]) – a dictionary used to perform name compression. Keys are the domain names, values are the indexes in the data array where the domain name begin.

Encode the intance to a bytearray compressing domain names according with the given name_compression_dictionary. The resulting bytearray will be attached to the given data parameter. If name_compression_dictionary is None no name compression will be applied.

name

The record domain name.

prefer_unicast

Flag that indicates if unicast reply are preferred (True) or not (False).

record_class

The record DnsClass.

record_type

The record DnsType.

DnsRRecord

class anubi.mdns.DnsRRecord(name, type_, class_, ttl)
Parameters:
  • name (str) – The record domain name.
  • type (int) – The record DnsType.
  • class (int) – The record DnsClass.
  • ttl (int) – The record TTL in milliseconds.

Represent a DNS record and allows to maipulate its properties. This class is a base class for specialized record types and connot be instantiated.

answer_to(question)
Parameters:question (DnsQRecord) – The DnsQRecord instance for which the current instance has to be tested.
Returns:True if the instance answer to the given question, False otherwse.
Return type:bool

Check if the record is a reply for the given question. A record answer to a question if all following criterias are satisfied:

  • The record and question domain names are the same.
  • The record and question types are the same OR the question type is DnsType.ANY OR the record type is DnsType.CNAME.
  • The record and question classes are the same OR the question class is DnsClass.ANY.
classmethod decode(data, idx)
Parameters:
  • data (bytearray) – Array of bytes that has to be decoded.
  • idx (Index) – The index from where the record data begins in the data array.

Decode the given data starting from the given index into a proper DnsRRecord subclass instance.

encode(data, name_compression_dictionary)
Parameters:
  • data (bytearray) – The bytearray to which the instance data will be appended.
  • name_compression_dictionary (dict[str, int]) – a dictionary used to perform name compression. Keys are the domain names, values are the indexes in the data array where the domain name begin.

Encode the intance to a bytearray compressing domain names according with the given name_compression_dictionary. The resulting bytearray will be attached to the given data parameter. If name_compression_dictionary is None no name compression will be applied.

flush_cache

A flag that indicates if the client that receive this record has to flush its cached copies (True) or not (False).

name

The record domain name.

record_class

The record DnsClass.

record_type

The record DnsType.

ttl

The record TTL in milliseconds.

DnsRRecordA

class anubi.mdns.DnsRRecordA(name, ttl, address)
Parameters:
  • name (str) – The record domain name.
  • ttl (int) – The record TTL in milliseconds.
  • address (str) – The record IP address (IPv4).

Represent a DNS Record of type A and allows to manipulate its properties.

answer_to(question)
Parameters:question (DnsQRecord) – The DnsQRecord instance for which the current instance has to be tested.
Returns:True if the instance answer to the given question, False otherwse.
Return type:bool

Check if the record is a reply for the given question. A record answer to a question if all following criterias are satisfied:

  • The record and question domain names are the same.
  • The record and question types are the same OR the question type is DnsType.ANY OR the record type is DnsType.CNAME.
  • The record and question classes are the same OR the question class is DnsClass.ANY.
classmethod decode(data, idx)
Parameters:
  • data (bytearray) – Array of bytes that has to be decoded.
  • idx (Index) – The index from where the record data begins in the data array.

Decode the given data starting from the given index into a proper DnsRRecord subclass instance.

encode(data, name_compression_dictionary)
Parameters:
  • data (bytearray) – The bytearray to which the instance data will be appended.
  • name_compression_dictionary (dict[str, int]) – a dictionary used to perform name compression. Keys are the domain names, values are the indexes in the data array where the domain name begin.

Encode the intance to a bytearray compressing domain names according with the given name_compression_dictionary. The resulting bytearray will be attached to the given data parameter. If name_compression_dictionary is None no name compression will be applied.

address

The record IPv4 address.

flush_cache

A flag that indicates if the client that receive this record has to flush its cached copies (True) or not (False).

name

The record domain name.

record_class

The record DnsClass.

record_type

The record DnsType.

ttl

The record TTL in milliseconds.

DnsRRecordAAAA

class anubi.mdns.DnsRRecordAAAA(name, ttl, address)
Parameters:
  • name (str) – The record domain name.
  • ttl (int) – The record TTL in milliseconds.
  • address (str) – The record IP address (IPv6).

Represent a DNS Record of type AAAA and allows to manipulate its properties.

answer_to(question)
Parameters:question (DnsQRecord) – The DnsQRecord instance for which the current instance has to be tested.
Returns:True if the instance answer to the given question, False otherwse.
Return type:bool

Check if the record is a reply for the given question. A record answer to a question if all following criterias are satisfied:

  • The record and question domain names are the same.
  • The record and question types are the same OR the question type is DnsType.ANY OR the record type is DnsType.CNAME.
  • The record and question classes are the same OR the question class is DnsClass.ANY.
classmethod decode(data, idx)
Parameters:
  • data (bytearray) – Array of bytes that has to be decoded.
  • idx (Index) – The index from where the record data begins in the data array.

Decode the given data starting from the given index into a proper DnsRRecord subclass instance.

encode(data, name_compression_dictionary)
Parameters:
  • data (bytearray) – The bytearray to which the instance data will be appended.
  • name_compression_dictionary (dict[str, int]) – a dictionary used to perform name compression. Keys are the domain names, values are the indexes in the data array where the domain name begin.

Encode the intance to a bytearray compressing domain names according with the given name_compression_dictionary. The resulting bytearray will be attached to the given data parameter. If name_compression_dictionary is None no name compression will be applied.

address

The record IPv6 address.

flush_cache

A flag that indicates if the client that receive this record has to flush its cached copies (True) or not (False).

name

The record domain name.

record_class

The record DnsClass.

record_type

The record DnsType.

ttl

The record TTL in milliseconds.

DnsRRecordNotImplemented

Note

This class does not actually decode/encode the raw data, just copy those data from/to the given bytearray.

class anubi.mdns.DnsRRecordNotImplemented(name, type_, class_, ttl, data_size, data)
Parameters:
  • name (str) – The record domain name.
  • type (int) – The record DnsType.
  • class (int) – The record DnsClass.
  • ttl (int) – The record TTL in milliseconds.
  • data_size (int) – The record data size in bytes.
  • data (bytearray) – The record raw data.

Represent a DNS record and allows to maipulate its properties. This class is a base class for specialized record types and connot be instantiated.

answer_to(question)
Parameters:question (DnsQRecord) – The DnsQRecord instance for which the current instance has to be tested.
Returns:True if the instance answer to the given question, False otherwse.
Return type:bool

Check if the record is a reply for the given question. A record answer to a question if all following criterias are satisfied:

  • The record and question domain names are the same.
  • The record and question types are the same OR the question type is DnsType.ANY OR the record type is DnsType.CNAME.
  • The record and question classes are the same OR the question class is DnsClass.ANY.
classmethod decode(data, idx)
Parameters:
  • data (bytearray) – Array of bytes that has to be decoded.
  • idx (Index) – The index from where the record data begins in the data array.

Decode the given data starting from the given index into a proper DnsRRecord subclass instance.

encode(data, name_compression_dictionary)
Parameters:
  • data (bytearray) – The bytearray to which the instance data will be appended.
  • name_compression_dictionary (dict[str, int]) – a dictionary used to perform name compression. Keys are the domain names, values are the indexes in the data array where the domain name begin.

Encode the intance to a bytearray compressing domain names according with the given name_compression_dictionary. The resulting bytearray will be attached to the given data parameter. If name_compression_dictionary is None no name compression will be applied.

data

The record data section (raw bytes representation).

data_size

The size of the record data section in bytes.

flush_cache

A flag that indicates if the client that receive this record has to flush its cached copies (True) or not (False).

is_obsolete

A flag that indicates if the record represented by the instance is of an obsolete :class`DnsType` (True) or not (False).

is_unknown

A flag that indicates if the record represented by the instance is of a known DnsType and DnsClass (True) or not (False).

name

The record domain name.

record_class

The record DnsClass.

record_type

The record DnsType.

ttl

The record TTL in milliseconds.

Utility Classes

Following classes implements utility functionality for the packet. Most of the time the user do not need to use this classes, they are usefull only for some advanced uses of the module.

Index

class anubi.mdns.Index(value=0)
Parameters:value (int or Index) – The initial value of the instance. Default to 0.

Represent a mutable integer that is very usefull when used as index in encode/decode methods since it will be automatically updated and so keeps track of the current position in the data array. this class mimic the int builtin class for addition and indexing.

advance(quantity)
Parameters:quantity (int) – The ammount to which the instance value has to be incremented.

Increment the instance value by the given quantity.

Decorators

Following decorators provides easy ways for the user to extend the module functionalities.

register_subclass

anubi.mdns.register_subclass(record_type)
Parameters:record_type (DnsType) – The DnsType that the decorated class implements.

This decorator can only be applied to class that derives from DnsRRecord. If applied to a class that does not satisfy the constrain a RuntimeError exception will be raised. Scope of this decorator is to register the class ad secoder for the given record_type.