diff --git a/lib/dns/include/dns/name.h b/lib/dns/include/dns/name.h index 90ad6cabea..ae49ef677f 100644 --- a/lib/dns/include/dns/name.h +++ b/lib/dns/include/dns/name.h @@ -195,6 +195,7 @@ struct dns_name { #define DNS_NAMEATTR_ABSOLUTE 0x0001 #define DNS_NAMEATTR_READONLY 0x0002 +#define DNS_NAMEATTR_DYNAMIC 0x0004 extern dns_name_t *dns_rootname; @@ -867,6 +868,47 @@ dns_name_split(dns_name_t *name, * have enough room to receive the split name. */ +dns_result_t +dns_name_dup(dns_name_t *source, isc_mem_t *mctx, unsigned char *offsets, + dns_name_t *target); +/* + * Make 'target' a dynamically allocated copy of 'source'. + * + * Notes: + * + * 'target' will be initialized. + * + * 'offsets' is never required to be non-NULL, but specifying a + * dns_offsets_t for 'offsets' will improve the performance of most + * name operations if the name is used more than once. + * + * Requires: + * + * 'source' is a valid non-empty name. + * + * 'mctx' is a valid memory context. + * + * offsets == NULL or offsets is a dns_offsets_t. + * + */ + +void +dns_name_free(dns_name_t *name, isc_mem_t *mctx); +/* + * Free 'name'. + * + * Requires: + * + * 'name' is a valid name created previously in 'mctx' by dns_name_dup(). + * + * 'mctx' is a valid memory context. + * + * Ensures: + * + * All dynamic resources used by 'name' are freed and the name is + * invalidated. + */ + ISC_LANG_ENDDECLS #endif /* DNS_NAME_H */ diff --git a/lib/dns/name.c b/lib/dns/name.c index 465b2e2777..1b2967c9d8 100644 --- a/lib/dns/name.c +++ b/lib/dns/name.c @@ -33,8 +33,6 @@ #define NAME_MAGIC 0x444E536EU /* DNSn. */ #define VALID_NAME(n) ((n) != NULL && \ (n)->magic == NAME_MAGIC) -isc_buffer_t x; -char xxxx[1024]; typedef enum { ft_init = 0, @@ -2721,3 +2719,50 @@ dns_name_split(dns_name_t *name, return (result); } + +dns_result_t +dns_name_dup(dns_name_t *source, isc_mem_t *mctx, unsigned char *offsets, + dns_name_t *target) { + REQUIRE(VALID_NAME(source)); + REQUIRE(source->length > 0); + REQUIRE((target->attributes & DNS_NAMEATTR_READONLY) == 0); + + /* + * Make 'target' a dynamically allocated copy of 'source'. + */ + + target->ndata = isc_mem_get(mctx, source->length); + if (target->ndata == NULL) + return (DNS_R_NOMEMORY); + + memcpy(target->ndata, source->ndata, source->length); + + target->magic = NAME_MAGIC; + target->length = source->length; + target->labels = source->labels; + target->attributes = DNS_NAMEATTR_DYNAMIC | DNS_NAMEATTR_READONLY; + if ((source->attributes & DNS_NAMEATTR_ABSOLUTE) != 0) + target->attributes |= DNS_NAMEATTR_ABSOLUTE; + target->offsets = offsets; + if (offsets != NULL) + set_offsets(target, target->offsets, ISC_FALSE, ISC_FALSE, + ISC_FALSE); + target->buffer = NULL; + ISC_LINK_INIT(target, link); + ISC_LIST_INIT(target->list); + + return (DNS_R_SUCCESS); +} + +void +dns_name_free(dns_name_t *name, isc_mem_t *mctx) { + REQUIRE(VALID_NAME(name)); + REQUIRE((name->attributes & DNS_NAMEATTR_DYNAMIC) != 0); + + /* + * Free 'name'. + */ + + isc_mem_put(mctx, name->ndata, name->length); + dns_name_invalidate(name); +}