From 3f8be559f0871022c78a229bad0eb09560b90909 Mon Sep 17 00:00:00 2001 From: Evan Hunt Date: Wed, 11 Mar 2009 07:02:34 +0000 Subject: [PATCH] 2575. [func] New functions dns_name_fromstring() and dns_name_tostring(), to simplify conversion of a string to a dns_name structure and vice versa. [RT #19451] --- CHANGES | 5 ++++ lib/dns/include/dns/name.h | 38 +++++++++++++++++++++++++- lib/dns/name.c | 55 +++++++++++++++++++++++++++++++++++++- 3 files changed, 96 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index e4217d85ba..4d556dcba2 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,8 @@ +2575. [func] New functions dns_name_fromstring() and + dns_name_tostring(), to simplify conversion + of a string to a dns_name structure and vice + versa. [RT #19451] + 2574. [doc] Document nsupdate -g and -o. [RT #19351] 2573. [bug] Replacing a non-CNAME record with a CNAME record in a diff --git a/lib/dns/include/dns/name.h b/lib/dns/include/dns/name.h index 5bbb602777..f42fcbb23c 100644 --- a/lib/dns/include/dns/name.h +++ b/lib/dns/include/dns/name.h @@ -15,7 +15,7 @@ * PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: name.h,v 1.128 2009/01/17 23:47:43 tbox Exp $ */ +/* $Id: name.h,v 1.129 2009/03/11 07:02:34 each Exp $ */ #ifndef DNS_NAME_H #define DNS_NAME_H 1 @@ -1129,6 +1129,42 @@ dns_name_format(dns_name_t *name, char *cp, unsigned int size); * */ +isc_result_t +dns_name_tostring(dns_name_t *source, char **target, isc_mem_t *mctx); +/*%< + * Convert 'name' to string format, allocating sufficient memory to + * hold it (free with isc_mem_free()). + * + * Differs from dns_name_format in that it allocates its own memory. + * + * Requires: + * + *\li 'name' is a valid name. + *\li 'target' is not NULL. + *\li '*target' is NULL. + * + * Returns: + * + *\li ISC_R_SUCCESS + * + *\li Any error that dns_name_totext() can return. + */ + +isc_result_t +dns_name_fromstring(dns_name_t *target, const char *src, isc_mem_t *mctx); +/*%< + * Convert a string to a name and place it in target, allocating memory + * as necessary. + * + * Returns: + * + *\li #ISC_R_SUCCESS + * + *\li Any error that dns_name_fromtext() can return. + * + *\li Any error that dns_name_dup() can return. + */ + isc_result_t dns_name_settotextfilter(dns_name_totextfilter_t proc); /*%< diff --git a/lib/dns/name.c b/lib/dns/name.c index f4ea3e9113..baf9cdb824 100644 --- a/lib/dns/name.c +++ b/lib/dns/name.c @@ -15,7 +15,7 @@ * PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: name.c,v 1.165 2008/04/01 23:47:10 tbox Exp $ */ +/* $Id: name.c,v 1.166 2009/03/11 07:02:34 each Exp $ */ /*! \file */ @@ -34,6 +34,7 @@ #include #include +#include #include #include @@ -2340,6 +2341,58 @@ dns_name_format(dns_name_t *name, char *cp, unsigned int size) { snprintf(cp, size, ""); } +/* + * dns_name_tostring() -- similar to dns_name_format() but allocates its own + * memory. + */ +isc_result_t +dns_name_tostring(dns_name_t *name, char **target, isc_mem_t *mctx) { + isc_result_t result; + isc_buffer_t buf; + isc_region_t reg; + char *p, txt[DNS_NAME_FORMATSIZE]; + + REQUIRE(VALID_NAME(name)); + REQUIRE(target != NULL && *target == NULL); + + isc_buffer_init(&buf, txt, sizeof(txt)); + result = dns_name_totext(name, ISC_FALSE, &buf); + if (result != ISC_R_SUCCESS) + return (result); + + isc_buffer_usedregion(&buf, ®); + p = isc_mem_allocate(mctx, reg.length + 1); + memcpy(p, (char *) reg.base, (int) reg.length); + p[reg.length] = '\0'; + + *target = p; + return (ISC_R_SUCCESS); +} + +/* + * dns_name_fromstring() -- convert directly from a string to a name, + * allocating memory as needed + */ +isc_result_t +dns_name_fromstring(dns_name_t *target, const char *src, isc_mem_t *mctx) { + isc_result_t result; + isc_buffer_t buf; + dns_fixedname_t fn; + dns_name_t *name; + + isc_buffer_init(&buf, src, strlen(src)); + isc_buffer_add(&buf, strlen(src)); + dns_fixedname_init(&fn); + name = dns_fixedname_name(&fn); + + result = dns_name_fromtext(name, &buf, dns_rootname, ISC_FALSE, NULL); + if (result != ISC_R_SUCCESS) + return (result); + + result = dns_name_dup(name, mctx, target); + return (result); +} + isc_result_t dns_name_copy(dns_name_t *source, dns_name_t *dest, isc_buffer_t *target) { unsigned char *ndata;