[sr-dev] git:3.2: modules_k/rls: srand() called frequently

Peter Dunkley peter.dunkley at crocodile-rcs.com
Wed Apr 18 18:40:03 CEST 2012


Module: sip-router
Branch: 3.2
Commit: 4e0865a9fa502b8e93cbb7b9c3fb07dd3e38bf8a
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=4e0865a9fa502b8e93cbb7b9c3fb07dd3e38bf8a

Author: Peter Dunkley <peter.dunkley at crocodile-rcs.com>
Committer: Peter Dunkley <peter.dunkley at crocodile-rcs.com>
Date:   Wed Apr 18 17:18:45 2012 +0100

modules_k/rls: srand() called frequently

- srand() was used to ensure that the instance IDs for use in
  RLS NOTIFY bodies were random, but consistent.
- The instance ID needs to be unique within a resource node in the
  NOTIFY body.  It does not have to be unique locally.  Although
  it needs to be unique it also needs to be repeatable as the same
  resource instance needs the same ID in subsequent NOTIFY requests.
- You will only get multiple instances for a resource when the
  back-end SUBSCRIBEs from RLS are forked.  Kamailio does not support
  this at the moment (the callback function in pua/send_subscribe.c
  is only called for the first final response) so we will only ever
  have one instance per resource.
- Because there is only one instance per resource the instance ID
  can just be a fixed string.  This will need to be changed if/when
  forking of back-end SUBSCRIBEs from PUA is supported.
(cherry picked from commit c4a4a94065374fd182fc13374a0c520afa410f45)

---

 modules_k/rls/notify.c          |   74 +++++++++++++++++++++------------------
 modules_k/rls/notify.h          |    4 ++-
 modules_k/rls/resource_notify.c |   18 +++++++---
 3 files changed, 56 insertions(+), 40 deletions(-)

diff --git a/modules_k/rls/notify.c b/modules_k/rls/notify.c
index 6cc3226..4a0e9b3 100644
--- a/modules_k/rls/notify.c
+++ b/modules_k/rls/notify.c
@@ -83,6 +83,8 @@ int rls_get_resource_list(str *rl_uri, str *username, str *domain,
 int add_resource_to_list(char* uri, void* param);
 int add_resource(char* uri, xmlNodePtr list_node, char * boundary_string, db1_res_t *result, int *len_est);
 
+char *instance_id = "Scf8UhwQ";
+
 int send_full_notify(subs_t* subs, xmlNodePtr rl_node, str* rl_uri,
 		unsigned int hash_code)
 {
@@ -165,7 +167,7 @@ int send_full_notify(subs_t* subs, xmlNodePtr rl_node, str* rl_uri,
 		goto error;
 	}
 
-	boundary_string= generate_string((int)time(NULL), BOUNDARY_STRING_LEN);
+	boundary_string= generate_string(BOUNDARY_STRING_LEN);
 	
 	while (uri_list_head)
 	{
@@ -371,12 +373,11 @@ int add_resource_instance(char* uri, xmlNodePtr resource_node,
 	db_val_t *row_vals;
 	int i, cmp_code;
 	char* auth_state= NULL;
-	int contor= 0;
 	int auth_state_flag;
-    int boundary_len = strlen(boundary_string);
-    str cid;
-  	str content_type= {0, 0};
-    str body= {0, 0};
+	int boundary_len = strlen(boundary_string);
+	str cid;
+	str content_type= {0, 0};
+	str body= {0, 0};
 
 	for(i= 0; i< result->n; i++)
 	{
@@ -390,8 +391,6 @@ int add_resource_instance(char* uri, xmlNodePtr resource_node,
 
 		if(cmp_code== 0)
 		{
-			contor++;
-		
 			auth_state_flag= row_vals[auth_state_col].val.int_val;
 			auth_state= get_auth_string(auth_state_flag );
 			if(auth_state== NULL)
@@ -399,17 +398,17 @@ int add_resource_instance(char* uri, xmlNodePtr resource_node,
 				LM_ERR("bad authorization status flag\n");
 				goto error;
 			}
-            *len_est += strlen(auth_state) + 38; /* <instance id="12345678" state="[auth_state]" />r/n */
+			*len_est += strlen(auth_state) + 38; /* <instance id="12345678" state="[auth_state]" />r/n */
 
 			if(auth_state_flag & ACTIVE_STATE)
 			{
 				cid.s= generate_cid(uri, strlen(uri));
 				cid.len= strlen(cid.s);
-                body.s= (char*)row_vals[pres_state_col].val.string_val;
-                body.len= strlen(body.s);
-                trim(&body);
+				body.s= (char*)row_vals[pres_state_col].val.string_val;
+				body.len= strlen(body.s);
+				trim(&body);
 
-                *len_est += cid.len + 8; /* cid="[cid]" */
+				*len_est += cid.len + 8; /* cid="[cid]" */
 				content_type.s = (char*)row_vals[content_type_col].val.string_val;
 				content_type.len = strlen(content_type.s);
 				*len_est += 4 + boundary_len
@@ -418,18 +417,17 @@ int add_resource_instance(char* uri, xmlNodePtr resource_node,
 						 + 18 + content_type.len
 						 + 4 + body.len + 8;
 			}
-			else
-			if(auth_state_flag & TERMINATED_STATE)
-				{
-			    *len_est += strlen(row_vals[resource_uri_col].val.string_val) + 10; /* reason="[resaon]" */
+			else if(auth_state_flag & TERMINATED_STATE)
+			{
+				*len_est += strlen(row_vals[resource_uri_col].val.string_val) + 10; /* reason="[resaon]" */
 			}
-            if (rls_max_notify_body_len > 0 && *len_est > rls_max_notify_body_len)
-            {
-                /* We have a limit on body length set, and we were about to exceed it */
-                return *len_est;
+			if (rls_max_notify_body_len > 0 && *len_est > rls_max_notify_body_len)
+			{
+				/* We have a limit on body length set, and we were about to exceed it */
+				return *len_est;
 			}
             
-            instance_node= xmlNewChild(resource_node, NULL, 
+			instance_node= xmlNewChild(resource_node, NULL, 
 					BAD_CAST "instance", NULL);
 			if(instance_node== NULL)
 			{
@@ -437,15 +435,24 @@ int add_resource_instance(char* uri, xmlNodePtr resource_node,
 				goto error;
 			}
 		
-            /* OK, we are happy this will fit */
+			/* OK, we are happy this will fit */
+			/* Instance ID should be unique for each instance node
+			   within a resource node.  The same instance ID can be
+			   used in different resource nodes.  Instance ID needs
+			   to remain the same for each resource instance in
+			   future updates.  We can just use a common string
+			   here because you will only get multiple instances
+			   for a resource when the back-end SUBSCRIBE is forked
+			   and pua does not support this.  If/when pua supports
+			   forking of the SUBSCRIBEs it sends this will need to
+			   be fixed properly. */
 			xmlNewProp(instance_node, BAD_CAST "id",
-					BAD_CAST generate_string(contor, 8));
+					BAD_CAST instance_id);
 			xmlNewProp(instance_node, BAD_CAST "state", BAD_CAST auth_state);
 
 			if(auth_state_flag & ACTIVE_STATE)
 			{
-                constr_multipart_body (&content_type, &body, &cid, boundary_len, boundary_string);
-
+				constr_multipart_body (&content_type, &body, &cid, boundary_len, boundary_string);
 				xmlNewProp(instance_node, BAD_CAST "cid", BAD_CAST cid.s);
 			}
 			else
@@ -1029,26 +1036,25 @@ int process_list_and_exec(xmlNodePtr list_node, str username, str domain,
 	return res;
 }
 
-char* generate_string(int seed, int length)
+char* generate_string(int length)
 {
 	static char buf[128];
-    int r,i;
+	int r,i;
 
-    if(length>= 128)
+	if(length>= 128)
 	{
 		LM_ERR("requested length exceeds buffer size\n");
 		return NULL;
 	}
-	srand(seed); 
 		
 	for(i=0; i<length; i++) 
 	{
 		r= rand() % ('z'- 'A') + 'A';
-	    if(r>'Z' && r< 'a')
-			r= '0'+ (r- 'Z');
+		if(r>'Z' && r< 'a')
+		r= '0'+ (r- 'Z');
 
-        sprintf(buf+i, "%c", r);
-    }
+		sprintf(buf+i, "%c", r);
+	}
 	buf[length]= '\0';
 
 	return buf;
diff --git a/modules_k/rls/notify.h b/modules_k/rls/notify.h
index c5c2ac4..04a3e80 100644
--- a/modules_k/rls/notify.h
+++ b/modules_k/rls/notify.h
@@ -53,11 +53,13 @@ typedef int (*list_func_t)(char* uri, void* param);
 
 int process_list_and_exec(xmlNodePtr list, str username, str domain,
 		list_func_t function, void* param);
-char* generate_string(int seed, int length);
+char* generate_string(int length);
 char* generate_cid(char* uri, int uri_len);
 char* get_auth_string(int flag);
 int agg_body_sendn_update(str* rl_uri, char* boundary_string, str* rlmi_body,
 		str* multipart_body, subs_t* subs, unsigned int hash_code);
 int rls_send_notify(subs_t* subs,str* body,char* start_cid,char* boundary_string);
 int create_empty_rlmi_doc(xmlDocPtr *rlmi_doc, xmlNodePtr *list_node, str *uri, int version, int full_state);
+
+extern char *instance_id;
 #endif
diff --git a/modules_k/rls/resource_notify.c b/modules_k/rls/resource_notify.c
index b63aca8..0ab3bbf 100644
--- a/modules_k/rls/resource_notify.c
+++ b/modules_k/rls/resource_notify.c
@@ -152,7 +152,7 @@ static void send_notifies(db1_res_t *result, int did_col, int resource_uri_col,
 	char* buf= NULL, *auth_state= NULL, *boundary_string= NULL;
 	str cid = {0,0};
 	str content_type= {0, 0};
-	int contor= 0, auth_state_flag;
+	int auth_state_flag;
 	int chunk_len=0;
 	str bstr= {0, 0};
 	subs_t* dialog= NULL;
@@ -160,7 +160,7 @@ static void send_notifies(db1_res_t *result, int did_col, int resource_uri_col,
 	int resource_added = 0; /* Flag to indicate that we have added at least one resource */
 
 	/* generate the boundary string */
-	boundary_string= generate_string((int)time(NULL), BOUNDARY_STRING_LEN);
+	boundary_string= generate_string(BOUNDARY_STRING_LEN);
 	bstr.len= strlen(boundary_string);
 	bstr.s= (char*)pkg_malloc((bstr.len+ 1)* sizeof(char));
 	if(bstr.s== NULL)
@@ -242,10 +242,8 @@ static void send_notifies(db1_res_t *result, int did_col, int resource_uri_col,
 		/* there might be more records with the same uri- more instances-
 		 * search and add them all */
 		
-		contor= 0;
 		while(1)
 		{
-			contor++;
 			cid.s= NULL;
 			cid.len= 0;
 			
@@ -311,8 +309,18 @@ static void send_notifies(db1_res_t *result, int did_col, int resource_uri_col,
 				goto error;
 			}	
 
+			/* Instance ID should be unique for each instance node
+ 			   within a resource node.  The same instance ID can be
+			   used in different resource nodes.  Instance ID needs
+			   to remain the same for each resource instance in
+			   future updates.  We can just use a common string
+			   here because you will only get multiple instances
+			   for a resource when the back-end SUBSCRIBE is forked
+			   and pua does not support this.  If/when pua supports
+			   forking of the SUBSCRIBEs it sends this will need to
+			   be fixed properly. */
 			xmlNewProp(instance_node, BAD_CAST "id", 
-					BAD_CAST generate_string(contor, 8));
+					BAD_CAST instance_id);
 			if(auth_state_flag & ACTIVE_STATE)
 			{
 				xmlNewProp(instance_node, BAD_CAST "state", BAD_CAST auth_state);




More information about the sr-dev mailing list