|
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 |
#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 |
/* AIX portability check */
|
|
Packit |
514978 |
#ifndef T_SRV
|
|
Packit |
514978 |
# define T_SRV 33 /* server selection */
|
|
Packit |
514978 |
#endif
|
|
Packit |
514978 |
|
|
Packit |
514978 |
int
|
|
Packit |
514978 |
ares_parse_srv_reply (const unsigned char *abuf, int alen,
|
|
Packit |
514978 |
struct ares_srv_reply **srv_out)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
unsigned int qdcount, ancount, i;
|
|
Packit |
514978 |
const unsigned char *aptr, *vptr;
|
|
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_srv_reply *srv_head = NULL;
|
|
Packit |
514978 |
struct ares_srv_reply *srv_last = NULL;
|
|
Packit |
514978 |
struct ares_srv_reply *srv_curr;
|
|
Packit |
514978 |
|
|
Packit |
514978 |
/* Set *srv_out to NULL for all failure cases. */
|
|
Packit |
514978 |
*srv_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 SRV record */
|
|
Packit |
514978 |
if (rr_class == C_IN && rr_type == T_SRV)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
/* parse the SRV record itself */
|
|
Packit |
514978 |
if (rr_len < 6)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
status = ARES_EBADRESP;
|
|
Packit |
514978 |
break;
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
|
|
Packit |
514978 |
/* Allocate storage for this SRV answer appending it to the list */
|
|
Packit |
514978 |
srv_curr = ares_malloc_data(ARES_DATATYPE_SRV_REPLY);
|
|
Packit |
514978 |
if (!srv_curr)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
status = ARES_ENOMEM;
|
|
Packit |
514978 |
break;
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
if (srv_last)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
srv_last->next = srv_curr;
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
else
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
srv_head = srv_curr;
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
srv_last = srv_curr;
|
|
Packit |
514978 |
|
|
Packit |
514978 |
vptr = aptr;
|
|
Packit |
514978 |
srv_curr->priority = DNS__16BIT(vptr);
|
|
Packit |
514978 |
vptr += sizeof(unsigned short);
|
|
Packit |
514978 |
srv_curr->weight = DNS__16BIT(vptr);
|
|
Packit |
514978 |
vptr += sizeof(unsigned short);
|
|
Packit |
514978 |
srv_curr->port = DNS__16BIT(vptr);
|
|
Packit |
514978 |
vptr += sizeof(unsigned short);
|
|
Packit |
514978 |
|
|
Packit |
514978 |
status = ares_expand_name (vptr, abuf, alen, &srv_curr->host, &len;;
|
|
Packit |
514978 |
if (status != ARES_SUCCESS)
|
|
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 (srv_head)
|
|
Packit |
514978 |
ares_free_data (srv_head);
|
|
Packit |
514978 |
return status;
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
|
|
Packit |
514978 |
/* everything looks fine, return the data */
|
|
Packit |
514978 |
*srv_out = srv_head;
|
|
Packit |
514978 |
|
|
Packit |
514978 |
return ARES_SUCCESS;
|
|
Packit |
514978 |
}
|