Browse code

presence: add sip_uri_match module parameter

added param sip_uri_match
0 - case sensitive (default)
1 - case insensitive

can be extended to a more compliant version, (sensitive user, insensitive domain).
the parameter sets the function to call to match uris.

Luis Azedo authored on 27/03/2015 01:19:29
Showing 7 changed files
... ...
@@ -756,6 +756,35 @@ modparam("presence", "retrieve_order", 1)
756 756
 	    </example>
757 757
 	</section>
758 758
 
759
+<section id="presence.p.sip_uri_match">
760
+    <title><varname>sip_uri_match</varname> (int)</title>
761
+    <para>
762
+        The mode used when comparing uris.
763
+    </para>
764
+    <para>
765
+        <itemizedlist>
766
+            <title>Possible Values</title>
767
+            <listitem>
768
+                <para> 0 : case sensitive</para>
769
+            </listitem>
770
+            <listitem>
771
+                <para> 1 : case insensitive</para>
772
+            </listitem>
773
+        </itemizedlist>
774
+    </para>
775
+    <para>
776
+        <emphasis>Default value is <quote>0</quote>.</emphasis>
777
+    </para>
778
+    <example>
779
+        <title>Set <varname>sip_uri_match</varname> parameter</title>
780
+        <programlisting format="linespecific">
781
+            ...
782
+            modparam("presence", "sip_uri_match", 1)
783
+            ...
784
+        </programlisting>
785
+    </example>
786
+</section>
787
+
759 788
 </section>
760 789
 
761 790
 <section>
... ...
@@ -375,7 +375,7 @@ int update_shtable(shtable_t htable,unsigned int hash_code,
375 375
 		subs->version = ++s->version;
376 376
 	}
377 377
 	
378
-	if(strncmp(s->contact.s, subs->contact.s, subs->contact.len))
378
+	if(presence_sip_uri_match(&s->contact, &subs->contact))
379 379
 	{
380 380
 		shm_free(s->contact.s);
381 381
 		s->contact.s= (char*)shm_malloc(subs->contact.len* sizeof(char));
... ...
@@ -483,7 +483,7 @@ pres_entry_t* search_phtable(str* pres_uri,int event, unsigned int hash_code)
483 483
 	while(p)
484 484
 	{
485 485
 		if(p->event== event && p->pres_uri.len== pres_uri->len &&
486
-				strncmp(p->pres_uri.s, pres_uri->s, pres_uri->len)== 0 )
486
+				presence_sip_uri_match(&p->pres_uri, pres_uri)== 0 )
487 487
 			return p;
488 488
 		p= p->next;
489 489
 	}
... ...
@@ -496,7 +496,7 @@ int insert_phtable(str* pres_uri, int event, char* sphere)
496 496
 	pres_entry_t* p= NULL;
497 497
 	int size;
498 498
 
499
-	hash_code= core_hash(pres_uri, NULL, phtable_size);
499
+	hash_code= core_case_hash(pres_uri, NULL, phtable_size);
500 500
 
501 501
 	lock_get(&pres_htable[hash_code].lock);
502 502
 	
... ...
@@ -554,7 +554,7 @@ int delete_phtable(str* pres_uri, int event)
554 554
 	unsigned int hash_code;
555 555
 	pres_entry_t* p= NULL, *prev_p= NULL;
556 556
 
557
-	hash_code= core_hash(pres_uri, NULL, phtable_size);
557
+	hash_code= core_case_hash(pres_uri, NULL, phtable_size);
558 558
 
559 559
 	lock_get(&pres_htable[hash_code].lock);
560 560
 	
... ...
@@ -611,7 +611,7 @@ int update_phtable(presentity_t* presentity, str pres_uri, str body)
611 611
 	}
612 612
 
613 613
 	/* search for record in hash table */
614
-	hash_code= core_hash(&pres_uri, NULL, phtable_size);
614
+	hash_code= core_case_hash(&pres_uri, NULL, phtable_size);
615 615
 	
616 616
 	lock_get(&pres_htable[hash_code].lock);
617 617
 
... ...
@@ -371,7 +371,7 @@ str* get_wi_notify_body(subs_t* subs, subs_t* watcher_subs)
371 371
 			goto error;
372 372
 		}
373 373
 	} else {
374
-		hash_code= core_hash(&subs->pres_uri, &subs->event->wipeer->name,
374
+		hash_code= core_case_hash(&subs->pres_uri, &subs->event->wipeer->name,
375 375
 				shtable_size);
376 376
 		lock_get(&subs_htable[hash_code].lock);
377 377
 		s= subs_htable[hash_code].entries;
... ...
@@ -387,7 +387,7 @@ str* get_wi_notify_body(subs_t* subs, subs_t* watcher_subs)
387 387
 
388 388
 			if(s->event== subs->event->wipeer &&
389 389
 				s->pres_uri.len== subs->pres_uri.len &&
390
-				strncmp(s->pres_uri.s, subs->pres_uri.s,subs->pres_uri.len)== 0)
390
+				presence_sip_uri_match(&s->pres_uri, &subs->pres_uri)== 0)
391 391
 			{
392 392
 				if(add_watcher_list(s, watchers)< 0)
393 393
 				{
... ...
@@ -609,7 +609,7 @@ str* get_p_notify_body(str pres_uri, pres_ev_t* event, str* etag,
609 609
 	if( publ_cache_enabled )
610 610
 	{
611 611
 		/* search in hash table if any record exists */
612
-		hash_code= core_hash(&pres_uri, NULL, phtable_size);
612
+		hash_code= core_case_hash(&pres_uri, NULL, phtable_size);
613 613
 		if(search_phtable(&pres_uri, event->evp->type, hash_code)== NULL)
614 614
 		{
615 615
 			LM_DBG("No record exists in hash_table\n");
... ...
@@ -701,7 +701,7 @@ str* get_p_notify_body(str pres_uri, pres_ev_t* event, str* etag,
701 701
 				sender.len= strlen(sender.s);
702 702
 			
703 703
 				if(sender.len== contact->len &&
704
-						strncmp(sender.s, contact->s, sender.len)== 0)
704
+						presence_sip_uri_match(&sender, contact)== 0)
705 705
 				{
706 706
 					notify_body= build_empty_bla_body(pres_uri);
707 707
 					pa_dbf.free_result(pa_db, result);
... ...
@@ -1188,7 +1188,7 @@ subs_t* get_subs_dialog(str* pres_uri, pres_ev_t* event, str* sender)
1188 1188
 			goto error;
1189 1189
 		}
1190 1190
 	}else {
1191
-		hash_code= core_hash(pres_uri, &event->name, shtable_size);
1191
+		hash_code= core_case_hash(pres_uri, &event->name, shtable_size);
1192 1192
 		
1193 1193
 		lock_get(&subs_htable[hash_code].lock);
1194 1194
 
... ...
@@ -1209,9 +1209,9 @@ subs_t* get_subs_dialog(str* pres_uri, pres_ev_t* event, str* sender)
1209 1209
 			if((!(s->status== ACTIVE_STATUS &&
1210 1210
 		    s->reason.len== 0 &&
1211 1211
 				s->event== event && s->pres_uri.len== pres_uri->len &&
1212
-				strncmp(s->pres_uri.s, pres_uri->s, pres_uri->len)== 0)) || 
1212
+				presence_sip_uri_match(&s->pres_uri, pres_uri)== 0)) || 
1213 1213
 				(sender && sender->len== s->contact.len && 
1214
-				strncmp(sender->s, s->contact.s, sender->len)== 0))
1214
+				presence_sip_uri_match(sender, &s->contact)== 0))
1215 1215
 				continue;
1216 1216
 
1217 1217
 			s_new= mem_copy_subs(s, PKG_MEM_TYPE);
... ...
@@ -1650,7 +1650,7 @@ int notify(subs_t* subs, subs_t * watcher_subs,str* n_body,int force_null_body)
1650 1650
 	if(subs->expires!= 0 && subs->status != TERMINATED_STATUS)
1651 1651
 	{
1652 1652
 		unsigned int hash_code;
1653
-		hash_code= core_hash(&subs->pres_uri, &subs->event->name, shtable_size);
1653
+		hash_code= core_case_hash(&subs->pres_uri, &subs->event->name, shtable_size);
1654 1654
 
1655 1655
 		/* if subscriptions are held also in memory, update the subscription hashtable */
1656 1656
 		if(subs_dbmode != DB_ONLY)
... ...
@@ -1885,7 +1885,7 @@ int watcher_found_in_list(watcher_t * watchers, str wuri)
1885 1885
 
1886 1886
 	while(w)
1887 1887
 	{
1888
-		if(w->uri.len == wuri.len && strncmp(w->uri.s, wuri.s, wuri.len)== 0)
1888
+		if(w->uri.len == wuri.len && presence_sip_uri_match(&w->uri, &wuri)== 0)
1889 1889
 			return 1;
1890 1890
 		w= w->next;
1891 1891
 	}
... ...
@@ -2262,7 +2262,7 @@ int set_wipeer_subs_updated(str *pres_uri, pres_ev_t *event, int full)
2262 2262
 		update_vals[n_update_cols].type = DB1_INT;
2263 2263
 		update_vals[n_update_cols].nul = 0;
2264 2264
 		update_vals[n_update_cols].val.int_val =
2265
-			core_hash(&callid, &from_tag, 0) % (pres_waitn_time *
2265
+			core_case_hash(&callid, &from_tag, 0) % (pres_waitn_time *
2266 2266
  			 pres_notifier_poll_rate * pres_notifier_processes);
2267 2267
 		n_update_cols++;
2268 2268
 
... ...
@@ -2323,7 +2323,7 @@ int set_updated(subs_t *sub)
2323 2323
 	update_vals[0].type = DB1_INT;
2324 2324
 	update_vals[0].nul = 0;
2325 2325
 	update_vals[0].val.int_val =
2326
-		core_hash(&sub->callid, &sub->from_tag, 0) %
2326
+		core_case_hash(&sub->callid, &sub->from_tag, 0) %
2327 2327
 			(pres_waitn_time * pres_notifier_poll_rate
2328 2328
 						* pres_notifier_processes);
2329 2329
 
... ...
@@ -97,6 +97,12 @@ str server_address= {0, 0};
97 97
 evlist_t* EvList= NULL;
98 98
 int pres_subs_remove_match = 0;
99 99
 
100
+/* sip uri match */
101
+sip_uri_match_f presence_sip_uri_match;
102
+static int sip_uri_case_sensitive_match(str* s1, str* s2);
103
+static int sip_uri_case_insensitive_match(str* s1, str* s2);
104
+int pres_uri_match = 0;
105
+
100 106
 /* to tag prefix */
101 107
 char* to_tag_pref = "10";
102 108
 
... ...
@@ -212,6 +218,7 @@ static param_export_t params[]={
212 212
 	{ "subs_remove_match",      PARAM_INT, &pres_subs_remove_match},
213 213
 	{ "xavp_cfg",               PARAM_STR, &pres_xavp_cfg},
214 214
 	{ "retrieve_order",         PARAM_INT, &pres_retrieve_order},
215
+	{ "sip_uri_match",          PARAM_INT, &pres_uri_match},
215 216
     {0,0,0}
216 217
 };
217 218
 
... ...
@@ -242,6 +249,12 @@ struct module_exports exports= {
242 242
  */
243 243
 static int mod_init(void)
244 244
 {
245
+	if(pres_uri_match == 1) {
246
+		presence_sip_uri_match = sip_uri_case_insensitive_match;
247
+	} else {
248
+		presence_sip_uri_match = sip_uri_case_sensitive_match;
249
+	}
250
+
245 251
 	if(register_mi_mod(exports.name, mi_cmds)!=0)
246 252
 	{
247 253
 		LM_ERR("failed to register MI commands\n");
... ...
@@ -1012,7 +1025,7 @@ int update_watchers_status(str pres_uri, pres_ev_t* ev, str* rules_doc)
1012 1012
 	}
1013 1013
 
1014 1014
 	LM_DBG("found %d record-uri in watchers_table\n", result->n);
1015
-	hash_code= core_hash(&pres_uri, &ev->name, shtable_size);
1015
+	hash_code= core_case_hash(&pres_uri, &ev->name, shtable_size);
1016 1016
 	subs.db_flag= hash_code;
1017 1017
 
1018 1018
 	/*must do a copy as sphere_check requires database queries */
... ...
@@ -1433,7 +1446,7 @@ static int update_pw_dialogs_dbonlymode(subs_t* subs, subs_t** subs_array)
1433 1433
 					* pres_notifier_processes));
1434 1434
 	} else {
1435 1435
 		db_vals[n_update_cols].val.int_val = 
1436
-			core_hash(&subs->callid, &subs->from_tag, 0) %
1436
+			core_case_hash(&subs->callid, &subs->from_tag, 0) %
1437 1437
 				  (pres_waitn_time * pres_notifier_poll_rate
1438 1438
 					* pres_notifier_processes);
1439 1439
 	}
... ...
@@ -1472,9 +1485,9 @@ static int update_pw_dialogs(subs_t* subs, unsigned int hash_code, subs_t** subs
1472 1472
 		if(s->event== subs->event && s->pres_uri.len== subs->pres_uri.len &&
1473 1473
 			s->watcher_user.len== subs->watcher_user.len && 
1474 1474
 			s->watcher_domain.len==subs->watcher_domain.len &&
1475
-			strncmp(s->pres_uri.s, subs->pres_uri.s, subs->pres_uri.len)== 0 &&
1476
-			strncmp(s->watcher_user.s, subs->watcher_user.s, s->watcher_user.len)== 0 &&
1477
-			strncmp(s->watcher_domain.s,subs->watcher_domain.s,s->watcher_domain.len)==0)
1475
+			presence_sip_uri_match(&s->pres_uri, &subs->pres_uri)== 0 &&
1476
+			presence_sip_uri_match(&s->watcher_user, &subs->watcher_user)== 0 &&
1477
+			presence_sip_uri_match(&s->watcher_domain, &subs->watcher_domain)==0)
1478 1478
 		{
1479 1479
 			i++;
1480 1480
 			s->status= subs->status;
... ...
@@ -1840,3 +1853,29 @@ static int presence_init_rpc(void)
1840 1840
 	}
1841 1841
 	return 0;
1842 1842
 }
1843
+
1844
+static int sip_uri_case_sensitive_match(str* s1, str* s2)
1845
+{
1846
+	if(!s1) {
1847
+		LM_ERR("null pointer (s1) in sip_uri_match\n");
1848
+		return -1;
1849
+	}
1850
+	if(!s2) {
1851
+		LM_ERR("null pointer (s2) in sip_uri_match\n");
1852
+		return -1;
1853
+	}
1854
+	return strncmp(s1->s, s2->s, s2->len);
1855
+}
1856
+
1857
+static int sip_uri_case_insensitive_match(str* s1, str* s2)
1858
+{
1859
+	if(!s1) {
1860
+		LM_ERR("null pointer (s1) in sip_uri_match\n");
1861
+		return -1;
1862
+	}
1863
+	if(!s2) {
1864
+		LM_ERR("null pointer (s2) in sip_uri_match\n");
1865
+		return -1;
1866
+	}
1867
+	return strncasecmp(s1->s, s2->s, s2->len);
1868
+}
... ...
@@ -99,4 +99,7 @@ extern db_locking_t db_table_lock;
99 99
 int update_watchers_status(str pres_uri, pres_ev_t* ev, str* rules_doc);
100 100
 int pres_auth_status(struct sip_msg* msg, str watcher_uri, str presentity_uri);
101 101
 
102
+typedef int (*sip_uri_match_f) (str* s1, str* s2);
103
+extern sip_uri_match_f presence_sip_uri_match;
104
+
102 105
 #endif /* PA_MOD_H */
... ...
@@ -550,7 +550,7 @@ int update_presentity(struct sip_msg* msg, presentity_t* presentity, str* body,
550 550
 			if(presentity->sender)
551 551
 			{
552 552
 				if(!(presentity->sender->len == sender.len && 
553
-				strncmp(presentity->sender->s, sender.s, sender.len)== 0))
553
+				presence_sip_uri_match(presentity->sender, &sender)== 0))
554 554
 					 bla_update_publish= 0;
555 555
 			}
556 556
 after_dialog_check:
... ...
@@ -1071,7 +1071,7 @@ char* get_sphere(str* pres_uri)
1071 1071
 	if ( publ_cache_enabled )
1072 1072
 	{
1073 1073
 		/* search in hash table*/
1074
-		hash_code= core_hash(pres_uri, NULL, phtable_size);
1074
+		hash_code= core_case_hash(pres_uri, NULL, phtable_size);
1075 1075
 
1076 1076
 		lock_get(&pres_htable[hash_code].lock);
1077 1077
 
... ...
@@ -463,7 +463,7 @@ void delete_subs(str* pres_uri, str* ev_name, str* to_tag,
463 463
 	/* delete record from hash table also if not in dbonly mode */
464 464
 	if(subs_dbmode != DB_ONLY)
465 465
 	{
466
-		unsigned int hash_code= core_hash(pres_uri, ev_name, shtable_size);
466
+		unsigned int hash_code= core_case_hash(pres_uri, ev_name, shtable_size);
467 467
 		if(delete_shtable(subs_htable, hash_code, &subs) < 0) {
468 468
 			LM_ERR("Failed to delete subscription from memory"
469 469
 					" [slot: %u ev: %.*s pu: %.*s ci: %.*s ft: %.*s tt: %.*s]\n",
... ...
@@ -486,7 +486,7 @@ int update_subscription_notifier(struct sip_msg* msg, subs_t* subs,
486 486
 	*sent_reply= 0;
487 487
 
488 488
 	/* Set the notifier/update fields for the subscription */
489
-	subs->updated = core_hash(&subs->callid, &subs->from_tag, 0) %
489
+	subs->updated = core_case_hash(&subs->callid, &subs->from_tag, 0) %
490 490
 				(pres_waitn_time * pres_notifier_poll_rate
491 491
 					* pres_notifier_processes);
492 492
 	if (subs->event->type & WINFO_TYPE)
... ...
@@ -606,7 +606,7 @@ int update_subscription(struct sip_msg* msg, subs_t* subs, int to_tag_gen,
606 606
 		/* if subscriptions are stored in memory, update them */
607 607
 		if(subs_dbmode != DB_ONLY)
608 608
 		{
609
-			hash_code= core_hash(&subs->pres_uri, &subs->event->name, shtable_size);
609
+			hash_code= core_case_hash(&subs->pres_uri, &subs->event->name, shtable_size);
610 610
 			if(update_shtable(subs_htable, hash_code, subs, REMOTE_TYPE)< 0)
611 611
 			{
612 612
 				LM_ERR("failed to update subscription in memory\n");
... ...
@@ -633,7 +633,7 @@ int update_subscription(struct sip_msg* msg, subs_t* subs, int to_tag_gen,
633 633
 			{
634 634
 				LM_DBG("inserting in shtable\n");
635 635
 				subs->db_flag = (subs_dbmode==WRITE_THROUGH)?WTHROUGHDB_FLAG:INSERTDB_FLAG;
636
-				hash_code= core_hash(&subs->pres_uri, &subs->event->name, shtable_size);
636
+				hash_code= core_case_hash(&subs->pres_uri, &subs->event->name, shtable_size);
637 637
 				subs->version = 0;
638 638
 				if(insert_shtable(subs_htable,hash_code,subs)< 0)
639 639
 				{
... ...
@@ -1367,7 +1367,7 @@ int get_stored_info(struct sip_msg* msg, subs_t* subs, int* reply_code,
1367 1367
 	else
1368 1368
 		pres_uri = subs->pres_uri;
1369 1369
 
1370
-	hash_code= core_hash(&pres_uri, &subs->event->name, shtable_size);
1370
+	hash_code= core_case_hash(&pres_uri, &subs->event->name, shtable_size);
1371 1371
 	lock_get(&subs_htable[hash_code].lock);
1372 1372
 	s= search_shtable(subs_htable, subs->callid, subs->to_tag,
1373 1373
 		subs->from_tag, hash_code);
... ...
@@ -2476,7 +2476,7 @@ int restore_db_subs(void)
2476 2476
 			s.sockinfo_str.s=(char*)row_vals[sockinfo_col].val.string_val;
2477 2477
 			s.sockinfo_str.len= strlen(s.sockinfo_str.s);
2478 2478
 			s.db_flag = (subs_dbmode==WRITE_THROUGH)?WTHROUGHDB_FLAG:NO_UPDATEDB_FLAG;
2479
-			hash_code= core_hash(&s.pres_uri, &s.event->name, shtable_size);
2479
+			hash_code= core_case_hash(&s.pres_uri, &s.event->name, shtable_size);
2480 2480
 			if(insert_shtable(subs_htable, hash_code, &s)< 0)
2481 2481
 			{
2482 2482
 				LM_ERR("adding new record in hash table\n");