/* Copyright 1998 by the Massachusetts Institute of Technology.
 *
 * Permission to use, copy, modify, and distribute this
 * software and its documentation for any purpose and without
 * fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting
 * documentation, and that the name of M.I.T. not be used in
 * advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 */

static const char rcsid[] = "$Id$";

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include "ares.h"
#include "ares_dns.h"
#include "ares_private.h"

int ares_parse_reply(const unsigned char *abuf, int alen,
		     struct ares_record **reply)
{
  unsigned int qdcount, ancount;
  int status, i;
  long len;
  int naliases;
  const unsigned char *aptr;
  char *hostname, *rr_name, *rr_data, **aliases;
  struct ares_record *head, **next, *rr;

  head = NULL;
  next = &head;

  /* Set *reply to NULL for all failure cases. */
  *reply = NULL;

  /* Give up if abuf doesn't have room for a header. */
  if (alen < HFIXEDSZ)
    return ARES_EBADRESP;

  /* Fetch the question and answer count from the header. */
  qdcount = DNS_HEADER_QDCOUNT(abuf);
  ancount = DNS_HEADER_ANCOUNT(abuf);
  if (qdcount != 1)
    return ARES_EBADRESP;

  /* Expand the name from the question, and skip past the question. */
  aptr = (unsigned char *)abuf + HFIXEDSZ;
  status = ares_expand_name(aptr, abuf, alen, &hostname, &len);
  if (status != ARES_SUCCESS)
    return status;
  free(hostname);
  if (aptr + len + QFIXEDSZ > abuf + alen)
    {
      return ARES_EBADRESP;
    }
  aptr += len + QFIXEDSZ;

  status = ARES_EBADRESP;

  /* Examine each answer resource record (RR) in turn. */
  for (i = 0; i < ancount; i++)
    {
      /* Decode the RR up to the data field. */
      status = ares_expand_name(aptr, abuf, alen, &rr_name, &len);
      if (status != ARES_SUCCESS)
	break;
      aptr += len;
      if (aptr + RRFIXEDSZ > abuf + alen)
	{
	  status = ARES_EBADRESP;
	  break;
	}

      rr = *next = malloc(sizeof(struct ares_record));
      if (rr == NULL)
      {
	free(rr_name);
        return ARES_ENOMEM;
      }

      next = &(*next)->next;
      memset(rr, 0, sizeof(*rr));

      rr->domain = rr_name;
      rr->type = DNS_RR_TYPE(aptr);
      rr->class = DNS_RR_CLASS(aptr);
      rr->size = DNS_RR_LEN(aptr);
      rr->ttl = DNS_RR_TTL(aptr);
      aptr += RRFIXEDSZ;

      if (aptr + rr->size > abuf + alen)
	{
	  status = ARES_EBADRESP;
	  break;
	}

      switch(rr->type)
	{
	case T_A:
	  if (rr->size != sizeof(struct in_addr))
	    {
	      status = ARES_EBADRESP;
	      break;
	    }
	  rr->data.a = malloc(sizeof(struct in_addr));
	  if (rr->data.a == NULL)
	    {
	      status = ARES_ENOMEM;
	      break;
	    }
	  memcpy(rr->data.a, aptr, sizeof(struct in_addr));
	  status = ARES_SUCCESS;
	  break;
	case T_SOA:
	  {
	     char *email;
	     const unsigned char *p = aptr;
             status = ares_expand_name(p, abuf, alen, &hostname, &len);
             if (status != ARES_SUCCESS)
               break;
	     p += len;
             status = ares_expand_name(p, abuf, alen, &email, &len);
	     if (status != ARES_SUCCESS)
	     {
               free(hostname);
	       break;
             }
	     p += len;
             if (p + 20 > aptr + rr->size)
	     {
	       free(hostname);
	       free(email);
	       status = ARES_EBADRESP;
	       break;
	     }

	     rr->data.soa = malloc(sizeof(struct ares_soa_record));
	     rr->data.soa->master = hostname;
	     rr->data.soa->mailbox = email;
	     rr->data.soa->serial = DNS__32BIT(p);
	     rr->data.soa->refresh = DNS__32BIT(p+4);
	     rr->data.soa->retry = DNS__32BIT(p+8);
	     rr->data.soa->expire = DNS__32BIT(p+12);
	     rr->data.soa->minimum = DNS__32BIT(p+16);
             break;
          }
	case T_MX:
	case T_AFSDB:

	  if (rr->size < 2)
	    {
	      status = ARES_EBADRESP;
	      break;
	    }
	  status = ares_expand_name(aptr + 2, abuf, alen, &hostname, &len);
	  if (status != ARES_SUCCESS)
	    break;
	  
	  rr->data.mx = malloc(sizeof(struct ares_mx_record));
	  if (rr->data.mx == NULL)
	    {
	      free(hostname);
	      status = ARES_ENOMEM;
	      break;
	    }
  
	  rr->data.mx->preference = DNS__16BIT(aptr);
	  rr->data.mx->domain = hostname;
	  break;
        case T_SRV:
	  if (rr->size < 6)
	    {
	      status = ARES_EBADRESP;
	      break;
	    }
	  status = ares_expand_name(aptr + 6, abuf, alen, &hostname, &len);
	  if (status != ARES_SUCCESS)
	    break;

	  rr->data.srv = malloc(sizeof(struct ares_srv_record));
	  if (rr->data.srv == NULL)
	    {
	      free(hostname);
	      status = ARES_ENOMEM;
	      break;
	    }
          rr->data.srv->priority = DNS__16BIT(aptr);
	  rr->data.srv->weight = DNS__16BIT(aptr+2);
	  rr->data.srv->port = DNS__16BIT(aptr+4);

	  rr->data.srv->target = hostname;
	  break;
	case T_CNAME:
	case T_PTR:
	case T_NS:
	  status = ares_expand_name(aptr, abuf, alen, &hostname, &len);
	  if (status != ARES_SUCCESS)
	    break;
	  rr->data.txt = hostname;
	  break;
	default:
	  break;
      }
      aptr += rr->size;

      if (status != ARES_SUCCESS)
	break;
    }

  if (status != ARES_SUCCESS)
    {
      ares_free_reply(head);
      head = NULL;
    }
    
  *reply = head;

  return status;
}

void ares_free_reply(struct ares_record *reply)
{
    struct resource_record *rr;
    while (reply != NULL)
      {
	struct ares_record *tmp = reply;
	if(reply->domain)
	  free(reply->domain);
	if (reply->data.data)
	{
	  switch (reply->type)
	    {
	      case T_MX:
	      case T_AFSDB:
	        free(reply->data.mx->domain);
                break;
	      case T_SRV:
		free(reply->data.srv->target);
		break;
	      case T_SOA:
		free(reply->data.soa->master);
		free(reply->data.soa->mailbox);
		break;
	      default:
            }
          free(reply->data.data);
        }
	reply = reply->next;
	free(tmp);
      }
}

