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

svn-commits at lists.digium.com svn-commits at lists.digium.com
Mon May 7 16:36:18 MST 2007


Author: russell
Date: Mon May  7 18:36:17 2007
New Revision: 63357

URL: http://svn.digium.com/view/asterisk?view=rev&rev=63357
Log:
Merge a bunch of coding guidelines type changes ... 

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=63357&r1=63356&r2=63357
==============================================================================
--- team/group/res_config_ldap/res/res_config_ldap.c (original)
+++ team/group/res_config_ldap/res/res_config_ldap.c Mon May  7 18:36:17 2007
@@ -53,22 +53,21 @@
 #include "asterisk/pbx.h"
 #include "asterisk/linkedlists.h"
 
-static char *res_config_ldap_desc = "LDAP RealTime Configuration Driver";
+#define RES_CONFIG_LDAP_CONF "res_ldap.conf"
+
 AST_MUTEX_DEFINE_STATIC(ldap_lock);
-#define RES_CONFIG_LDAP_CONF "res_ldap.conf"
-static LDAP *ldapConn = NULL;
-static char host[512] = "";
-static char user[512] = "";
-static char pass[50] = "";
-static char basedn[512] = "";
+
+static LDAP *ldapConn;
+static char host[512];
+static char user[512];
+static char pass[50];
+static char basedn[512];
 static int port = 389;
-static time_t connect_time = 0;
+static time_t connect_time;
 
 static int parse_config(void);
 static int ldap_reconnect(void);
 static int realtime_ldap_status(int fd, int argc, char **argv);
-
-int LOCAL_USER_DECL;
 
 struct category_and_metric {
 	char *name;
@@ -90,53 +89,53 @@
 
 /*! \brief Table configuration */
 struct ldap_table_config {
-	char *table_name;			/*!< table name */
-	char *additional_filter;		/*!< additional filter        */
-	struct ast_variable *attributes;	/*!< attribute names conversion */
-	struct ast_variable *escapes; /*!< characters to escape(i.e., * - the ldap wildchar) */
-	struct ast_variable *delimiters; /*!< the current delimiter is semicolon, so we are not using this variable */
-	struct ldap_table_config *next;		/*!< next entry */
-	
+	char *table_name;                 /*!< table name */
+	char *additional_filter;          /*!< additional filter        */
+	struct ast_variable *attributes;  /*!< attribute names conversion */
+	struct ast_variable *escapes;     /*!< characters to escape(i.e., * - the ldap wildchar) */
+	struct ast_variable *delimiters;  /*!< the current delimiter is semicolon, so we are not using this variable */
+	AST_LIST_ENTRY(ldap_table_config) entry;
 };
 
 /*! \brief Should be locked before using it */
-static struct ldap_table_config *table_configs = NULL;
-static struct ldap_table_config *base_table_config = NULL;
-static struct ldap_table_config *static_table_config = NULL;
+static AST_LIST_HEAD_NOLOCK_STATIC(table_configs, ldap_table_config);
+static struct ldap_table_config *base_table_config;
+static struct ldap_table_config *static_table_config;
 
 /*! \brief Create a new table_config */
 static struct ldap_table_config *table_config_new(const char *table_name)
 {
 	struct ldap_table_config *p = ast_calloc(1, sizeof(*p));
+
 	if (table_name)
 		p->table_name = strdup(table_name);
+
 	return p;
 }
 
-/*! \brief Find a table_config - Should be locked before using it */
+/*! \brief Find a table_config - Should be locked before using it 
+ *  \note This function assumes ldap_lock to be locked. */
 static struct ldap_table_config *table_config_for_table_name(const char *table_name)
 {
-	struct ldap_table_config *c = table_configs;
-	while (c) {
-		if (strcmp(c->table_name, table_name) == 0)
-			return c;
-		else
-			c = c->next;
-	}
-	return NULL;
+	struct ldap_table_config *c = NULL;
+
+	AST_LIST_TRAVERSE(&table_configs, c, entry) {
+		if (!strcmp(c->table_name, table_name))
+			break;
+	}
+
+	return c;
 }
 
 /*! \brief Find variable by name */
-static struct ast_variable *variable_named(struct ast_variable *var,
-					   const char *name)
-{
-	while (var) {
-		if (strcasecmp(name, var->name) == 0)
-			return var;
-		else
-			var = var->next;
-	}
-	return NULL;
+static struct ast_variable *variable_named(struct ast_variable *var, const char *name)
+{
+	for (; var; var = var->next) {
+		if (!strcasecmp(name, var->name))
+			break;
+	}
+
+	return var;
 }
 
 /*! \brief for the semicolon delimiter
@@ -144,47 +143,45 @@
 
 	\return number of occurances of the delimiter(semicolon)
  */
-static int semicolon_count_str(const char * somestr){
-	int i=0;
-	int count=0;
-	while (somestr[i] != '\0'){
-		if (somestr[i] == ';'){
+static int semicolon_count_str(const char *somestr)
+{
+	int count = 0;
+
+	for (; *somestr; somestr++) {
+		if (*somestr == ';')
 			count++;
-		}
-		i++;
-	}
+	}
+
 	return count;
 } 
 
 /* takes a linked list of \a ast_variable variables, finds the one with the name variable_value
  * and returns the number of semicolons in the value for that \a ast_variable
  */
-static int semicolon_count_var(struct ast_variable * var){
-	int count = 0;
-	struct ast_variable * var_value =
-		variable_named(var, "variable_value");
-	if(var_value == NULL)
+static int semicolon_count_var(struct ast_variable *var)
+{
+	struct ast_variable *var_value = variable_named(var, "variable_value");
+
+	if (!var_value)
 		return 0;
 
 	if (option_debug)
 		ast_log(LOG_DEBUG, "LINE(%d) semicolon_count_var: %s\n", __LINE__, var_value->value);
 
-	count = semicolon_count_str(var_value->value);
-
-	return count;
+	return semicolon_count_str(var_value->value);
 }
 
 /*! \brief add escape characters to the table config (e.g., the * character)
  */
 static void ldap_table_config_add_escape(struct ldap_table_config *table_config,
-					    const char *escape_string)
+	const char *escape_string)
 {
 	if (escape_string && *escape_string) {
 		char *string = strdup(escape_string);
 		char *start = string;
+		char *p = strstr(start, "=>");
 		if (option_debug)
 			ast_log(LOG_DEBUG, "LDAP RealTime: Add escape: start: %s\n", start);
-		char *p = strstr(start, "=>");
 		if (!p) {
 			ast_log(LOG_WARNING,
 					"LDAP RealTime: Missing '=>' in escape: %s in %s\n",
@@ -230,7 +227,6 @@
 		free(string);
 	}
 }
-					   
 
 /*! \brief add attribute to table config - Should be locked before using it */
 static void ldap_table_config_add_attribute(struct ldap_table_config *table_config,
@@ -289,50 +285,48 @@
 	}
 }
 
-/*! \brief Free table_config */
+/*! \brief Free table_config 
+ *  \note assumes ldap_lock to be locked */
 static void table_configs_free(void)
 {
-	struct ldap_table_config *c = table_configs;
-
-	while (c) {
-		struct ldap_table_config *next = c->next;
+	struct ldap_table_config *c;
+
+	while ((c = AST_LIST_REMOVE_HEAD(&table_configs, entry))) {
 		if (c->table_name)
 			free(c->table_name);
 		if (c->additional_filter)
 			free(c->additional_filter);
-		if (c->attributes) {
+		if (c->attributes)
 			ast_variables_destroy(c->attributes);
-		}
-		if (c->escapes) {
+		if (c->escapes)
 			ast_variables_destroy(c->escapes);
-		};
 		free(c);
-		c = next;
-	}
-	table_configs = NULL;
+	}
+
 	base_table_config = NULL;
+	static_table_config = NULL;
 }
 
 /*! \brief Convert variable name to ldap attribute name - Should be locked before using it */
 static const char *convert_attribute_name_to_ldap(struct ldap_table_config *table_config,
-						  const char *attribute_name)
+	const char *attribute_name)
 {
 	int i = 0;
-	for (i = 0; i < 2; i++) {
-		if (table_config) {
-			struct ast_variable *attribute = table_config->attributes;
-			while (attribute) {
-				if (strcasecmp(attribute_name, attribute->name) == 0)
-					return attribute->value;
-				else
-					attribute = attribute->next;
-			}
-		}
-		if (table_config == base_table_config)
-			break;
-		else
-			table_config = base_table_config;
-	}
+	struct ldap_table_config *configs[] = { table_config, base_table_config };
+
+	for (i = 0; i < ARRAY_LEN(configs); i++) {
+		struct ast_variable *attribute;
+
+		if (!configs[i])
+			continue;
+
+		attribute = configs[i]->attributes;
+		for (; attribute; attribute = attribute->next) {
+			if (!strcasecmp(attribute_name, attribute->name))
+				return attribute->value;
+		}
+	}
+
 	return attribute_name;
 }
 
@@ -341,23 +335,21 @@
 						    const char *attribute_name)
 {
 	int i = 0;
-
-	for (i = 0; i < 2; i++) {
-		if (table_config) {
-			struct ast_variable *attribute = table_config->attributes;
-
-			while (attribute) {
-				if (strcasecmp(attribute_name, attribute->value) == 0)
-					return attribute->name;
-				else
-					attribute = attribute->next;
-			}
-		}
-		if (table_config == base_table_config)
-			break;
-		else
-			table_config = base_table_config;
-	}
+	struct ldap_table_config *configs[] = { table_config, base_table_config };
+
+	for (i = 0; i < ARRAY_LEN(configs); i++) {
+		struct ast_variable *attribute;
+	
+		if (!configs[i])
+			continue;
+
+		attribute = configs[i]->attributes;
+		for (; attribute; attribute = attribute->next) {
+			if (strcasecmp(attribute_name, attribute->value) == 0)
+				return attribute->name;
+		}
+	}
+
 	return attribute_name;
 }
 
@@ -372,24 +364,21 @@
 	BerElement *ber = NULL;
 	struct ast_variable *var = NULL;
 	struct ast_variable *prev = NULL;
-	int is_delimited=0;
-	int i=0;
-
-	char *ldap_attribute_name = ldap_first_attribute(ldapConn, ldap_entry, &ber);
-//	char *value;
+	int is_delimited = 0;
+	int i = 0;
+	char *ldap_attribute_name;
 	struct berval *value;
-	int pos=0;
+	int pos = 0;
+
+	ldap_attribute_name = ldap_first_attribute(ldapConn, ldap_entry, &ber);
 
 	while (ldap_attribute_name) {
-		const char *attribute_name =
-			convert_attribute_name_from_ldap(table_config,ldap_attribute_name);
+		struct berval **values = NULL;
+		const char *attribute_name = convert_attribute_name_from_ldap(table_config,ldap_attribute_name);
 		int is_realmed_password_attribute = strcasecmp(attribute_name, "md5secret") == 0;
-//		char **values = NULL;
-		struct berval **values = NULL;
 
 		values = ldap_get_values_len(ldapConn, ldap_entry, ldap_attribute_name);/*these are freed at the end*/
 		if (values) {
-//			char **v = values;
 			struct berval **v = values;
 
 			while (*v) {
@@ -510,12 +499,10 @@
 				const char *attribute_name =
 					convert_attribute_name_from_ldap(table_config,ldap_attribute_name);
 				int is_realmed_password_attribute = strcasecmp(attribute_name, "md5secret") == 0;
-			//	char **values = NULL;
 				struct berval **values = NULL;
 
 				values = ldap_get_values_len(ldapConn, ldap_entry, ldap_attribute_name);
 				if (values) {
-					//char **v = values;
 					struct berval **v = values;
 
 					while (*v) {
@@ -909,10 +896,14 @@
 	struct ast_variable **vars = NULL;
 	const char *newparam = NULL;
 	const char *newval = NULL;
-
-	if (option_debug > 1)
-		ast_log(LOG_DEBUG,
-			"realtime_ldap_base: basedn: %s table_name: %s\n", basedn, table_name);
+	struct ldap_table_config *table_config = NULL;
+	char *clean_basedn = cleaned_basedn(NULL, basedn);
+	char *filter = NULL;
+	int filter_size = 0;
+	int tries = 0;
+	int result = 0;
+	LDAPMessage *ldap_result = NULL;
+
 	if (!table_name) {
 		ast_log(LOG_WARNING, "No table_name specified.\n");
 		return NULL;
@@ -921,9 +912,10 @@
 	/* Get the first parameter and first value in our list of passed paramater/value pairs  */
 	newparam = va_arg(ap, const char *);
 	newval = va_arg(ap, const char *);
+
 	if (!newparam || !newval) {
-		ast_log(LOG_WARNING,
-				"Realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
+		ast_log(LOG_WARNING, "Realtime retrieval requires at least 1 parameter"
+			" and 1 value to search on.\n");
 		return NULL;
 	}
 
@@ -935,52 +927,33 @@
 		return NULL;
 	}
 	
-	struct ldap_table_config *table_config = NULL;
-
 	table_config = table_config_for_table_name(table_name);
 	if (!table_config) {
-		ast_log(LOG_WARNING,
-				"No table named '%s'.\n",
-				table_name);
+		ast_log(LOG_WARNING, "No table named '%s'.\n", table_name);
 		ast_mutex_unlock(&ldap_lock);
 		return NULL;
 	}
 	
-	char *clean_basedn = cleaned_basedn(NULL, basedn);
-	char *filter = NULL;
-	int filter_size = 0;
-	int tries = 0;
-
-	int result = 0;
-	LDAPMessage *ldap_result = 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);
 	}
-	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);
+	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);
 	}
 
 	/* 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, &filter_size, 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_var_and_value_to_filter(&filter, &filter_size, table_config, newparam, newval);
 	}
 	append_string_to_filter(&filter, &filter_size, ")");
-
-	if (option_debug)
-		ast_log(LOG_DEBUG, "filter: %s\n", filter);
-
 
 	do {
 		/* freeing ldap_result further down */
@@ -1004,13 +977,9 @@
 	} while (result < 0 && tries < 3 && is_ldap_connect_error(result));
 
 	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 Failed because: %s\n",
-					ldap_err2string(result));
+		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 Failed because: %s\n", ldap_err2string(result));
 	} else {
 		/* this is where we create the variables from the search result 
 		 * freeing this \a vars outside this function */
@@ -1021,7 +990,6 @@
 			ast_log(LOG_WARNING, "Could not find any entry matching %s in base dn %s.\n",
 				filter, clean_basedn);
 		}
-
 
 		ldap_msgfree(ldap_result);
 
@@ -1073,11 +1041,15 @@
 			}
 		}
 	}
+
 	if (filter)
 		free(filter);
+
 	if (clean_basedn)
 		free(clean_basedn);
+
 	ast_mutex_unlock(&ldap_lock);
+
 	return vars;
 }
 
@@ -1091,6 +1063,7 @@
 	va_start(ap, table_name);
 	vars = realtime_ldap_base(entries_count_ptr, basedn, table_name, ap);
 	va_end(ap);
+
 	return vars;
 }
 
@@ -1335,12 +1308,10 @@
 static
 */
 static int update_ldap(const char *basedn, const char *table_name, const char *attribute,
-                                                const char *lookup, va_list ap)
+	const char *lookup, va_list ap)
 {
 	int error=0;
 	LDAPMessage *ldap_entry = NULL;
-	//struct ast_variable **vars = NULL;
-	/*!< done defining counting vars */
 	LDAPMod ** ldap_mods;
 	const char *newparam = NULL;
 	const char *newval = NULL;
@@ -1357,7 +1328,6 @@
 	int result = 0;
 	LDAPMessage *ldap_result = NULL;
 
-
 	if (option_debug > 1)
 		ast_log(LOG_DEBUG,
 			"realtime_ldap_base: basedn: %s table_name: %s\n", basedn, table_name);
@@ -1381,9 +1351,7 @@
 
 	table_config = table_config_for_table_name(table_name);
 	if (!table_config) {
-		ast_log(LOG_WARNING,
-				"No table named '%s'.\n",
-				table_name);
+		ast_log(LOG_WARNING, "No table named '%s'.\n", table_name);
 		ast_mutex_unlock(&ldap_lock);
 		return -1;
 	}
@@ -1398,15 +1366,14 @@
 	}
 	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_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, ")");
-	if (option_debug)
-		ast_log(LOG_DEBUG, "filter: %s\n", filter);
-	
-	/* 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 one parameter/value pair and delimit them with a semicolon */
+	
+	/* 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 
+	 * one parameter/value pair and delimit them with a semicolon */
 	newparam = va_arg(ap, const char *);
 	newparam = convert_attribute_name_to_ldap(table_config, newparam);
 	newval = va_arg(ap, const char *);
@@ -1541,7 +1508,7 @@
 	.update_func = update_ldap
 };
 
-static int load_module(void *mod) {
+static int load_module(void) {
 	
 	if (parse_config() < 0) {
 		ast_log(LOG_NOTICE, "Cannot load LDAP RealTime driver.\n");
@@ -1563,7 +1530,7 @@
 	return 0;
 }
 
-static int unload_module(void *mod)
+static int unload_module(void)
 {
 	/* Aquire control before doing anything to the module itself. */
 	ast_mutex_lock(&ldap_lock);
@@ -1587,7 +1554,7 @@
 	return 0;
 }
 
-static int reload(void *mod)
+static int reload(void)
 {
 	/* Aquire control before doing anything to the module itself. */
 	ast_mutex_lock(&ldap_lock);
@@ -1677,9 +1644,7 @@
 				table_config_for_table_name(category_name);
 			if (!table_config) {
 				table_config = table_config_new(category_name);
-				if (table_configs)
-					table_config->next = table_configs;
-				table_configs = table_config;
+				AST_LIST_INSERT_HEAD(&table_configs, table_config, entry);
 				if (is_general)
 					base_table_config = table_config;
 				if (is_config)
@@ -1711,16 +1676,6 @@
 	}
 	return 1;
 }
-
-static const char *description(void)
-{
-	return res_config_ldap_desc;
-}
-
-//static const char *key(void)
-//{
-//	return ASTERISK_GPL_KEY;
-//}
 
 static int ldap_reconnect(void)
 {
@@ -1808,8 +1763,7 @@
 }
 
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "LDAP realtime interface",
-                .load = load_module,
-				.unload = unload_module,
-				.reload = reload,
-				.description = description,
-			   );
+	.load = load_module,
+	.unload = unload_module,
+	.reload = reload,
+);



More information about the svn-commits mailing list