Blame ares_parse_txt_reply.c

Packit 514978
Packit 514978
/* Copyright 1998 by the Massachusetts Institute of Technology.
Packit 514978
 * Copyright (C) 2009 by Jakub Hrozek <jhrozek@redhat.com>
Packit 514978
 *
Packit 514978
 * Permission to use, copy, modify, and distribute this
Packit 514978
 * software and its documentation for any purpose and without
Packit 514978
 * fee is hereby granted, provided that the above copyright
Packit 514978
 * notice appear in all copies and that both that copyright
Packit 514978
 * notice and this permission notice appear in supporting
Packit 514978
 * documentation, and that the name of M.I.T. not be used in
Packit 514978
 * advertising or publicity pertaining to distribution of the
Packit 514978
 * software without specific, written prior permission.
Packit 514978
 * M.I.T. makes no representations about the suitability of
Packit 514978
 * this software for any purpose.  It is provided "as is"
Packit 514978
 * without express or implied warranty.
Packit 514978
 */
Packit 514978
Packit 514978
#include "ares_setup.h"
Packit 514978
Packit 514978
#ifdef HAVE_NETINET_IN_H
Packit 514978
#  include <netinet/in.h>
Packit 514978
#endif
Packit 514978
#ifdef HAVE_NETDB_H
Packit 514978
#  include <netdb.h>
Packit 514978
#endif
Packit 514978
#ifdef HAVE_ARPA_INET_H
Packit 514978
#  include <arpa/inet.h>
Packit 514978
#endif
Packit 514978
#ifdef HAVE_ARPA_NAMESER_H
Packit 514978
#  include <arpa/nameser.h>
Packit 514978
#else
Packit 514978
#  include "nameser.h"
Packit 514978
#endif
Packit 514978
#ifdef HAVE_ARPA_NAMESER_COMPAT_H
Packit 514978
#  include <arpa/nameser_compat.h>
Packit 514978
#endif
Packit 514978
Packit 514978
#ifdef HAVE_STRINGS_H
Packit 514978
#  include <strings.h>
Packit 514978
#endif
Packit 514978
Packit 514978
#include "ares.h"
Packit 514978
#include "ares_dns.h"
Packit 514978
#include "ares_data.h"
Packit 514978
#include "ares_private.h"
Packit 514978
Packit 514978
static int
Packit 514978
ares__parse_txt_reply (const unsigned char *abuf, int alen,
Packit 514978
                       int ex, void **txt_out)
Packit 514978
{
Packit 514978
  size_t substr_len;
Packit 514978
  unsigned int qdcount, ancount, i;
Packit 514978
  const unsigned char *aptr;
Packit 514978
  const unsigned char *strptr;
Packit 514978
  int status, rr_type, rr_class, rr_len;
Packit 514978
  long len;
Packit 514978
  char *hostname = NULL, *rr_name = NULL;
Packit 514978
  struct ares_txt_ext *txt_head = NULL;
Packit 514978
  struct ares_txt_ext *txt_last = NULL;
Packit 514978
  struct ares_txt_ext *txt_curr;
Packit 514978
Packit 514978
  /* Set *txt_out to NULL for all failure cases. */
Packit 514978
  *txt_out = NULL;
Packit 514978
Packit 514978
  /* Give up if abuf doesn't have room for a header. */
Packit 514978
  if (alen < HFIXEDSZ)
Packit 514978
    return ARES_EBADRESP;
Packit 514978
Packit 514978
  /* Fetch the question and answer count from the header. */
Packit 514978
  qdcount = DNS_HEADER_QDCOUNT (abuf);
Packit 514978
  ancount = DNS_HEADER_ANCOUNT (abuf);
Packit 514978
  if (qdcount != 1)
Packit 514978
    return ARES_EBADRESP;
Packit 514978
  if (ancount == 0)
Packit 514978
    return ARES_ENODATA;
Packit 514978
Packit 514978
  /* Expand the name from the question, and skip past the question. */
Packit 514978
  aptr = abuf + HFIXEDSZ;
Packit 514978
  status = ares_expand_name (aptr, abuf, alen, &hostname, &len;;
Packit 514978
  if (status != ARES_SUCCESS)
Packit 514978
    return status;
Packit 514978
Packit 514978
  if (aptr + len + QFIXEDSZ > abuf + alen)
Packit 514978
    {
Packit 514978
      ares_free (hostname);
Packit 514978
      return ARES_EBADRESP;
Packit 514978
    }
Packit 514978
  aptr += len + QFIXEDSZ;
Packit 514978
Packit 514978
  /* Examine each answer resource record (RR) in turn. */
Packit 514978
  for (i = 0; i < ancount; i++)
Packit 514978
    {
Packit 514978
      /* Decode the RR up to the data field. */
Packit 514978
      status = ares_expand_name (aptr, abuf, alen, &rr_name, &len;;
Packit 514978
      if (status != ARES_SUCCESS)
Packit 514978
        {
Packit 514978
          break;
Packit 514978
        }
Packit 514978
      aptr += len;
Packit 514978
      if (aptr + RRFIXEDSZ > abuf + alen)
Packit 514978
        {
Packit 514978
          status = ARES_EBADRESP;
Packit 514978
          break;
Packit 514978
        }
Packit 514978
      rr_type = DNS_RR_TYPE (aptr);
Packit 514978
      rr_class = DNS_RR_CLASS (aptr);
Packit 514978
      rr_len = DNS_RR_LEN (aptr);
Packit 514978
      aptr += RRFIXEDSZ;
Packit 514978
      if (aptr + rr_len > abuf + alen)
Packit 514978
        {
Packit 514978
          status = ARES_EBADRESP;
Packit 514978
          break;
Packit 514978
        }
Packit 514978
Packit 514978
      /* Check if we are really looking at a TXT record */
Packit 514978
      if (rr_class == C_IN && rr_type == T_TXT)
Packit 514978
        {
Packit 514978
          /*
Packit 514978
           * There may be multiple substrings in a single TXT record. Each
Packit 514978
           * substring may be up to 255 characters in length, with a
Packit 514978
           * "length byte" indicating the size of the substring payload.
Packit 514978
           * RDATA contains both the length-bytes and payloads of all
Packit 514978
           * substrings contained therein.
Packit 514978
           */
Packit 514978
Packit 514978
          strptr = aptr;
Packit 514978
          while (strptr < (aptr + rr_len))
Packit 514978
            {
Packit 514978
              substr_len = (unsigned char)*strptr;
Packit 514978
              if (strptr + substr_len + 1 > aptr + rr_len)
Packit 514978
                {
Packit 514978
                  status = ARES_EBADRESP;
Packit 514978
                  break;
Packit 514978
                }
Packit 514978
Packit 514978
              /* Allocate storage for this TXT answer appending it to the list */
Packit 514978
              txt_curr = ares_malloc_data(ex ? ARES_DATATYPE_TXT_EXT :
Packit 514978
                                               ARES_DATATYPE_TXT_REPLY);
Packit 514978
              if (!txt_curr)
Packit 514978
                {
Packit 514978
                  status = ARES_ENOMEM;
Packit 514978
                  break;
Packit 514978
                }
Packit 514978
              if (txt_last)
Packit 514978
                {
Packit 514978
                  txt_last->next = txt_curr;
Packit 514978
                }
Packit 514978
              else
Packit 514978
                {
Packit 514978
                  txt_head = txt_curr;
Packit 514978
                }
Packit 514978
              txt_last = txt_curr;
Packit 514978
Packit 514978
              if (ex)
Packit 514978
                txt_curr->record_start = (strptr == aptr);
Packit 514978
              txt_curr->length = substr_len;
Packit 514978
              txt_curr->txt = ares_malloc (substr_len + 1/* Including null byte */);
Packit 514978
              if (txt_curr->txt == NULL)
Packit 514978
                {
Packit 514978
                  status = ARES_ENOMEM;
Packit 514978
                  break;
Packit 514978
                }
Packit 514978
Packit 514978
              ++strptr;
Packit 514978
              memcpy ((char *) txt_curr->txt, strptr, substr_len);
Packit 514978
Packit 514978
              /* Make sure we NULL-terminate */
Packit 514978
              txt_curr->txt[substr_len] = 0;
Packit 514978
Packit 514978
              strptr += substr_len;
Packit 514978
            }
Packit 514978
        }
Packit 514978
Packit 514978
      /* Propagate any failures */
Packit 514978
      if (status != ARES_SUCCESS)
Packit 514978
        {
Packit 514978
          break;
Packit 514978
        }
Packit 514978
Packit 514978
      /* Don't lose memory in the next iteration */
Packit 514978
      ares_free (rr_name);
Packit 514978
      rr_name = NULL;
Packit 514978
Packit 514978
      /* Move on to the next record */
Packit 514978
      aptr += rr_len;
Packit 514978
    }
Packit 514978
Packit 514978
  if (hostname)
Packit 514978
    ares_free (hostname);
Packit 514978
  if (rr_name)
Packit 514978
    ares_free (rr_name);
Packit 514978
Packit 514978
  /* clean up on error */
Packit 514978
  if (status != ARES_SUCCESS)
Packit 514978
    {
Packit 514978
      if (txt_head)
Packit 514978
        ares_free_data (txt_head);
Packit 514978
      return status;
Packit 514978
    }
Packit 514978
Packit 514978
  /* everything looks fine, return the data */
Packit 514978
  *txt_out = txt_head;
Packit 514978
Packit 514978
  return ARES_SUCCESS;
Packit 514978
}
Packit 514978
Packit 514978
int
Packit 514978
ares_parse_txt_reply (const unsigned char *abuf, int alen,
Packit 514978
                      struct ares_txt_reply **txt_out)
Packit 514978
{
Packit 514978
  return ares__parse_txt_reply(abuf, alen, 0, (void **) txt_out);
Packit 514978
}
Packit 514978
Packit 514978
Packit 514978
int
Packit 514978
ares_parse_txt_reply_ext (const unsigned char *abuf, int alen,
Packit 514978
                          struct ares_txt_ext **txt_out)
Packit 514978
{
Packit 514978
  return ares__parse_txt_reply(abuf, alen, 1, (void **) txt_out);
Packit 514978
}