DNS (Domain Name Service) packet: C++98/STL parsing library

(No support for Auth-Name + Add-Name for simplicity)

KS implementation details

License: CC0-1.0

References

This page hosts a formal specification of DNS (Domain Name Service) packet using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Runtime library

All parsing code for C++98/STL generated by Kaitai Struct depends on the C++/STL runtime library. You have to install it before you can parse data.

For C++, the easiest way is to clone the runtime library sources and build them along with your project.

Code

Using Kaitai Struct in C++/STL usually consists of 3 steps.

  1. We need to create an STL input stream (std::istream). One can open local file for that, or use existing std::string or char* buffer.
    #include <fstream>
    
    std::ifstream is("path/to/local/file.bin", std::ifstream::binary);
    
    #include <sstream>
    
    std::istringstream is(str);
    
    #include <sstream>
    
    const char buf[] = { ... };
    std::string str(buf, sizeof buf);
    std::istringstream is(str);
    
  2. We need to wrap our input stream into Kaitai stream:
    #include "kaitai/kaitaistream.h"
    
    kaitai::kstream ks(&is);
    
  3. And finally, we can invoke the parsing:
    dns_packet_t data(&ks);
    

After that, one can get various attributes from the structure by invoking getter methods like:

data.transaction_id() // => ID to keep track of request/responces

C++98/STL source code to parse DNS (Domain Name Service) packet

dns_packet.h

#ifndef DNS_PACKET_H_
#define DNS_PACKET_H_

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "kaitai/kaitaistruct.h"
#include <stdint.h>
#include <vector>

#if KAITAI_STRUCT_VERSION < 9000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.9 or later is required"
#endif

/**
 * (No support for Auth-Name + Add-Name for simplicity)
 */

class dns_packet_t : public kaitai::kstruct {

public:
    class mx_info_t;
    class pointer_struct_t;
    class label_t;
    class query_t;
    class domain_name_t;
    class address_v6_t;
    class service_t;
    class txt_t;
    class txt_body_t;
    class address_t;
    class answer_t;
    class packet_flags_t;
    class authority_info_t;

    enum class_type_t {
        CLASS_TYPE_IN_CLASS = 1,
        CLASS_TYPE_CS = 2,
        CLASS_TYPE_CH = 3,
        CLASS_TYPE_HS = 4
    };

    enum type_type_t {
        TYPE_TYPE_A = 1,
        TYPE_TYPE_NS = 2,
        TYPE_TYPE_MD = 3,
        TYPE_TYPE_MF = 4,
        TYPE_TYPE_CNAME = 5,
        TYPE_TYPE_SOA = 6,
        TYPE_TYPE_MB = 7,
        TYPE_TYPE_MG = 8,
        TYPE_TYPE_MR = 9,
        TYPE_TYPE_NULL = 10,
        TYPE_TYPE_WKS = 11,
        TYPE_TYPE_PTR = 12,
        TYPE_TYPE_HINFO = 13,
        TYPE_TYPE_MINFO = 14,
        TYPE_TYPE_MX = 15,
        TYPE_TYPE_TXT = 16,
        TYPE_TYPE_AAAA = 28,
        TYPE_TYPE_SRV = 33
    };

    dns_packet_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, dns_packet_t* p__root = 0);

private:
    void _read();
    void _clean_up();

public:
    ~dns_packet_t();

    class mx_info_t : public kaitai::kstruct {

    public:

        mx_info_t(kaitai::kstream* p__io, dns_packet_t::answer_t* p__parent = 0, dns_packet_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~mx_info_t();

    private:
        uint16_t m_preference;
        domain_name_t* m_mx;
        dns_packet_t* m__root;
        dns_packet_t::answer_t* m__parent;

    public:
        uint16_t preference() const { return m_preference; }
        domain_name_t* mx() const { return m_mx; }
        dns_packet_t* _root() const { return m__root; }
        dns_packet_t::answer_t* _parent() const { return m__parent; }
    };

    class pointer_struct_t : public kaitai::kstruct {

    public:

        pointer_struct_t(kaitai::kstream* p__io, dns_packet_t::label_t* p__parent = 0, dns_packet_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~pointer_struct_t();

    private:
        bool f_contents;
        domain_name_t* m_contents;

    public:
        domain_name_t* contents();

    private:
        uint8_t m_value;
        dns_packet_t* m__root;
        dns_packet_t::label_t* m__parent;

    public:

        /**
         * Read one byte, then offset to that position, read one domain-name and return
         */
        uint8_t value() const { return m_value; }
        dns_packet_t* _root() const { return m__root; }
        dns_packet_t::label_t* _parent() const { return m__parent; }
    };

    class label_t : public kaitai::kstruct {

    public:

        label_t(kaitai::kstream* p__io, dns_packet_t::domain_name_t* p__parent = 0, dns_packet_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~label_t();

    private:
        bool f_is_pointer;
        bool m_is_pointer;

    public:
        bool is_pointer();

    private:
        uint8_t m_length;
        pointer_struct_t* m_pointer;
        bool n_pointer;

    public:
        bool _is_null_pointer() { pointer(); return n_pointer; };

    private:
        std::string m_name;
        bool n_name;

    public:
        bool _is_null_name() { name(); return n_name; };

    private:
        dns_packet_t* m__root;
        dns_packet_t::domain_name_t* m__parent;

    public:

        /**
         * RFC1035 4.1.4: If the first two bits are raised it's a pointer-offset to a previously defined name
         */
        uint8_t length() const { return m_length; }
        pointer_struct_t* pointer() const { return m_pointer; }

        /**
         * Otherwise its a string the length of the length value
         */
        std::string name() const { return m_name; }
        dns_packet_t* _root() const { return m__root; }
        dns_packet_t::domain_name_t* _parent() const { return m__parent; }
    };

    class query_t : public kaitai::kstruct {

    public:

        query_t(kaitai::kstream* p__io, dns_packet_t* p__parent = 0, dns_packet_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~query_t();

    private:
        domain_name_t* m_name;
        type_type_t m_type;
        class_type_t m_query_class;
        dns_packet_t* m__root;
        dns_packet_t* m__parent;

    public:
        domain_name_t* name() const { return m_name; }
        type_type_t type() const { return m_type; }
        class_type_t query_class() const { return m_query_class; }
        dns_packet_t* _root() const { return m__root; }
        dns_packet_t* _parent() const { return m__parent; }
    };

    class domain_name_t : public kaitai::kstruct {

    public:

        domain_name_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, dns_packet_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~domain_name_t();

    private:
        std::vector<label_t*>* m_name;
        dns_packet_t* m__root;
        kaitai::kstruct* m__parent;

    public:

        /**
         * Repeat until the length is 0 or it is a pointer (bit-hack to get around lack of OR operator)
         */
        std::vector<label_t*>* name() const { return m_name; }
        dns_packet_t* _root() const { return m__root; }
        kaitai::kstruct* _parent() const { return m__parent; }
    };

    class address_v6_t : public kaitai::kstruct {

    public:

        address_v6_t(kaitai::kstream* p__io, dns_packet_t::answer_t* p__parent = 0, dns_packet_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~address_v6_t();

    private:
        std::string m_ip_v6;
        dns_packet_t* m__root;
        dns_packet_t::answer_t* m__parent;

    public:
        std::string ip_v6() const { return m_ip_v6; }
        dns_packet_t* _root() const { return m__root; }
        dns_packet_t::answer_t* _parent() const { return m__parent; }
    };

    class service_t : public kaitai::kstruct {

    public:

        service_t(kaitai::kstream* p__io, dns_packet_t::answer_t* p__parent = 0, dns_packet_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~service_t();

    private:
        uint16_t m_priority;
        uint16_t m_weight;
        uint16_t m_port;
        domain_name_t* m_target;
        dns_packet_t* m__root;
        dns_packet_t::answer_t* m__parent;

    public:
        uint16_t priority() const { return m_priority; }
        uint16_t weight() const { return m_weight; }
        uint16_t port() const { return m_port; }
        domain_name_t* target() const { return m_target; }
        dns_packet_t* _root() const { return m__root; }
        dns_packet_t::answer_t* _parent() const { return m__parent; }
    };

    class txt_t : public kaitai::kstruct {

    public:

        txt_t(kaitai::kstream* p__io, dns_packet_t::txt_body_t* p__parent = 0, dns_packet_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~txt_t();

    private:
        uint8_t m_length;
        std::string m_text;
        dns_packet_t* m__root;
        dns_packet_t::txt_body_t* m__parent;

    public:
        uint8_t length() const { return m_length; }
        std::string text() const { return m_text; }
        dns_packet_t* _root() const { return m__root; }
        dns_packet_t::txt_body_t* _parent() const { return m__parent; }
    };

    class txt_body_t : public kaitai::kstruct {

    public:

        txt_body_t(kaitai::kstream* p__io, dns_packet_t::answer_t* p__parent = 0, dns_packet_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~txt_body_t();

    private:
        std::vector<txt_t*>* m_data;
        dns_packet_t* m__root;
        dns_packet_t::answer_t* m__parent;

    public:
        std::vector<txt_t*>* data() const { return m_data; }
        dns_packet_t* _root() const { return m__root; }
        dns_packet_t::answer_t* _parent() const { return m__parent; }
    };

    class address_t : public kaitai::kstruct {

    public:

        address_t(kaitai::kstream* p__io, dns_packet_t::answer_t* p__parent = 0, dns_packet_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~address_t();

    private:
        std::string m_ip;
        dns_packet_t* m__root;
        dns_packet_t::answer_t* m__parent;

    public:
        std::string ip() const { return m_ip; }
        dns_packet_t* _root() const { return m__root; }
        dns_packet_t::answer_t* _parent() const { return m__parent; }
    };

    class answer_t : public kaitai::kstruct {

    public:

        answer_t(kaitai::kstream* p__io, dns_packet_t* p__parent = 0, dns_packet_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~answer_t();

    private:
        domain_name_t* m_name;
        type_type_t m_type;
        class_type_t m_answer_class;
        int32_t m_ttl;
        uint16_t m_rdlength;
        kaitai::kstruct* m_payload;
        bool n_payload;

    public:
        bool _is_null_payload() { payload(); return n_payload; };

    private:
        dns_packet_t* m__root;
        dns_packet_t* m__parent;
        std::string m__raw_payload;
        kaitai::kstream* m__io__raw_payload;

    public:
        domain_name_t* name() const { return m_name; }
        type_type_t type() const { return m_type; }
        class_type_t answer_class() const { return m_answer_class; }

        /**
         * Time to live (in seconds)
         */
        int32_t ttl() const { return m_ttl; }

        /**
         * Length in octets of the following payload
         */
        uint16_t rdlength() const { return m_rdlength; }
        kaitai::kstruct* payload() const { return m_payload; }
        dns_packet_t* _root() const { return m__root; }
        dns_packet_t* _parent() const { return m__parent; }
        std::string _raw_payload() const { return m__raw_payload; }
        kaitai::kstream* _io__raw_payload() const { return m__io__raw_payload; }
    };

    class packet_flags_t : public kaitai::kstruct {

    public:

        packet_flags_t(kaitai::kstream* p__io, dns_packet_t* p__parent = 0, dns_packet_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~packet_flags_t();

    private:
        bool f_qr;
        int32_t m_qr;

    public:
        int32_t qr();

    private:
        bool f_ra;
        int32_t m_ra;

    public:
        int32_t ra();

    private:
        bool f_tc;
        int32_t m_tc;

    public:
        int32_t tc();

    private:
        bool f_is_opcode_valid;
        bool m_is_opcode_valid;

    public:
        bool is_opcode_valid();

    private:
        bool f_rcode;
        int32_t m_rcode;

    public:
        int32_t rcode();

    private:
        bool f_opcode;
        int32_t m_opcode;

    public:
        int32_t opcode();

    private:
        bool f_aa;
        int32_t m_aa;

    public:
        int32_t aa();

    private:
        bool f_z;
        int32_t m_z;

    public:
        int32_t z();

    private:
        bool f_rd;
        int32_t m_rd;

    public:
        int32_t rd();

    private:
        bool f_cd;
        int32_t m_cd;

    public:
        int32_t cd();

    private:
        bool f_ad;
        int32_t m_ad;

    public:
        int32_t ad();

    private:
        uint16_t m_flag;
        dns_packet_t* m__root;
        dns_packet_t* m__parent;

    public:
        uint16_t flag() const { return m_flag; }
        dns_packet_t* _root() const { return m__root; }
        dns_packet_t* _parent() const { return m__parent; }
    };

    class authority_info_t : public kaitai::kstruct {

    public:

        authority_info_t(kaitai::kstream* p__io, dns_packet_t::answer_t* p__parent = 0, dns_packet_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~authority_info_t();

    private:
        domain_name_t* m_primary_ns;
        domain_name_t* m_resoponsible_mailbox;
        uint32_t m_serial;
        uint32_t m_refresh_interval;
        uint32_t m_retry_interval;
        uint32_t m_expire_limit;
        uint32_t m_min_ttl;
        dns_packet_t* m__root;
        dns_packet_t::answer_t* m__parent;

    public:
        domain_name_t* primary_ns() const { return m_primary_ns; }
        domain_name_t* resoponsible_mailbox() const { return m_resoponsible_mailbox; }
        uint32_t serial() const { return m_serial; }
        uint32_t refresh_interval() const { return m_refresh_interval; }
        uint32_t retry_interval() const { return m_retry_interval; }
        uint32_t expire_limit() const { return m_expire_limit; }
        uint32_t min_ttl() const { return m_min_ttl; }
        dns_packet_t* _root() const { return m__root; }
        dns_packet_t::answer_t* _parent() const { return m__parent; }
    };

private:
    uint16_t m_transaction_id;
    packet_flags_t* m_flags;
    uint16_t m_qdcount;
    bool n_qdcount;

public:
    bool _is_null_qdcount() { qdcount(); return n_qdcount; };

private:
    uint16_t m_ancount;
    bool n_ancount;

public:
    bool _is_null_ancount() { ancount(); return n_ancount; };

private:
    uint16_t m_nscount;
    bool n_nscount;

public:
    bool _is_null_nscount() { nscount(); return n_nscount; };

private:
    uint16_t m_arcount;
    bool n_arcount;

public:
    bool _is_null_arcount() { arcount(); return n_arcount; };

private:
    std::vector<query_t*>* m_queries;
    bool n_queries;

public:
    bool _is_null_queries() { queries(); return n_queries; };

private:
    std::vector<answer_t*>* m_answers;
    bool n_answers;

public:
    bool _is_null_answers() { answers(); return n_answers; };

private:
    std::vector<answer_t*>* m_authorities;
    bool n_authorities;

public:
    bool _is_null_authorities() { authorities(); return n_authorities; };

private:
    std::vector<answer_t*>* m_additionals;
    bool n_additionals;

public:
    bool _is_null_additionals() { additionals(); return n_additionals; };

private:
    dns_packet_t* m__root;
    kaitai::kstruct* m__parent;

public:

    /**
     * ID to keep track of request/responces
     */
    uint16_t transaction_id() const { return m_transaction_id; }
    packet_flags_t* flags() const { return m_flags; }

    /**
     * How many questions are there
     */
    uint16_t qdcount() const { return m_qdcount; }

    /**
     * Number of resource records answering the question
     */
    uint16_t ancount() const { return m_ancount; }

    /**
     * Number of resource records pointing toward an authority
     */
    uint16_t nscount() const { return m_nscount; }

    /**
     * Number of resource records holding additional information
     */
    uint16_t arcount() const { return m_arcount; }
    std::vector<query_t*>* queries() const { return m_queries; }
    std::vector<answer_t*>* answers() const { return m_answers; }
    std::vector<answer_t*>* authorities() const { return m_authorities; }
    std::vector<answer_t*>* additionals() const { return m_additionals; }
    dns_packet_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // DNS_PACKET_H_

dns_packet.cpp

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "dns_packet.h"

dns_packet_t::dns_packet_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, dns_packet_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_flags = 0;
    m_queries = 0;
    m_answers = 0;
    m_authorities = 0;
    m_additionals = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void dns_packet_t::_read() {
    m_transaction_id = m__io->read_u2be();
    m_flags = new packet_flags_t(m__io, this, m__root);
    n_qdcount = true;
    if (flags()->is_opcode_valid()) {
        n_qdcount = false;
        m_qdcount = m__io->read_u2be();
    }
    n_ancount = true;
    if (flags()->is_opcode_valid()) {
        n_ancount = false;
        m_ancount = m__io->read_u2be();
    }
    n_nscount = true;
    if (flags()->is_opcode_valid()) {
        n_nscount = false;
        m_nscount = m__io->read_u2be();
    }
    n_arcount = true;
    if (flags()->is_opcode_valid()) {
        n_arcount = false;
        m_arcount = m__io->read_u2be();
    }
    n_queries = true;
    if (flags()->is_opcode_valid()) {
        n_queries = false;
        m_queries = new std::vector<query_t*>();
        const int l_queries = qdcount();
        for (int i = 0; i < l_queries; i++) {
            m_queries->push_back(new query_t(m__io, this, m__root));
        }
    }
    n_answers = true;
    if (flags()->is_opcode_valid()) {
        n_answers = false;
        m_answers = new std::vector<answer_t*>();
        const int l_answers = ancount();
        for (int i = 0; i < l_answers; i++) {
            m_answers->push_back(new answer_t(m__io, this, m__root));
        }
    }
    n_authorities = true;
    if (flags()->is_opcode_valid()) {
        n_authorities = false;
        m_authorities = new std::vector<answer_t*>();
        const int l_authorities = nscount();
        for (int i = 0; i < l_authorities; i++) {
            m_authorities->push_back(new answer_t(m__io, this, m__root));
        }
    }
    n_additionals = true;
    if (flags()->is_opcode_valid()) {
        n_additionals = false;
        m_additionals = new std::vector<answer_t*>();
        const int l_additionals = arcount();
        for (int i = 0; i < l_additionals; i++) {
            m_additionals->push_back(new answer_t(m__io, this, m__root));
        }
    }
}

dns_packet_t::~dns_packet_t() {
    _clean_up();
}

void dns_packet_t::_clean_up() {
    if (m_flags) {
        delete m_flags; m_flags = 0;
    }
    if (!n_qdcount) {
    }
    if (!n_ancount) {
    }
    if (!n_nscount) {
    }
    if (!n_arcount) {
    }
    if (!n_queries) {
        if (m_queries) {
            for (std::vector<query_t*>::iterator it = m_queries->begin(); it != m_queries->end(); ++it) {
                delete *it;
            }
            delete m_queries; m_queries = 0;
        }
    }
    if (!n_answers) {
        if (m_answers) {
            for (std::vector<answer_t*>::iterator it = m_answers->begin(); it != m_answers->end(); ++it) {
                delete *it;
            }
            delete m_answers; m_answers = 0;
        }
    }
    if (!n_authorities) {
        if (m_authorities) {
            for (std::vector<answer_t*>::iterator it = m_authorities->begin(); it != m_authorities->end(); ++it) {
                delete *it;
            }
            delete m_authorities; m_authorities = 0;
        }
    }
    if (!n_additionals) {
        if (m_additionals) {
            for (std::vector<answer_t*>::iterator it = m_additionals->begin(); it != m_additionals->end(); ++it) {
                delete *it;
            }
            delete m_additionals; m_additionals = 0;
        }
    }
}

dns_packet_t::mx_info_t::mx_info_t(kaitai::kstream* p__io, dns_packet_t::answer_t* p__parent, dns_packet_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_mx = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void dns_packet_t::mx_info_t::_read() {
    m_preference = m__io->read_u2be();
    m_mx = new domain_name_t(m__io, this, m__root);
}

dns_packet_t::mx_info_t::~mx_info_t() {
    _clean_up();
}

void dns_packet_t::mx_info_t::_clean_up() {
    if (m_mx) {
        delete m_mx; m_mx = 0;
    }
}

dns_packet_t::pointer_struct_t::pointer_struct_t(kaitai::kstream* p__io, dns_packet_t::label_t* p__parent, dns_packet_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_contents = 0;
    f_contents = false;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void dns_packet_t::pointer_struct_t::_read() {
    m_value = m__io->read_u1();
}

dns_packet_t::pointer_struct_t::~pointer_struct_t() {
    _clean_up();
}

void dns_packet_t::pointer_struct_t::_clean_up() {
    if (f_contents) {
        if (m_contents) {
            delete m_contents; m_contents = 0;
        }
    }
}

dns_packet_t::domain_name_t* dns_packet_t::pointer_struct_t::contents() {
    if (f_contents)
        return m_contents;
    kaitai::kstream *io = _root()->_io();
    std::streampos _pos = io->pos();
    io->seek((value() + ((_parent()->length() - 192) << 8)));
    m_contents = new domain_name_t(io, this, m__root);
    io->seek(_pos);
    f_contents = true;
    return m_contents;
}

dns_packet_t::label_t::label_t(kaitai::kstream* p__io, dns_packet_t::domain_name_t* p__parent, dns_packet_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_pointer = 0;
    f_is_pointer = false;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void dns_packet_t::label_t::_read() {
    m_length = m__io->read_u1();
    n_pointer = true;
    if (is_pointer()) {
        n_pointer = false;
        m_pointer = new pointer_struct_t(m__io, this, m__root);
    }
    n_name = true;
    if (!(is_pointer())) {
        n_name = false;
        m_name = kaitai::kstream::bytes_to_str(m__io->read_bytes(length()), std::string("utf-8"));
    }
}

dns_packet_t::label_t::~label_t() {
    _clean_up();
}

void dns_packet_t::label_t::_clean_up() {
    if (!n_pointer) {
        if (m_pointer) {
            delete m_pointer; m_pointer = 0;
        }
    }
    if (!n_name) {
    }
}

bool dns_packet_t::label_t::is_pointer() {
    if (f_is_pointer)
        return m_is_pointer;
    m_is_pointer = length() >= 192;
    f_is_pointer = true;
    return m_is_pointer;
}

dns_packet_t::query_t::query_t(kaitai::kstream* p__io, dns_packet_t* p__parent, dns_packet_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_name = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void dns_packet_t::query_t::_read() {
    m_name = new domain_name_t(m__io, this, m__root);
    m_type = static_cast<dns_packet_t::type_type_t>(m__io->read_u2be());
    m_query_class = static_cast<dns_packet_t::class_type_t>(m__io->read_u2be());
}

dns_packet_t::query_t::~query_t() {
    _clean_up();
}

void dns_packet_t::query_t::_clean_up() {
    if (m_name) {
        delete m_name; m_name = 0;
    }
}

dns_packet_t::domain_name_t::domain_name_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, dns_packet_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_name = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void dns_packet_t::domain_name_t::_read() {
    m_name = new std::vector<label_t*>();
    {
        int i = 0;
        label_t* _;
        do {
            _ = new label_t(m__io, this, m__root);
            m_name->push_back(_);
            i++;
        } while (!( ((_->length() == 0) || (_->length() >= 192)) ));
    }
}

dns_packet_t::domain_name_t::~domain_name_t() {
    _clean_up();
}

void dns_packet_t::domain_name_t::_clean_up() {
    if (m_name) {
        for (std::vector<label_t*>::iterator it = m_name->begin(); it != m_name->end(); ++it) {
            delete *it;
        }
        delete m_name; m_name = 0;
    }
}

dns_packet_t::address_v6_t::address_v6_t(kaitai::kstream* p__io, dns_packet_t::answer_t* p__parent, dns_packet_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void dns_packet_t::address_v6_t::_read() {
    m_ip_v6 = m__io->read_bytes(16);
}

dns_packet_t::address_v6_t::~address_v6_t() {
    _clean_up();
}

void dns_packet_t::address_v6_t::_clean_up() {
}

dns_packet_t::service_t::service_t(kaitai::kstream* p__io, dns_packet_t::answer_t* p__parent, dns_packet_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_target = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void dns_packet_t::service_t::_read() {
    m_priority = m__io->read_u2be();
    m_weight = m__io->read_u2be();
    m_port = m__io->read_u2be();
    m_target = new domain_name_t(m__io, this, m__root);
}

dns_packet_t::service_t::~service_t() {
    _clean_up();
}

void dns_packet_t::service_t::_clean_up() {
    if (m_target) {
        delete m_target; m_target = 0;
    }
}

dns_packet_t::txt_t::txt_t(kaitai::kstream* p__io, dns_packet_t::txt_body_t* p__parent, dns_packet_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void dns_packet_t::txt_t::_read() {
    m_length = m__io->read_u1();
    m_text = kaitai::kstream::bytes_to_str(m__io->read_bytes(length()), std::string("utf-8"));
}

dns_packet_t::txt_t::~txt_t() {
    _clean_up();
}

void dns_packet_t::txt_t::_clean_up() {
}

dns_packet_t::txt_body_t::txt_body_t(kaitai::kstream* p__io, dns_packet_t::answer_t* p__parent, dns_packet_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_data = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void dns_packet_t::txt_body_t::_read() {
    m_data = new std::vector<txt_t*>();
    {
        int i = 0;
        while (!m__io->is_eof()) {
            m_data->push_back(new txt_t(m__io, this, m__root));
            i++;
        }
    }
}

dns_packet_t::txt_body_t::~txt_body_t() {
    _clean_up();
}

void dns_packet_t::txt_body_t::_clean_up() {
    if (m_data) {
        for (std::vector<txt_t*>::iterator it = m_data->begin(); it != m_data->end(); ++it) {
            delete *it;
        }
        delete m_data; m_data = 0;
    }
}

dns_packet_t::address_t::address_t(kaitai::kstream* p__io, dns_packet_t::answer_t* p__parent, dns_packet_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void dns_packet_t::address_t::_read() {
    m_ip = m__io->read_bytes(4);
}

dns_packet_t::address_t::~address_t() {
    _clean_up();
}

void dns_packet_t::address_t::_clean_up() {
}

dns_packet_t::answer_t::answer_t(kaitai::kstream* p__io, dns_packet_t* p__parent, dns_packet_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_name = 0;
    m__io__raw_payload = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void dns_packet_t::answer_t::_read() {
    m_name = new domain_name_t(m__io, this, m__root);
    m_type = static_cast<dns_packet_t::type_type_t>(m__io->read_u2be());
    m_answer_class = static_cast<dns_packet_t::class_type_t>(m__io->read_u2be());
    m_ttl = m__io->read_s4be();
    m_rdlength = m__io->read_u2be();
    n_payload = true;
    switch (type()) {
    case dns_packet_t::TYPE_TYPE_SRV: {
        n_payload = false;
        m__raw_payload = m__io->read_bytes(rdlength());
        m__io__raw_payload = new kaitai::kstream(m__raw_payload);
        m_payload = new service_t(m__io__raw_payload, this, m__root);
        break;
    }
    case dns_packet_t::TYPE_TYPE_A: {
        n_payload = false;
        m__raw_payload = m__io->read_bytes(rdlength());
        m__io__raw_payload = new kaitai::kstream(m__raw_payload);
        m_payload = new address_t(m__io__raw_payload, this, m__root);
        break;
    }
    case dns_packet_t::TYPE_TYPE_CNAME: {
        n_payload = false;
        m__raw_payload = m__io->read_bytes(rdlength());
        m__io__raw_payload = new kaitai::kstream(m__raw_payload);
        m_payload = new domain_name_t(m__io__raw_payload, this, m__root);
        break;
    }
    case dns_packet_t::TYPE_TYPE_NS: {
        n_payload = false;
        m__raw_payload = m__io->read_bytes(rdlength());
        m__io__raw_payload = new kaitai::kstream(m__raw_payload);
        m_payload = new domain_name_t(m__io__raw_payload, this, m__root);
        break;
    }
    case dns_packet_t::TYPE_TYPE_SOA: {
        n_payload = false;
        m__raw_payload = m__io->read_bytes(rdlength());
        m__io__raw_payload = new kaitai::kstream(m__raw_payload);
        m_payload = new authority_info_t(m__io__raw_payload, this, m__root);
        break;
    }
    case dns_packet_t::TYPE_TYPE_MX: {
        n_payload = false;
        m__raw_payload = m__io->read_bytes(rdlength());
        m__io__raw_payload = new kaitai::kstream(m__raw_payload);
        m_payload = new mx_info_t(m__io__raw_payload, this, m__root);
        break;
    }
    case dns_packet_t::TYPE_TYPE_TXT: {
        n_payload = false;
        m__raw_payload = m__io->read_bytes(rdlength());
        m__io__raw_payload = new kaitai::kstream(m__raw_payload);
        m_payload = new txt_body_t(m__io__raw_payload, this, m__root);
        break;
    }
    case dns_packet_t::TYPE_TYPE_PTR: {
        n_payload = false;
        m__raw_payload = m__io->read_bytes(rdlength());
        m__io__raw_payload = new kaitai::kstream(m__raw_payload);
        m_payload = new domain_name_t(m__io__raw_payload, this, m__root);
        break;
    }
    case dns_packet_t::TYPE_TYPE_AAAA: {
        n_payload = false;
        m__raw_payload = m__io->read_bytes(rdlength());
        m__io__raw_payload = new kaitai::kstream(m__raw_payload);
        m_payload = new address_v6_t(m__io__raw_payload, this, m__root);
        break;
    }
    default: {
        m__raw_payload = m__io->read_bytes(rdlength());
        break;
    }
    }
}

dns_packet_t::answer_t::~answer_t() {
    _clean_up();
}

void dns_packet_t::answer_t::_clean_up() {
    if (m_name) {
        delete m_name; m_name = 0;
    }
    if (!n_payload) {
        if (m__io__raw_payload) {
            delete m__io__raw_payload; m__io__raw_payload = 0;
        }
        if (m_payload) {
            delete m_payload; m_payload = 0;
        }
    }
}

dns_packet_t::packet_flags_t::packet_flags_t(kaitai::kstream* p__io, dns_packet_t* p__parent, dns_packet_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    f_qr = false;
    f_ra = false;
    f_tc = false;
    f_is_opcode_valid = false;
    f_rcode = false;
    f_opcode = false;
    f_aa = false;
    f_z = false;
    f_rd = false;
    f_cd = false;
    f_ad = false;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void dns_packet_t::packet_flags_t::_read() {
    m_flag = m__io->read_u2be();
}

dns_packet_t::packet_flags_t::~packet_flags_t() {
    _clean_up();
}

void dns_packet_t::packet_flags_t::_clean_up() {
}

int32_t dns_packet_t::packet_flags_t::qr() {
    if (f_qr)
        return m_qr;
    m_qr = ((flag() & 32768) >> 15);
    f_qr = true;
    return m_qr;
}

int32_t dns_packet_t::packet_flags_t::ra() {
    if (f_ra)
        return m_ra;
    m_ra = ((flag() & 128) >> 7);
    f_ra = true;
    return m_ra;
}

int32_t dns_packet_t::packet_flags_t::tc() {
    if (f_tc)
        return m_tc;
    m_tc = ((flag() & 512) >> 9);
    f_tc = true;
    return m_tc;
}

bool dns_packet_t::packet_flags_t::is_opcode_valid() {
    if (f_is_opcode_valid)
        return m_is_opcode_valid;
    m_is_opcode_valid =  ((opcode() == 0) || (opcode() == 1) || (opcode() == 2)) ;
    f_is_opcode_valid = true;
    return m_is_opcode_valid;
}

int32_t dns_packet_t::packet_flags_t::rcode() {
    if (f_rcode)
        return m_rcode;
    m_rcode = ((flag() & 15) >> 0);
    f_rcode = true;
    return m_rcode;
}

int32_t dns_packet_t::packet_flags_t::opcode() {
    if (f_opcode)
        return m_opcode;
    m_opcode = ((flag() & 30720) >> 11);
    f_opcode = true;
    return m_opcode;
}

int32_t dns_packet_t::packet_flags_t::aa() {
    if (f_aa)
        return m_aa;
    m_aa = ((flag() & 1024) >> 10);
    f_aa = true;
    return m_aa;
}

int32_t dns_packet_t::packet_flags_t::z() {
    if (f_z)
        return m_z;
    m_z = ((flag() & 64) >> 6);
    f_z = true;
    return m_z;
}

int32_t dns_packet_t::packet_flags_t::rd() {
    if (f_rd)
        return m_rd;
    m_rd = ((flag() & 256) >> 8);
    f_rd = true;
    return m_rd;
}

int32_t dns_packet_t::packet_flags_t::cd() {
    if (f_cd)
        return m_cd;
    m_cd = ((flag() & 16) >> 4);
    f_cd = true;
    return m_cd;
}

int32_t dns_packet_t::packet_flags_t::ad() {
    if (f_ad)
        return m_ad;
    m_ad = ((flag() & 32) >> 5);
    f_ad = true;
    return m_ad;
}

dns_packet_t::authority_info_t::authority_info_t(kaitai::kstream* p__io, dns_packet_t::answer_t* p__parent, dns_packet_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_primary_ns = 0;
    m_resoponsible_mailbox = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void dns_packet_t::authority_info_t::_read() {
    m_primary_ns = new domain_name_t(m__io, this, m__root);
    m_resoponsible_mailbox = new domain_name_t(m__io, this, m__root);
    m_serial = m__io->read_u4be();
    m_refresh_interval = m__io->read_u4be();
    m_retry_interval = m__io->read_u4be();
    m_expire_limit = m__io->read_u4be();
    m_min_ttl = m__io->read_u4be();
}

dns_packet_t::authority_info_t::~authority_info_t() {
    _clean_up();
}

void dns_packet_t::authority_info_t::_clean_up() {
    if (m_primary_ns) {
        delete m_primary_ns; m_primary_ns = 0;
    }
    if (m_resoponsible_mailbox) {
        delete m_resoponsible_mailbox; m_resoponsible_mailbox = 0;
    }
}