Class ldns_pkt

Class ldns_pkt

class ldns.ldns_pkt

LDNS packet object.

The ldns_pkt object contains DNS packed (either a query or an answer). It is the complete representation of what you actually send to a name server, and what you get back (see ldns.ldns_resolver).

Usage

>>> import ldns
>>> resolver = ldns.ldns_resolver.new_frm_file("/etc/resolv.conf")
>>> pkt = resolver.query("nic.cz", ldns.LDNS_RR_TYPE_NS,ldns.LDNS_RR_CLASS_IN)
>>> print pkt
;; ->>HEADER<<- opcode: QUERY, rcode: NOERROR, id: 63004
;; flags: qr rd ra ; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0 
;; QUESTION SECTION:
;; nic.cz.  IN      NS
;; ANSWER SECTION:
nic.cz.     758     IN      NS      a.ns.nic.cz.
nic.cz.     758     IN      NS      c.ns.nic.cz.
nic.cz.     758     IN      NS      e.ns.nic.cz.
;; AUTHORITY SECTION:
;; ADDITIONAL SECTION:
;; Query time: 8 msec
;; SERVER: 82.100.38.2
;; WHEN: Thu Jan 11 12:54:33 2009
;; MSG SIZE  rcvd: 75

This simple example instances a resolver in order to resolve NS for nic.cz.

aa()

Read the packet’s aa bit.

Returns:(bool) Value of the bit.
ad()

Read the packet’s ad bit.

Returns:(bool) Value of the bit.
additional()

Return the packet’s additional section.

Returns:(ldns_rr_list) The additional section.
static algorithm2str(alg)

Converts a signing algorithms to its mnemonic and returns that as an allocated null-terminated string.

Parameters:alg (ldns_algorithm) – The algorithm to convert to text.
Returns:(str)
all()

Return the packet’s question, answer, authority and additional sections concatenated.

Returns:(ldns_rr_list) Concatenated sections.
all_noquestion()

Return the packet’s answer, authority and additional sections concatenated. Like all() but without the questions.

Returns:(ldns_rr_list) Concatenated sections except questions.
ancount()

Return the packet’s an count.

Returns:(int) The an count.
answer()

Return the packet’s answer section.

Returns:(ldns_rr_list) The answer section.
answerfrom()

Return the packet’s answerfrom.

Returns:(ldns_rdf) The name of the server.
arcount()

Return the packet’s ar count.

Returns:(int) The ar count.
authority()

Return the packet’s authority section.

Returns:(ldns_rr_list) The authority section.
cd()

Read the packet’s cd bit.

Returns:(bool) Value of the bit.
static cert_algorithm2str(alg)

Converts a cert algorithm to its mnemonic and returns that as an allocated null-terminated string.

Parameters:alg (ldns_cert_algorithm) – Cert algorithm to convert to text.
Returns:(str)
clone()

Clones the packet, creating a fully allocated copy.

Returns:(ldns_pkt) New packet clone.
edns()

Returns True if this packet needs and EDNS rr to be sent.

At the moment the only reason is an expected packet size larger than 512 bytes, but for instance DNSSEC would be a good reason too.

Returns:(bool) True if packet needs EDNS rr.
edns_data()

Return the packet’s edns data.

Returns:(ldns_rdf) The ensd data.
edns_do()

Return the packet’s edns do bit

Returns:(bool) The bit’s value.
edns_extended_rcode()

Return the packet’s edns extended rcode.

Returns:(uint8_t) The rcode.
edns_udp_size()

Return the packet’s edns udp size.

Returns:(uint16_t) The udp size.
edns_version()

Return the packet’s edns version.

Returns:(uint8_t) The edns version.
edns_z()

Return the packet’s edns z value.

Returns:(uint16_t) The z value.
empty()

Check if a packet is empty.

Returns:(bool) True: empty, False: not empty
get_opcode()

Read the packet’s code.

Returns:(ldns_pkt_opcode) the opcode
get_rcode()

Return the packet’s response code.

Returns:(ldns_pkt_rcode) The response code.
get_section_clone(s)

Return the selected rr_list’s in the packet.

Parameters:s (ldns_pkt_section) – What section(s) to return.
Throws TypeError:
 When arguments of inappropriate types.
Returns:(ldns_rr_list) RR list with the rr’s or None if none were found.
id()

Read the packet id.

Returns:(uint16_t) The packet id.
static new()

Creates new empty packet structure.

Returns:(ldns_pkt ) New empty packet.
static new_query(rr_name, rr_type, rr_class, flags)

Creates a packet with a query in it for the given name, type and class.

Parameters:
  • rr_name (ldns_dname) – The name to query for.
  • rr_type (ldns_rr_type) – The type to query for.
  • rr_class (ldns_rr_class) – The class to query for.
  • flags (uint16_t) – Packet flags.
Throws TypeError:
 

When arguments of inappropriate types.

Returns:

(ldns_pkt) New object.

Note

The type checking of parameter rr_name is benevolent. It allows also to pass a dname ldns_rdf object. This will probably change in future.

static new_query_frm_str(rr_name, rr_type, rr_class, flags, raiseException=True)

Creates a query packet for the given name, type, class.

Parameters:
  • rr_name (str) – The name to query for.
  • rr_type (ldns_rr_type) – The type to query for.
  • rr_class (ldns_rr_class) – The class to query for.
  • flags (uint16_t) – Packet flags.
  • raiseException – If True, an exception occurs in case a packet object can’t be created.
Throws TypeError:
 

When arguments of inappropriate types.

Throws Exception:
 

When raiseException set and packet couldn’t be created.

Returns:

(ldns_pkt) Query packet object or None. If the object can’t be created and raiseException is True, an exception occurs.

Usage

>>> pkt = ldns.ldns_pkt.new_query_frm_str("test.nic.cz",ldns.LDNS_RR_TYPE_ANY, ldns.LDNS_RR_CLASS_IN, ldns.LDNS_QR | ldns.LDNS_AA)
>>> rra = ldns.ldns_rr.new_frm_str("test.nic.cz. IN A 192.168.1.1",300)
>>> list = ldns.ldns_rr_list()
>>> if (rra): list.push_rr(rra)
>>> pkt.push_rr_list(ldns.LDNS_SECTION_ANSWER, list)
>>> print pkt
;; ->>HEADER<<- opcode: QUERY, rcode: NOERROR, id: 0
;; flags: qr aa ; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0 
;; QUESTION SECTION:
;; test.nic.cz.      IN      ANY
;; ANSWER SECTION:
test.nic.cz. 300     IN      A       192.168.1.1
;; AUTHORITY SECTION:
;; ADDITIONAL SECTION:
;; Query time: 0 msec
;; WHEN: Thu Jan  1 01:00:00 1970
;; MSG SIZE  rcvd: 0
nscount()

Return the packet’s ns count.

Returns:(uint16_t) The ns count.
opcode2str()

Converts a packet opcode to its mnemonic and returns that as an allocated null-terminated string.

Returns:(str)
print_to_file(output)

Prints the data in the DNS packet to the given file stream (in presentation format).

Parameters:output (file) – Opened file to write to.
Throws TypeError:
 When arguments of inappropriate types.
push_rr(section, rr)

Push an rr on a packet.

Parameters:
  • section (ldns_pkt_section) – Where to put it.
  • rr (ldns_rr) – RR to push.
Throws TypeError:
 

When arguments of inappropriate types.

Returns:

(bool) A boolean which is True when the rr was added.

push_rr_list(section, list)

Push a rr_list on a packet.

Parameters:
  • section (ldns_pkt_section) – Where to put it.
  • list (ldns_rr_list) – The rr_list to push.
Throws TypeError:
 

When arguments of inappropriate types.

Returns:

(bool) A boolean which is True when the rr was added.

qdcount()

Return the packet’s qd count.

Returns:(uint16_t) The qd count.
qr()

Read the packet’s qr bit.

Returns:(bool) value of the bit
querytime()

Return the packet’s query time.

Returns:(uint32_t) The query time.
question()

Return the packet’s question section.

Returns:(ldns_rr_list) The question section.
ra()

Read the packet’s ra bit.

Returns:(bool) Value of the bit.
rcode2str()

Converts a packet rcode to its mnemonic and returns that as an allocated null-terminated string.

Returns:(str)
rd()

Read the packet’s rd bit.

Returns:(bool) Value of the bit.
reply_type()

Looks inside the packet to determine what kind of packet it is, AUTH, NXDOMAIN, REFERRAL, etc.

Returns:(ldns_pkt_type) The type of packet.
rr(sec, rr)

Check to see if an rr exist in the packet.

Parameters:
  • sec (ldns_pkt_section) – In which section to look.
  • rr (ldns_rr) – The rr to look for.
Throws TypeError:
 

When arguments of inappropriate types.

Returns:

(bool) Return True is exists.

rr_list_by_name(r, s)

Return all the rr with a specific name from a packet.

Parameters:
  • r (ldns_rdf) – The name.
  • s (ldns_pkt_section) – The packet’s section.
Throws TypeError:
 

When arguments of inappropriate types.

Returns:

(ldns_rr_list) A list with the rr’s or None if none were found.

rr_list_by_name_and_type(ownername, atype, sec)

Return all the rr with a specific type and type from a packet.

Parameters:
  • ownername (ldns_rdf) – The name.
  • atype (ldns_rr_type) – The type.
  • sec (ldns_pkt_section) – The packet’s section.
Throws TypeError:
 

When arguments of inappropriate types.

Returns:

(ldns_rr_list) A list with the rr’s or None if none were found.

rr_list_by_type(t, s)

Return all the rr with a specific type from a packet.

Parameters:
  • t (ldns_rr_type) – The type.
  • s (ldns_pkt_section) – The packet’s section.
Throws TypeError:
 

When arguments of inappropriate types.

Returns:

(ldns_rr_list) A list with the rr’s or None if none were found.

safe_push_rr(sec, rr)

Push an rr on a packet, provided the RR is not there.

Parameters:
  • sec (ldns_pkt_section) – Where to put it.
  • rr (ldns_rr) – RR to push.
Throws TypeError:
 

When arguments of inappropriate types.

Returns:

(bool) A boolean which is True when the rr was added.

safe_push_rr_list(sec, list)

Push an rr_list to a packet, provided the RRs are not already there.

Parameters:
  • sec (ldns_pkt_section) – Where to put it.
  • list (ldns_rr_list) – The rr_list to push.
Throws TypeError:
 

When arguments of inappropriate types.

Returns:

(bool) A boolean which is True when the list was added.

set_aa(b)

Set the packet’s aa bit.

Parameters:b (bool) – The value to set.
set_ad(b)

Set the packet’s ad bit.

Parameters:b (bool) – The value to set.
set_additional(rr)

Directly set the additional section.

Parameters:rr (ldns_rr_list) – The rr list to set.
Throws TypeError:
 When arguments of inappropriate types.
set_ancount(c)

Set the packet’s an count.

Parameters:c (int) – The count.
Throws TypeError:
 When arguments of inappropriate types.
set_answer(rr)

Directly set the answer section.

Parameters:rr (ldns_rr_list) – The rr list to set.
Throws TypeError:
 When arguments of inappropriate types.
set_answerfrom(r)

Set the packet’s answering server.

Parameters:r (ldns_rdf) – The address.
Throws TypeError:
 When arguments of inappropriate types.
set_arcount(c)

Set the packet’s arcount.

Parameters:c (int) – The count.
Throws TypeError:
 When arguments of inappropriate types.
set_authority(rr)

Directly set the authority section.

Parameters:rr (ldns_rr_list) – The rr list to set.
Throws TypeError:
 When arguments of inappropriate types.
set_cd(b)

Set the packet’s cd bit.

Parameters:b (bool) – The value to set.
set_edns_data(data)

Set the packet’s edns data.

Parameters:data (ldns_rdf) – The data.
Throws TypeError:
 When arguments of inappropriate types.
set_edns_do(value)

Set the packet’s edns do bit.

Parameters:value (bool) – The bit’s new value.
set_edns_extended_rcode(c)

Set the packet’s edns extended rcode.

Parameters:c (uint8_t) – The code.
Throws TypeError:
 When arguments of inappropriate types.
set_edns_udp_size(s)

Set the packet’s edns udp size.

Parameters:s (uint16_t) – The size.
Throws TypeError:
 When arguments of inappropriate types.
set_edns_version(v)

Set the packet’s edns version.

Parameters:v (uint8_t) – The version.
Throws TypeError:
 When arguments of inappropriate types.
set_edns_z(z)

Set the packet’s edns z value.

Parameters:z (uint16_t) – The value.
Throws TypeError:
 When arguments of inappropriate types.
set_flags(flags)

Sets the flags in a packet.

Parameters:flags (int) – ORed values: LDNS_QR| LDNS_AR for instance.
Throws TypeError:
 When arguments of inappropriate types.
Returns:(bool) True on success, False otherwise.
set_id(id)

Set the packet’s id.

Parameters:id (uint16_t) – The id to set.
Throws TypeError:
 When arguments of inappropriate types.
set_nscount(c)

Set the packet’s ns count.

Parameters:c (int) – The count.
Throws TypeError:
 When arguments of inappropriate types.
set_opcode(c)

Set the packet’s opcode.

Parameters:c (ldns_pkt_opcode) – The opcode.
Throws TypeError:
 When arguments of inappropriate types.
set_qdcount(c)

Set the packet’s qd count.

Parameters:c (int) – The count.
Throws TypeError:
 When arguments of inappropriate types.
set_qr(b)

Set the packet’s qr bit.

Parameters:b (bool) – The value to set.
set_querytime(t)

Set the packet’s query time.

Parameters:t (uint32_t) – The query time in msec.
Throws TypeError:
 When arguments of inappropriate types.
set_question(rr)

Directly set the question section.

Parameters:rr (ldns_rr_list) – The rr list to set.
Throws TypeError:
 When arguments of inappropriate types.
set_ra(b)

Set the packet’s ra bit.

Parameters:b (bool) – The value to set.
set_random_id()

Set the packet’s id to a random value.

set_rcode(c)

Set the packet’s respons code.

Parameters:c (uint8_t) – The rcode.
Throws TypeError:
 When arguments of inappropriate types.
set_rd(b)

Set the packet’s rd bit.

Parameters:b (bool) – The value to set.
set_section_count(s, x)

Set a packet’s section count to x.

Parameters:
  • s (ldns_pkt_section) – The section.
  • x (uint16_t) – The section count.
Throws TypeError:
 

When arguments of inappropriate types.

set_size(s)

Set the packet’s size.

Parameters:s (int) – The size.
Throws TypeError:
 When arguments of inappropriate types.
set_tc(b)

Set the packet’s tc bit.

Parameters:b (bool) – The value to set.
set_timestamp(timeval)

Set the packet’s time stamp.

Parameters:timestamp (struct timeval) – The time stamp.
Throws TypeError:
 When arguments of inappropriate types.
set_tsig(t)

Set the packet’s tsig rr.

Parameters:t (ldns_rr) – The tsig rr.
Throws TypeError:
 When arguments of inappropriate types.
size()

Return the packet’s size in bytes.

Returns:(size_t) The size.
tc()

Read the packet’s tc bit.

Returns:(bool) Value of the bit.
timestamp()

Return the packet’s time stamp.

Returns:(struct timeval) The time stamp.
tsig()

Return the packet’s tsig pseudo rr’s.

Returns:(ldns_rr) The tsig rr.
update_ad()

Get the ad count.

Returns:(uint16_t) The ad count.
update_pkt_tsig_add(r)

Add tsig credentials to a packet from a resolver.

Parameters:r (ldns_resolver) – Resolver to copy from.
Throws TypeError:
 When arguments of inappropriate types.
Returns:(ldns_status) Status whether successful or not.
update_prcount()

Get the pr count.

Returns:(uint16_t) The pr count.
update_set_adcount(c)

Set the ad count.

Parameters:c (uint16_t) – The ad count to set.
Throws TypeError:
 When arguments of inappropriate types.
update_set_prcount(c)

Set the pr count.

Parameters:c (uint16_t) – The pr count to set.
Throws TypeError:
 When arguments of inappropriate types.
update_set_upcount(c)

Set the up count.

Parameters:c (uint16_t) – The up count to set.
Throws TypeError:
 When arguments of inappropriate types.
update_set_zo(c)

Set the zo count.

Parameters:c (uint16_t) – The zo count to set.
Throws TypeError:
 When arguments of inappropriate types.
update_upcount()

Get the up count.

Returns:(uint16_t) The up count.
update_zocount()

Get the zo count.

Returns:(uint16_t) The zo count.
write_to_buffer(buffer)

Copies the packet data to the buffer in wire format.

Parameters:buffer (ldns_buffer) – Buffer to append the result to.
Throws TypeError:
 When arguments of inappropriate types.
Returns:(ldns_status) ldns_status

Table Of Contents

Previous topic

Class ldns_resolver

Next topic

Class ldns_rr