Blame ares_parse_ns_reply.c

Packit 514978
/* Copyright 1998 by the Massachusetts Institute of Technology.
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
/*
Packit 514978
 * ares_parse_ns_reply created by Vlad Dinulescu <vlad.dinulescu@avira.com>
Packit 514978
 *      on behalf of AVIRA Gmbh - http://www.avira.com
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
#include "ares.h"
Packit 514978
#include "ares_dns.h"
Packit 514978
#include "ares_private.h"
Packit 514978
Packit 514978
int ares_parse_ns_reply( const unsigned char* abuf, int alen,
Packit 514978
                         struct hostent** host )
Packit 514978
{
Packit 514978
  unsigned int qdcount, ancount;
Packit 514978
  int status, i, rr_type, rr_class, rr_len;
Packit 514978
  int nameservers_num;
Packit 514978
  long len;
Packit 514978
  const unsigned char *aptr;
Packit 514978
  char* hostname, *rr_name, *rr_data, **nameservers;
Packit 514978
  struct hostent *hostent;
Packit 514978
Packit 514978
  /* Set *host to NULL for all failure cases. */
Packit 514978
  *host = 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
Packit 514978
  /* Expand the name from the question, and skip past the question. */
Packit 514978
  aptr = abuf + HFIXEDSZ;
Packit 514978
  status = ares__expand_name_for_response( aptr, abuf, alen, &hostname, &len;;
Packit 514978
  if ( status != ARES_SUCCESS )
Packit 514978
    return status;
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
  /* Allocate nameservers array; ancount gives an upper bound */
Packit 514978
  nameservers = ares_malloc( ( ancount + 1 ) * sizeof( char * ) );
Packit 514978
  if ( !nameservers )
Packit 514978
  {
Packit 514978
    ares_free( hostname );
Packit 514978
    return ARES_ENOMEM;
Packit 514978
  }
Packit 514978
  nameservers_num = 0;
Packit 514978
Packit 514978
  /* Examine each answer resource record (RR) in turn. */
Packit 514978
  for ( i = 0; i < ( int ) ancount; i++ )
Packit 514978
  {
Packit 514978
    /* Decode the RR up to the data field. */
Packit 514978
    status = ares__expand_name_for_response( aptr, abuf, alen, &rr_name, &len );
Packit 514978
    if ( status != ARES_SUCCESS )
Packit 514978
      break;
Packit 514978
    aptr += len;
Packit 514978
    if ( aptr + RRFIXEDSZ > abuf + alen )
Packit 514978
    {
Packit 514978
      status = ARES_EBADRESP;
Packit 514978
      ares_free(rr_name);
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
        ares_free(rr_name);
Packit 514978
        status = ARES_EBADRESP;
Packit 514978
        break;
Packit 514978
      }
Packit 514978
Packit 514978
    if ( rr_class == C_IN && rr_type == T_NS )
Packit 514978
    {
Packit 514978
      /* Decode the RR data and add it to the nameservers list */
Packit 514978
      status = ares__expand_name_for_response( aptr, abuf, alen, &rr_data,
Packit 514978
                                               &len;;
Packit 514978
      if ( status != ARES_SUCCESS )
Packit 514978
      {
Packit 514978
        ares_free(rr_name);
Packit 514978
        break;
Packit 514978
      }
Packit 514978
Packit 514978
      nameservers[nameservers_num] = ares_malloc(strlen(rr_data)+1);
Packit 514978
Packit 514978
      if (nameservers[nameservers_num]==NULL)
Packit 514978
      {
Packit 514978
        ares_free(rr_name);
Packit 514978
        ares_free(rr_data);
Packit 514978
        status=ARES_ENOMEM;
Packit 514978
        break;
Packit 514978
      }
Packit 514978
      strcpy(nameservers[nameservers_num],rr_data);
Packit 514978
      ares_free(rr_data);
Packit 514978
Packit 514978
      nameservers_num++;
Packit 514978
    }
Packit 514978
Packit 514978
    ares_free( rr_name );
Packit 514978
Packit 514978
    aptr += rr_len;
Packit 514978
    if ( aptr > abuf + alen )
Packit 514978
    {  /* LCOV_EXCL_START: already checked above */
Packit 514978
      status = ARES_EBADRESP;
Packit 514978
      break;
Packit 514978
    }  /* LCOV_EXCL_STOP */
Packit 514978
  }
Packit 514978
Packit 514978
  if ( status == ARES_SUCCESS && nameservers_num == 0 )
Packit 514978
  {
Packit 514978
    status = ARES_ENODATA;
Packit 514978
  }
Packit 514978
  if ( status == ARES_SUCCESS )
Packit 514978
  {
Packit 514978
    /* We got our answer.  Allocate memory to build the host entry. */
Packit 514978
    nameservers[nameservers_num] = NULL;
Packit 514978
    hostent = ares_malloc( sizeof( struct hostent ) );
Packit 514978
    if ( hostent )
Packit 514978
    {
Packit 514978
      hostent->h_addr_list = ares_malloc( 1 * sizeof( char * ) );
Packit 514978
      if ( hostent->h_addr_list )
Packit 514978
      {
Packit 514978
        /* Fill in the hostent and return successfully. */
Packit 514978
        hostent->h_name = hostname;
Packit 514978
        hostent->h_aliases = nameservers;
Packit 514978
        hostent->h_addrtype = AF_INET;
Packit 514978
        hostent->h_length = sizeof( struct in_addr );
Packit 514978
        hostent->h_addr_list[0] = NULL;
Packit 514978
        *host = hostent;
Packit 514978
        return ARES_SUCCESS;
Packit 514978
      }
Packit 514978
      ares_free( hostent );
Packit 514978
    }
Packit 514978
    status = ARES_ENOMEM;
Packit 514978
  }
Packit 514978
  for ( i = 0; i < nameservers_num; i++ )
Packit 514978
    ares_free( nameservers[i] );
Packit 514978
  ares_free( nameservers );
Packit 514978
  ares_free( hostname );
Packit 514978
  return status;
Packit 514978
}