Bläddra i källkod

utils: clang-format for coherent indentation and coding style

Victor Seva 2 år sedan
förälder
incheckning
e0d162afb8

+ 120 - 108
src/modules/utils/conf.c

@@ -40,27 +40,26 @@
 #define BUFSIZE 1000
 
 /*! \brief special filter indices */
-enum {
+enum
+{
 	sfidx_request = 0,
 	sfidx_reply,
 	sfilter_cnt
 };
 
 /*! special filter masks */
-static int sfilter_mask[sfilter_cnt] = { 1, 2 };
+static int sfilter_mask[sfilter_cnt] = {1, 2};
 
 /*! special filter names */
-static char *sfilter_str[sfilter_cnt] = {
-	"REQUEST",
-	"REPLY"
-};
+static char *sfilter_str[sfilter_cnt] = {"REQUEST", "REPLY"};
 
 
-struct fwd_setting {
+struct fwd_setting
+{
 	int active;
 	int sfilter;
 	char *filter_methods;
-	struct proxy_l* proxy;
+	struct proxy_l *proxy;
 };
 
 
@@ -77,11 +76,11 @@ static int fwd_max_id = 0;
 static void remove_spaces(char *s)
 {
 	char *p, *dst;
-	for (p = s, dst = s; *p != '\0'; ++p) {
-		if (!isspace(*p)) *dst++ = *p;
+	for(p = s, dst = s; *p != '\0'; ++p) {
+		if(!isspace(*p))
+			*dst++ = *p;
 	}
 	*dst = '\0';
-
 }
 
 
@@ -96,12 +95,13 @@ static void remove_spaces(char *s)
  */
 static int conf_str2int(char *s)
 {
-	if (s == NULL) return -1;
+	if(s == NULL)
+		return -1;
 
 	errno = 0;
 	char *end = NULL;
 	long int i = strtol(s, &end, 10);
-	if ((errno != 0) || (i == LONG_MIN) || (i == LONG_MAX) || (end == s)) {
+	if((errno != 0) || (i == LONG_MIN) || (i == LONG_MAX) || (end == s)) {
 		LM_ERR("invalid string '%s'.\n", s);
 		return -1;
 	}
@@ -123,9 +123,9 @@ int conf_str2id(char *id_str)
 {
 	int id = conf_str2int(id_str);
 
-	if ((id<0) || (id > fwd_max_id)) {
-	LM_ERR("id %d is out of range.\n", id);
-	return -1;
+	if((id < 0) || (id > fwd_max_id)) {
+		LM_ERR("id %d is out of range.\n", id);
+		return -1;
 	}
 
 	return id;
@@ -138,17 +138,17 @@ int conf_str2id(char *id_str)
  * \param param_str can be either "off" or "on".
  * \return 0 on success, -1 otherwise
  */
-static int update_switch(int id, char* param_str)
+static int update_switch(int id, char *param_str)
 {
-	if (param_str == NULL) {
+	if(param_str == NULL) {
 		LM_ERR("param_str is NULL.\n");
 		return -1;
 	}
 
-	if (strcmp(param_str, "on") == 0) {
+	if(strcmp(param_str, "on") == 0) {
 		fwd_settings[id].active = 1;
 		return 0;
-	} else if (strcmp(param_str, "off") == 0) {
+	} else if(strcmp(param_str, "off") == 0) {
 		fwd_settings[id].active = 0;
 		return 0;
 	}
@@ -170,63 +170,63 @@ static int update_switch(int id, char* param_str)
  */
 static int update_filter(int id, char *flist)
 {
-	if (flist == NULL) {
+	if(flist == NULL) {
 		LM_ERR("flist is NULL.\n");
 		return -1;
 	}
 
 	/* reset special filter mask and filter methods*/
 	fwd_settings[id].sfilter = 0;
-	if (fwd_settings[id].filter_methods != NULL) {
+	if(fwd_settings[id].filter_methods != NULL) {
 		shm_free(fwd_settings[id].filter_methods);
 		fwd_settings[id].filter_methods = NULL;
 	}
 
 	int i;
-	for (i=0; i<sfilter_cnt; i++) {
-		if (strstr(flist, sfilter_str[i]) != NULL) {
+	for(i = 0; i < sfilter_cnt; i++) {
+		if(strstr(flist, sfilter_str[i]) != NULL) {
 			/* special filter name is found in flist -> add to special filter mask */
 			fwd_settings[id].sfilter |= sfilter_mask[i];
 		}
 	}
 
-	char buf[BUFSIZE+1], tmp[BUFSIZE+1];
+	char buf[BUFSIZE + 1], tmp[BUFSIZE + 1];
 	buf[0] = '\0';
 	char *set_p = flist;
 	char *token = NULL;
-	while ((token = strsep(&set_p, ":"))) {  /* iterate through list of filters */
-		int found  = 0;
+	while((token = strsep(&set_p, ":"))) { /* iterate through list of filters */
+		int found = 0;
 		/* is it a special filter? */
-		for (i=0; i<sfilter_cnt; i++) {
-			if (strcmp(token, sfilter_str[i]) == 0) {
+		for(i = 0; i < sfilter_cnt; i++) {
+			if(strcmp(token, sfilter_str[i]) == 0) {
 				found = 1;
 				break;
 			}
 		}
 
-		if (found == 0) {
-		/* no special filter! */
-			if (buf[0]) {
+		if(found == 0) {
+			/* no special filter! */
+			if(buf[0]) {
 				strcpy(tmp, buf);
-				if(snprintf(buf, BUFSIZE, "%s:%s", tmp, token)>BUFSIZE) {
+				if(snprintf(buf, BUFSIZE, "%s:%s", tmp, token) > BUFSIZE) {
 					LM_BUG("output was truncated\n");
 				}
-				buf[BUFSIZE]='\0';
+				buf[BUFSIZE] = '\0';
 			} else {
 				snprintf(buf, BUFSIZE, "%s", token);
-				buf[BUFSIZE]='\0';
+				buf[BUFSIZE] = '\0';
 			}
 		}
 	}
 
 	int len = strlen(buf);
-	if (len > 0) {
-		char *flc = shm_malloc(len+1);
-		if (flc == NULL) {
+	if(len > 0) {
+		char *flc = shm_malloc(len + 1);
+		if(flc == NULL) {
 			SHM_MEM_ERROR;
 			return -1;
 		}
-		memcpy(flc, buf, len+1);
+		memcpy(flc, buf, len + 1);
 		fwd_settings[id].filter_methods = flc;
 	}
 	return 0;
@@ -242,17 +242,17 @@ static int update_filter(int id, char *flist)
  */
 static int update_proxy(int id, char *host_str, char *port_str)
 {
-	if (host_str == NULL) {
+	if(host_str == NULL) {
 		LM_ERR("host_str is NULL.\n");
 		return -1;
 	}
-	if (port_str == NULL) {
+	if(port_str == NULL) {
 		LM_ERR("port_str is NULL.\n");
 		return -1;
 	}
 
 	int port = conf_str2int(port_str);
-	if (port < 0) {
+	if(port < 0) {
 		LM_ERR("invalid port '%s'.\n", port_str);
 		return -1;
 	}
@@ -260,31 +260,31 @@ static int update_proxy(int id, char *host_str, char *port_str)
 	/* make copy of host string since mk_proxy does not */
 	str host;
 	host.len = strlen(host_str);
-	host.s = shm_malloc(host.len+1);
-	if (host.s == NULL) {
+	host.s = shm_malloc(host.len + 1);
+	if(host.s == NULL) {
 		SHM_MEM_ERROR;
 		return -1;
 	}
 	strcpy(host.s, host_str);
 
 	/* make proxy in shared memory */
-	struct proxy_l* proxy;
+	struct proxy_l *proxy;
 	proxy = mk_shm_proxy(&host, port, PROTO_UDP);
-	if (proxy == NULL) {
+	if(proxy == NULL) {
 		LM_ERR("cannot make proxy (host='%s', port=%d).\n", host_str, port);
 		shm_free(host.s);
 		return -1;
 	}
 
-	if (fwd_settings[id].proxy) {
+	if(fwd_settings[id].proxy) {
 		/* cleaning up old proxy */
-		if (fwd_settings[id].proxy->name.s) {
+		if(fwd_settings[id].proxy->name.s) {
 			shm_free(fwd_settings[id].proxy->name.s);
 		}
 		free_shm_proxy(fwd_settings[id].proxy);
 		shm_free(fwd_settings[id].proxy);
 	}
-	fwd_settings[id].proxy = proxy;  /* new proxy is now acitvated */
+	fwd_settings[id].proxy = proxy; /* new proxy is now acitvated */
 
 	return 0;
 }
@@ -304,28 +304,30 @@ int conf_parse_switch(char *settings)
 {
 	/* make a copy since we are modifying it */
 	int len = strlen(settings);
-	if (len==0) return 1;
-	char *strc = (char *)pkg_malloc(len+1);
-	if (strc == NULL) {
+	if(len == 0)
+		return 1;
+	char *strc = (char *)pkg_malloc(len + 1);
+	if(strc == NULL) {
 		PKG_MEM_ERROR;
 		return -1;
 	}
-	memcpy(strc, settings, len+1);
+	memcpy(strc, settings, len + 1);
 	remove_spaces(strc);
 
 	char *set_p = strc;
 	char *token = NULL;
-	while ((token = strsep(&set_p, ","))) {  /* iterate through list of settings */
+	while((token = strsep(
+				   &set_p, ","))) { /* iterate through list of settings */
 		char *id_str = strsep(&token, "=");
 		int id = conf_str2id(id_str);
-		if (id < 0) {
+		if(id < 0) {
 			LM_ERR("cannot parse id '%s'.\n", id_str);
 			pkg_free(strc);
 			return -1;
 		}
 
 		/* got all data for one setting -> update configuration now */
-		if (update_switch(id, token) < 0) {
+		if(update_switch(id, token) < 0) {
 			LM_ERR("cannot update switch.\n");
 			pkg_free(strc);
 			return -1;
@@ -343,48 +345,50 @@ int conf_parse_switch(char *settings)
  * \param rpl_tree FIFO root
  * \return 0 on success, -1 on failure
  */
-int conf_show(struct mi_root* rpl_tree)
+int conf_show(struct mi_root *rpl_tree)
 {
 	int id, sfilter;
-	struct mi_node * node = NULL;
+	struct mi_node *node = NULL;
 
-	node = addf_mi_node_child( &rpl_tree->node, 0, 0, 0, "id switch %30s proxy\n", "filter");
+	node = addf_mi_node_child(
+			&rpl_tree->node, 0, 0, 0, "id switch %30s proxy\n", "filter");
 	if(node == NULL)
 		goto error;
 
-	for (id=0; id<=fwd_max_id; id++) {
-		char buf[BUFSIZE+1];
-		char tmp[BUFSIZE+1];
-		buf[0]='\0';
-		for (sfilter=0; sfilter<sfilter_cnt; sfilter++) {
-			if (fwd_settings[id].sfilter&sfilter_mask[sfilter]) {
-				if (buf[0]) {
+	for(id = 0; id <= fwd_max_id; id++) {
+		char buf[BUFSIZE + 1];
+		char tmp[BUFSIZE + 1];
+		buf[0] = '\0';
+		for(sfilter = 0; sfilter < sfilter_cnt; sfilter++) {
+			if(fwd_settings[id].sfilter & sfilter_mask[sfilter]) {
+				if(buf[0]) {
 					strcpy(tmp, buf);
 					snprintf(buf, BUFSIZE, "%s:%s", tmp, sfilter_str[sfilter]);
-					buf[BUFSIZE]='\0';
+					buf[BUFSIZE] = '\0';
 				} else {
 					snprintf(buf, BUFSIZE, "%s", sfilter_str[sfilter]);
-					buf[BUFSIZE]='\0';
+					buf[BUFSIZE] = '\0';
 				}
 			}
 		}
-		if (fwd_settings[id].filter_methods) {
-			if (buf[0]) {
+		if(fwd_settings[id].filter_methods) {
+			if(buf[0]) {
 				strcpy(tmp, buf);
-				snprintf(buf, BUFSIZE, "%s:%s", tmp, fwd_settings[id].filter_methods);
-				buf[BUFSIZE]='\0';
+				snprintf(buf, BUFSIZE, "%s:%s", tmp,
+						fwd_settings[id].filter_methods);
+				buf[BUFSIZE] = '\0';
 			} else {
 				snprintf(buf, BUFSIZE, "%s", fwd_settings[id].filter_methods);
-				buf[BUFSIZE]='\0';
+				buf[BUFSIZE] = '\0';
 			}
 		}
-		node = addf_mi_node_child( &rpl_tree->node, 0, 0, 0, "%2d %s %33s %s:%d\n", id,
-			fwd_settings[id].active ? "on " : "off", buf,
-			fwd_settings[id].proxy ? fwd_settings[id].proxy->name.s : "",
-			fwd_settings[id].proxy ? fwd_settings[id].proxy->port : 0);
+		node = addf_mi_node_child(&rpl_tree->node, 0, 0, 0,
+				"%2d %s %33s %s:%d\n", id,
+				fwd_settings[id].active ? "on " : "off", buf,
+				fwd_settings[id].proxy ? fwd_settings[id].proxy->name.s : "",
+				fwd_settings[id].proxy ? fwd_settings[id].proxy->port : 0);
 		if(node == NULL)
 			goto error;
-
 	}
 	return 0;
 
@@ -407,26 +411,28 @@ int conf_parse_filter(char *settings)
 {
 	/* make a copy since we are modifying it */
 	int len = strlen(settings);
-	if (len==0) return 1;
-	char *strc = (char *)pkg_malloc(len+1);
-	if (strc == NULL) {
+	if(len == 0)
+		return 1;
+	char *strc = (char *)pkg_malloc(len + 1);
+	if(strc == NULL) {
 		PKG_MEM_ERROR;
 		return -1;
 	}
-	memcpy(strc, settings, len+1);
+	memcpy(strc, settings, len + 1);
 	remove_spaces(strc);
 
 	char *set_p = strc;
 	char *token = NULL;
-	while ((token = strsep(&set_p, ","))) {  /* iterate through list of settings */
+	while((token = strsep(
+				   &set_p, ","))) { /* iterate through list of settings */
 		char *id_str = strsep(&token, "=");
 		int id = conf_str2id(id_str);
-		if (id<0) {
+		if(id < 0) {
 			LM_ERR("cannot parse id '%s'.\n", id_str);
 			pkg_free(strc);
 			return -1;
 		}
-		if (update_filter(id, token) < 0) {
+		if(update_filter(id, token) < 0) {
 			LM_ERR("cannot extract filters.\n");
 			pkg_free(strc);
 			return -1;
@@ -452,21 +458,23 @@ int conf_parse_proxy(char *settings)
 {
 	/* make a copy since we are modifying it */
 	int len = strlen(settings);
-	if (len==0) return 1;
-	char *strc = (char *)pkg_malloc(len+1);
-	if (strc == NULL) {
+	if(len == 0)
+		return 1;
+	char *strc = (char *)pkg_malloc(len + 1);
+	if(strc == NULL) {
 		PKG_MEM_ERROR;
 		return -1;
 	}
-	memcpy(strc, settings, len+1);
+	memcpy(strc, settings, len + 1);
 	remove_spaces(strc);
 
 	char *set_p = strc;
 	char *token = NULL;
-	while ((token = strsep(&set_p, ","))) {  /* iterate through list of settings */
+	while((token = strsep(
+				   &set_p, ","))) { /* iterate through list of settings */
 		char *id_str = strsep(&token, "=");
 		int id = conf_str2id(id_str);
-		if (id<0) {
+		if(id < 0) {
 			LM_ERR("cannot parse id '%s'.\n", id_str);
 			pkg_free(strc);
 			return -1;
@@ -474,7 +482,7 @@ int conf_parse_proxy(char *settings)
 		char *host = strsep(&token, ":");
 
 		/* got all data for one setting -> update configuration now */
-		if (update_proxy(id, host, token) < 0) {
+		if(update_proxy(id, host, token) < 0) {
 			LM_ERR("cannot update proxy.\n");
 			pkg_free(strc);
 			return -1;
@@ -497,12 +505,13 @@ static int filter_methods_contains_request(int id, char *method, int method_len)
 {
 	char *p = fwd_settings[id].filter_methods;
 
-	while (p != NULL) {
-		if (strncmp(p, method, method_len) == 0) {
+	while(p != NULL) {
+		if(strncmp(p, method, method_len) == 0) {
 			return 1;
 		}
 		p = strchr(p, ':');
-		if (p != NULL) p++;
+		if(p != NULL)
+			p++;
 	}
 
 	return 0;
@@ -517,22 +526,25 @@ static int filter_methods_contains_request(int id, char *method, int method_len)
  */
 struct proxy_l *conf_needs_forward(struct sip_msg *msg, int id)
 {
-	if ((msg == NULL) || (fwd_settings[id].active == 0)) {
+	if((msg == NULL) || (fwd_settings[id].active == 0)) {
 		return NULL;
 	}
 
-	if (msg->first_line.type == SIP_REPLY) {
-		if (fwd_settings[id].sfilter&sfilter_mask[sfidx_reply]) {
+	if(msg->first_line.type == SIP_REPLY) {
+		if(fwd_settings[id].sfilter & sfilter_mask[sfidx_reply]) {
 			return fwd_settings[id].proxy;
 		}
 	}
 
-	if (msg->first_line.type == SIP_REQUEST) {
-		if (fwd_settings[id].sfilter&sfilter_mask[sfidx_request]) {
+	if(msg->first_line.type == SIP_REQUEST) {
+		if(fwd_settings[id].sfilter & sfilter_mask[sfidx_request]) {
 			return fwd_settings[id].proxy;
 		}
 
-		if (filter_methods_contains_request(id, msg->first_line.u.request.method.s, msg->first_line.u.request.method.len) > 0) {
+		if(filter_methods_contains_request(id,
+				   msg->first_line.u.request.method.s,
+				   msg->first_line.u.request.method.len)
+				> 0) {
 			return fwd_settings[id].proxy;
 		}
 	}
@@ -549,12 +561,12 @@ struct proxy_l *conf_needs_forward(struct sip_msg *msg, int id)
 int conf_init(int max_id)
 {
 	/* allocate and initialize memory for configuration */
-	fwd_settings = shm_malloc(sizeof(struct fwd_setting)*(max_id+1));
-	if (fwd_settings == NULL) {
+	fwd_settings = shm_malloc(sizeof(struct fwd_setting) * (max_id + 1));
+	if(fwd_settings == NULL) {
 		SHM_MEM_ERROR;
 		return -1;
 	}
-	memset(fwd_settings, 0, sizeof(struct fwd_setting)*(max_id+1));
+	memset(fwd_settings, 0, sizeof(struct fwd_setting) * (max_id + 1));
 	fwd_max_id = max_id;
 	return 0;
 }
@@ -567,11 +579,11 @@ void conf_destroy(void)
 {
 	int id;
 
-	if (fwd_settings) {
-		for (id=0; id<=fwd_max_id; id++) {
+	if(fwd_settings) {
+		for(id = 0; id <= fwd_max_id; id++) {
 			fwd_settings[id].active = 0;
-			if (fwd_settings[id].proxy) {
-				if (fwd_settings[id].proxy->name.s) {
+			if(fwd_settings[id].proxy) {
+				if(fwd_settings[id].proxy->name.s) {
 					shm_free(fwd_settings[id].proxy->name.s);
 				}
 				free_shm_proxy(fwd_settings[id].proxy);

+ 48 - 52
src/modules/utils/pidf.c

@@ -32,9 +32,9 @@
  * _XOPEN_SOURCE creates conflict in header definitions in Solaris
  */
 #ifndef __OS_solaris
-	#define _XOPEN_SOURCE 600          /* glibc2 on linux, bsd */
+#define _XOPEN_SOURCE 600 /* glibc2 on linux, bsd */
 #else
-	#define _XOPEN_SOURCE_EXTENDED 1   /* solaris */
+#define _XOPEN_SOURCE_EXTENDED 1 /* solaris */
 #endif
 
 #include <time.h>
@@ -51,8 +51,8 @@
 xmlAttrPtr xmlNodeGetAttrByName(xmlNodePtr node, const char *name)
 {
 	xmlAttrPtr attr = node->properties;
-	while (attr) {
-		if (xmlStrcasecmp(attr->name, (unsigned char*)name) == 0)
+	while(attr) {
+		if(xmlStrcasecmp(attr->name, (unsigned char *)name) == 0)
 			return attr;
 		attr = attr->next;
 	}
@@ -62,8 +62,8 @@ xmlAttrPtr xmlNodeGetAttrByName(xmlNodePtr node, const char *name)
 char *xmlNodeGetAttrContentByName(xmlNodePtr node, const char *name)
 {
 	xmlAttrPtr attr = xmlNodeGetAttrByName(node, name);
-	if (attr)
-		return (char*)xmlNodeGetContent(attr->children);
+	if(attr)
+		return (char *)xmlNodeGetContent(attr->children);
 	else
 		return NULL;
 }
@@ -71,39 +71,42 @@ char *xmlNodeGetAttrContentByName(xmlNodePtr node, const char *name)
 xmlNodePtr xmlNodeGetChildByName(xmlNodePtr node, const char *name)
 {
 	xmlNodePtr cur = node->children;
-	while (cur) {
-		if (xmlStrcasecmp(cur->name, (unsigned char*)name) == 0)
+	while(cur) {
+		if(xmlStrcasecmp(cur->name, (unsigned char *)name) == 0)
 			return cur;
 		cur = cur->next;
 	}
 	return NULL;
 }
 
-xmlNodePtr xmlNodeGetNodeByName(xmlNodePtr node, const char *name,
-															const char *ns)
+xmlNodePtr xmlNodeGetNodeByName(
+		xmlNodePtr node, const char *name, const char *ns)
 {
 	xmlNodePtr cur = node;
-	while (cur) {
+	while(cur) {
 		xmlNodePtr match = NULL;
-		if (xmlStrcasecmp(cur->name, (unsigned char*)name) == 0) {
-			if (!ns || (cur->ns && xmlStrcasecmp(cur->ns->prefix,
-							(unsigned char*)ns) == 0))
+		if(xmlStrcasecmp(cur->name, (unsigned char *)name) == 0) {
+			if(!ns
+					|| (cur->ns
+							&& xmlStrcasecmp(
+									   cur->ns->prefix, (unsigned char *)ns)
+									   == 0))
 				return cur;
 		}
 		match = xmlNodeGetNodeByName(cur->children, name, ns);
-		if (match)
+		if(match)
 			return match;
 		cur = cur->next;
 	}
 	return NULL;
 }
 
-char *xmlNodeGetNodeContentByName(xmlNodePtr root, const char *name,
-		const char *ns)
+char *xmlNodeGetNodeContentByName(
+		xmlNodePtr root, const char *name, const char *ns)
 {
 	xmlNodePtr node = xmlNodeGetNodeByName(root, name, ns);
-	if (node)
-		return (char*)xmlNodeGetContent(node->children);
+	if(node)
+		return (char *)xmlNodeGetContent(node->children);
 	else
 		return NULL;
 }
@@ -114,77 +117,70 @@ xmlNodePtr xmlDocGetNodeByName(xmlDocPtr doc, const char *name, const char *ns)
 	return xmlNodeGetNodeByName(cur, name, ns);
 }
 
-char *xmlDocGetNodeContentByName(xmlDocPtr doc, const char *name, 
-		const char *ns)
+char *xmlDocGetNodeContentByName(
+		xmlDocPtr doc, const char *name, const char *ns)
 {
 	xmlNodePtr node = xmlDocGetNodeByName(doc, name, ns);
-	if (node)
-		return (char*)xmlNodeGetContent(node->children);
+	if(node)
+		return (char *)xmlNodeGetContent(node->children);
 	else
 		return NULL;
 }
 
-time_t xml_parse_dateTime(char* xml_time_str)
+time_t xml_parse_dateTime(char *xml_time_str)
 {
 	struct tm tm;
-	char * p;
+	char *p;
 	int h, m;
 	char h1, h2, m1, m2;
-	int sign= 1;
-	signed int timezone_diff= 0;
+	int sign = 1;
+	signed int timezone_diff = 0;
 
-	p= strptime(xml_time_str, "%F", &tm);
-	if(p== NULL)
-	{
+	p = strptime(xml_time_str, "%F", &tm);
+	if(p == NULL) {
 		printf("error: failed to parse time\n");
 		return 0;
 	}
 	p++;
-	p= strptime(p, "%T", &tm);
-	if(p== NULL)
-	{
+	p = strptime(p, "%T", &tm);
+	if(p == NULL) {
 		printf("error: failed to parse time\n");
 		return 0;
 	}
-	
-	if(*p== '\0')
+
+	if(*p == '\0')
 		goto done;
 
-	if(*p== '.')
-	{
+	if(*p == '.') {
 		p++;
 		/* read the fractionar part of the seconds*/
-		while(*p!= '\0' && *p>= '0' && *p<= '9')
-		{
+		while(*p != '\0' && *p >= '0' && *p <= '9') {
 			p++;
 		}
 	}
 
-	if(*p== '\0')
+	if(*p == '\0')
 		goto done;
 
-	
+
 	/* read time zone */
 
-	if(*p== 'Z')
-	{
+	if(*p == 'Z') {
 		goto done;
 	}
 
-	if(*p== '+')
-		sign= -1;
+	if(*p == '+')
+		sign = -1;
 
 	p++;
 
 	sscanf(p, "%c%c:%c%c", &h1, &h2, &m1, &m2);
-	
-	h= (h1- '0')*10+ h2- '0';
-	m= (m1- '0')*10+ m2- '0';
 
-	timezone_diff= sign* ((m+ h* 60)* 60);
+	h = (h1 - '0') * 10 + h2 - '0';
+	m = (m1 - '0') * 10 + m2 - '0';
+
+	timezone_diff = sign * ((m + h * 60) * 60);
 
 done:
-	return (mktime(&tm) + timezone_diff);	
+	return (mktime(&tm) + timezone_diff);
 }
-
-

+ 6 - 6
src/modules/utils/pidf.h

@@ -33,15 +33,15 @@
 #include "../../core/str.h"
 #include <libxml/parser.h>
 
-xmlNodePtr xmlNodeGetNodeByName(xmlNodePtr node, const char *name,
-															const char *ns);
+xmlNodePtr xmlNodeGetNodeByName(
+		xmlNodePtr node, const char *name, const char *ns);
 xmlNodePtr xmlDocGetNodeByName(xmlDocPtr doc, const char *name, const char *ns);
 xmlNodePtr xmlNodeGetChildByName(xmlNodePtr node, const char *name);
 
-char *xmlNodeGetNodeContentByName(xmlNodePtr root, const char *name,
-		const char *ns);
+char *xmlNodeGetNodeContentByName(
+		xmlNodePtr root, const char *name, const char *ns);
 char *xmlNodeGetAttrContentByName(xmlNodePtr node, const char *name);
 
-time_t xml_parse_dateTime(char* xml_time_str);
+time_t xml_parse_dateTime(char *xml_time_str);
 
-#endif 
+#endif

+ 86 - 81
src/modules/utils/utils.c

@@ -33,8 +33,6 @@
  */
 
 
-
-
 #include <curl/curl.h>
 
 #include "../../core/mod_fix.h"
@@ -58,11 +56,11 @@ MODULE_VERSION
 
 /* Module parameter variables */
 static int forward_active = 0;
-static int   mp_max_id = 0;
-static char* mp_switch = "";
-static char* mp_filter = "";
-static char* mp_proxy  = "";
-str xcap_table= str_init("xcap");
+static int mp_max_id = 0;
+static char *mp_switch = "";
+static char *mp_filter = "";
+static char *mp_proxy = "";
+str xcap_table = str_init("xcap");
 str pres_db_url = {0, 0};
 
 /* lock for configuration access */
@@ -70,10 +68,13 @@ static gen_lock_t *conf_lock = NULL;
 
 #ifdef MI_REMOVED
 /* FIFO interface functions */
-static struct mi_root* forward_fifo_list(struct mi_root* cmd_tree, void *param);
-static struct mi_root* forward_fifo_switch(struct mi_root* cmd_tree, void* param);
-static struct mi_root* forward_fifo_filter(struct mi_root* cmd_tree, void* param);
-static struct mi_root* forward_fifo_proxy(struct mi_root* cmd_tree, void* param);
+static struct mi_root *forward_fifo_list(struct mi_root *cmd_tree, void *param);
+static struct mi_root *forward_fifo_switch(
+		struct mi_root *cmd_tree, void *param);
+static struct mi_root *forward_fifo_filter(
+		struct mi_root *cmd_tree, void *param);
+static struct mi_root *forward_fifo_proxy(
+		struct mi_root *cmd_tree, void *param);
 #endif
 
 /* Database connection */
@@ -90,53 +91,47 @@ int utils_forward(struct sip_msg *msg, int id, int proto);
 
 /* Exported functions */
 static cmd_export_t cmds[] = {
-	{"xcap_auth_status", (cmd_function)w_xcap_auth_status, 2, fixup_spve_spve,
-		fixup_free_spve_spve, REQUEST_ROUTE},
-	{0, 0, 0, 0, 0, 0}
-};
+		{"xcap_auth_status", (cmd_function)w_xcap_auth_status, 2,
+				fixup_spve_spve, fixup_free_spve_spve, REQUEST_ROUTE},
+		{0, 0, 0, 0, 0, 0}};
 
 
 /* Exported parameters */
-static param_export_t params[] = {
-	{"pres_db_url", PARAM_STR, &pres_db_url},
-	{"xcap_table", PARAM_STR, &xcap_table},
-	{"forward_active", INT_PARAM, &forward_active},
-	{0, 0, 0}
-};
+static param_export_t params[] = {{"pres_db_url", PARAM_STR, &pres_db_url},
+		{"xcap_table", PARAM_STR, &xcap_table},
+		{"forward_active", INT_PARAM, &forward_active}, {0, 0, 0}};
 
 #ifdef MI_REMOVED
 static mi_export_t mi_cmds[] = {
-	{ "forward_list",   forward_fifo_list,   MI_NO_INPUT_FLAG, 0,  0 },
-	{ "forward_switch", forward_fifo_switch, 0, 0,  0 },
-	{ "forward_filter", forward_fifo_filter, 0, 0,  0 },
-	{ "forward_proxy",  forward_fifo_proxy,  0, 0,  0 },
-	{ 0, 0, 0, 0, 0}
-};
+		{"forward_list", forward_fifo_list, MI_NO_INPUT_FLAG, 0, 0},
+		{"forward_switch", forward_fifo_switch, 0, 0, 0},
+		{"forward_filter", forward_fifo_filter, 0, 0, 0},
+		{"forward_proxy", forward_fifo_proxy, 0, 0, 0}, {0, 0, 0, 0, 0}};
 #endif
 
 /* Module interface */
 struct module_exports exports = {
-	"utils",         /* module name */
-	DEFAULT_DLFLAGS, /* dlopen flags */
-	cmds,            /* exported functions */
-	params,          /* exported parameters */
-	0,               /* exported rpc functions */
-	0,               /* exported pseudo-variables */
-	0,               /* response handling function*/
-	mod_init,        /* module init function */
-	child_init,      /* per-child init function */
-	destroy          /* destroy function */
+		"utils",		 /* module name */
+		DEFAULT_DLFLAGS, /* dlopen flags */
+		cmds,			 /* exported functions */
+		params,			 /* exported parameters */
+		0,				 /* exported rpc functions */
+		0,				 /* exported pseudo-variables */
+		0,				 /* response handling function*/
+		mod_init,		 /* module init function */
+		child_init,		 /* per-child init function */
+		destroy			 /* destroy function */
 };
 
 
 static int init_shmlock(void)
 {
 	conf_lock = lock_alloc();
-	if (conf_lock == NULL) {
+	if(conf_lock == NULL) {
 		LM_CRIT("cannot allocate memory for lock.\n");
 		return -1;
 	}
-	if (lock_init(conf_lock) == 0) {
+	if(lock_init(conf_lock) == 0) {
 		LM_CRIT("cannot initialize lock.\n");
 		return -1;
 	}
@@ -145,7 +140,8 @@ static int init_shmlock(void)
 }
 
 
-static int pre_script_filter(struct sip_msg *msg, unsigned int flags, void *unused)
+static int pre_script_filter(
+		struct sip_msg *msg, unsigned int flags, void *unused)
 {
 	/* use id 0 for pre script callback */
 	utils_forward(msg, 0, PROTO_UDP);
@@ -157,7 +153,7 @@ static int pre_script_filter(struct sip_msg *msg, unsigned int flags, void *unus
 
 static void destroy_shmlock(void)
 {
-	if (conf_lock) {
+	if(conf_lock) {
 		lock_destroy(conf_lock);
 		lock_dealloc((void *)conf_lock);
 		conf_lock = NULL;
@@ -165,28 +161,31 @@ static void destroy_shmlock(void)
 }
 
 
-static void pres_db_close(void) {
-	if (pres_dbh) {
+static void pres_db_close(void)
+{
+	if(pres_dbh) {
 		pres_dbf.close(pres_dbh);
 		pres_dbh = NULL;
 	}
 }
 
-static int pres_db_init(void) {
-	if (!pres_db_url.s || !pres_db_url.len) {
+static int pres_db_init(void)
+{
+	if(!pres_db_url.s || !pres_db_url.len) {
 		LM_INFO("xcap_auth_status function is disabled\n");
 		return 0;
 	}
-	if (db_bind_mod(&pres_db_url, &pres_dbf) < 0) {
+	if(db_bind_mod(&pres_db_url, &pres_dbf) < 0) {
 		LM_ERR("can't bind database module\n");
 		return -1;
 	}
-	if ((pres_dbh = pres_dbf.init(&pres_db_url)) == NULL) {
+	if((pres_dbh = pres_dbf.init(&pres_db_url)) == NULL) {
 		LM_ERR("can't connect to database\n");
 		return -1;
 	}
-	if (db_check_table_version(&pres_dbf, pres_dbh, &xcap_table,
-				XCAP_TABLE_VERSION) < 0) {
+	if(db_check_table_version(
+			   &pres_dbf, pres_dbh, &xcap_table, XCAP_TABLE_VERSION)
+			< 0) {
 		DB_TABLE_VERSION_ERROR(xcap_table);
 		pres_db_close();
 		return -1;
@@ -195,18 +194,19 @@ static int pres_db_init(void) {
 	return 0;
 }
 
-static int pres_db_open(void) {
-	if (!pres_db_url.s || !pres_db_url.len) {
+static int pres_db_open(void)
+{
+	if(!pres_db_url.s || !pres_db_url.len) {
 		return 0;
 	}
-	if (pres_dbh) {
+	if(pres_dbh) {
 		pres_dbf.close(pres_dbh);
 	}
-	if ((pres_dbh = pres_dbf.init(&pres_db_url)) == NULL) {
+	if((pres_dbh = pres_dbf.init(&pres_db_url)) == NULL) {
 		LM_ERR("can't connect to database\n");
 		return -1;
 	}
-	if (pres_dbf.use_table(pres_dbh, &xcap_table) < 0) {
+	if(pres_dbf.use_table(pres_dbh, &xcap_table) < 0) {
 		LM_ERR("in use_table: %.*s\n", xcap_table.len, xcap_table.s);
 		return -1;
 	}
@@ -217,37 +217,39 @@ static int pres_db_open(void) {
 /* Module initialization function */
 static int mod_init(void)
 {
-	if (init_shmlock() != 0) {
+	if(init_shmlock() != 0) {
 		LM_CRIT("cannot initialize shmlock.\n");
 		return -1;
 	}
 
-	if (conf_init(mp_max_id) < 0) {
+	if(conf_init(mp_max_id) < 0) {
 		LM_CRIT("cannot initialize configuration.\n");
 		return -1;
 	}
 
 	/* read module parameters and update configuration structure */
-	if (conf_parse_proxy(mp_proxy) < 0) {
+	if(conf_parse_proxy(mp_proxy) < 0) {
 		LM_CRIT("cannot parse proxy module parameter.\n");
 		return -1;
 	}
-	if (conf_parse_filter(mp_filter) < 0) {
+	if(conf_parse_filter(mp_filter) < 0) {
 		LM_CRIT("cannot parse filter module parameter.\n");
 		return -1;
 	}
-	if (conf_parse_switch(mp_switch) < 0) {
+	if(conf_parse_switch(mp_switch) < 0) {
 		LM_CRIT("cannot parse switch module parameter.\n");
 		return -1;
 	}
 
-	if (forward_active == 1) {
+	if(forward_active == 1) {
 		/* register callback for id 0 */
-		if (register_script_cb(pre_script_filter, PRE_SCRIPT_CB|ONREPLY_CB, 0) < 0) {
+		if(register_script_cb(pre_script_filter, PRE_SCRIPT_CB | ONREPLY_CB, 0)
+				< 0) {
 			LM_CRIT("cannot register script callback for requests.\n");
 			return -1;
 		}
-		if (register_script_cb(pre_script_filter, PRE_SCRIPT_CB|ONREPLY_CB, 0) < 0) {
+		if(register_script_cb(pre_script_filter, PRE_SCRIPT_CB | ONREPLY_CB, 0)
+				< 0) {
 			LM_CRIT("cannot register script callback for replies.\n");
 			return -1;
 		}
@@ -269,8 +271,8 @@ static int mod_init(void)
 
 /* Child initialization function */
 static int child_init(int rank)
-{	
-	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+{
+	if(rank == PROC_INIT || rank == PROC_MAIN || rank == PROC_TCP_MAIN)
 		return 0; /* do nothing for the main process */
 
 	return pres_db_open();
@@ -307,9 +309,9 @@ int utils_forward(struct sip_msg *msg, int id, int proto)
 
 	struct proxy_l *proxy = conf_needs_forward(msg, id);
 
-	if (proxy != NULL) {
+	if(proxy != NULL) {
 		proxy2su(&dst.to, proxy);
-		if (forward_request(msg, NULL, 0, &dst) < 0){
+		if(forward_request(msg, NULL, 0, &dst) < 0) {
 			LM_ERR("could not forward message\n");
 		}
 		ret = 0;
@@ -352,14 +354,15 @@ int mod_register(char *path, int *dlflags, void *p1, void *p2)
  * \brief fifo command for listing configuration
  * \return pointer to the mi_root on success, 0 otherwise
  */
-static struct mi_root* forward_fifo_list(struct mi_root* cmd_tree, void *param)
+static struct mi_root *forward_fifo_list(struct mi_root *cmd_tree, void *param)
 {
 	struct mi_node *node = NULL;
-	struct mi_root * ret = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
+	struct mi_root *ret = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
 	if(ret == NULL)
 		return 0;
 
-	node = addf_mi_node_child( &ret->node, 0, 0, 0, "Printing forwarding information:");
+	node = addf_mi_node_child(
+			&ret->node, 0, 0, 0, "Printing forwarding information:");
 	if(node == NULL)
 		goto error;
 
@@ -384,13 +387,14 @@ error:
  * \brief fifo command for configuring switch
  * \return pointer to the mi_root on success, 0 otherwise
  */
-static struct mi_root* forward_fifo_switch(struct mi_root* cmd_tree, void* param)
+static struct mi_root *forward_fifo_switch(
+		struct mi_root *cmd_tree, void *param)
 {
 	struct mi_node *node = NULL;
 	int result;
 
 	node = cmd_tree->node.kids;
-	if (node==NULL || node->next!=NULL || node->value.s==NULL)
+	if(node == NULL || node->next != NULL || node->value.s == NULL)
 		return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
 
 	// critical section start:
@@ -402,9 +406,9 @@ static struct mi_root* forward_fifo_switch(struct mi_root* cmd_tree, void* param
 	// critical section end
 	lock_release(conf_lock);
 
-	if (result < 0) {
+	if(result < 0) {
 		LM_ERR("cannot parse parameter\n");
-		return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
+		return init_mi_tree(400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
 	}
 	return init_mi_tree(200, MI_OK_S, MI_OK_LEN);
 }
@@ -414,13 +418,14 @@ static struct mi_root* forward_fifo_switch(struct mi_root* cmd_tree, void* param
  * \brief fifo command for configuring filter
  * \return pointer to the mi_root on success, 0 otherwise
  */
-static struct mi_root* forward_fifo_filter(struct mi_root* cmd_tree, void* param)
+static struct mi_root *forward_fifo_filter(
+		struct mi_root *cmd_tree, void *param)
 {
 	struct mi_node *node = NULL;
 	int result;
 
 	node = cmd_tree->node.kids;
-	if (node==NULL || node->next!=NULL || node->value.s==NULL)
+	if(node == NULL || node->next != NULL || node->value.s == NULL)
 		return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
 
 	// critical section start:
@@ -432,9 +437,9 @@ static struct mi_root* forward_fifo_filter(struct mi_root* cmd_tree, void* param
 	// critical section end
 	lock_release(conf_lock);
 
-	if (result < 0) {
+	if(result < 0) {
 		LM_ERR("cannot parse parameter\n");
-		return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
+		return init_mi_tree(400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
 	}
 	return init_mi_tree(200, MI_OK_S, MI_OK_LEN);
 }
@@ -444,13 +449,13 @@ static struct mi_root* forward_fifo_filter(struct mi_root* cmd_tree, void* param
  * \brief fifo command for configuring proxy
  * \return pointer to the mi_root on success, 0 otherwise
  */
-static struct mi_root* forward_fifo_proxy(struct mi_root* cmd_tree, void* param)
+static struct mi_root *forward_fifo_proxy(struct mi_root *cmd_tree, void *param)
 {
 	struct mi_node *node = NULL;
 	int result;
 
 	node = cmd_tree->node.kids;
-	if (node==NULL || node->next!=NULL || node->value.s==NULL)
+	if(node == NULL || node->next != NULL || node->value.s == NULL)
 		return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
 
 	// critical section start:
@@ -462,9 +467,9 @@ static struct mi_root* forward_fifo_proxy(struct mi_root* cmd_tree, void* param)
 	// critical section end
 	lock_release(conf_lock);
 
-	if (result < 0) {
+	if(result < 0) {
 		LM_ERR("cannot parse parameter\n");
-		return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
+		return init_mi_tree(400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
 	}
 	return init_mi_tree(200, MI_OK_S, MI_OK_LEN);
 }

+ 5 - 5
src/modules/utils/utils.h

@@ -29,7 +29,6 @@
  */
 
 
-
 #ifndef UTILS_H
 #define UTILS_H
 
@@ -40,10 +39,11 @@ extern int http_query_timeout;
 extern db1_con_t *pres_dbh;
 extern db_func_t pres_dbf;
 
-typedef struct {
-	char		*buf;
-	size_t		curr_size;
-	size_t		pos;
+typedef struct
+{
+	char *buf;
+	size_t curr_size;
+	size_t pos;
 } http_res_stream_t;
 
 #endif /* UTILS_H */

+ 125 - 126
src/modules/utils/xcap_auth.c

@@ -48,31 +48,31 @@
 #include "utils.h"
 #include "pidf.h"
 
-xmlNodePtr get_rule_node(subs_t* subs, xmlDocPtr xcap_tree)
+xmlNodePtr get_rule_node(subs_t *subs, xmlDocPtr xcap_tree)
 {
 	str w_uri = {0, 0};
-	char* id = NULL, *domain = NULL, *time_cont= NULL;
+	char *id = NULL, *domain = NULL, *time_cont = NULL;
 	int apply_rule = -1;
-	xmlNodePtr ruleset_node = NULL, node1= NULL, node2= NULL;
+	xmlNodePtr ruleset_node = NULL, node1 = NULL, node2 = NULL;
 	xmlNodePtr cond_node = NULL, except_node = NULL;
 	xmlNodePtr identity_node = NULL;
 	xmlNodePtr iden_child;
 	xmlNodePtr validity_node, time_node;
 	time_t t_init, t_fin, t;
-	int valid= 0;
+	int valid = 0;
 
 	uandd_to_uri(subs->from_user, subs->from_domain, &w_uri);
-	if (w_uri.s == NULL) {
+	if(w_uri.s == NULL) {
 		LM_ERR("while creating uri\n");
 		return NULL;
 	}
 	ruleset_node = xmlDocGetNodeByName(xcap_tree, "ruleset", NULL);
-	if (ruleset_node == NULL) {
+	if(ruleset_node == NULL) {
 		LM_DBG("ruleset_node NULL\n");
 		goto error;
 	}
-	for (node1 = ruleset_node->children; node1; node1 = node1->next) {
-		if (xmlStrcasecmp(node1->name, (unsigned char*)"text") == 0)
+	for(node1 = ruleset_node->children; node1; node1 = node1->next) {
+		if(xmlStrcasecmp(node1->name, (unsigned char *)"text") == 0)
 			continue;
 
 		/* process conditions */
@@ -86,85 +86,83 @@ xmlNodePtr get_rule_node(subs_t* subs, xmlDocPtr xcap_tree)
 		LM_DBG("cond_node->name= %s\n", cond_node->name);
 
 		validity_node = xmlNodeGetChildByName(cond_node, "validity");
-		if (validity_node != NULL) {
+		if(validity_node != NULL) {
 			LM_DBG("found validity tag\n");
 
-			t= time(NULL);
+			t = time(NULL);
 
 			/* search all from-until pair */
-			for (time_node = validity_node->children; time_node;
+			for(time_node = validity_node->children; time_node;
 					time_node = time_node->next) {
-				if (xmlStrcasecmp(time_node->name, (unsigned char*)"from")!= 0)
+				if(xmlStrcasecmp(time_node->name, (unsigned char *)"from") != 0)
 					continue;
 
-				time_cont= (char*)xmlNodeGetContent(time_node);
-				t_init= xml_parse_dateTime(time_cont);
+				time_cont = (char *)xmlNodeGetContent(time_node);
+				t_init = xml_parse_dateTime(time_cont);
 				xmlFree(time_cont);
-				if (t_init< 0) {
+				if(t_init < 0) {
 					LM_ERR("failed to parse xml dateTime\n");
 					goto error;
 				}
 
-				if (t< t_init) {
+				if(t < t_init) {
 					LM_DBG("the lower time limit is not respected\n");
 					continue;
 				}
 
-				time_node= time_node->next;
-				while (1) {
-					if (time_node == NULL) {
+				time_node = time_node->next;
+				while(1) {
+					if(time_node == NULL) {
 						LM_ERR("bad formatted xml doc:until child not found in"
-								" validity pair\n");
+							   " validity pair\n");
 						goto error;
 					}
-					if( xmlStrcasecmp(time_node->name,
-								(unsigned char*)"until")== 0)
+					if(xmlStrcasecmp(time_node->name, (unsigned char *)"until")
+							== 0)
 						break;
-					time_node= time_node->next;
+					time_node = time_node->next;
 				}
 
-				time_cont = (char*)xmlNodeGetContent(time_node);
-				t_fin= xml_parse_dateTime(time_cont);
+				time_cont = (char *)xmlNodeGetContent(time_node);
+				t_fin = xml_parse_dateTime(time_cont);
 				xmlFree(time_cont);
 
-				if (t_fin< 0) {
+				if(t_fin < 0) {
 					LM_ERR("failed to parse xml dateTime\n");
 					goto error;
 				}
 
-				if (t <= t_fin) {
+				if(t <= t_fin) {
 					LM_DBG("the rule is active at this time\n");
-					valid= 1;
+					valid = 1;
 				}
-
 			}
 
-			if (!valid) {
+			if(!valid) {
 				LM_DBG("the rule is not active at this time\n");
 				continue;
 			}
-
 		}
 
 		identity_node = xmlNodeGetChildByName(cond_node, "identity");
-		if (identity_node == NULL) {
+		if(identity_node == NULL) {
 			LM_ERR("didn't find identity tag\n");
 			goto error;
 		}
 
 		iden_child = xmlNodeGetChildByName(identity_node, "one");
 		if(iden_child) {
-			for (node2 = identity_node->children; node2; node2 = node2->next) {
-				if(xmlStrcasecmp(node2->name, (unsigned char*)"one")!= 0)
+			for(node2 = identity_node->children; node2; node2 = node2->next) {
+				if(xmlStrcasecmp(node2->name, (unsigned char *)"one") != 0)
 					continue;
 
 				id = xmlNodeGetAttrContentByName(node2, "id");
-				if(id== NULL) {
+				if(id == NULL) {
 					LM_ERR("while extracting attribute\n");
 					goto error;
 				}
-				if ((strlen(id)== w_uri.len &&
-							(strncmp(id, w_uri.s, w_uri.len)==0))) {
+				if((strlen(id) == w_uri.len
+						   && (strncmp(id, w_uri.s, w_uri.len) == 0))) {
 					apply_rule = 1;
 					xmlFree(id);
 					break;
@@ -175,39 +173,39 @@ xmlNodePtr get_rule_node(subs_t* subs, xmlDocPtr xcap_tree)
 
 		/* search for many node*/
 		iden_child = xmlNodeGetChildByName(identity_node, "many");
-		if (iden_child)	{
+		if(iden_child) {
 			domain = NULL;
-			for (node2 = identity_node->children; node2; node2 = node2->next) {
-				if (xmlStrcasecmp(node2->name, (unsigned char*)"many") != 0)
+			for(node2 = identity_node->children; node2; node2 = node2->next) {
+				if(xmlStrcasecmp(node2->name, (unsigned char *)"many") != 0)
 					continue;
 
 				domain = xmlNodeGetAttrContentByName(node2, "domain");
 				if(domain == NULL) {
 					LM_DBG("No domain attribute to many\n");
-				} else	{
+				} else {
 					LM_DBG("<many domain= %s>\n", domain);
-					if((strlen(domain)!= subs->from_domain.len &&
-								strncmp(domain, subs->from_domain.s,
-									subs->from_domain.len) )) {
+					if((strlen(domain) != subs->from_domain.len
+							   && strncmp(domain, subs->from_domain.s,
+									   subs->from_domain.len))) {
 						xmlFree(domain);
 						continue;
 					}
 				}
 				xmlFree(domain);
 				apply_rule = 1;
-				if (node2->children == NULL)       /* there is no exception */
+				if(node2->children == NULL) /* there is no exception */
 					break;
 
-				for (except_node = node2->children; except_node;
-						except_node= except_node->next) {
-					if(xmlStrcasecmp(except_node->name,
-								(unsigned char*)"except"))
+				for(except_node = node2->children; except_node;
+						except_node = except_node->next) {
+					if(xmlStrcasecmp(
+							   except_node->name, (unsigned char *)"except"))
 						continue;
 
 					id = xmlNodeGetAttrContentByName(except_node, "id");
-					if (id != NULL) {
-						if((strlen(id)- 1== w_uri.len &&
-									(strncmp(id, w_uri.s, w_uri.len)==0))) {
+					if(id != NULL) {
+						if((strlen(id) - 1 == w_uri.len
+								   && (strncmp(id, w_uri.s, w_uri.len) == 0))) {
 							xmlFree(id);
 							apply_rule = 0;
 							break;
@@ -215,14 +213,16 @@ xmlNodePtr get_rule_node(subs_t* subs, xmlDocPtr xcap_tree)
 						xmlFree(id);
 					} else {
 						domain = NULL;
-						domain = xmlNodeGetAttrContentByName(except_node,
-								"domain");
-						if(domain!=NULL) {
-							LM_DBG("Found except domain= %s\n- strlen(domain)= %d\n",
+						domain = xmlNodeGetAttrContentByName(
+								except_node, "domain");
+						if(domain != NULL) {
+							LM_DBG("Found except domain= %s\n- strlen(domain)= "
+								   "%d\n",
 									domain, (int)strlen(domain));
-							if (strlen(domain)==subs->from_domain.len &&
-									(strncmp(domain,subs->from_domain.s ,
-											subs->from_domain.len)==0)) {
+							if(strlen(domain) == subs->from_domain.len
+									&& (strncmp(domain, subs->from_domain.s,
+												subs->from_domain.len)
+											== 0)) {
 								LM_DBG("except domain match\n");
 								xmlFree(domain);
 								apply_rule = 0;
@@ -232,19 +232,20 @@ xmlNodePtr get_rule_node(subs_t* subs, xmlDocPtr xcap_tree)
 						}
 					}
 				}
-				if (apply_rule == 1)  /* if a match was found no need to keep searching*/
+				if(apply_rule
+						== 1) /* if a match was found no need to keep searching*/
 					break;
 			}
 		}
-		if (apply_rule ==1)
+		if(apply_rule == 1)
 			break;
 	}
 
 	LM_DBG("apply_rule= %d\n", apply_rule);
-	if(w_uri.s!=NULL)
+	if(w_uri.s != NULL)
 		pkg_free(w_uri.s);
 
-	if( !apply_rule || !node1)
+	if(!apply_rule || !node1)
 		return NULL;
 
 	return node1;
@@ -255,36 +256,36 @@ error:
 	return NULL;
 }
 
-int pres_watcher_allowed(subs_t* subs)
+int pres_watcher_allowed(subs_t *subs)
 {
-	xmlDocPtr xcap_tree= NULL;
-	xmlNodePtr node= NULL,  actions_node = NULL;
+	xmlDocPtr xcap_tree = NULL;
+	xmlNodePtr node = NULL, actions_node = NULL;
 	xmlNodePtr sub_handling_node = NULL;
-	char* sub_handling = NULL;
+	char *sub_handling = NULL;
 
-	subs->status= PENDING_STATUS;
-	subs->reason.s= NULL;
-	subs->reason.len= 0;
+	subs->status = PENDING_STATUS;
+	subs->reason.s = NULL;
+	subs->reason.len = 0;
 
-	if (subs->auth_rules_doc== NULL)
+	if(subs->auth_rules_doc == NULL)
 		return 0;
 
-	xcap_tree= xmlParseMemory(subs->auth_rules_doc->s,
-			subs->auth_rules_doc->len);
-	if (xcap_tree== NULL) {
+	xcap_tree =
+			xmlParseMemory(subs->auth_rules_doc->s, subs->auth_rules_doc->len);
+	if(xcap_tree == NULL) {
 		LM_ERR("parsing xml memory\n");
 		return -1;
 	}
 
-	node= get_rule_node(subs, xcap_tree);
-	if (node== NULL) {
+	node = get_rule_node(subs, xcap_tree);
+	if(node == NULL) {
 		xmlFreeDoc(xcap_tree);
 		return 0;
 	}
 
 	/* process actions */
 	actions_node = xmlNodeGetChildByName(node, "actions");
-	if (actions_node == NULL) {
+	if(actions_node == NULL) {
 		LM_DBG("actions_node NULL\n");
 		xmlFreeDoc(xcap_tree);
 		return 0;
@@ -292,34 +293,34 @@ int pres_watcher_allowed(subs_t* subs)
 	LM_DBG("actions_node->name= %s\n", actions_node->name);
 
 	sub_handling_node = xmlNodeGetChildByName(actions_node, "sub-handling");
-	if (sub_handling_node== NULL) {
+	if(sub_handling_node == NULL) {
 		LM_DBG("sub_handling_node NULL\n");
 		xmlFreeDoc(xcap_tree);
 		return 0;
 	}
-	sub_handling = (char*)xmlNodeGetContent(sub_handling_node);
+	sub_handling = (char *)xmlNodeGetContent(sub_handling_node);
 	LM_DBG("sub_handling_node->name= %s\n", sub_handling_node->name);
 	LM_DBG("sub_handling_node->content= %s\n", sub_handling);
 
-	if (sub_handling == NULL) {
+	if(sub_handling == NULL) {
 		LM_ERR("Couldn't get sub-handling content\n");
 		xmlFreeDoc(xcap_tree);
 		return -1;
 	}
-	if (strncmp((char*)sub_handling, "block", 5) == 0) {
+	if(strncmp((char *)sub_handling, "block", 5) == 0) {
 		subs->status = TERMINATED_STATUS;
-		subs->reason.s= "rejected";
+		subs->reason.s = "rejected";
 		subs->reason.len = 8;
 	} else {
-		if (strncmp((char*)sub_handling, "confirm", 7) == 0) {
+		if(strncmp((char *)sub_handling, "confirm", 7) == 0) {
 			subs->status = PENDING_STATUS;
 		} else {
-			if (strncmp((char*)sub_handling , "polite-block", 12) == 0) {
+			if(strncmp((char *)sub_handling, "polite-block", 12) == 0) {
 				subs->status = ACTIVE_STATUS;
-				subs->reason.s= "polite-block";
+				subs->reason.s = "polite-block";
 				subs->reason.len = 12;
 			} else {
-				if (strncmp((char*)sub_handling, "allow", 5) == 0) {
+				if(strncmp((char *)sub_handling, "allow", 5) == 0) {
 					subs->status = ACTIVE_STATUS;
 					subs->reason.s = NULL;
 				} else {
@@ -336,10 +337,9 @@ int pres_watcher_allowed(subs_t* subs)
 	xmlFree(sub_handling);
 
 	return 0;
-
 }
 
-int get_rules_doc(str* user, str* domain, int type, str** rules_doc)
+int get_rules_doc(str *user, str *domain, int type, str **rules_doc)
 {
 	db_key_t query_cols[5];
 	db_val_t query_vals[5];
@@ -349,15 +349,15 @@ int get_rules_doc(str* user, str* domain, int type, str** rules_doc)
 	db_row_t *row;
 	db_val_t *row_vals;
 	str body;
-	str* doc= NULL;
-	int n_result_cols= 0, xcap_doc_col;
+	str *doc = NULL;
+	int n_result_cols = 0, xcap_doc_col;
 	static str tmp1 = str_init("username");
 	static str tmp2 = str_init("domain");
 	static str tmp3 = str_init("doc_type");
 	static str tmp4 = str_init("doc");
 
-	LM_DBG("[user]= %.*s\t[domain]= %.*s",
-			user->len, user->s, domain->len, domain->s);
+	LM_DBG("[user]= %.*s\t[domain]= %.*s", user->len, user->s, domain->len,
+			domain->s);
 
 	query_cols[n_query_cols] = &tmp1;
 	query_vals[n_query_cols].type = DB1_STR;
@@ -374,16 +374,17 @@ int get_rules_doc(str* user, str* domain, int type, str** rules_doc)
 	query_cols[n_query_cols] = &tmp3;
 	query_vals[n_query_cols].type = DB1_INT;
 	query_vals[n_query_cols].nul = 0;
-	query_vals[n_query_cols].val.int_val= type;
+	query_vals[n_query_cols].val.int_val = type;
 	n_query_cols++;
 
-	result_cols[xcap_doc_col= n_result_cols++] = &tmp4;
+	result_cols[xcap_doc_col = n_result_cols++] = &tmp4;
 
-	if (pres_dbf.query(pres_dbh, query_cols, 0 , query_vals, result_cols,
-				n_query_cols, 1, 0, &result) < 0) {
+	if(pres_dbf.query(pres_dbh, query_cols, 0, query_vals, result_cols,
+			   n_query_cols, 1, 0, &result)
+			< 0) {
 		LM_ERR("while querying table xcap for [user]=%.*s\t[domain]= %.*s\n",
 				user->len, user->s, domain->len, domain->s);
-		if (result)
+		if(result)
 			pres_dbf.free_result(pres_dbh, result);
 		return -1;
 	}
@@ -391,10 +392,10 @@ int get_rules_doc(str* user, str* domain, int type, str** rules_doc)
 	if(result == NULL)
 		return -1;
 
-	if (result->n <= 0) {
+	if(result->n <= 0) {
 		LM_DBG("No document found in db table for [user]=%.*s"
-				"\t[domain]= %.*s\t[doc_type]= %d\n",user->len, user->s,
-				domain->len, domain->s, type);
+			   "\t[domain]= %.*s\t[doc_type]= %d\n",
+				user->len, user->s, domain->len, domain->s, type);
 		pres_dbf.free_result(pres_dbh, result);
 		return 0;
 	}
@@ -402,43 +403,42 @@ int get_rules_doc(str* user, str* domain, int type, str** rules_doc)
 	row = &result->rows[xcap_doc_col];
 	row_vals = ROW_VALUES(row);
 
-	body.s = (char*)row_vals[0].val.string_val;
-	if (body.s== NULL) {
+	body.s = (char *)row_vals[0].val.string_val;
+	if(body.s == NULL) {
 		LM_ERR("Xcap doc NULL\n");
 		goto error;
 	}
 	body.len = strlen(body.s);
-	if (body.len== 0) {
+	if(body.len == 0) {
 		LM_ERR("Xcap doc empty\n");
 		goto error;
 	}
-	LM_DBG("xcap document:\n%.*s", body.len,body.s);
+	LM_DBG("xcap document:\n%.*s", body.len, body.s);
 
-	doc= (str*)pkg_malloc(sizeof(str));
-	if (doc== NULL) {
+	doc = (str *)pkg_malloc(sizeof(str));
+	if(doc == NULL) {
 		PKG_MEM_ERROR;
 	}
-	doc->s= (char*)pkg_malloc(body.len* sizeof(char));
-	if (doc->s== NULL) {
+	doc->s = (char *)pkg_malloc(body.len * sizeof(char));
+	if(doc->s == NULL) {
 		pkg_free(doc);
 		PKG_MEM_ERROR;
 	}
 	memcpy(doc->s, body.s, body.len);
-	doc->len= body.len;
+	doc->len = body.len;
 
-	*rules_doc= doc;
+	*rules_doc = doc;
 
-	if (result)
+	if(result)
 		pres_dbf.free_result(pres_dbh, result);
 
 	return 0;
 
 error:
-	if (result)
+	if(result)
 		pres_dbf.free_result(pres_dbh, result);
 
 	return -1;
-
 }
 
 
@@ -446,24 +446,24 @@ error:
  * Checks from presence server xcap table if watcher is authorized
  * to subscribe event 'presence' of presentity.
  */
-int ki_xcap_auth_status(sip_msg_t* _msg, str* watcher_uri, str* presentity_uri)
+int ki_xcap_auth_status(sip_msg_t *_msg, str *watcher_uri, str *presentity_uri)
 {
 	struct sip_uri uri;
-	str* rules_doc = NULL;
+	str *rules_doc = NULL;
 	subs_t subs;
 	int res;
 
-	if (pres_dbh == 0) {
+	if(pres_dbh == 0) {
 		LM_ERR("function is disabled, to enable define pres_db_url\n");
 		return -1;
 	}
 
-	if (parse_uri(presentity_uri->s, presentity_uri->len, &uri) < 0) {
+	if(parse_uri(presentity_uri->s, presentity_uri->len, &uri) < 0) {
 		LM_ERR("failed to parse presentity uri\n");
 		return -1;
 	}
 	res = get_rules_doc(&uri.user, &uri.host, PRES_RULES, &rules_doc);
-	if ((res < 0) || (rules_doc == NULL) || (rules_doc->s == NULL)) {
+	if((res < 0) || (rules_doc == NULL) || (rules_doc->s == NULL)) {
 		LM_DBG("no xcap rules doc found for presentity uri\n");
 		if(rules_doc) {
 			if(rules_doc->s) {
@@ -474,7 +474,7 @@ int ki_xcap_auth_status(sip_msg_t* _msg, str* watcher_uri, str* presentity_uri)
 		return PENDING_STATUS;
 	}
 
-	if (parse_uri(watcher_uri->s, watcher_uri->len, &uri) < 0) {
+	if(parse_uri(watcher_uri->s, watcher_uri->len, &uri) < 0) {
 		LM_ERR("failed to parse watcher uri\n");
 		goto err;
 	}
@@ -483,14 +483,13 @@ int ki_xcap_auth_status(sip_msg_t* _msg, str* watcher_uri, str* presentity_uri)
 	subs.from_domain = uri.host;
 	subs.pres_uri = *presentity_uri;
 	subs.auth_rules_doc = rules_doc;
-	if (pres_watcher_allowed(&subs) < 0) {
+	if(pres_watcher_allowed(&subs) < 0) {
 		LM_ERR("getting status from rules document\n");
 		goto err;
 	}
 	LM_DBG("auth status of watcher <%.*s> on presentity <%.*s> is %d\n",
-			watcher_uri->len, watcher_uri->s,
-			presentity_uri->len, presentity_uri->s,
-			subs.status);
+			watcher_uri->len, watcher_uri->s, presentity_uri->len,
+			presentity_uri->s, subs.status);
 	pkg_free(rules_doc->s);
 	pkg_free(rules_doc);
 	return subs.status;
@@ -501,15 +500,15 @@ err:
 	return -1;
 }
 
-int w_xcap_auth_status(struct sip_msg* _msg, char* _sp1, char* _sp2)
+int w_xcap_auth_status(struct sip_msg *_msg, char *_sp1, char *_sp2)
 {
 	str watcher_uri, presentity_uri;
 
-	if(fixup_get_svalue(_msg, (gparam_t*)_sp1, &watcher_uri)<0) {
+	if(fixup_get_svalue(_msg, (gparam_t *)_sp1, &watcher_uri) < 0) {
 		LM_ERR("cannot get the watcher uri\n");
 		return -1;
 	}
-	if(fixup_get_svalue(_msg, (gparam_t*)_sp2, &presentity_uri)<0) {
+	if(fixup_get_svalue(_msg, (gparam_t *)_sp2, &presentity_uri) < 0) {
 		LM_ERR("cannot get the presentity uri\n");
 		return -1;
 	}

+ 2 - 2
src/modules/utils/xcap_auth.h

@@ -39,8 +39,8 @@
  * Checks from presence server xcap table if watcher is authorized
  * to subscribe event 'presence' of presentity.
  */
-int w_xcap_auth_status(struct sip_msg* _msg, char* _sp1, char* _sp2);
+int w_xcap_auth_status(struct sip_msg *_msg, char *_sp1, char *_sp2);
 
-int ki_xcap_auth_status(sip_msg_t* _msg, str* watcher_uri, str* presentity_uri);
+int ki_xcap_auth_status(sip_msg_t *_msg, str *watcher_uri, str *presentity_uri);
 
 #endif /* XCAP_AUTH_FUNCTIONS_H */