Skip to content

DnsRecord

Description

One decoded resource record.

Fields

Name Description
name decoded label string (e.g. “example.com.”)
type record type
rclass record class (typically 1 = IN)
ttl seconds the record may be cached
ipv4 valid when type == DNS_TYPE_A; otherwise zero
ipv6 valid when type == DNS_TYPE_AAAA; otherwise zero
target valid when type is CNAME / NS / PTR; otherwise empty
rdata raw rdata bytes; always populated regardless of type

Usage example (Cross-references)

Usage examples (Cross-references)
            Str        target;
            DnsWireBuf rdata;
        } DnsRecord;
    
        typedef Vec(DnsRecord) DnsRecords;
        } DnsRecord;
    
        typedef Vec(DnsRecord) DnsRecords;
    
        ///
        /// TAGS: Dns, Parser, Deinit, Lifecycle
        ///
        void DnsRecordDeinit(DnsRecord *self);
    
    #ifdef __cplusplus
    
    // Decode one resource record. Advances the iter past the record.
    static bool decode_record(BufIter *it, DnsRecord *rec, Allocator *alloc) {
        rec->name   = StrInit(alloc);
        rec->target = StrInit(alloc);
    static bool decode_record_list(BufIter *it, u16 count, DnsRecords *out, Allocator *alloc) {
        for (u16 i = 0; i < count; ++i) {
            DnsRecord rec = {0};
            if (!decode_record(it, &rec, alloc)) {
                DnsRecordDeinit(&rec);
    // ---------------------------------------------------------------------------
    
    void DnsRecordDeinit(DnsRecord *self) {
        if (!self) {
            return;
    
        if (match) {
            DnsRecord *r0 = VecPtrAt(&resp.answers, 0);
            match         = r0->type == DNS_TYPE_A && r0->ttl == 300 && r0->ipv4[0] == 93 && r0->ipv4[1] == 184 &&
                    r0->ipv4[2] == 216 && r0->ipv4[3] == 34 && ZstrCompare(StrBegin(&r0->name), "example.com") == 0;
        }
        if (match) {
            DnsRecord *r1 = VecPtrAt(&resp.answers, 1);
            match = r1->type == DNS_TYPE_AAAA && r1->ipv6[0] == 0x26 && r1->ipv6[1] == 0x06 && r1->ipv6[14] == 0x19 &&
                    r1->ipv6[15] == 0x46;
        bool        match = ok && VecLen(&resp.answers) == 1;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = r->type == DNS_TYPE_CNAME && ZstrCompare(StrBegin(&r->target), "example.com") == 0;
        }
        bool        match = ok && VecLen(&resp.answers) == 1;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = r->type == DNS_TYPE_A && r->ttl == 300 && r->ipv4[0] == 0x0a && r->ipv4[1] == 0x0b &&
                    r->ipv4[2] == 0x0c && r->ipv4[3] == 0x0d && ZstrCompare(StrBegin(&r->name), "www.example.com") == 0;
        bool        match = ok && q2_off == 256 && VecLen(&resp.answers) == 1;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = r->type == DNS_TYPE_A && ZstrCompare(StrBegin(&r->name), "zz") == 0;
        }
        bool        match = ok && VecLen(&resp.answers) == 1;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match =
                r->type == DNS_TYPE_A && r->ttl == 2 && ZstrCompare(StrBegin(&r->name), "abc.de") == 0 && r->ipv4[0] == 9;
        bool        match = ok && VecLen(&resp.answers) == 2;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = r->type == DNS_TYPE_A && r->rclass == 1 && r->ttl == 0x01020304u && r->ipv4[0] == 8 &&
                    r->ipv4[1] == 8 && r->ipv4[2] == 4 && r->ipv4[3] == 4 && r->ipv6[0] == 0 &&
        bool        match = ok && VecLen(&resp.answers) == 1;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = r->rclass == 0x00FE;
        }
        bool        match = ok && VecLen(&resp.answers) == 1;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = r->ttl == 0x80112233u;
        }
        bool        match = ok && VecLen(&resp.answers) == 2;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = r->type == DNS_TYPE_AAAA && r->ttl == 10 && VecLen(&r->target) == 0;
            static const u8 ip6[] =
        bool        match = ok && VecLen(&resp.answers) == 2;
        if (match) {
            DnsRecord *r0 = VecPtrAt(&resp.answers, 0);
            DnsRecord *r1 = VecPtrAt(&resp.answers, 1);
            match = ZstrCompare(StrBegin(&r0->name), "x") == 0 && r0->ttl == 1 && r0->ipv4[0] == 1 && r0->ipv4[3] == 4 &&
        if (match) {
            DnsRecord *r0 = VecPtrAt(&resp.answers, 0);
            DnsRecord *r1 = VecPtrAt(&resp.answers, 1);
            match = ZstrCompare(StrBegin(&r0->name), "x") == 0 && r0->ttl == 1 && r0->ipv4[0] == 1 && r0->ipv4[3] == 4 &&
                    ZstrCompare(StrBegin(&r1->name), "y") == 0 && r1->ttl == 2 && r1->ipv4[0] == 5 && r1->ipv4[3] == 8;
        bool match = ok && VecLen(&resp.answers) == 1 && VecLen(&resp.authority) == 1 && VecLen(&resp.additional) == 1;
        if (match) {
            DnsRecord *an = VecPtrAt(&resp.answers, 0);
            DnsRecord *ns = VecPtrAt(&resp.authority, 0);
            DnsRecord *ar = VecPtrAt(&resp.additional, 0);
        if (match) {
            DnsRecord *an = VecPtrAt(&resp.answers, 0);
            DnsRecord *ns = VecPtrAt(&resp.authority, 0);
            DnsRecord *ar = VecPtrAt(&resp.additional, 0);
            match         = ZstrCompare(StrBegin(&an->name), "an") == 0 && an->ipv4[0] == 11 &&
            DnsRecord *an = VecPtrAt(&resp.answers, 0);
            DnsRecord *ns = VecPtrAt(&resp.authority, 0);
            DnsRecord *ar = VecPtrAt(&resp.additional, 0);
            match         = ZstrCompare(StrBegin(&an->name), "an") == 0 && an->ipv4[0] == 11 &&
                    ZstrCompare(StrBegin(&ns->name), "ns") == 0 && ns->ipv4[0] == 22 &&
        bool        match = ok && VecLen(&resp.answers) == 1;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = ZstrCompare(StrBegin(&r->name), "q") == 0 && r->ipv4[0] == 9 && r->ttl == 5;
        }
        bool        match = ok && VecLen(&resp.answers) == 1;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = r->type == DNS_TYPE_CNAME && ZstrCompare(StrBegin(&r->target), "to") == 0;
        }
        bool        match = ok && VecLen(&resp.answers) == 1;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = r->type == DNS_TYPE_A && r->ipv4[0] == 1 && r->ipv4[3] == 4;
        }
        bool        match = ok && VecLen(&resp.answers) == 1;
        if (match) {
            DnsRecord *r = VecPtrAt(&resp.answers, 0);
            match        = r->type == DNS_TYPE_A && r->ttl == 7 && ZstrCompare(StrBegin(&r->name), "zz") == 0;
        }
        bool        ok   = DnsParseResponse(&resp, wire, sizeof(wire), adbg);
        ok               = ok && (VecLen(&resp.answers) == 1);
        DnsRecord *r     = ok ? VecPtrAt(&resp.answers, 0) : NULL;
        ok               = ok && (r->type == DNS_TYPE_CNAME);
        // Heap-backed target ("example.com") -- the Str the dropped StrDeinit leaks.
Last updated on