Jakub Hrozek modified ares_parse_srv_reply() and ares_parse_txt_reply() API

to return a linked lists of results. These were also modified to internally
use the ares_data memory struct and as such its result must be free'ed with
ares_free_data().
This commit is contained in:
Yang Tse
2009-11-20 09:06:33 +00:00
parent 5a0a473c30
commit 7e6a67b436
4 changed files with 78 additions and 76 deletions

View File

@@ -476,13 +476,11 @@ CARES_EXTERN int ares_parse_ns_reply(const unsigned char *abuf,
CARES_EXTERN int ares_parse_srv_reply(const unsigned char* abuf, CARES_EXTERN int ares_parse_srv_reply(const unsigned char* abuf,
int alen, int alen,
struct ares_srv_reply** srv_out, struct ares_srv_reply** srv_out);
int *nsrvreply);
CARES_EXTERN int ares_parse_txt_reply(const unsigned char* abuf, CARES_EXTERN int ares_parse_txt_reply(const unsigned char* abuf,
int alen, int alen,
struct ares_txt_reply** txt_out, struct ares_txt_reply** txt_out);
int *nsrvreply);
CARES_EXTERN void ares_free_string(void *str); CARES_EXTERN void ares_free_string(void *str);

View File

@@ -34,7 +34,8 @@
** of c-ares functions returning pointers that must be free'ed using this ** of c-ares functions returning pointers that must be free'ed using this
** function is: ** function is:
** **
** FIXME: specify function list. ** ares_parse_srv_reply()
** ares_parse_txt_reply()
*/ */
void ares_free_data(void *dataptr) void ares_free_data(void *dataptr)

View File

@@ -1,6 +1,7 @@
/* Id$ */ /* $Id$ */
/* Copyright 1998 by the Massachusetts Institute of Technology. /* Copyright 1998 by the Massachusetts Institute of Technology.
* Copyright (C) 2009 by Jakub Hrozek <jhrozek@redhat.com>
* *
* Permission to use, copy, modify, and distribute this * Permission to use, copy, modify, and distribute this
* software and its documentation for any purpose and without * software and its documentation for any purpose and without
@@ -15,11 +16,6 @@
* without express or implied warranty. * without express or implied warranty.
*/ */
/*
* ares_parse_srv_reply created by Jakub Hrozek <jhrozek@redhat.com>
* on behalf of Red Hat - http://www.redhat.com
*/
#include "ares_setup.h" #include "ares_setup.h"
#ifdef HAVE_SYS_SOCKET_H #ifdef HAVE_SYS_SOCKET_H
@@ -47,6 +43,7 @@
#include <string.h> #include <string.h>
#include "ares.h" #include "ares.h"
#include "ares_dns.h" #include "ares_dns.h"
#include "ares_data.h"
#include "ares_private.h" #include "ares_private.h"
/* AIX portability check */ /* AIX portability check */
@@ -56,21 +53,20 @@
int int
ares_parse_srv_reply (const unsigned char *abuf, int alen, ares_parse_srv_reply (const unsigned char *abuf, int alen,
struct ares_srv_reply **srv_out, int *nsrvreply) struct ares_srv_reply **srv_out)
{ {
unsigned int qdcount, ancount; unsigned int qdcount, ancount, i;
const unsigned char *aptr; const unsigned char *aptr;
int status, i, rr_type, rr_class, rr_len; int status, rr_type, rr_class, rr_len;
long len; long len;
char *hostname = NULL, *rr_name = NULL; char *hostname = NULL, *rr_name = NULL;
struct ares_srv_reply *srv = NULL; struct ares_srv_reply *srv_head = NULL;
struct ares_srv_reply *srv_last = NULL;
struct ares_srv_reply *srv_curr;
/* Set *srv_out to NULL for all failure cases. */ /* Set *srv_out to NULL for all failure cases. */
*srv_out = NULL; *srv_out = NULL;
/* Same with *nsrvreply. */
*nsrvreply = 0;
/* Give up if abuf doesn't have room for a header. */ /* Give up if abuf doesn't have room for a header. */
if (alen < HFIXEDSZ) if (alen < HFIXEDSZ)
return ARES_EBADRESP; return ARES_EBADRESP;
@@ -96,14 +92,6 @@ ares_parse_srv_reply (const unsigned char *abuf, int alen,
} }
aptr += len + QFIXEDSZ; aptr += len + QFIXEDSZ;
/* Allocate ares_srv_reply array; ancount gives an upper bound */
srv = malloc ((ancount) * sizeof (struct ares_srv_reply));
if (!srv)
{
free (hostname);
return ARES_ENOMEM;
}
/* Examine each answer resource record (RR) in turn. */ /* Examine each answer resource record (RR) in turn. */
for (i = 0; i < (int) ancount; i++) for (i = 0; i < (int) ancount; i++)
{ {
@@ -134,40 +122,58 @@ ares_parse_srv_reply (const unsigned char *abuf, int alen,
break; break;
} }
srv[i].priority = ntohs (*((unsigned short *)aptr)); /* Allocate storage for this SRV answer appending it to the list */
srv_curr = ares_malloc_data(ARES_DATATYPE_SRV_REPLY);
if (!srv_curr)
{
status = ARES_ENOMEM;
break;
}
if (srv_last)
{
srv_last->next = srv_curr;
}
else
{
srv_head = srv_curr;
}
srv_last = srv_curr;
srv_curr->priority = ntohs (*((unsigned short *)aptr));
aptr += sizeof(unsigned short); aptr += sizeof(unsigned short);
srv[i].weight = ntohs (*((unsigned short *)aptr)); srv_curr->weight = ntohs (*((unsigned short *)aptr));
aptr += sizeof(unsigned short); aptr += sizeof(unsigned short);
srv[i].port = ntohs (*((unsigned short *)aptr)); srv_curr->port = ntohs (*((unsigned short *)aptr));
aptr += sizeof(unsigned short); aptr += sizeof(unsigned short);
status = ares_expand_name (aptr, abuf, alen, &srv[i].host, &len); status = ares_expand_name (aptr, abuf, alen, &srv_curr->host, &len);
if (status != ARES_SUCCESS) if (status != ARES_SUCCESS)
break; break;
/* Move on to the next record */ /* Move on to the next record */
aptr += len; aptr += len;
/* Don't lose memory in the next iteration */
free (rr_name);
rr_name = NULL;
} }
/* Don't lose memory in the next iteration */
free (rr_name);
rr_name = NULL;
} }
if (hostname)
free (hostname);
if (rr_name)
free (rr_name);
/* clean up on error */ /* clean up on error */
if (status != ARES_SUCCESS) if (status != ARES_SUCCESS)
{ {
free (srv); if (srv_head)
free (hostname); ares_free_data (srv_head);
free (rr_name);
return status; return status;
} }
/* everything looks fine, return the data */ /* everything looks fine, return the data */
*srv_out = srv; *srv_out = srv_head;
*nsrvreply = ancount;
free (hostname); return ARES_SUCCESS;
free (rr_name);
return status;
} }

View File

@@ -1,8 +1,7 @@
/* $Id$ */ /* $Id$ */
/* Copyright 1998 by the Massachusetts Institute of Technology. /* Copyright 1998 by the Massachusetts Institute of Technology.
* Copyright (C) 2009 Jakub Hrozek <jhrozek@redhat.com> * Copyright (C) 2009 by Jakub Hrozek <jhrozek@redhat.com>
* Copyright (C) 2009 Yang Tse <yangsita@gmail.com>
* *
* Permission to use, copy, modify, and distribute this * Permission to use, copy, modify, and distribute this
* software and its documentation for any purpose and without * software and its documentation for any purpose and without
@@ -49,11 +48,12 @@
#include "ares.h" #include "ares.h"
#include "ares_dns.h" #include "ares_dns.h"
#include "ares_data.h"
#include "ares_private.h" #include "ares_private.h"
int int
ares_parse_txt_reply (const unsigned char *abuf, int alen, ares_parse_txt_reply (const unsigned char *abuf, int alen,
struct ares_txt_reply **txt_out, int *ntxtreply) struct ares_txt_reply **txt_out)
{ {
size_t substr_len, str_len; size_t substr_len, str_len;
unsigned int qdcount, ancount, i; unsigned int qdcount, ancount, i;
@@ -62,14 +62,13 @@ ares_parse_txt_reply (const unsigned char *abuf, int alen,
int status, rr_type, rr_class, rr_len; int status, rr_type, rr_class, rr_len;
long len; long len;
char *hostname = NULL, *rr_name = NULL; char *hostname = NULL, *rr_name = NULL;
struct ares_txt_reply *txt = NULL; struct ares_txt_reply *txt_head = NULL;
struct ares_txt_reply *txt_last = NULL;
struct ares_txt_reply *txt_curr;
/* Set *txt_out to NULL for all failure cases. */ /* Set *txt_out to NULL for all failure cases. */
*txt_out = NULL; *txt_out = NULL;
/* Same with *ntxtreply. */
*ntxtreply = 0;
/* Give up if abuf doesn't have room for a header. */ /* Give up if abuf doesn't have room for a header. */
if (alen < HFIXEDSZ) if (alen < HFIXEDSZ)
return ARES_EBADRESP; return ARES_EBADRESP;
@@ -95,21 +94,6 @@ ares_parse_txt_reply (const unsigned char *abuf, int alen,
} }
aptr += len + QFIXEDSZ; aptr += len + QFIXEDSZ;
/* Allocate ares_txt_reply array; ancount gives an upper bound */
txt = malloc ((ancount) * sizeof (struct ares_txt_reply));
if (!txt)
{
free (hostname);
return ARES_ENOMEM;
}
/* Initialize ares_txt_reply array */
for (i = 0; i < ancount; i++)
{
txt[i].txt = NULL;
txt[i].length = 0;
}
/* Examine each answer resource record (RR) in turn. */ /* Examine each answer resource record (RR) in turn. */
for (i = 0; i < ancount; i++) for (i = 0; i < ancount; i++)
{ {
@@ -133,6 +117,23 @@ ares_parse_txt_reply (const unsigned char *abuf, int alen,
/* Check if we are really looking at a TXT record */ /* Check if we are really looking at a TXT record */
if (rr_class == C_IN && rr_type == T_TXT) if (rr_class == C_IN && rr_type == T_TXT)
{ {
/* Allocate storage for this SRV answer appending it to the list */
txt_curr = ares_malloc_data(ARES_DATATYPE_TXT_REPLY);
if (!txt_curr)
{
status = ARES_ENOMEM;
break;
}
if (txt_last)
{
txt_last->next = txt_curr;
}
else
{
txt_head = txt_curr;
}
txt_last = txt_curr;
/* /*
* There may be multiple substrings in a single TXT record. Each * There may be multiple substrings in a single TXT record. Each
* substring may be up to 255 characters in length, with a * substring may be up to 255 characters in length, with a
@@ -146,13 +147,13 @@ ares_parse_txt_reply (const unsigned char *abuf, int alen,
while (strptr < (aptr + rr_len)) while (strptr < (aptr + rr_len))
{ {
substr_len = (unsigned char)*strptr; substr_len = (unsigned char)*strptr;
txt[i].length += substr_len; txt_curr->length += substr_len;
strptr += substr_len + 1; strptr += substr_len + 1;
} }
/* Including null byte */ /* Including null byte */
txt[i].txt = malloc (txt[i].length + 1); txt_curr->txt = malloc (txt_curr->length + 1);
if (txt[i].txt == NULL) if (txt_curr->txt == NULL)
{ {
status = ARES_ENOMEM; status = ARES_ENOMEM;
break; break;
@@ -165,12 +166,12 @@ ares_parse_txt_reply (const unsigned char *abuf, int alen,
{ {
substr_len = (unsigned char)*strptr; substr_len = (unsigned char)*strptr;
strptr++; strptr++;
memcpy ((char *) txt[i].txt + str_len, strptr, substr_len); memcpy ((char *) txt_curr->txt + str_len, strptr, substr_len);
str_len += substr_len; str_len += substr_len;
strptr += substr_len; strptr += substr_len;
} }
/* Make sure we NULL-terminate */ /* Make sure we NULL-terminate */
txt[i].txt[txt[i].length] = '\0'; *((char *) txt_curr->txt + txt_curr->length) = '\0';
/* Move on to the next record */ /* Move on to the next record */
aptr += rr_len; aptr += rr_len;
@@ -189,17 +190,13 @@ ares_parse_txt_reply (const unsigned char *abuf, int alen,
/* clean up on error */ /* clean up on error */
if (status != ARES_SUCCESS) if (status != ARES_SUCCESS)
{ {
for (i = 0; i < ancount; i++) if (txt_head)
{ ares_free_data (txt_head);
if (txt[i].txt)
free (txt[i].txt);
}
return status; return status;
} }
/* everything looks fine, return the data */ /* everything looks fine, return the data */
*txt_out = txt; *txt_out = txt_head;
*ntxtreply = ancount;
return ARES_SUCCESS; return ARES_SUCCESS;
} }