aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYang Tse <yangsita@gmail.com>2009-11-20 09:06:33 +0000
committerYang Tse <yangsita@gmail.com>2009-11-20 09:06:33 +0000
commit7e6a67b436cef781513c2037ca8775531e92e329 (patch)
tree9e34b1f54dd0901d63b5aad2638828dc6c6aa886
parent5a0a473c30925d63e1695a7b8b1cee8f73ad6e68 (diff)
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().
-rw-r--r--ares/ares.h6
-rw-r--r--ares/ares_data.c3
-rw-r--r--ares/ares_parse_srv_reply.c80
-rw-r--r--ares/ares_parse_txt_reply.c65
4 files changed, 78 insertions, 76 deletions
diff --git a/ares/ares.h b/ares/ares.h
index d2b6cecc5..9f4feebf9 100644
--- a/ares/ares.h
+++ b/ares/ares.h
@@ -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,
int alen,
- struct ares_srv_reply** srv_out,
- int *nsrvreply);
+ struct ares_srv_reply** srv_out);
CARES_EXTERN int ares_parse_txt_reply(const unsigned char* abuf,
int alen,
- struct ares_txt_reply** txt_out,
- int *nsrvreply);
+ struct ares_txt_reply** txt_out);
CARES_EXTERN void ares_free_string(void *str);
diff --git a/ares/ares_data.c b/ares/ares_data.c
index 5d710ee65..16a5cc507 100644
--- a/ares/ares_data.c
+++ b/ares/ares_data.c
@@ -34,7 +34,8 @@
** of c-ares functions returning pointers that must be free'ed using this
** function is:
**
-** FIXME: specify function list.
+** ares_parse_srv_reply()
+** ares_parse_txt_reply()
*/
void ares_free_data(void *dataptr)
diff --git a/ares/ares_parse_srv_reply.c b/ares/ares_parse_srv_reply.c
index 883331052..e296a9868 100644
--- a/ares/ares_parse_srv_reply.c
+++ b/ares/ares_parse_srv_reply.c
@@ -1,6 +1,7 @@
-/* Id$ */
+/* $Id$ */
/* 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
* software and its documentation for any purpose and without
@@ -15,11 +16,6 @@
* 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"
#ifdef HAVE_SYS_SOCKET_H
@@ -47,6 +43,7 @@
#include <string.h>
#include "ares.h"
#include "ares_dns.h"
+#include "ares_data.h"
#include "ares_private.h"
/* AIX portability check */
@@ -56,21 +53,20 @@
int
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;
- int status, i, rr_type, rr_class, rr_len;
+ int status, rr_type, rr_class, rr_len;
long len;
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. */
*srv_out = NULL;
- /* Same with *nsrvreply. */
- *nsrvreply = 0;
-
/* Give up if abuf doesn't have room for a header. */
if (alen < HFIXEDSZ)
return ARES_EBADRESP;
@@ -96,14 +92,6 @@ ares_parse_srv_reply (const unsigned char *abuf, int alen,
}
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. */
for (i = 0; i < (int) ancount; i++)
{
@@ -134,40 +122,58 @@ ares_parse_srv_reply (const unsigned char *abuf, int alen,
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);
- srv[i].weight = ntohs (*((unsigned short *)aptr));
+ srv_curr->weight = ntohs (*((unsigned short *)aptr));
aptr += sizeof(unsigned short);
- srv[i].port = ntohs (*((unsigned short *)aptr));
+ srv_curr->port = ntohs (*((unsigned short *)aptr));
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)
break;
/* Move on to the next record */
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 */
if (status != ARES_SUCCESS)
{
- free (srv);
- free (hostname);
- free (rr_name);
+ if (srv_head)
+ ares_free_data (srv_head);
return status;
}
/* everything looks fine, return the data */
- *srv_out = srv;
- *nsrvreply = ancount;
+ *srv_out = srv_head;
- free (hostname);
- free (rr_name);
- return status;
+ return ARES_SUCCESS;
}
diff --git a/ares/ares_parse_txt_reply.c b/ares/ares_parse_txt_reply.c
index 583b64f1c..465a38716 100644
--- a/ares/ares_parse_txt_reply.c
+++ b/ares/ares_parse_txt_reply.c
@@ -1,8 +1,7 @@
/* $Id$ */
/* Copyright 1998 by the Massachusetts Institute of Technology.
- * Copyright (C) 2009 Jakub Hrozek <jhrozek@redhat.com>
- * Copyright (C) 2009 Yang Tse <yangsita@gmail.com>
+ * Copyright (C) 2009 by Jakub Hrozek <jhrozek@redhat.com>
*
* Permission to use, copy, modify, and distribute this
* software and its documentation for any purpose and without
@@ -49,11 +48,12 @@
#include "ares.h"
#include "ares_dns.h"
+#include "ares_data.h"
#include "ares_private.h"
int
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;
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;
long len;
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. */
*txt_out = NULL;
- /* Same with *ntxtreply. */
- *ntxtreply = 0;
-
/* Give up if abuf doesn't have room for a header. */
if (alen < HFIXEDSZ)
return ARES_EBADRESP;
@@ -95,21 +94,6 @@ ares_parse_txt_reply (const unsigned char *abuf, int alen,
}
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. */
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 */
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
* 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))
{
substr_len = (unsigned char)*strptr;
- txt[i].length += substr_len;
+ txt_curr->length += substr_len;
strptr += substr_len + 1;
}
/* Including null byte */
- txt[i].txt = malloc (txt[i].length + 1);
- if (txt[i].txt == NULL)
+ txt_curr->txt = malloc (txt_curr->length + 1);
+ if (txt_curr->txt == NULL)
{
status = ARES_ENOMEM;
break;
@@ -165,12 +166,12 @@ ares_parse_txt_reply (const unsigned char *abuf, int alen,
{
substr_len = (unsigned char)*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;
strptr += substr_len;
}
/* 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 */
aptr += rr_len;
@@ -189,17 +190,13 @@ ares_parse_txt_reply (const unsigned char *abuf, int alen,
/* clean up on error */
if (status != ARES_SUCCESS)
{
- for (i = 0; i < ancount; i++)
- {
- if (txt[i].txt)
- free (txt[i].txt);
- }
+ if (txt_head)
+ ares_free_data (txt_head);
return status;
}
/* everything looks fine, return the data */
- *txt_out = txt;
- *ntxtreply = ancount;
+ *txt_out = txt_head;
return ARES_SUCCESS;
}