---
name: "dnsjit.core.object.dns"
section: "3"
platform: "linux"
package: "dnsjit"
version: "1.5.0-2"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/dnsjit.core.object.dns"
---

# dnsjit.core.object.dns(3)

Container of a DNS message

## NAME

dnsjit.core.object.dns - Container of a DNS message

## SYNOPSIS

### Parse DNS header and check if query or response

  local dns = require("dnsjit.core.object.dns").new(payload)\
   if dns:parse\_header() == 0 then\
     if dns.qr == 0 then\
       print(dns.id, dns.opcode\_tostring(dns.opcode))\
     else\
       print(dns.id, dns.rcode\_tostring(dns.rcode))\
     end\
   end

### Print a DNS payload

  local dns = require("dnsjit.core.object.dns").new(payload)\
   dns:print()

### Parse a DNS payload

  local dns = require("dnsjit.core.object.dns").new(payload)\
   local qs, q\_labels, rrs, rr\_labels = dns:parse()\
   if qs and q\_labels then\
     ...\
     if rrs and rr\_labels then\
       ...\
     end\
   end

## DESCRIPTION

The object that describes a DNS message.

### Attributes

- includes\_dnslen

  If non-zero then this indicates that the DNS length is included in the payload (for example if the transport is TCP) and will affect parsing of it.

- have\_dnslen

  Set if the dnslen was included in the payload and could be read.

- have\_id

  Set if there is a DNS ID.

- have\_qr

  Set if there is a QR flag.

- have\_opcode

  Set if there is an OPCODE.

- have\_aa

  Set if there is a AA flag.

- have\_tc

  Set if there is a TC flag.

- have\_rd

  Set if there is a RD flag.

- have\_ra

  Set if there is a RA flag.

- have\_z

  Set if there is a Z flag.

- have\_ad

  Set if there is a AD flag.

- have\_cd

  Set if there is a CD flag.

- have\_rcode

  Set if there is a RCODE.

- have\_qdcount

  Set if there is an QDCOUNT.

- have\_ancount

  Set if there is an ANCOUNT.

- have\_nscount

  Set if there is a NSCOUNT.

- have\_arcount

  Set if there is an ARCOUNT.

- dnslen

  The DNS length found in the payload.

- id

  The DNS ID.

- qr

  The QR flag.

- opcode

  The OPCODE.

- aa

  The AA flag.

- tc

  The TC flag.

- rd

  The RD flag.

- ra

  The RA flag.

- z

  The Z flag.

- ad

  The AD flag.

- cd

  The CD flag.

- rcode

  The RCODE.

- qdcount

  The QDCOUNT.

- ancount

  The ANCOUNT.

- nscount

  The NSCOUNT.

- arcount

  The ARCOUNT.

### Constants

The following tables exists for DNS parameters, taken from *https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml* on the 2016-12-09.

*CLASS*, *CLASS\_STR*, *TYPE*, *TYPE\_STR*, *OPCODE*, *OPCODE\_STR*, *RCODE*, *RCODE\_STR*, *AFSDB*, *AFSDB\_STR*, *DHCID*, *DHCID\_STR*, *ENDS0*, *ENDS0\_STR*

The *\*\_STR* tables can be used to get a textual representation of the numbers, see also *class\_tostring()*, *type\_tostring()*, *opcode\_tostring()*, *rcode\_tostring()*, *afsdb\_tostring()*, *dhcid\_tostring()* and *edns0\_tostring()*.

### Functions

- **Dns.new**(obj)

  Create a new DNS object, optionally on-top of another object.

- **Dns:type**()

  Return the textual type of the object.

- **Dns:prev**()

  Return the previous object.

- **Dns:cast**()

  Cast the object to the underlining object module and return it.

- **Dns:uncast**()

  Cast the object to the generic object module and return it.

- **Dns:copy**()

  Make a copy of the object and return it.

- **Dns:free**()

  Free the object, should only be used on copies or otherwise allocated.

- **Dns:reset**()

  Reset the object readying it for reuse.

- **Dns:log**()

  Return the Log object to control logging of this module.

- **Dns:parse\_header**()

  Begin parsing the underlaying object, first the header is parsed then optionally continue calling *parse\_q*() for the number of questions (see *qdcount*). After that continue calling *parse\_rr*() for the number of answers, authorities and additionals resource records (see *ancount*,  *nscount* and *arcount*). Returns 0 on success or negative integer on error which can be for malformed or truncated DNS (-2) or if more space for labels is needed (-3).

- **Dns:parse\_q**(q, labels, num\_labels)

  Parse the next resource record as a question. Returns 0 on success or negative integer on error which can be for malformed or truncated DNS (-2) or if more space for labels is needed (-3).

- **Dns:parse\_rr**(rr, labels, num\_labels)

  Parse the next resource record. Returns 0 on success or negative integer on error which can be for malformed or truncated DNS (-2) or if more space for labels is needed (-3).

- **Dns:parse**(num\_labels)

  Begin parsing the underlaying object using *parse\_header*(),  *parse\_q*() and *parse\_rr*(). The optional *num\_labels* can be used to set a specific number of labels used for each question and resource record (default 16). Returns result code, an array of questions, an array of question labels, an array of resource records and an array of resource records labels. Result code is 0 on success or negative integer on error which can be for malformed or truncated DNS (-2) or if more space for labels is needed (-3).

- **Dns:print**(num\_labels)

  Begin parsing the underlaying object using *parse\_header*(),  *parse\_q*() and *parse\_rr*(), and print it's content. The optional *num\_labels* can be used to set a specific number of labels used for each question and resource record (default 16).

- **Dns.class\_tostring**(class)

  Return the textual name for a class.

- **Dns.type\_tostring**(type)

  Return the textual name for a type.

- **Dns.opcode\_tostring**(opcode)

  Return the textual name for an opcode.

- **Dns.rcode\_tostring**(rcode)

  Return the textual name for a rcode.

- **Dns.afsdb\_tostring**(afsdb)

  Return the textual name for an afsdb subtype.

- **Dns.dhcid\_tostring**(dhcid)

  Return the textual name for a dhcid type.

- **Dns.edns0\_tostring**(edns0)

  Return the textual name for an EDNS0 OPT record.

## SEE ALSO

[**dnsjit.core.object**(3)](/dnsjit.core.object/3.md), [**dnsjit.core.object.payload**(3)](/dnsjit.core.object.payload/3.md), [**dnsjit.core.object.dns.label**(3)](/dnsjit.core.object.dns.label/3.md), [**dnsjit.core.object.dns.q**(3)](/dnsjit.core.object.dns.q/3.md), [**dnsjit.core.object.dns.rr**(3)](/dnsjit.core.object.dns.rr/3.md)

## AUTHORS and CONTRIBUTORS

Jerry Lundström (DNS-OARC), Tomáš Křížek (CZ.NIC), Petr Špaček (ISC)

Maintained by DNS-OARC

*https://www.dns-oarc.net/*

## BUGS

For issues and feature requests please use:

*https://github.com/DNS-OARC/dnsjit/issues*

For question and help please use:

*admin@dns-oarc.net*
