Browse code

presence: skip body operations on empty string

Daniel-Constantin Mierla authored on 09/09/2021 08:27:01
Showing 1 changed files
... ...
@@ -1700,7 +1700,8 @@ jump_over_body:
1700 1700
 	}
1701 1701
 
1702 1702
 	/* build extra headers */
1703
-	if(build_str_hdr(subs, notify_body ? 1 : 0, &str_hdr) < 0) {
1703
+	if(build_str_hdr(subs, (notify_body && notify_body->len>0) ? 1 : 0,
1704
+				&str_hdr) < 0) {
1704 1705
 		LM_ERR("while building headers\n");
1705 1706
 		goto error;
1706 1707
 	}
Browse code

presence: implemented more functions to work with in-memory presentity records

Daniel-Constantin Mierla authored on 22/04/2020 19:01:38
Showing 1 changed files
... ...
@@ -891,7 +891,7 @@ str *ps_cache_get_p_notify_body(str pres_uri, pres_ev_t *event, str *etag,
891 891
 		ptm.expires = (int)time(NULL);
892 892
 	}
893 893
 
894
-	ptlist = ps_ptable_search(&ptm, pres_retrieve_order);
894
+	ptlist = ps_ptable_search(&ptm, 1, pres_retrieve_order);
895 895
 
896 896
 	if(ptlist == NULL) {
897 897
 		LM_DBG("the query returned no result\n[username]= %.*s"
Browse code

presence: function to search presentity items for in memory subscriptions

Daniel-Constantin Mierla authored on 22/04/2020 14:57:24
Showing 1 changed files
... ...
@@ -579,7 +579,8 @@ error:
579 579
 	return NULL;
580 580
 }
581 581
 
582
-str *get_p_notify_body(str pres_uri, pres_ev_t *event, str *etag, str *contact)
582
+str *ps_db_get_p_notify_body(str pres_uri, pres_ev_t *event, str *etag,
583
+		str *contact)
583 584
 {
584 585
 	db_key_t query_cols[4];
585 586
 	db_val_t query_vals[4];
... ...
@@ -862,6 +863,214 @@ error:
862 863
 	return NULL;
863 864
 }
864 865
 
866
+str *ps_cache_get_p_notify_body(str pres_uri, pres_ev_t *event, str *etag,
867
+		str *contact)
868
+{
869
+	sip_uri_t uri;
870
+	ps_presentity_t ptm;
871
+	ps_presentity_t *pti;
872
+	ps_presentity_t *ptlist = NULL;
873
+	int n = 0;
874
+	int i = 0;
875
+	str **body_array = NULL;
876
+	str *notify_body = NULL;
877
+	str *body;
878
+	int size = 0;
879
+	int build_off_n = -1;
880
+
881
+	if(parse_uri(pres_uri.s, pres_uri.len, &uri) < 0) {
882
+		LM_ERR("while parsing uri\n");
883
+		return NULL;
884
+	}
885
+	memset(&ptm, 0, sizeof(ps_presentity_t));
886
+
887
+	ptm.user = uri.user;
888
+	ptm.domain = uri.host;
889
+	ptm.event = event->name;
890
+	if(pres_startup_mode == 1) {
891
+		ptm.expires = (int)time(NULL);
892
+	}
893
+
894
+	ptlist = ps_ptable_search(&ptm, pres_retrieve_order);
895
+
896
+	if(ptlist == NULL) {
897
+		LM_DBG("the query returned no result\n[username]= %.*s"
898
+			   "\t[domain]= %.*s\t[event]= %.*s\n",
899
+				uri.user.len, uri.user.s, uri.host.len, uri.host.s,
900
+				event->name.len, event->name.s);
901
+
902
+		if(event->agg_nbody) {
903
+			notify_body = event->agg_nbody(&uri.user, &uri.host, NULL, 0, -1);
904
+			if(notify_body) {
905
+				goto done;
906
+			}
907
+		}
908
+		return NULL;
909
+	}
910
+
911
+	if(event->agg_nbody == NULL) {
912
+		LM_DBG("event does not require aggregation\n");
913
+		pti = ptlist;
914
+		while(pti->next) {
915
+			pti = pti->next;
916
+		}
917
+
918
+		/* if event BLA - check if sender is the same as contact */
919
+		/* if so, send an empty dialog info document */
920
+		if(EVENT_DIALOG_SLA(event->evp) && contact) {
921
+			if(pti->sender.s == NULL || pti->sender.len <= 0) {
922
+				LM_DBG("no sender address\n");
923
+				goto after_sender_check;
924
+			}
925
+
926
+			if(pti->sender.len == contact->len
927
+					&& presence_sip_uri_match(&pti->sender, contact) == 0) {
928
+				notify_body = build_empty_bla_body(pres_uri);
929
+				ps_presentity_list_free(ptlist, 1);
930
+				return notify_body;
931
+			}
932
+		}
933
+
934
+	after_sender_check:
935
+		if(pti->body.s == NULL || pti->body.len <= 0) {
936
+			LM_ERR("NULL notify body record\n");
937
+			goto error;
938
+		}
939
+
940
+		notify_body = (str *)pkg_malloc(sizeof(str));
941
+		if(notify_body == NULL) {
942
+			ERR_MEM(PKG_MEM_STR);
943
+		}
944
+		memset(notify_body, 0, sizeof(str));
945
+		notify_body->s = (char *)pkg_malloc((pti->body.len+1) * sizeof(char));
946
+		if(notify_body->s == NULL) {
947
+			pkg_free(notify_body);
948
+			ERR_MEM(PKG_MEM_STR);
949
+		}
950
+		memcpy(notify_body->s, pti->body.s, pti->body.len);
951
+		notify_body->len = pti->body.len;
952
+		ps_presentity_list_free(ptlist, 1);
953
+
954
+		return notify_body;
955
+	}
956
+
957
+	LM_DBG("event requires aggregation\n");
958
+
959
+	n = 0;
960
+	pti = ptlist;
961
+	while(pti) {
962
+		n++;
963
+		pti = pti->next;
964
+	}
965
+	body_array = (str **)pkg_malloc((n + 2) * sizeof(str *));
966
+	if(body_array == NULL) {
967
+		ERR_MEM(PKG_MEM_STR);
968
+	}
969
+	memset(body_array, 0, (n + 2) * sizeof(str *));
970
+
971
+	if(etag != NULL) {
972
+		LM_DBG("searched etag = %.*s len= %d\n", etag->len, etag->s,
973
+				etag->len);
974
+		LM_DBG("etag not NULL\n");
975
+		pti = ptlist;
976
+		i = 0;
977
+		while(pti) {
978
+			LM_DBG("etag = %.*s len= %d\n", pti->etag.len, pti->etag.s,
979
+					pti->etag.len);
980
+			if((pti->etag.len == etag->len)
981
+					&& (strncmp(pti->etag.s, etag->s, pti->etag.len) == 0)) {
982
+				LM_DBG("found etag\n");
983
+				build_off_n = i;
984
+			}
985
+			if(pti->body.s == NULL || pti->body.len <= 0) {
986
+				LM_ERR("Empty notify body record\n");
987
+				goto error;
988
+			}
989
+
990
+			size = sizeof(str) + (pti->body.len +1) * sizeof(char);
991
+			body = (str *)pkg_malloc(size);
992
+			if(body == NULL) {
993
+				ERR_MEM(PKG_MEM_STR);
994
+			}
995
+			memset(body, 0, size);
996
+			size = sizeof(str);
997
+			body->s = (char *)body + size;
998
+			memcpy(body->s, pti->body.s, pti->body.len);
999
+			body->len = pti->body.len;
1000
+
1001
+			body_array[i] = body;
1002
+			i++;
1003
+			pti = pti->next;
1004
+		}
1005
+	} else {
1006
+		pti = ptlist;
1007
+		i = 0;
1008
+		while(pti) {
1009
+			if(pti->body.s == NULL || pti->body.len <= 0) {
1010
+				LM_ERR("Empty notify body record\n");
1011
+				goto error;
1012
+			}
1013
+
1014
+			size = sizeof(str) + (pti->body.len+1) * sizeof(char);
1015
+			body = (str *)pkg_malloc(size);
1016
+			if(body == NULL) {
1017
+				ERR_MEM(PKG_MEM_STR);
1018
+			}
1019
+			memset(body, 0, size);
1020
+			size = sizeof(str);
1021
+			body->s = (char *)body + size;
1022
+			memcpy(body->s, pti->body.s, pti->body.len);
1023
+			body->len = pti->body.len;
1024
+
1025
+			body_array[i] = body;
1026
+			i++;
1027
+			pti = pti->next;
1028
+		}
1029
+	}
1030
+
1031
+	ps_presentity_list_free(ptlist, 1);
1032
+
1033
+	notify_body = event->agg_nbody(
1034
+			&uri.user, &uri.host, body_array, n, build_off_n);
1035
+
1036
+done:
1037
+	if(body_array != NULL) {
1038
+		for(i = 0; i < n; i++) {
1039
+			if(body_array[i]) {
1040
+				pkg_free(body_array[i]);
1041
+			}
1042
+		}
1043
+		pkg_free(body_array);
1044
+	}
1045
+	return notify_body;
1046
+
1047
+error:
1048
+	if(ptlist != NULL) {
1049
+		ps_presentity_list_free(ptlist, 1);
1050
+	}
1051
+
1052
+	if(body_array != NULL) {
1053
+		for(i = 0; i < n; i++) {
1054
+			if(body_array[i])
1055
+				pkg_free(body_array[i]);
1056
+			else
1057
+				break;
1058
+		}
1059
+
1060
+		pkg_free(body_array);
1061
+	}
1062
+	return NULL;
1063
+}
1064
+
1065
+str *get_p_notify_body(str pres_uri, pres_ev_t *event, str *etag, str *contact)
1066
+{
1067
+	if(publ_cache_mode == PS_PCACHE_RECORD) {
1068
+		return ps_cache_get_p_notify_body(pres_uri, event, etag, contact);
1069
+	} else {
1070
+		return ps_db_get_p_notify_body(pres_uri, event, etag, contact);
1071
+	}
1072
+}
1073
+
865 1074
 void free_notify_body(str *body, pres_ev_t *ev)
866 1075
 {
867 1076
 	if(body != NULL) {
Browse code

presence: define modes for publ_cache parameter

Daniel-Constantin Mierla authored on 19/04/2020 07:02:09
Showing 1 changed files
... ...
@@ -609,7 +609,7 @@ str *get_p_notify_body(str pres_uri, pres_ev_t *event, str *etag, str *contact)
609 609
 	}
610 610
 
611 611
 	/* if in db_only mode, get the presentity information from database - skip htable search */
612
-	if(publ_cache_enabled) {
612
+	if(publ_cache_mode == PS_PCACHE_HYBRID) {
613 613
 		/* search in hash table if any record exists */
614 614
 		hash_code = core_case_hash(&pres_uri, NULL, phtable_size);
615 615
 		if(search_phtable(&pres_uri, event->evp->type, hash_code) == NULL) {
Browse code

presence: renamed global vars to add common module prefix

- better protection against name conflicts, as this module is used by
other modules and can run in library mode

Daniel-Constantin Mierla authored on 14/04/2020 09:10:55
Showing 1 changed files
... ...
@@ -294,7 +294,7 @@ int get_wi_subs_db(subs_t *subs, watcher_t *watchers)
294 294
 	query_ops[n_query_cols] = OP_GT;
295 295
 	query_vals[n_query_cols].type = DB1_INT;
296 296
 	query_vals[n_query_cols].nul = 0;
297
-	query_vals[n_query_cols].val.int_val = (int)time(NULL) + expires_offset;
297
+	query_vals[n_query_cols].val.int_val = (int)time(NULL) + pres_expires_offset;
298 298
 	n_query_cols++;
299 299
 
300 300
 	result_cols[status_col = n_result_cols++] = &str_status_col;
... ...
@@ -387,7 +387,7 @@ str *get_wi_notify_body(subs_t *subs, subs_t *watcher_subs)
387 387
 		goto done;
388 388
 	}
389 389
 
390
-	if(subs_dbmode == DB_ONLY) {
390
+	if(pres_subs_dbmode == DB_ONLY) {
391 391
 		if(get_wi_subs_db(subs, watchers) < 0) {
392 392
 			LM_ERR("getting watchers from database\n");
393 393
 			goto error;
... ...
@@ -1131,7 +1131,7 @@ int get_subs_db(
1131 1131
 
1132 1132
 		s.event = event;
1133 1133
 		s.local_cseq = row_vals[cseq_col].val.int_val + 1;
1134
-		if(row_vals[expires_col].val.int_val < (int)time(NULL) + expires_offset)
1134
+		if(row_vals[expires_col].val.int_val < (int)time(NULL) + pres_expires_offset)
1135 1135
 			s.expires = 0;
1136 1136
 		else
1137 1137
 			s.expires = row_vals[expires_col].val.int_val - (int)time(NULL);
... ...
@@ -1169,11 +1169,10 @@ subs_t *get_subs_dialog(str *pres_uri, pres_ev_t *event, str *sender)
1169 1169
 	subs_t *s_array = NULL;
1170 1170
 	int n = 0;
1171 1171
 
1172
-	/* if subs_dbmode!=DB_ONLY, should take the subscriptions from the hashtable only
1173
-	   in DB_ONLY mode should take all dialogs from db
1174
-	*/
1172
+	/* if pres_subs_dbmode!=DB_ONLY, should take the subscriptions from the
1173
+		hashtable only in DB_ONLY mode should take all dialogs from db */
1175 1174
 
1176
-	if(subs_dbmode == DB_ONLY) {
1175
+	if(pres_subs_dbmode == DB_ONLY) {
1177 1176
 		if(get_subs_db(pres_uri, event, sender, &s_array, &n) < 0) {
1178 1177
 			LM_ERR("getting dialogs from database\n");
1179 1178
 			goto error;
... ...
@@ -1578,7 +1577,7 @@ int notify(subs_t *subs, subs_t *watcher_subs, str *n_body, int force_null_body,
1578 1577
 				&subs->pres_uri, &subs->event->name, shtable_size);
1579 1578
 
1580 1579
 		/* if subscriptions are held also in memory, update the subscription hashtable */
1581
-		if(subs_dbmode != DB_ONLY) {
1580
+		if(pres_subs_dbmode != DB_ONLY) {
1582 1581
 			if(update_shtable(subs_htable, hash_code, subs, LOCAL_TYPE) < 0) {
1583 1582
 				/* subscriptions are held only in memory, and hashtable update failed */
1584 1583
 				LM_ERR("updating subscription record in hash table\n");
... ...
@@ -1587,8 +1586,8 @@ int notify(subs_t *subs, subs_t *watcher_subs, str *n_body, int force_null_body,
1587 1586
 		}
1588 1587
 		/* if DB_ONLY mode or WRITE_THROUGH update in database */
1589 1588
 		if(subs->recv_event != PRES_SUBSCRIBE_RECV
1590
-				&& ((subs_dbmode == DB_ONLY && pres_notifier_processes == 0)
1591
-						   || subs_dbmode == WRITE_THROUGH)) {
1589
+				&& ((pres_subs_dbmode == DB_ONLY && pres_notifier_processes == 0)
1590
+						   || pres_subs_dbmode == WRITE_THROUGH)) {
1592 1591
 			LM_DBG("updating subscription to database\n");
1593 1592
 			if(update_subs_db(subs, LOCAL_TYPE) < 0) {
1594 1593
 				LM_ERR("updating subscription in database\n");
... ...
@@ -1764,7 +1763,7 @@ void p_tm_callback(struct cell *t, int type, struct tmcb_params *ps)
1764 1763
 	run_notify_reply_event(t, ps);
1765 1764
 
1766 1765
 	if(ps->code == 404 || ps->code == 481
1767
-			|| (ps->code == 408 && timeout_rm_subs
1766
+			|| (ps->code == 408 && pres_timeout_rm_subs
1768 1767
 					   && subs->status != TERMINATED_STATUS)
1769 1768
 			|| pres_get_delete_sub()) {
1770 1769
 		delete_subs(&subs->pres_uri, &subs->event->name, &subs->to_tag,
... ...
@@ -2601,7 +2600,7 @@ static int notifier_notify(subs_t *sub, int *updated, int *end_transaction)
2601 2600
 				goto error;
2602 2601
 			} else if(num_other_watchers == 0)
2603 2602
 				attempt_delete_presentities = 1;
2604
-		} else if(!send_fast_notify)
2603
+		} else if(!pres_send_fast_notify)
2605 2604
 			goto done;
2606 2605
 	}
2607 2606
 
... ...
@@ -2692,7 +2691,7 @@ int process_dialogs(int round, int presence_winfo)
2692 2691
 	}
2693 2692
 
2694 2693
 	if(pa_dbf.start_transaction) {
2695
-		if(pa_dbf.start_transaction(pa_db, db_table_lock) < 0) {
2694
+		if(pa_dbf.start_transaction(pa_db, pres_db_table_lock) < 0) {
2696 2695
 			LM_ERR("in start_transaction\n");
2697 2696
 			goto error;
2698 2697
 		}
... ...
@@ -2796,7 +2795,7 @@ int process_dialogs(int round, int presence_winfo)
2796 2795
 		}
2797 2796
 
2798 2797
 		if(pa_dbf.start_transaction) {
2799
-			if(pa_dbf.start_transaction(pa_db, db_table_lock) < 0) {
2798
+			if(pa_dbf.start_transaction(pa_db, pres_db_table_lock) < 0) {
2800 2799
 				LM_ERR("in start_transaction\n");
2801 2800
 				goto error;
2802 2801
 			}
... ...
@@ -2856,7 +2855,7 @@ int process_dialogs(int round, int presence_winfo)
2856 2855
 		cached_updated_winfo = sub.updated_winfo =
2857 2856
 				VAL_INT(&dvalues[updated_winfo_col]);
2858 2857
 
2859
-		if(VAL_INT(&dvalues[expires_col]) > now + expires_offset)
2858
+		if(VAL_INT(&dvalues[expires_col]) > now + pres_expires_offset)
2860 2859
 			sub.expires = VAL_INT(&dvalues[expires_col]) - now;
2861 2860
 		else
2862 2861
 			sub.expires = 0;
Browse code

presence: new parameter pres_subs_mode

- allow disabling cloning subscription structure for pv use, saving the
pkg memory and copy operations for all its fields
- default 1 - clone, like so far; 0 - disable

Daniel-Constantin Mierla authored on 15/11/2019 19:14:33
Showing 1 changed files
... ...
@@ -56,6 +56,7 @@ int goto_on_notify_reply = -1;
56 56
 extern int pres_local_log_level;
57 57
 extern int pres_local_log_facility;
58 58
 extern subs_t *_pres_subs_last_sub;
59
+extern int _pres_subs_mode;
59 60
 
60 61
 c_back_param *shm_dup_cbparam(subs_t *);
61 62
 void free_cbparam(c_back_param *cb_param);
... ...
@@ -1507,13 +1508,17 @@ jump_over_body:
1507 1508
 	LM_DBG("expires %d status %d\n", subs->expires, subs->status);
1508 1509
 	cb_param = mem_copy_subs(subs, SHM_MEM_TYPE);
1509 1510
 
1510
-	backup_subs = _pres_subs_last_sub;
1511
-	_pres_subs_last_sub = subs;
1511
+	if(_pres_subs_mode==1) {
1512
+		backup_subs = _pres_subs_last_sub;
1513
+		_pres_subs_last_sub = subs;
1514
+	}
1512 1515
 
1513 1516
 	set_uac_req(&uac_r, &met, &str_hdr, notify_body, td, TMCB_LOCAL_COMPLETED,
1514 1517
 			p_tm_callback, (void *)cb_param);
1515 1518
 	result = tmb.t_request_within(&uac_r);
1516
-	_pres_subs_last_sub = backup_subs;
1519
+	if(_pres_subs_mode==1) {
1520
+		_pres_subs_last_sub = backup_subs;
1521
+	}
1517 1522
 	if(result < 0) {
1518 1523
 		LM_ERR("in function tmb.t_request_within\n");
1519 1524
 		if(cb_param)
... ...
@@ -1705,8 +1710,10 @@ void run_notify_reply_event(struct cell *t, struct tmcb_params *ps)
1705 1710
 		_pres_subs_notify_reply_msg = ps->rpl;
1706 1711
 	}
1707 1712
 
1708
-	backup_subs = _pres_subs_last_sub;
1709
-	_pres_subs_last_sub = mem_copy_subs((subs_t *)(*ps->param), PKG_MEM_TYPE);
1713
+	if(_pres_subs_mode==1) {
1714
+		backup_subs = _pres_subs_last_sub;
1715
+		_pres_subs_last_sub = mem_copy_subs((subs_t *)(*ps->param), PKG_MEM_TYPE);
1716
+	}
1710 1717
 
1711 1718
 	backup_route_type = get_route_type();
1712 1719
 	set_route_type(LOCAL_ROUTE);
... ...
@@ -1715,8 +1722,10 @@ void run_notify_reply_event(struct cell *t, struct tmcb_params *ps)
1715 1722
 
1716 1723
 	_pres_subs_notify_reply_msg = NULL;
1717 1724
 	_pres_subs_notify_reply_code = 0;
1718
-	pkg_free(_pres_subs_last_sub);
1719
-	_pres_subs_last_sub = backup_subs;
1725
+	if(_pres_subs_mode==1) {
1726
+		pkg_free(_pres_subs_last_sub);
1727
+		_pres_subs_last_sub = backup_subs;
1728
+	}
1720 1729
 	free_sip_msg(&msg);
1721 1730
 }
1722 1731
 
Browse code

presence: remove double extern declaration of _pres_subs_last_sub

- set some global variables static

Daniel-Constantin Mierla authored on 15/11/2019 19:06:14
Showing 1 changed files
... ...
@@ -1623,9 +1623,8 @@ int notify(subs_t *subs, subs_t *watcher_subs, str *n_body, int force_null_body,
1623 1623
 	return 0;
1624 1624
 }
1625 1625
 
1626
-extern subs_t *_pres_subs_last_sub;
1627
-sip_msg_t *_pres_subs_notify_reply_msg = NULL;
1628
-int _pres_subs_notify_reply_code = 0;
1626
+static sip_msg_t *_pres_subs_notify_reply_msg = NULL;
1627
+static int _pres_subs_notify_reply_code = 0;
1629 1628
 
1630 1629
 int pv_parse_notify_reply_var_name(pv_spec_p sp, str *in)
1631 1630
 {
Browse code

presence: free allocated memory in case of error

Daniel-Constantin Mierla authored on 05/10/2019 18:41:50
Showing 1 changed files
... ...
@@ -2018,6 +2018,7 @@ int add_waiting_watchers(watcher_t *watchers, str pres_uri, str event)
2018 2018
 
2019 2019
 		w = (watcher_t *)pkg_malloc(sizeof(watcher_t));
2020 2020
 		if(w == NULL) {
2021
+			pkg_free(wuri.s);
2021 2022
 			ERR_MEM(PKG_MEM_STR);
2022 2023
 		}
2023 2024
 		memset(w, 0, sizeof(watcher_t));
Browse code

presence: clang format the code

Daniel-Constantin Mierla authored on 16/01/2019 07:30:25
Showing 1 changed files
... ...
@@ -51,21 +51,21 @@
51 51
 #define ALLOC_SIZE 3000
52 52
 #define MAX_FORWARD 70
53 53
 
54
-int goto_on_notify_reply=-1;
54
+int goto_on_notify_reply = -1;
55 55
 
56 56
 extern int pres_local_log_level;
57 57
 extern int pres_local_log_facility;
58
-extern subs_t* _pres_subs_last_sub;
58
+extern subs_t *_pres_subs_last_sub;
59 59
 
60
-c_back_param* shm_dup_cbparam(subs_t*);
61
-void free_cbparam(c_back_param* cb_param);
60
+c_back_param *shm_dup_cbparam(subs_t *);
61
+void free_cbparam(c_back_param *cb_param);
62 62
 
63
-void p_tm_callback( struct cell *t, int type, struct tmcb_params *ps);
63
+void p_tm_callback(struct cell *t, int type, struct tmcb_params *ps);
64 64
 int add_waiting_watchers(watcher_t *watchers, str pres_uri, str event);
65 65
 int add_watcher_list(subs_t *s, watcher_t *watchers);
66
-str* create_winfo_xml(watcher_t* watchers, char* version,
67
-		str resource, str event, int STATE_FLAG);
68
-void free_watcher_list(watcher_t* watchers);
66
+str *create_winfo_xml(watcher_t *watchers, char *version, str resource,
67
+		str event, int STATE_FLAG);
68
+void free_watcher_list(watcher_t *watchers);
69 69
 
70 70
 str str_to_user_col = str_init("to_user");
71 71
 str str_username_col = str_init("username");
... ...
@@ -104,31 +104,44 @@ str str_priority_col = str_init("priority");
104 104
 str str_flags_col = str_init("flags");
105 105
 str str_user_agent_col = str_init("user_agent");
106 106
 
107
-int subset=0;
107
+int subset = 0;
108 108
 
109
-char* get_status_str(int status_flag)
109
+char *get_status_str(int status_flag)
110 110
 {
111
-	switch(status_flag)
112
-	{
113
-		case ACTIVE_STATUS: return "active";
114
-		case PENDING_STATUS: return "pending";
115
-		case TERMINATED_STATUS: return "terminated";
116
-		case WAITING_STATUS: return "waiting";
111
+	switch(status_flag) {
112
+		case ACTIVE_STATUS:
113
+			return "active";
114
+		case PENDING_STATUS:
115
+			return "pending";
116
+		case TERMINATED_STATUS:
117
+			return "terminated";
118
+		case WAITING_STATUS:
119
+			return "waiting";
117 120
 	}
118 121
 	return NULL;
119 122
 }
120 123
 
121
-void printf_subs(subs_t* subs)
124
+void printf_subs(subs_t *subs)
122 125
 {
123 126
 	LM_DBG("pres_uri: %.*s\n", subs->pres_uri.len, subs->pres_uri.s);
124
-	LM_DBG("watcher_user@watcher_domain: %.*s@%.*s\n", subs->watcher_user.len, subs->watcher_user.s, subs->watcher_domain.len, subs->watcher_domain.s);
125
-	LM_DBG("to_user@to_domain: %.*s@%.*s\n", subs->to_user.len, subs->to_user.s, subs->to_domain.len, subs->to_domain.s);
126
-	LM_DBG("from_user@from_domain: %.*s@%.*s\n", subs->from_user.len, subs->from_user.s, subs->from_domain.len, subs->from_domain.s);
127
-	LM_DBG("callid/from_tag/to_tag: %.*s/%.*s/%.*s\n", subs->callid.len, subs->callid.s, subs->from_tag.len, subs->from_tag.s, subs->to_tag.len, subs->to_tag.s);
128
-	LM_DBG("local_cseq/remote_cseq: %u/%u\n", subs->local_cseq, subs->remote_cseq);
129
-	LM_DBG("local_contact/contact: %.*s/%.*s\n", subs->local_contact.len, subs->local_contact.s, subs->contact.len, subs->contact.s);
130
-	LM_DBG("record_route: %.*s\n", subs->record_route.len, subs->record_route.s);
131
-	LM_DBG("sockinfo_str: %.*s\n", subs->sockinfo_str.len, subs->sockinfo_str.s);
127
+	LM_DBG("watcher_user@watcher_domain: %.*s@%.*s\n", subs->watcher_user.len,
128
+			subs->watcher_user.s, subs->watcher_domain.len,
129
+			subs->watcher_domain.s);
130
+	LM_DBG("to_user@to_domain: %.*s@%.*s\n", subs->to_user.len, subs->to_user.s,
131
+			subs->to_domain.len, subs->to_domain.s);
132
+	LM_DBG("from_user@from_domain: %.*s@%.*s\n", subs->from_user.len,
133
+			subs->from_user.s, subs->from_domain.len, subs->from_domain.s);
134
+	LM_DBG("callid/from_tag/to_tag: %.*s/%.*s/%.*s\n", subs->callid.len,
135
+			subs->callid.s, subs->from_tag.len, subs->from_tag.s,
136
+			subs->to_tag.len, subs->to_tag.s);
137
+	LM_DBG("local_cseq/remote_cseq: %u/%u\n", subs->local_cseq,
138
+			subs->remote_cseq);
139
+	LM_DBG("local_contact/contact: %.*s/%.*s\n", subs->local_contact.len,
140
+			subs->local_contact.s, subs->contact.len, subs->contact.s);
141
+	LM_DBG("record_route: %.*s\n", subs->record_route.len,
142
+			subs->record_route.s);
143
+	LM_DBG("sockinfo_str: %.*s\n", subs->sockinfo_str.len,
144
+			subs->sockinfo_str.s);
132 145
 
133 146
 	LM_DBG("event: %.*s\n", subs->event->name.len, subs->event->name.s);
134 147
 	LM_DBG("status: %s\n", get_status_str(subs->status));
... ...
@@ -136,15 +149,16 @@ void printf_subs(subs_t* subs)
136 149
 	LM_DBG("version: %u\n", subs->version);
137 150
 	LM_DBG("expires: %u\n", subs->expires);
138 151
 
139
-	LM_DBG("updated/updated_winfo: %d/%d\n", subs->updated, subs->updated_winfo);
152
+	LM_DBG("updated/updated_winfo: %d/%d\n", subs->updated,
153
+			subs->updated_winfo);
140 154
 }
141 155
 
142
-int build_str_hdr(subs_t* subs, int is_body, str* hdr)
156
+int build_str_hdr(subs_t *subs, int is_body, str *hdr)
143 157
 {
144
-	pres_ev_t* event= subs->event;
158
+	pres_ev_t *event = subs->event;
145 159
 	str expires = {0, 0};
146 160
 	str status = {0, 0};
147
-	char* p;
161
+	char *p;
148 162
 	str trans = {";transport=", 11};
149 163
 
150 164
 	if(hdr == NULL) {
... ...
@@ -153,23 +167,29 @@ int build_str_hdr(subs_t* subs, int is_body, str* hdr)
153 167
 	}
154 168
 	expires.s = int2str(subs->expires, &expires.len);
155 169
 
156
-	status.s= get_status_str(subs->status);
170
+	status.s = get_status_str(subs->status);
157 171
 	if(status.s == NULL) {
158 172
 		LM_ERR("bad status %d\n", subs->status);
159 173
 		return -1;
160 174
 	}
161 175
 	status.len = strlen(status.s);
162 176
 
163
-	hdr->len = 18 /*Max-Forwards:  + val*/ + CRLF_LEN +
164
-		7 /*Event: */ + subs->event->name.len +4 /*;id=*/+ subs->event_id.len+
165
-		CRLF_LEN + 10 /*Contact: <*/ + subs->local_contact.len + 1/*>*/ +
166
-		15/*";transport=xxxx"*/ + CRLF_LEN + 20 /*Subscription-State: */ +
167
-		status.len + 10 /*reason/expires params*/
168
-		+ (subs->reason.len>expires.len?subs->reason.len:expires.len)
169
-		+ CRLF_LEN + (is_body?
170
-		(14 /*Content-Type: */+subs->event->content_type.len + CRLF_LEN):0) + 1;
171
-
172
-	hdr->s = (char*)pkg_malloc(hdr->len);
177
+	hdr->len =
178
+			18 /*Max-Forwards:  + val*/ + CRLF_LEN
179
+			+ 7 /*Event: */ + subs->event->name.len
180
+			+ 4 /*;id=*/ + subs->event_id.len + CRLF_LEN
181
+			+ 10 /*Contact: <*/ + subs->local_contact.len
182
+			+ 1 /*>*/ + 15 /*";transport=xxxx"*/ + CRLF_LEN
183
+			+ 20 /*Subscription-State: */ + status.len
184
+			+ 10 /*reason/expires params*/
185
+			+ (subs->reason.len > expires.len ? subs->reason.len : expires.len)
186
+			+ CRLF_LEN
187
+			+ (is_body ? (14 /*Content-Type: */ + subs->event->content_type.len
188
+								 + CRLF_LEN)
189
+					   : 0)
190
+			+ 1;
191
+
192
+	hdr->s = (char *)pkg_malloc(hdr->len);
173 193
 	if(hdr->s == NULL) {
174 194
 		LM_ERR("no more pkg memory\n");
175 195
 		return -1;
... ...
@@ -178,23 +198,24 @@ int build_str_hdr(subs_t* subs, int is_body, str* hdr)
178 198
 	p = hdr->s;
179 199
 	p += sprintf(p, "Max-Forwards: %d\r\n", MAX_FORWARD);
180 200
 
181
-	p += sprintf(p  ,"Event: %.*s", event->name.len, event->name.s);
201
+	p += sprintf(p, "Event: %.*s", event->name.len, event->name.s);
182 202
 	if(subs->event_id.len && subs->event_id.s) {
183 203
 		p += sprintf(p, ";id=%.*s", subs->event_id.len, subs->event_id.s);
184 204
 	}
185 205
 	memcpy(p, CRLF, CRLF_LEN);
186 206
 	p += CRLF_LEN;
187 207
 
188
-	p += sprintf(p, "Contact: <%.*s", subs->local_contact.len, subs->local_contact.s);
189
-	if(subs->sockinfo_str.s!=NULL
190
-			&& str_search(&subs->local_contact, &trans)==0) {
208
+	p += sprintf(p, "Contact: <%.*s", subs->local_contact.len,
209
+			subs->local_contact.s);
210
+	if(subs->sockinfo_str.s != NULL
211
+			&& str_search(&subs->local_contact, &trans) == 0) {
191 212
 		/* fix me */
192 213
 		switch(subs->sockinfo_str.s[0]) {
193 214
 			case 's':
194 215
 			case 'S':
195 216
 				memcpy(p, ";transport=sctp", 15);
196 217
 				p += 15;
197
-			break;
218
+				break;
198 219
 			case 't':
199 220
 			case 'T':
200 221
 				switch(subs->sockinfo_str.s[1]) {
... ...
@@ -202,17 +223,17 @@ int build_str_hdr(subs_t* subs, int is_body, str* hdr)
202 223
 					case 'C':
203 224
 						memcpy(p, ";transport=tcp", 14);
204 225
 						p += 14;
205
-					break;
226
+						break;
206 227
 					case 'l':
207 228
 					case 'L':
208 229
 						memcpy(p, ";transport=tls", 14);
209 230
 						p += 14;
210
-					break;
231
+						break;
211 232
 				}
212
-			break;
233
+				break;
213 234
 		}
214 235
 	}
215
-	*p =  '>';
236
+	*p = '>';
216 237
 	p++;
217 238
 	memcpy(p, CRLF, CRLF_LEN);
218 239
 	p += CRLF_LEN;
... ...
@@ -229,7 +250,7 @@ int build_str_hdr(subs_t* subs, int is_body, str* hdr)
229 250
 	p += CRLF_LEN;
230 251
 
231 252
 	if(is_body) {
232
-		p += sprintf(p,"Content-Type: %.*s\r\n", event->content_type.len,
253
+		p += sprintf(p, "Content-Type: %.*s\r\n", event->content_type.len,
233 254
 				event->content_type.s);
234 255
 	}
235 256
 
... ...
@@ -239,15 +260,15 @@ int build_str_hdr(subs_t* subs, int is_body, str* hdr)
239 260
 	return 0;
240 261
 }
241 262
 
242
-int get_wi_subs_db(subs_t* subs, watcher_t* watchers)
263
+int get_wi_subs_db(subs_t *subs, watcher_t *watchers)
243 264
 {
244 265
 	subs_t sb;
245 266
 	db_key_t query_cols[3];
246
-	db_op_t  query_ops[3];
267
+	db_op_t query_ops[3];
247 268
 	db_val_t query_vals[3];
248 269
 	db_key_t result_cols[5];
249 270
 	db1_res_t *result = NULL;
250
-	db_row_t *row = NULL ;
271
+	db_row_t *row = NULL;
251 272
 	db_val_t *row_vals = NULL;
252 273
 	int n_result_cols = 0;
253 274
 	int n_query_cols = 0;
... ...
@@ -258,7 +279,7 @@ int get_wi_subs_db(subs_t* subs, watcher_t* watchers)
258 279
 	query_ops[n_query_cols] = OP_EQ;
259 280
 	query_vals[n_query_cols].type = DB1_STR;
260 281
 	query_vals[n_query_cols].nul = 0;
261
-	query_vals[n_query_cols].val.str_val= subs->pres_uri;
282
+	query_vals[n_query_cols].val.str_val = subs->pres_uri;
262 283
 	n_query_cols++;
263 284
 
264 285
 	query_cols[n_query_cols] = &str_event_col;
... ...
@@ -275,55 +296,52 @@ int get_wi_subs_db(subs_t* subs, watcher_t* watchers)
275 296
 	query_vals[n_query_cols].val.int_val = (int)time(NULL) + expires_offset;
276 297
 	n_query_cols++;
277 298
 
278
-	result_cols[status_col=n_result_cols++] = &str_status_col;
279
-	result_cols[watcher_user_col=n_result_cols++] = &str_watcher_username_col;
280
-	result_cols[watcher_domain_col=n_result_cols++] = &str_watcher_domain_col;
281
-	result_cols[callid_col=n_result_cols++] = &str_callid_col;
299
+	result_cols[status_col = n_result_cols++] = &str_status_col;
300
+	result_cols[watcher_user_col = n_result_cols++] = &str_watcher_username_col;
301
+	result_cols[watcher_domain_col = n_result_cols++] = &str_watcher_domain_col;
302
+	result_cols[callid_col = n_result_cols++] = &str_callid_col;
282 303
 
283
-	if (pa_dbf.use_table(pa_db, &active_watchers_table) < 0)
284
-	{
304
+	if(pa_dbf.use_table(pa_db, &active_watchers_table) < 0) {
285 305
 		LM_ERR("in use_table\n");
286 306
 		goto error;
287 307
 	}
288 308
 
289
-	if (pa_dbf.query (pa_db, query_cols, query_ops, query_vals,
290
-		 result_cols, n_query_cols, n_result_cols, 0,  &result) < 0)
291
-	{
309
+	if(pa_dbf.query(pa_db, query_cols, query_ops, query_vals, result_cols,
310
+			   n_query_cols, n_result_cols, 0, &result)
311
+			< 0) {
292 312
 		LM_ERR("querying active_watchers db table\n");
293 313
 		goto error;
294 314
 	}
295 315
 
296
-	if(result== NULL )
297
-	{
316
+	if(result == NULL) {
298 317
 		goto error;
299 318
 	}
300 319
 
301
-	if(result->n <= 0)
302
-	{
320
+	if(result->n <= 0) {
303 321
 		LM_DBG("The query in db table for active subscription"
304
-				" returned no result\n");
322
+			   " returned no result\n");
305 323
 		pa_dbf.free_result(pa_db, result);
306 324
 		return 0;
307 325
 	}
308 326
 
309
-	for(i=0; i<result->n; i++)
310
-	{
327
+	for(i = 0; i < result->n; i++) {
311 328
 		row = &result->rows[i];
312 329
 		row_vals = ROW_VALUES(row);
313 330
 
314
-		sb.watcher_user.s= (char*)row_vals[watcher_user_col].val.string_val;
315
-		sb.watcher_user.len= strlen(sb.watcher_user.s);
331
+		sb.watcher_user.s = (char *)row_vals[watcher_user_col].val.string_val;
332
+		sb.watcher_user.len = strlen(sb.watcher_user.s);
316 333
 
317
-		sb.watcher_domain.s= (char*)row_vals[watcher_domain_col].val.string_val;
318
-		sb.watcher_domain.len= strlen(sb.watcher_domain.s);
334
+		sb.watcher_domain.s =
335
+				(char *)row_vals[watcher_domain_col].val.string_val;
336
+		sb.watcher_domain.len = strlen(sb.watcher_domain.s);
319 337
 
320
-		sb.callid.s= (char*)row_vals[callid_col].val.string_val;
321
-		sb.callid.len= strlen(sb.callid.s);
338
+		sb.callid.s = (char *)row_vals[callid_col].val.string_val;
339
+		sb.callid.len = strlen(sb.callid.s);
322 340
 
323
-		sb.event =subs->event->wipeer;
324
-		sb.status= row_vals[status_col].val.int_val;
341
+		sb.event = subs->event->wipeer;
342
+		sb.status = row_vals[status_col].val.int_val;
325 343
 
326
-		if(add_watcher_list(&sb, watchers)<0)
344
+		if(add_watcher_list(&sb, watchers) < 0)
327 345
 			goto error;
328 346
 	}
329 347
 
... ...
@@ -336,69 +354,61 @@ error:
336 354
 	return -1;
337 355
 }
338 356
 
339
-str* get_wi_notify_body(subs_t* subs, subs_t* watcher_subs)
357
+str *get_wi_notify_body(subs_t *subs, subs_t *watcher_subs)
340 358
 {
341
-	str* notify_body = NULL;
342
-	char* version_str;
359
+	str *notify_body = NULL;
360
+	char *version_str;
343 361
 	watcher_t *watchers = NULL;
344 362
 	int len = 0;
345 363
 	unsigned int hash_code;
346
-	subs_t* s= NULL;
364
+	subs_t *s = NULL;
347 365
 	int state = FULL_STATE_FLAG;
348 366
 	unsigned int now = (int)time(NULL);
349 367
 
350 368
 	hash_code = 0;
351 369
 	version_str = int2str(subs->version, &len);
352
-	if(version_str ==NULL)
353
-	{
370
+	if(version_str == NULL) {
354 371
 		LM_ERR("converting int to str\n ");
355 372
 		goto error;
356 373
 	}
357 374
 
358
-	watchers= (watcher_t*)pkg_malloc(sizeof(watcher_t));
359
-	if(watchers== NULL)
360
-	{
375
+	watchers = (watcher_t *)pkg_malloc(sizeof(watcher_t));
376
+	if(watchers == NULL) {
361 377
 		ERR_MEM(PKG_MEM_STR);
362 378
 	}
363 379
 	memset(watchers, 0, sizeof(watcher_t));
364 380
 
365
-	if(watcher_subs != NULL)
366
-	{
367
-		if(add_watcher_list(watcher_subs, watchers)< 0)
381
+	if(watcher_subs != NULL) {
382
+		if(add_watcher_list(watcher_subs, watchers) < 0)
368 383
 			goto error;
369 384
 		state = PARTIAL_STATE_FLAG;
370 385
 
371 386
 		goto done;
372 387
 	}
373 388
 
374
-	if(subs_dbmode == DB_ONLY)
375
-	{
376
-		if(get_wi_subs_db(subs, watchers)< 0)
377
-		{
389
+	if(subs_dbmode == DB_ONLY) {
390
+		if(get_wi_subs_db(subs, watchers) < 0) {
378 391
 			LM_ERR("getting watchers from database\n");
379 392
 			goto error;
380 393
 		}
381 394
 	} else {
382
-		hash_code= core_case_hash(&subs->pres_uri, &subs->event->wipeer->name,
383
-				shtable_size);
395
+		hash_code = core_case_hash(
396
+				&subs->pres_uri, &subs->event->wipeer->name, shtable_size);
384 397
 		lock_get(&subs_htable[hash_code].lock);
385
-		s= subs_htable[hash_code].entries;
386
-		while(s->next)
387
-		{
388
-			s= s->next;
398
+		s = subs_htable[hash_code].entries;
399
+		while(s->next) {
400
+			s = s->next;
389 401
 
390
-			if(s->expires< now)
391
-			{
402
+			if(s->expires < now) {
392 403
 				LM_DBG("expired record\n");
393 404
 				continue;
394 405
 			}
395 406
 
396
-			if(s->event== subs->event->wipeer &&
397
-				s->pres_uri.len== subs->pres_uri.len &&
398
-				presence_sip_uri_match(&s->pres_uri, &subs->pres_uri)== 0)
399
-			{
400
-				if(add_watcher_list(s, watchers)< 0)
401
-				{
407
+			if(s->event == subs->event->wipeer
408
+					&& s->pres_uri.len == subs->pres_uri.len
409
+					&& presence_sip_uri_match(&s->pres_uri, &subs->pres_uri)
410
+							   == 0) {
411
+				if(add_watcher_list(s, watchers) < 0) {
402 412
 					lock_release(&subs_htable[hash_code].lock);
403 413
 					goto error;
404 414
 				}
... ...
@@ -406,19 +416,18 @@ str* get_wi_notify_body(subs_t* subs, subs_t* watcher_subs)
406 416
 		}
407 417
 		lock_release(&subs_htable[hash_code].lock);
408 418
 
409
-		if(add_waiting_watchers(watchers, subs->pres_uri,
410
-					subs->event->wipeer->name)< 0 )
411
-		{
419
+		if(add_waiting_watchers(
420
+				   watchers, subs->pres_uri, subs->event->wipeer->name)
421
+				< 0) {
412 422
 			LM_ERR("failed to add waiting watchers\n");
413 423
 			goto error;
414 424
 		}
415 425
 	}
416 426
 
417 427
 done:
418
-	notify_body = create_winfo_xml(watchers,version_str,subs->pres_uri,
428
+	notify_body = create_winfo_xml(watchers, version_str, subs->pres_uri,
419 429
 			subs->event->wipeer->name, state);
420
-	if(notify_body== NULL)
421
-	{
430
+	if(notify_body == NULL) {
422 431
 		LM_ERR("in function create_winfo_xml\n");
423 432
 		goto error;
424 433
 	}
... ...
@@ -430,17 +439,16 @@ error:
430 439
 	return NULL;
431 440
 }
432 441
 
433
-void free_watcher_list(watcher_t* watchers)
442
+void free_watcher_list(watcher_t *watchers)
434 443
 {
435
-	watcher_t* w;
436
-	while(watchers)
437
-	{
438
-		w= watchers;
439
-		if(w->uri.s !=NULL)
444
+	watcher_t *w;
445
+	while(watchers) {
446
+		w = watchers;
447
+		if(w->uri.s != NULL)
440 448
 			pkg_free(w->uri.s);
441
-		if(w->id.s !=NULL)
449
+		if(w->id.s != NULL)
442 450
 			pkg_free(w->id.s);
443
-		watchers= watchers->next;
451
+		watchers = watchers->next;
444 452
 		pkg_free(w);
445 453
 	}
446 454
 
... ...
@@ -449,23 +457,20 @@ void free_watcher_list(watcher_t* watchers)
449 457
 
450 458
 int add_watcher_list(subs_t *s, watcher_t *watchers)
451 459
 {
452
-	watcher_t* w;
460
+	watcher_t *w;
453 461