2006-07-22 17:37:10 +02:00
|
|
|
/* $Id$ */
|
2006-07-22 17:21:13 +02:00
|
|
|
|
2003-10-07 23:54:04 +02:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2009-11-02 12:55:53 +01:00
|
|
|
#include "ares_setup.h"
|
2003-10-07 23:54:04 +02:00
|
|
|
|
2008-09-15 19:14:29 +02:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
2008-09-17 03:02:57 +02:00
|
|
|
# include <sys/socket.h>
|
2008-09-15 19:14:29 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
2008-09-17 03:02:57 +02:00
|
|
|
# include <netinet/in.h>
|
2008-09-15 19:14:29 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
2008-09-17 03:02:57 +02:00
|
|
|
# include <netdb.h>
|
2008-09-15 19:14:29 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARPA_NAMESER_H
|
2008-09-17 03:02:57 +02:00
|
|
|
# include <arpa/nameser.h>
|
|
|
|
#else
|
|
|
|
# include "nameser.h"
|
2008-09-15 19:14:29 +02:00
|
|
|
#endif
|
2005-04-07 00:27:42 +02:00
|
|
|
#ifdef HAVE_ARPA_NAMESER_COMPAT_H
|
2008-09-17 03:02:57 +02:00
|
|
|
# include <arpa/nameser_compat.h>
|
2003-10-07 23:54:04 +02:00
|
|
|
#endif
|
2008-09-17 03:02:57 +02:00
|
|
|
|
2008-05-09 00:11:38 +02:00
|
|
|
#ifdef HAVE_STRINGS_H
|
2008-09-17 03:02:57 +02:00
|
|
|
# include <strings.h>
|
2008-05-09 00:11:38 +02:00
|
|
|
#endif
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "ares.h"
|
|
|
|
#include "ares_dns.h"
|
|
|
|
#include "ares_private.h"
|
|
|
|
|
|
|
|
int ares_parse_ptr_reply(const unsigned char *abuf, int alen, const void *addr,
|
2004-10-06 09:50:18 +02:00
|
|
|
int addrlen, int family, struct hostent **host)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
|
|
|
unsigned int qdcount, ancount;
|
2004-02-23 08:52:20 +01:00
|
|
|
int status, i, rr_type, rr_class, rr_len;
|
|
|
|
long len;
|
2003-10-07 23:54:04 +02:00
|
|
|
const unsigned char *aptr;
|
|
|
|
char *ptrname, *hostname, *rr_name, *rr_data;
|
|
|
|
struct hostent *hostent;
|
2008-05-07 23:20:45 +02:00
|
|
|
int aliascnt = 0;
|
2008-11-26 18:04:35 +01:00
|
|
|
int alias_alloc = 8;
|
2008-05-07 23:20:45 +02:00
|
|
|
char ** aliases;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
/* Set *host to NULL for all failure cases. */
|
|
|
|
*host = 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 = abuf + HFIXEDSZ;
|
2009-01-11 14:49:13 +01:00
|
|
|
status = ares__expand_name_for_response(aptr, abuf, alen, &ptrname, &len);
|
2003-10-07 23:54:04 +02:00
|
|
|
if (status != ARES_SUCCESS)
|
|
|
|
return status;
|
|
|
|
if (aptr + len + QFIXEDSZ > abuf + alen)
|
|
|
|
{
|
|
|
|
free(ptrname);
|
|
|
|
return ARES_EBADRESP;
|
|
|
|
}
|
|
|
|
aptr += len + QFIXEDSZ;
|
|
|
|
|
|
|
|
/* Examine each answer resource record (RR) in turn. */
|
|
|
|
hostname = NULL;
|
2008-11-26 18:04:35 +01:00
|
|
|
aliases = malloc(alias_alloc * sizeof(char *));
|
2008-05-07 23:20:45 +02:00
|
|
|
if (!aliases)
|
|
|
|
{
|
|
|
|
free(ptrname);
|
|
|
|
return ARES_ENOMEM;
|
|
|
|
}
|
2004-02-04 08:51:30 +01:00
|
|
|
for (i = 0; i < (int)ancount; i++)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
|
|
|
/* Decode the RR up to the data field. */
|
2009-01-11 14:49:13 +01:00
|
|
|
status = ares__expand_name_for_response(aptr, abuf, alen, &rr_name, &len);
|
2003-10-07 23:54:04 +02:00
|
|
|
if (status != ARES_SUCCESS)
|
2004-10-06 09:50:18 +02:00
|
|
|
break;
|
2003-10-07 23:54:04 +02:00
|
|
|
aptr += len;
|
|
|
|
if (aptr + RRFIXEDSZ > abuf + alen)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
status = ARES_EBADRESP;
|
|
|
|
break;
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
rr_type = DNS_RR_TYPE(aptr);
|
|
|
|
rr_class = DNS_RR_CLASS(aptr);
|
|
|
|
rr_len = DNS_RR_LEN(aptr);
|
|
|
|
aptr += RRFIXEDSZ;
|
|
|
|
|
|
|
|
if (rr_class == C_IN && rr_type == T_PTR
|
2004-10-06 09:50:18 +02:00
|
|
|
&& strcasecmp(rr_name, ptrname) == 0)
|
|
|
|
{
|
|
|
|
/* Decode the RR data and set hostname to it. */
|
2009-01-11 14:49:13 +01:00
|
|
|
status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
|
|
|
|
&len);
|
2004-10-06 09:50:18 +02:00
|
|
|
if (status != ARES_SUCCESS)
|
|
|
|
break;
|
|
|
|
if (hostname)
|
|
|
|
free(hostname);
|
|
|
|
hostname = rr_data;
|
2008-05-07 23:20:45 +02:00
|
|
|
aliases[aliascnt] = malloc((strlen(rr_data)+1) * sizeof(char *));
|
|
|
|
if (!aliases[aliascnt])
|
|
|
|
{
|
|
|
|
status = ARES_ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
strncpy(aliases[aliascnt], rr_data, strlen(rr_data)+1);
|
|
|
|
aliascnt++;
|
2008-11-26 18:04:35 +01:00
|
|
|
if (aliascnt >= alias_alloc) {
|
2008-11-16 00:07:35 +01:00
|
|
|
char **ptr;
|
2008-11-26 18:04:35 +01:00
|
|
|
alias_alloc *= 2;
|
|
|
|
ptr = realloc(aliases, alias_alloc * sizeof(char *));
|
2008-11-16 00:07:35 +01:00
|
|
|
if(!ptr) {
|
|
|
|
status = ARES_ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
aliases = ptr;
|
|
|
|
}
|
2004-10-06 09:50:18 +02:00
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
if (rr_class == C_IN && rr_type == T_CNAME)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
/* Decode the RR data and replace ptrname with it. */
|
2009-01-11 14:49:13 +01:00
|
|
|
status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
|
|
|
|
&len);
|
2004-10-06 09:50:18 +02:00
|
|
|
if (status != ARES_SUCCESS)
|
|
|
|
break;
|
|
|
|
free(ptrname);
|
|
|
|
ptrname = rr_data;
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
free(rr_name);
|
|
|
|
aptr += rr_len;
|
|
|
|
if (aptr > abuf + alen)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
status = ARES_EBADRESP;
|
|
|
|
break;
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (status == ARES_SUCCESS && !hostname)
|
|
|
|
status = ARES_ENODATA;
|
|
|
|
if (status == ARES_SUCCESS)
|
|
|
|
{
|
|
|
|
/* We got our answer. Allocate memory to build the host entry. */
|
|
|
|
hostent = malloc(sizeof(struct hostent));
|
|
|
|
if (hostent)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
hostent->h_addr_list = malloc(2 * sizeof(char *));
|
|
|
|
if (hostent->h_addr_list)
|
|
|
|
{
|
|
|
|
hostent->h_addr_list[0] = malloc(addrlen);
|
|
|
|
if (hostent->h_addr_list[0])
|
|
|
|
{
|
2008-05-07 23:20:45 +02:00
|
|
|
hostent->h_aliases = malloc((aliascnt+1) * sizeof (char *));
|
2004-10-06 09:50:18 +02:00
|
|
|
if (hostent->h_aliases)
|
|
|
|
{
|
|
|
|
/* Fill in the hostent and return successfully. */
|
|
|
|
hostent->h_name = hostname;
|
2008-05-07 23:20:45 +02:00
|
|
|
for (i=0 ; i<aliascnt ; i++)
|
|
|
|
hostent->h_aliases[i] = aliases[i];
|
|
|
|
hostent->h_aliases[aliascnt] = NULL;
|
2004-10-06 09:50:18 +02:00
|
|
|
hostent->h_addrtype = family;
|
|
|
|
hostent->h_length = addrlen;
|
|
|
|
memcpy(hostent->h_addr_list[0], addr, addrlen);
|
|
|
|
hostent->h_addr_list[1] = NULL;
|
|
|
|
*host = hostent;
|
2008-05-07 23:20:45 +02:00
|
|
|
free(aliases);
|
2004-10-06 09:50:18 +02:00
|
|
|
free(ptrname);
|
|
|
|
return ARES_SUCCESS;
|
|
|
|
}
|
|
|
|
free(hostent->h_addr_list[0]);
|
|
|
|
}
|
|
|
|
free(hostent->h_addr_list);
|
|
|
|
}
|
|
|
|
free(hostent);
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
status = ARES_ENOMEM;
|
|
|
|
}
|
2008-05-07 23:20:45 +02:00
|
|
|
for (i=0 ; i<aliascnt ; i++)
|
|
|
|
if (aliases[i])
|
|
|
|
free(aliases[i]);
|
|
|
|
free(aliases);
|
2003-10-07 23:54:04 +02:00
|
|
|
if (hostname)
|
|
|
|
free(hostname);
|
|
|
|
free(ptrname);
|
|
|
|
return status;
|
|
|
|
}
|