ldns  1.7.0
Tutorial 2: Reading a zone file

The full source code can be found in examples/ldns-read-zone.c

ldns-read-zone reads a zone file, and prints it to stdout, with 1 resource record per line.

% cat example.zone
$ORIGIN example.
$TTL 600
example.        IN SOA  example. op.example. (
                                2004022501 ; serial
                                28800      ; refresh (8 hours)
                                7200       ; retry (2 hours)
                                604800     ; expire (1 week)
                                18000      ; minimum (5 hours)
@       IN      MX      10 mail.example.
@       IN      NS      ns1
@       IN      NS      ns2
@       IN      A
% ldns-read-zone example.zone
example.        600     IN      SOA     example. op.example. 2004022501 28800 7200 604800 18000
example.        600     IN      MX      10 mail.example.
example.        600     IN      NS      ns1.example.
example.        600     IN      NS      ns2.example.
example.        600     IN      A

Again, let's start with including some necessary header files:

#include "config.h"
#include <unistd.h>
#include <stdlib.h>
#include <ldns/ldns.h>
#include <ldns/host2str.h>
#include <errno.h>

In this example, we are going to open a file, if that fails, we'll need errno.h to display an error message.

Okay, let's declare the variables we are going to need today:

char *filename;
FILE *fp;
int line_nr = 0;
int c;
bool canonicalize = false;
bool sort = false;
bool print_soa = true;

The only two ldns-specific types here are ldns_zone and ldns_status.

If we get no filename, we'll read standard input, otherwise, we'll try to open the given filename:

if (argc == 0) {
fp = stdin;
} else {
filename = argv[0];
fp = fopen(filename, "r");
if (!fp) {
fprintf(stderr, "Unable to open %s: %s\n", filename, strerror(errno));

With the FILE pointer in our hands, we visit ldns to pour it into a zone structure:

s = ldns_zone_new_frm_fp_l(&z, fp, NULL, 0, LDNS_RR_CLASS_IN, &line_nr);

There is also a ldns_zone_new_frm_fp, but this one also remembers the line number it was on, so we can use that if we encounter a parse error.

Just like in Tutorial 1: Querying for MX records, the first argument is a pointer to the place ldns should store its creation in, and again, the return value is the status code.

The second argument is the file pointer where our zone data should reside.

The third argument, if not NULL, is a dname that contains the zones origin. It will place this dname after every name in the file that is not a fully qualified domain name.

The fourth argument, if not 0, is the default TTL to use.

Both these values can be specified in the zone file by setting $ORIGIN and $TTL.

The fifth argument specifies the default class, which defaults to IN (LDNS_RR_CLASS_IN).

And finally, every time ldns_zone_new_frm_fp_l reads a line from the input file pointer, it will increment the value pointed to by the last argument with 1.

Okay, with that, we should have a nice zone structure. Of course we need to check whether it has succeeded.

if (s != LDNS_STATUS_OK) {
fprintf(stderr, "%s at %d\n",
if (show_types) {
if (print_soa)
print_soa = ldns_nsec_bitmap_covers_type(show_types,
stripped_list = ldns_rr_list_new();
while ((cur_rr = ldns_rr_list_pop_rr(ldns_zone_rrs(z))))
ldns_rr_list_push_rr(stripped_list, cur_rr);
ldns_zone_set_rrs(z, stripped_list);
if (canonicalize) {
for (i = 0; i < ldns_rr_list_rr_count(ldns_zone_rrs(z)); i++) {
if (sort) {
if (print_soa && ldns_zone_soa(z)) {
if (soa_serial_increment_func) {
, soa_serial_increment_func
, soa_serial_increment_func_data
ldns_rr_print_fmt(stdout, fmt, ldns_zone_soa(z));

If everything went well, we sort the zone if necessary, print it, and free it.

Since ldns_zone contains other ldns structures, we use ldns_deep_free so that every ldns_rr_list, ldns_rr et cetera are freed too.

If something went wrong, we use ldns_get_errorstr_by_id() to get a nice error string instead of just a status integer.

And of course, we should play nice and close the file: