ARES_PARSE_CAA_REPLY(3)

πŸ“¦
version 1.34.9 Β· 3 versions
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, centos-stream 10, debian trixie, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– ARES_PARSE_CAA_REPLY πŸ’–

okay bestie, so this one's from the Library Functions Manual and it's called ares_parse_caa_reply, and no cap it is SO much simpler than it sounds. it's the CAA parser. that's it. that's the whole vibe. πŸ¦‹


✨ NAME ✨

ares_parse_caa_reply β€” parse a reply to a DNS query of type CAA

like. the name literally tells you everything. she parses. she's a CAA reply. iconic behaviour honestly. πŸ’…


🌸 SYNOPSIS 🌸

#include <ares.h>

int ares_parse_caa_reply(const unsigned char* abuf, int alen,
                         struct ares_caa_reply **caa_out);

πŸ’œ DESCRIPTION πŸ’œ

so ares_parse_caa_reply takes the response to a query of type CAA and turns it into a linked list β€” one element per sub-string, bestie, don't forget that part β€” made of struct ares_caa_reply ✨

the params abuf and alen are literally just the contents of the response, hand them over and she'll do her thing. the result gets stored in allocated memory, and a pointer to it gets stored into the variable pointed to by caa_out. πŸŽ€

⚠️ and listen β€” it is the caller's responsibility to free the resulting caa_out structure when it's no longer needed, using ares_free_data(3). do NOT forget. she will not clean up after you. that's your job, bestie. 😭

the structure ares_caa_reply(3) has these fields, and they're giving very much:

struct ares_caa_reply {
  struct ares_caa_reply *next;
  int                    critical;
  unsigned char         *property;
  size_t                 plength; /* plength excludes null */
  unsigned char         *value;
  size_t                 length;  /* length excludes null */
};

quick notes so you don't get confused later: plength does NOT include the null, and length also does NOT include the null. she's precise like that. πŸ’…


⭐ RETURN VALUES ⭐

ares_parse_caa_reply can return any of these, so let's go through them one by one, no skipping:

  • ARES_SUCCESS β€” the response was successfully parsed. slay! πŸŽ‰
  • ARES_EBADRESP β€” the response was malformatted. it's giving broken. πŸ’€
  • ARES_ENODATA β€” the response did not contain an answer to the query. empty. void. nothing there. πŸ™ˆ
  • ARES_ENOMEM β€” memory was exhausted. she ran out of room, it happens. 😭

🌈 EXAMPLE 🌈

okay here's a whole entire working example, copy it, love it, run it:

#include <arpa/inet.h>
#include <time.h>
#include <sys/time.h>
#include <netdb.h>

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#include "ares.h"

static void dns_callback(void *arg,
                         int status,
                         int timeouts,
                         unsigned char *abuf,
                         int alen)
  {
    struct ares_caa_reply *caa_out;
    int err;

    err = ares_parse_caa_reply (abuf, alen, &caa_out);
    if (err == ARES_SUCCESS)
      {
        struct ares_caa_reply *caa_curr;
        for (caa_curr=caa_out; caa_curr; caa_curr=caa_curr->next)
          printf ("%s. CAA %i %s \"%s\"\n", arg,
                                            caa_curr->critical,
                                            caa_curr->property,
                                            caa_curr->value);
      }
    else
      {
        printf ("err=%i\n", err);
      }
    ares_free_data (caa_out);
  }

static void main_loop(ares_channel_t **channel)
  {
    int nfds, count;
    fd_set readers, writers;
    struct timeval tv, *tvp;
    while (1)
      {
        FD_ZERO (&readers);
        FD_ZERO (&writers);
        nfds = ares_fds (*channel, &readers, &writers);
        if (nfds == 0)
          break;
        tvp = ares_timeout (*channel, NULL, &tv);
        count = select (nfds, &readers, &writers, NULL, tvp);
        ares_process (*channel, &readers, &writers);
      }
  }

int main(int argc, char **argv)
  {
    const char *sversion;
    int iversion;
    int err;

    sversion = ares_version (&iversion);
    printf ("c-ares version %s\n", sversion);

    char *domain = "wikipedia.org";
    if (argc > 1)
      domain = argv[1];

    ares_channel_t *channel;
    if ((err = ares_init (&channel)) != ARES_SUCCESS)
      {
        printf ("ares_init() failed (%i)\n", err);
        exit (EXIT_FAILURE);
      }

    ares_query (channel, domain,
                1,   /* ns_c_in */
                257, /* T_CAA */
                dns_callback, domain);

    main_loop (&channel);

    ares_destroy (channel);

    exit (EXIT_SUCCESS);
  }

see how she loops over caa_curr=caa_out; caa_curr; caa_curr=caa_curr->next? that's the linked list walk, bestie. and notice 257 is T_CAA and 1 is ns_c_in β€” the comments are right there, iykyk. πŸ”₯


🧚 AVAILABILITY 🧚

this function was first introduced in c-ares version 1.17.0. so if you're on something older, sorry babe, she doesn't exist yet. πŸ’œ


πŸ‘‘ SEE ALSO πŸ‘‘


and that's the whole page!! mwah πŸ’‹ this was ARES_PARSE_CAA_REPLY(3) from the Library Functions Manual, dated 16 September 2020. go parse some CAA records and free your memory, bestie πŸ«ΆπŸ’–βœ¨