cereal 1,524 Nearly a Senior Poster Featured Poster


I'm wondering if I can get help to understand the behaviour of a PHP function: dns_get_record(). This returns an array with DNS records for the given domain name, here is an example of the output:

php > print_r(dns_get_record('php.net', DNS_TXT));
    [0] => Array
            [host] => php.net
            [type] => TXT
            [txt] => v=spf1 ptr ?all
            [entries] => Array
                    [0] => v=spf1 ptr ?all

            [class] => IN
            [ttl] => 272


Now, if the DNS server returns multiple TXT records I get n keys ([0], [1], ...). What I'm not sure about is if the entries array will always return one entry. From the RFC 1035 I see the format of a TXT record is a text string.

But the PHP documentation does not mention the entries array nor it explains if it will always count one value. So I've searched the source file and this is the relevant part:

case DNS_T_TXT:
        int ll = 0;
        zval *entries = NULL;

        add_assoc_string(*subarray, "type", "TXT", 1);
        tp = emalloc(dlen + 1);


        while (ll < dlen) {
            n = cp[ll];
            memcpy(tp + ll , cp + ll + 1, n);
            add_next_index_stringl(entries, cp + ll + 1, n, 1);
            ll = ll + n + 1;
        tp[dlen] = '\0';
        cp += dlen;

        add_assoc_stringl(*subarray, "txt", tp, (dlen>0)?dlen - 1:0, 0);
        add_assoc_zval(*subarray, "entries", entries);

I understand that add_assoc_* functions are adding the values to the subarray variable, that will return the results at the end. But it is not clear to me what happens inside the while loop. Is this related to the length of the TXT record? Could this string be splitted into multiple entries? For example:

[entries] => Array
        [0] => part 1
        [1] => part 2
        [2] => part 3

In practice I'm just trying to understand if I can completely ignore the entries array and rely only on txt key or if, in some cases, I can miss some data. From what I understand there should be a difference between txt and entries, because txt is defined by add_assoc_stringl(), the documentation says:

Adds a string with the desired length length to the array. Otherwise, behaves like add_assoc_string().

But I would like to have another opinion. I don't have experience in C so I will be grateful to who can help me to understand.

Thanks for your attention.

Documentation links:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.