Name

cyg_mdns_build_txt_vector — Build vector of pointers to individual TXT Record fields

Synopsis

#include <mdns.h>

cyg_uint8 cyg_mdns_build_txt_vector(const cyg_uint8 *txt, cyg_uint16 txtlen, cyg_uint8 **txtkeys, cyg_uint8 vlen);

Description

This helper function is used to construct a vector of the individual TXT Resource Record fields for easier client processing.

[Note]Note

This function is not available if the system is not configured with CYGIMP_NET_MDNS_DNSSD enabled.

The txt parameter should reference a contiguous buffer containing a TXT Record string-table - length byte followed by character data, possibly followed by another length byte and more character data and so on. NOTE: An empty TXT string-table is marked with a NUL character, so txtlen should always be 1 or more, but a string-table containing entries does NOT have a terminating NUL, which is why the txtlen parameter is required.

If the passed txtkeys value is NULL then the function counts the number of entries needed to hold the individual key[=value] entries within the TXT Record, so can be used to ascertain the size of vector needed for a specific TXT Record. When txtkeys is non-NULL it points to the beginning of an array of cyg_uint8 pointers. The vlen parameter specifies the number of vector slots available in this array to receive pointers into the txt buffer for the individual entries (which for TXT Records will be in the form key or key=value). When populating the supplied txtkeys vector a terminating NULL pointer is added if the supplied vlen indicates the supplied vector is larger than the number of TXT fields found.

For example:

static const cyg_uint8 example_txt[] = {
  0x04,'a','b','c','d',
  0x0C,'s','o','m','e','k','e','y','=','d','a','t','a'
};
cyg_uint16 txtlen = (cyg_uint16)sizeof(example_txt);

cyg_uint8 entries = cyg_mdns_build_txt_vector(example_txt,txtlen,NULL,0);
cyg_uint8 *txtkeys[entries + 1]; // extra “+1” for terminating NULL

(void)cyg_mdns_build_txt_vector(example_txt,txtlen,txtkeys,(entries + 1));
diag_printf("  TXT-%u contains %u entr%s\n",ti,entries,((entries == 1) ? "y" : "ies"));

unsigned int idx;
for (idx = 0; (idx < entries); idx++) {
  cyg_uint8 klen = txtkeys[idx][0];
  if (klen) {
    cyg_uint8 key[klen + 1];
    memcpy(key,&txtkeys[idx][1],klen);
    key[klen] = '\0';
    diag_printf("    [%u]=\"%s\"\n",idx,key);
  } else {
    diag_printf("    [%u]={EMPTY}\n",idx);
  }
}

Return value

Returns a count of the number of key[=value] entries in the referenced TXT Record.