[svn-commits] russell: branch group/res_config_ldap r63985 - /team/group/res_config_ldap/res/

svn-commits at lists.digium.com svn-commits at lists.digium.com
Fri May 11 13:52:30 MST 2007


Author: russell
Date: Fri May 11 15:52:29 2007
New Revision: 63985

URL: http://svn.digium.com/view/asterisk?view=rev&rev=63985
Log:
Simplify building the filter string by using the ast_str API

Modified:
    team/group/res_config_ldap/res/res_config_ldap.c

Modified: team/group/res_config_ldap/res/res_config_ldap.c
URL: http://svn.digium.com/view/asterisk/team/group/res_config_ldap/res/res_config_ldap.c?view=diff&rev=63985&r1=63984&r2=63985
==============================================================================
--- team/group/res_config_ldap/res/res_config_ldap.c (original)
+++ team/group/res_config_ldap/res/res_config_ldap.c Fri May 11 15:52:29 2007
@@ -745,33 +745,6 @@
 	return cbasedn;
 }
 
-/*! \brief Append a string to a filter string. The filter string can grow */
-static void append_string_to_filter(char **filter_ptr, int *filter_size_ptr, const char *filter)
-{
-	int current_len = 0;
-	int needed_len = 0;
-	char *r_filter = NULL;
-
-	if (strchr(filter, '$')) {
-		r_filter = substituted(NULL, filter);
-		filter = r_filter;
-	}
-	current_len = (*filter_ptr ? strlen(*filter_ptr) : 0);
-	needed_len = current_len + strlen(filter);
-	if (*filter_size_ptr < (needed_len + 1)) {
-		if (*filter_size_ptr == 0)
-			*filter_size_ptr = (needed_len > 128 ? needed_len : 128);
-		else
-			*filter_size_ptr = (*filter_size_ptr) * 2;
-		*filter_ptr = realloc(*filter_ptr, *filter_size_ptr);
-	}
-	if (*filter_ptr) {
-		strcpy((*filter_ptr) + current_len, filter);
-	}
-	if (r_filter)
-		free(r_filter);
-}
-
 /*! \brief Replace search by by in string. No check is done on string allocated size ! */
 static int replace_string_in_string(char *string, const char *search,const char *by)
 {
@@ -797,22 +770,17 @@
 
 /*! \brief Append a name=value filter string. The filter string can grow. */
 /*! \brief convert name and value if "LIKE' is used (see http://bugs.digium.com/view.php?id=5765) */
-static void append_var_and_value_to_filter(char **filter_ptr,
-					   int *filter_size_ptr,
-					   struct ldap_table_config
-					   *table_config, const char *name,
-					   const char *value)
+static void append_var_and_value_to_filter(struct ast_str **filter,
+	struct ldap_table_config *table_config,
+	const char *name, const char *value)
 {
 	char *new_name = NULL;
 	char *new_value = NULL;
 	char *wsPos = strstr(name, " LIKE");
-
-	/* getting some escape character filter mods */
 	int foundEscape = 0;
 	struct ast_variable *nextEscape = table_config->escapes;
 	char *esc_value = NULL;
 	char esc_tmp[2];
-	
 	
 	for(nextEscape = table_config->escapes; nextEscape ; nextEscape = nextEscape->next ){
 		if(strcmp(name,nextEscape->name) == 0){
@@ -841,11 +809,9 @@
 		value = new_value;
 	}
 	name = convert_attribute_name_to_ldap(table_config, name);
-	append_string_to_filter(filter_ptr, filter_size_ptr, "(");
-	append_string_to_filter(filter_ptr, filter_size_ptr, name);
-	append_string_to_filter(filter_ptr, filter_size_ptr, "=");
-	append_string_to_filter(filter_ptr, filter_size_ptr, value);
-	append_string_to_filter(filter_ptr, filter_size_ptr, ")");
+
+	ast_str_append(filter, 0, "(%s=%s)", name, value);
+
 	if (new_name)
 		free(new_name);
 	if (new_value)
@@ -868,8 +834,7 @@
 	const char *newval = NULL;
 	struct ldap_table_config *table_config = NULL;
 	char *clean_basedn = cleaned_basedn(NULL, basedn);
-	char *filter = NULL;
-	int filter_size = 0;
+	struct ast_str *filter = NULL;
 	int tries = 0;
 	int result = 0;
 	LDAPMessage *ldap_result = NULL;
@@ -878,6 +843,9 @@
 		ast_log(LOG_WARNING, "No table_name specified.\n");
 		return NULL;
 	} 
+
+	if (!(filter = ast_str_create(80)))
+		return NULL;
 
 	/* Get the first parameter and first value in our list of passed paramater/value pairs  */
 	newparam = va_arg(ap, const char *);
@@ -904,31 +872,29 @@
 		return NULL;
 	}
 	
-	append_string_to_filter(&filter, &filter_size, "(&");
-
-	if (table_config && table_config->additional_filter) {
-		append_string_to_filter(&filter, &filter_size,
-			table_config->additional_filter);
-	}
+	ast_str_append(&filter, 0, "(&");
+
+	if (table_config && table_config->additional_filter)
+		ast_str_append(&filter, 0, table_config->additional_filter);
 	if (table_config != base_table_config && base_table_config && 
 		base_table_config->additional_filter) {
-		append_string_to_filter(&filter, &filter_size, base_table_config->additional_filter);
+		ast_str_append(&filter, 0, base_table_config->additional_filter);
 	}
 
 	/* Create the first part of the query using the first parameter/value pairs we just extracted */
 	/*   If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
 
-	append_var_and_value_to_filter(&filter, &filter_size, table_config, newparam, newval);
+	append_var_and_value_to_filter(&filter, table_config, newparam, newval);
 	while ((newparam = va_arg(ap, const char *))) {
 		newval = va_arg(ap, const char *);
-		append_var_and_value_to_filter(&filter, &filter_size, table_config, newparam, newval);
-	}
-	append_string_to_filter(&filter, &filter_size, ")");
+		append_var_and_value_to_filter(&filter, table_config, newparam, newval);
+	}
+	ast_str_append(&filter, 0, ")");
 
 	do {
 		/* freeing ldap_result further down */
 		result = ldap_search_ext_s(ldapConn, clean_basedn,
-				  LDAP_SCOPE_SUBTREE, filter, NULL, 0, NULL, NULL, NULL, LDAP_NO_LIMIT,
+				  LDAP_SCOPE_SUBTREE, filter->str, NULL, 0, NULL, NULL, NULL, LDAP_NO_LIMIT,
 				  &ldap_result);
 		if (result < 0 && is_ldap_connect_error(result)) {
 			ast_log(LOG_WARNING, "Failed to query database. Try %d/3\n",
@@ -948,7 +914,7 @@
 
 	if (result < 0) {
 		ast_log(LOG_WARNING, "Failed to query database. Check debug for more info.\n");
-		ast_log(LOG_WARNING, "Query: %s\n", filter);
+		ast_log(LOG_WARNING, "Query: %s\n", filter->str);
 		ast_log(LOG_WARNING, "Query Failed because: %s\n", ldap_err2string(result));
 	} else {
 		/* this is where we create the variables from the search result 
@@ -958,7 +924,7 @@
 			vars = realtime_ldap_result_to_vars(table_config,ldap_result,entries_count_ptr);
 		} else {
 			ast_log(LOG_WARNING, "Could not find any entry matching %s in base dn %s.\n",
-				filter, clean_basedn);
+				filter->str, clean_basedn);
 		}
 
 		ldap_msgfree(ldap_result);
@@ -1274,8 +1240,7 @@
 	int mod_exists = 0;
 	struct ldap_table_config *table_config = NULL;
 	char *clean_basedn = NULL;
-	char *filter = NULL;
-	int filter_size = 0;
+	struct ast_str *filter = NULL;
 	int tries = 0;
 	int result = 0;
 	LDAPMessage *ldap_result = NULL;
@@ -1284,6 +1249,9 @@
 		ast_log(LOG_WARNING, "No table_name specified.\n");
 		return -1;
 	} 
+
+	if (!(filter = ast_str_create(80)))
+		return -1;
 
 	if (!attribute || !lookup) {
 		ast_log(LOG_WARNING,
@@ -1308,17 +1276,16 @@
 	clean_basedn = cleaned_basedn(NULL, basedn);
 
 	/* Create the filter with the table additional filter and the parameter/value pairs we were given */
-	append_string_to_filter(&filter, &filter_size, "(&");
+	ast_str_append(&filter, 0, "(&");
 	if (table_config && table_config->additional_filter) {
-		append_string_to_filter(&filter, &filter_size,
-			table_config->additional_filter);
+		ast_str_append(&filter, 0, table_config->additional_filter);
 	}
 	if (table_config != base_table_config && base_table_config
 		&& base_table_config->additional_filter) {
-		append_string_to_filter(&filter, &filter_size, base_table_config->additional_filter);
-	}
-	append_var_and_value_to_filter(&filter, &filter_size, table_config, attribute, lookup);
-	append_string_to_filter(&filter, &filter_size, ")");
+		ast_str_append(&filter, 0, base_table_config->additional_filter);
+	}
+	append_var_and_value_to_filter(&filter, table_config, attribute, lookup);
+	ast_str_append(&filter, 0, ")");
 	
 	/* Create the modification array with the parameter/value pairs we were given, 
 	 * if there are several parameters with the same name, we collect them into 
@@ -1383,7 +1350,7 @@
 	do {
 		/* freeing ldap_result further down */
 		result = ldap_search_ext_s(ldapConn, clean_basedn,
-				  LDAP_SCOPE_SUBTREE, filter, NULL, 0, NULL, NULL, NULL, LDAP_NO_LIMIT,
+				  LDAP_SCOPE_SUBTREE, filter->str, NULL, 0, NULL, NULL, NULL, LDAP_NO_LIMIT,
 				  &ldap_result);
 		if (result < 0 && is_ldap_connect_error(result)) {
 			ast_log(LOG_WARNING, "Failed to query database. Try %d/3\n",
@@ -1402,13 +1369,10 @@
 	} while (result < 0 && tries < 3 && is_ldap_connect_error(result));
 
 	if (result < 0) {
-		ast_log(LOG_WARNING,
-				"Failed to query directory. Check debug for more info.\n");
-		ast_log(LOG_WARNING, "Query: %s\n",
-				filter);
-		ast_log(LOG_WARNING,
-				"Query Failed because: %s\n",
-					ldap_err2string(result));
+		ast_log(LOG_WARNING, "Failed to query directory. Check debug for more info.\n");
+		ast_log(LOG_WARNING, "Query: %s\n", filter->str);
+		ast_log(LOG_WARNING, "Query Failed because: %s\n",
+			ldap_err2string(result));
 
 		ast_mutex_unlock(&ldap_lock);
 		if (filter)



More information about the svn-commits mailing list