Hello,

Move data structures for parser from ldap_qresult_t to ldap_entry_t.


The target branch is master.

--
Petr^2 Spacek
From b32ba3f02460eb3e9d81c53b6774bb34b2ff08bb Mon Sep 17 00:00:00 2001
From: Petr Spacek <pspa...@redhat.com>
Date: Mon, 22 Jul 2013 15:18:55 +0200
Subject: [PATCH] Move data structures for parser from ldap_qresult_t to
 ldap_entry_t.

Signed-off-by: Petr Spacek <pspa...@redhat.com>
---
 src/ldap_entry.c  | 18 +++++++++--
 src/ldap_entry.h  | 11 +++++++
 src/ldap_helper.c | 96 ++++++++++++++++++++-----------------------------------
 3 files changed, 61 insertions(+), 64 deletions(-)

diff --git a/src/ldap_entry.c b/src/ldap_entry.c
index 986126dd10fdfdd93666362b87433138615d9bca..96ff183d85330f65c4c1a739c194cc59beebc338 100644
--- a/src/ldap_entry.c
+++ b/src/ldap_entry.c
@@ -198,8 +198,6 @@ ldap_entry_create(isc_mem_t *mctx, LDAP *ld, LDAPMessage *ldap_entry,
 	INIT_LIST(entry->attrs);
 	INIT_LINK(entry, link);
 
-	result = ISC_R_SUCCESS;
-
 	for (attribute = ldap_first_attribute(ld, ldap_entry, &ber);
 	     attribute != NULL;
 	     attribute = ldap_next_attribute(ld, ldap_entry, ber)) {
@@ -221,14 +219,21 @@ ldap_entry_create(isc_mem_t *mctx, LDAP *ld, LDAPMessage *ldap_entry,
 		CLEANUP_WITH(ISC_R_FAILURE);
 	}
 
+	CHECKED_MEM_GET(mctx, entry->rdata_target_mem, MINTSIZ);
+	CHECK(isc_lex_create(mctx, TOKENSIZ, &entry->lex));
+
 	*entryp = entry;
 
 cleanup:
 	if (ber != NULL)
 		ber_free(ber, 0);
 	if (result != ISC_R_SUCCESS) {
-		if (entry != NULL)
+		if (entry != NULL) {
 			ldap_attributelist_destroy(mctx, &entry->attrs);
+			SAFE_MEM_PUT(mctx, entry->rdata_target_mem, MINTSIZ);
+			if (entry->lex != NULL)
+				isc_lex_destroy(&entry->lex);
+		}
 		SAFE_MEM_PUT_PTR(mctx, entry);
 		SAFE_MEM_PUT_PTR(mctx, attr);
 	}
@@ -250,6 +255,13 @@ ldap_entry_destroy(isc_mem_t *mctx, ldap_entry_t **entryp)
 	ldap_attributelist_destroy(mctx, &entry->attrs);
 	if (entry->dn != NULL)
 		ldap_memfree(entry->dn);
+	if (entry->lex != NULL) {
+		isc_lex_close(entry->lex);
+		isc_lex_destroy(&entry->lex);
+	}
+	if (entry->rdata_target_mem != NULL)
+		isc_mem_put(mctx, entry->rdata_target_mem, MINTSIZ);
+
 	SAFE_MEM_PUT_PTR(mctx, entry);
 
 	*entryp = NULL;
diff --git a/src/ldap_entry.h b/src/ldap_entry.h
index a71be0a32ee7b4179799052bf2b6dc13c7cfa076..6311b694f720929231ce44addfc18acc6c251617 100644
--- a/src/ldap_entry.h
+++ b/src/ldap_entry.h
@@ -22,6 +22,7 @@
 #ifndef _LD_LDAP_ENTRY_H_
 #define _LD_LDAP_ENTRY_H_
 
+#include <isc/lex.h>
 #include <isc/util.h>
 #include <dns/types.h>
 
@@ -51,6 +52,11 @@ struct ldap_entry {
 	ldap_attribute_t	*lastattr;
 	ldap_attributelist_t	attrs;
 	LINK(ldap_entry_t)	link;
+
+	/* Parsing. */
+	isc_lex_t		*lex;
+	isc_buffer_t		rdata_target;
+	unsigned char		*rdata_target_mem;
 };
 
 /* Represents LDAP attribute and it's values */
@@ -70,6 +76,11 @@ struct ldap_attribute {
 
 typedef unsigned char		ldap_entryclass_t;
 
+/* Max type length definitions, from lib/dns/master.c */
+#define MINTSIZ (65535 - 12 - 1 - 2 - 2 - 4 - 2)
+#define TOKENSIZ (8*1024)
+
+
 isc_result_t
 ldap_entrylist_create(isc_mem_t *mctx, LDAP *ld, LDAPMessage *msg,
 		      ldap_entrylist_t *entrylist) ATTR_NONNULLS;
diff --git a/src/ldap_helper.c b/src/ldap_helper.c
index 5fd82fb4b9f72eb9cf2129810bfaaa2186d5d84b..8ba8a398ae013c9b7603a783ea14c9ffff4e80c2 100644
--- a/src/ldap_helper.c
+++ b/src/ldap_helper.c
@@ -38,7 +38,6 @@
 #include <isc/serial.h>
 
 #include <isc/buffer.h>
-#include <isc/lex.h>
 #include <isc/mem.h>
 #include <isc/mutex.h>
 #include <isc/region.h>
@@ -82,11 +81,6 @@
 #include "rbt_helper.h"
 #include "fwd_register.h"
 
-
-/* Max type length definitions, from lib/dns/master.c */
-#define MINTSIZ (65535 - 12 - 1 - 2 - 2 - 4 - 2)
-#define TOKENSIZ (8*1024)
-
 const enum_txt_assoc_t forwarder_policy_txts[] = {
 	{ dns_fwdpolicy_none,	"none"	},
 	{ dns_fwdpolicy_first,	"first"	},
@@ -199,11 +193,6 @@ struct ldap_qresult {
 	ld_string_t		*query_string;
 	LDAPMessage		*result;
 	ldap_entrylist_t	ldap_entries;
-
-	/* Parsing. */
-	isc_lex_t		*lex;
-	isc_buffer_t		rdata_target;
-	unsigned char		*rdata_target_mem;
 };
 
 /*
@@ -284,17 +273,13 @@ static void destroy_ldap_connection(ldap_connection_t **ldap_connp) ATTR_NONNULL
 static isc_result_t findrdatatype_or_create(isc_mem_t *mctx,
 		ldapdb_rdatalist_t *rdatalist, dns_rdataclass_t rdclass,
 		dns_rdatatype_t rdtype, dns_ttl_t ttl, dns_rdatalist_t **rdlistp) ATTR_NONNULLS;
-static isc_result_t add_soa_record(isc_mem_t *mctx, ldap_qresult_t *qresult,
-		dns_name_t *origin, ldap_entry_t *entry,
-		ldapdb_rdatalist_t *rdatalist, const char *fake_mname) ATTR_NONNULLS;
-static isc_result_t parse_rdata(isc_mem_t *mctx, ldap_qresult_t *qresult,
+static isc_result_t add_soa_record(isc_mem_t *mctx, dns_name_t *origin,
+		ldap_entry_t *entry, ldapdb_rdatalist_t *rdatalist,
+		const char *fake_mname) ATTR_NONNULLS;
+static isc_result_t parse_rdata(isc_mem_t *mctx, ldap_entry_t *entry,
 		dns_rdataclass_t rdclass, dns_rdatatype_t rdtype,
 		dns_name_t *origin, const char *rdata_text,
 		dns_rdata_t **rdatap) ATTR_NONNULLS;
-static isc_result_t ldap_parse_rrentry(isc_mem_t *mctx, ldap_entry_t *entry,
-		ldap_qresult_t *qresult, dns_name_t *origin,
-		const char *fake_mname, ld_string_t *buf,
-		ldapdb_rdatalist_t *rdatalist) ATTR_NONNULLS;
 static inline isc_result_t ldap_get_zone_serial(ldap_instance_t *inst,
 		dns_name_t *zone_name, isc_uint32_t *serial) ATTR_NONNULLS;
 
@@ -1645,10 +1630,8 @@ free_rdatalist(isc_mem_t *mctx, dns_rdatalist_t *rdlist)
 }
 
 static isc_result_t
-ldap_parse_rrentry(isc_mem_t *mctx, ldap_entry_t *entry,
-		   ldap_qresult_t *qresult, dns_name_t *origin,
-		   const char *fake_mname, ld_string_t *buf,
-		   ldapdb_rdatalist_t *rdatalist)
+ldap_parse_rrentry(isc_mem_t *mctx, ldap_entry_t *entry, dns_name_t *origin,
+		   const char *fake_mname, ldapdb_rdatalist_t *rdatalist)
 {
 	isc_result_t result;
 	dns_rdataclass_t rdclass;
@@ -1659,12 +1642,13 @@ ldap_parse_rrentry(isc_mem_t *mctx, ldap_entry_t *entry,
 	dns_rdatalist_t *rdlist = NULL;
 	ldap_attribute_t *attr;
 	const char *dn = "<NULL entry>";
-	const char *data = "<NULL data>";
+	const char *data_str = "<NULL data>";
+	ld_string_t *data_buf = NULL;
 
+	CHECK(str_new(mctx, &data_buf));
 	CHECK(ldap_entry_getclass(entry, &objclass));
 	if ((objclass & LDAP_ENTRYCLASS_MASTER) != 0)
-		CHECK(add_soa_record(mctx, qresult, origin, entry,
-				     rdatalist, fake_mname));
+		CHECK(add_soa_record(mctx, origin, entry, rdatalist, fake_mname));
 
 	rdclass = ldap_entry_getrdclass(entry);
 	ttl = ldap_entry_getttl(entry);
@@ -1675,24 +1659,26 @@ ldap_parse_rrentry(isc_mem_t *mctx, ldap_entry_t *entry,
 
 		CHECK(findrdatatype_or_create(mctx, rdatalist, rdclass,
 					      rdtype, ttl, &rdlist));
-		while (ldap_attr_nextvalue(attr, buf) != NULL) {
-			CHECK(parse_rdata(mctx, qresult, rdclass,
+		while (ldap_attr_nextvalue(attr, data_buf) != NULL) {
+			CHECK(parse_rdata(mctx, entry, rdclass,
 					  rdtype, origin,
-					  str_buf(buf), &rdata));
+					  str_buf(data_buf), &rdata));
 			APPEND(rdlist->rdata, rdata, link);
 			rdata = NULL;
 		}
 		rdlist = NULL;
 	}
+	str_destroy(&data_buf);
 
 	return ISC_R_SUCCESS;
 
 cleanup:
 	if (entry != NULL)
 		dn = entry->dn;
-	if (buf != NULL && str_buf(buf) != NULL)
-		data = str_buf(buf);
-	log_error_r("failed to parse RR entry: dn '%s': data '%s'", dn, data);
+	if (data_buf != NULL && str_buf(data_buf) != NULL)
+		data_str = str_buf(data_buf);
+	log_error_r("failed to parse RR entry: dn '%s': data '%s'", dn, data_str);
+	str_destroy(&data_buf);
 	return result;
 }
 
@@ -1740,9 +1726,9 @@ ldapdb_nodelist_get(isc_mem_t *mctx, ldap_instance_t *ldap_inst, dns_name_t *nam
 		result = ldapdbnode_create(mctx, &node_name, &node);
 		dns_name_free(&node_name, mctx);
 		if (result == ISC_R_SUCCESS) {
-			result = ldap_parse_rrentry(mctx, entry, ldap_qresult,
-		                       origin, fake_mname,
-		                       string, &node->rdatalist);
+			result = ldap_parse_rrentry(mctx, entry, origin,
+						    fake_mname,
+						    &node->rdatalist);
 		}
 		if (result != ISC_R_SUCCESS) {
 			/* node cleaning */	
@@ -1805,9 +1791,8 @@ ldapdb_rdatalist_get(isc_mem_t *mctx, ldap_instance_t *ldap_inst, dns_name_t *na
 	for (entry = HEAD(ldap_qresult->ldap_entries);
 		entry != NULL;
 		entry = NEXT(entry, link)) {
-		CHECK(ldap_parse_rrentry(mctx, entry, ldap_qresult,
-				   origin, fake_mname,
-				   string, rdatalist));
+		CHECK(ldap_parse_rrentry(mctx, entry, origin, fake_mname,
+					 rdatalist));
 	}
 
 	if (!EMPTY(*rdatalist)) {
@@ -1828,7 +1813,7 @@ cleanup:
 }
 
 static isc_result_t
-add_soa_record(isc_mem_t *mctx, ldap_qresult_t *qresult, dns_name_t *origin,
+add_soa_record(isc_mem_t *mctx, dns_name_t *origin,
 	       ldap_entry_t *entry, ldapdb_rdatalist_t *rdatalist,
 	       const char *fake_mname)
 {
@@ -1842,7 +1827,7 @@ add_soa_record(isc_mem_t *mctx, ldap_qresult_t *qresult, dns_name_t *origin,
 
 	CHECK(ldap_entry_getfakesoa(entry, fake_mname, string));
 	rdclass = ldap_entry_getrdclass(entry);
-	CHECK(parse_rdata(mctx, qresult, rdclass, dns_rdatatype_soa, origin,
+	CHECK(parse_rdata(mctx, entry, rdclass, dns_rdatatype_soa, origin,
 			  str_buf(string), &rdata));
 
 	CHECK(findrdatatype_or_create(mctx, rdatalist, rdclass, dns_rdatatype_soa,
@@ -1859,17 +1844,17 @@ cleanup:
 }
 
 static isc_result_t
-parse_rdata(isc_mem_t *mctx, ldap_qresult_t *qresult,
+parse_rdata(isc_mem_t *mctx, ldap_entry_t *entry,
 	    dns_rdataclass_t rdclass, dns_rdatatype_t rdtype,
 	    dns_name_t *origin, const char *rdata_text, dns_rdata_t **rdatap)
 {
 	isc_result_t result;
 	isc_consttextregion_t text;
 	isc_buffer_t lex_buffer;
 	isc_region_t rdatamem;
 	dns_rdata_t *rdata;
 
-	REQUIRE(qresult != NULL);
+	REQUIRE(entry != NULL);
 	REQUIRE(rdata_text != NULL);
 	REQUIRE(rdatap != NULL);
 
@@ -1883,30 +1868,30 @@ parse_rdata(isc_mem_t *mctx, ldap_qresult_t *qresult,
 	isc_buffer_add(&lex_buffer, text.length);
 	isc_buffer_setactive(&lex_buffer, text.length);
 
-	CHECK(isc_lex_openbuffer(qresult->lex, &lex_buffer));
+	CHECK(isc_lex_openbuffer(entry->lex, &lex_buffer));
 
-	isc_buffer_init(&qresult->rdata_target, qresult->rdata_target_mem,
+	isc_buffer_init(&entry->rdata_target, entry->rdata_target_mem,
 			MINTSIZ);
-	CHECK(dns_rdata_fromtext(NULL, rdclass, rdtype, qresult->lex, origin,
-				 0, mctx, &qresult->rdata_target, NULL));
+	CHECK(dns_rdata_fromtext(NULL, rdclass, rdtype, entry->lex, origin,
+				 0, mctx, &entry->rdata_target, NULL));
 
 	CHECKED_MEM_GET_PTR(mctx, rdata);
 	dns_rdata_init(rdata);
 
-	rdatamem.length = isc_buffer_usedlength(&qresult->rdata_target);
+	rdatamem.length = isc_buffer_usedlength(&entry->rdata_target);
 	CHECKED_MEM_GET(mctx, rdatamem.base, rdatamem.length);
 
-	memcpy(rdatamem.base, isc_buffer_base(&qresult->rdata_target),
+	memcpy(rdatamem.base, isc_buffer_base(&entry->rdata_target),
 	       rdatamem.length);
 	dns_rdata_fromregion(rdata, rdclass, rdtype, &rdatamem);
 
-	isc_lex_close(qresult->lex);
+	isc_lex_close(entry->lex);
 
 	*rdatap = rdata;
 	return ISC_R_SUCCESS;
 
 cleanup:
-	isc_lex_close(qresult->lex);
+	isc_lex_close(entry->lex);
 	SAFE_MEM_PUT_PTR(mctx, rdata);
 	if (rdatamem.base != NULL)
 		isc_mem_put(mctx, rdatamem.base, rdatamem.length);
@@ -2022,18 +2007,12 @@ ldap_query_create(isc_mem_t *mctx, ldap_qresult_t **ldap_qresultp) {
 	INIT_LIST(ldap_qresult->ldap_entries);
 	CHECK(str_new(mctx, &ldap_qresult->query_string));
 
-	CHECKED_MEM_GET(ldap_qresult->mctx, ldap_qresult->rdata_target_mem, MINTSIZ);
-	CHECK(isc_lex_create(ldap_qresult->mctx, TOKENSIZ, &ldap_qresult->lex));
-
 	*ldap_qresultp = ldap_qresult;
 	return ISC_R_SUCCESS;
 
 cleanup:
 	if (ldap_qresult != NULL) {
 		str_destroy(&ldap_qresult->query_string);
-		SAFE_MEM_PUT(ldap_qresult->mctx, ldap_qresult->rdata_target_mem, MINTSIZ);
-		if (ldap_qresult->lex != NULL)
-			isc_lex_destroy(&ldap_qresult->lex);
 		SAFE_MEM_PUT_PTR(mctx, ldap_qresult);
 	}
 
@@ -2069,13 +2048,8 @@ ldap_query_free(isc_boolean_t prepare_reuse, ldap_qresult_t **ldap_qresultp)
 	if (prepare_reuse) {
 		str_clear(qresult->query_string);
 		INIT_LIST(qresult->ldap_entries);
-		isc_lex_close(qresult->lex);
 	} else { /* free the whole structure */
 		str_destroy(&qresult->query_string);
-		if (qresult->lex != NULL)
-			isc_lex_destroy(&qresult->lex);
-		if (qresult->rdata_target_mem != NULL)
-			isc_mem_put(qresult->mctx, qresult->rdata_target_mem, MINTSIZ);
 		SAFE_MEM_PUT_PTR(qresult->mctx, qresult);
 		*ldap_qresultp = NULL;
 	}
-- 
1.8.3.1

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to