Browse code

tsilo: Merge similar functions in ts_append.c

We need to merge similar functions in ts_append.c implementation
to give less work for maintaining similar code.
Furthermore the purpose of them is quite the same,
but differes just in some details (usage of TM's API).

Merged functions are:
- ts_append() and ts_append_by_contact()
- ts_append_to() and ts_append_by_contact_to()

Now all appending in TSILO happens only through ts_append() / ts_append_to()
An implementation in tsilo.c has been updated accordingly.

Usage.
In case we do not want to append based on specific Contact (location),
the contact parameter being passed to either ts_append() or ts_append_to()
must be set to zero.
In case we do want to append by a specific Contact, then we must
set the Contact parameter of str type beforehand calling ts_append() / ts_append_to().

Donat Zenichev authored on 17/11/2021 14:27:37 • Victor Seva committed on 19/11/2021 08:18:55
Showing 3 changed files
... ...
@@ -36,26 +36,37 @@
36 36
 #include "ts_hash.h"
37 37
 #include "ts_append.h"
38 38
 
39
-int ts_append(struct sip_msg* msg, str *ruri, char *table) {
39
+int ts_append(struct sip_msg* msg, str *ruri, str *contact, char *table) {
40 40
 	ts_urecord_t* _r;
41 41
 	ts_transaction_t* ptr;
42 42
 
43 43
 	struct sip_uri p_uri;
44
+	struct sip_uri c_uri;
44 45
 	str *t_uri;
45 46
 
46 47
 	int res;
47 48
 	int appended;
48
-	
49
+
50
+	/* parse R-URI */
49 51
 	if (use_domain) {
50 52
 		t_uri = ruri;
51 53
 	} else {
52
-		if(parse_uri(ruri->s, ruri->len, &p_uri)<0) {
54
+		if(parse_uri(ruri->s, ruri->len, &p_uri) < 0) {
53 55
 			LM_ERR("failed to parse uri %.*s\n", ruri->len, ruri->s);
54 56
 			return -1;
55 57
 		}
56 58
 		t_uri = &p_uri.user;
57 59
 	}
58 60
 
61
+	/* parse contact if given */
62
+	if(contact->s != NULL && contact->len != 0) {
63
+		if (parse_uri(contact->s, contact->len, &c_uri) < 0) {
64
+			LM_ERR("failed to parse contact %.*s\n", ruri->len, ruri->s);
65
+			return -1;
66
+		}
67
+	}
68
+
69
+	/* find urecord in TSILO cache */
59 70
 	lock_entry_by_ruri(t_uri);
60 71
 
61 72
 	res = get_ts_urecord(t_uri, &_r);
... ...
@@ -66,12 +77,13 @@ int ts_append(struct sip_msg* msg, str *ruri, char *table) {
66 77
 		return -1;
67 78
 	}
68 79
 
80
+	/* cycle through existing transactions */
69 81
 	ptr = _r->transactions;
70 82
 
71 83
 	while(ptr) {
72 84
 		LM_DBG("transaction %u:%u found for %.*s, going to append branches\n",ptr->tindex, ptr->tlabel, t_uri->len, t_uri->s);
73 85
 
74
-		appended = ts_append_to(msg, ptr->tindex, ptr->tlabel, table, ruri);
86
+		appended = ts_append_to(msg, ptr->tindex, ptr->tlabel, table, ruri, contact);
75 87
 		if (appended > 0)
76 88
 			update_stat(added_branches, appended);
77 89
 		ptr = ptr->next;
... ...
@@ -82,27 +94,28 @@ int ts_append(struct sip_msg* msg, str *ruri, char *table) {
82 94
 	return 1;
83 95
 }
84 96
 
85
-int ts_append_to(struct sip_msg* msg, int tindex, int tlabel, char *table, str *uri) {
97
+int ts_append_to(struct sip_msg* msg, int tindex, int tlabel, char *table, str *uri, str *contact) {
86 98
 	struct cell     *t=0;
87
-	struct cell     *orig_t;
99
+	struct cell     *orig_t;	/* a pointer to an existing transaction or 0 if lookup fails */
88 100
 	struct sip_msg *orig_msg;
89 101
 	int ret;
90 102
 	str stable;
91 103
 
92
-	str contact;       /* needed for usage of TM's API, t_append_branches() */
93
-	contact.s = NULL;  /* must be emptied */
94
-	contact.len = 0;
104
+	if(contact->s!=NULL && contact->len > 0) {
105
+		LM_DBG("trying to append based on specific contact <%.*s>\n", contact->len, contact->s);
106
+	}
95 107
 
108
+	/* lookup a transaction based on its identifier (hash_index:label) */
96 109
 	orig_t = _tmb.t_gett();
97 110
 
98 111
 	if(_tmb.t_lookup_ident(&t, tindex, tlabel) < 0)
99 112
 	{
100
-		LM_ERR("transaction [%u:%u] not found\n",
101
-				tindex, tlabel);
113
+		LM_ERR("transaction [%u:%u] not found\n",	tindex, tlabel);
102 114
 		ret = -1;
103 115
 		goto done;
104 116
 	}
105 117
 
118
+	/* check if the dialog is still in the early stage */
106 119
 	if (t->flags & T_CANCELED) {
107 120
 		LM_DBG("trasaction [%u:%u] was cancelled\n",
108 121
 				tindex, tlabel);
... ...
@@ -117,119 +130,6 @@ int ts_append_to(struct sip_msg* msg, int tindex, int tlabel, char *table, str *
117 130
 		goto done;
118 131
 	}
119 132
 
120
-	orig_msg = t->uas.request;
121
-
122
-	stable.s = table;
123
-	stable.len = strlen(stable.s);
124
-	if(uri==NULL || uri->s==NULL || uri->len<=0) {
125
-		ret = _regapi.lookup_to_dset(orig_msg, &stable, NULL);
126
-	} else {
127
-		ret = _regapi.lookup_to_dset(orig_msg, &stable, uri);
128
-	}
129
-
130
-	if(ret != 1) {
131
-		LM_DBG("transaction %u:%u: error updating dset (%d)\n", tindex, tlabel, ret);
132
-		ret = -4;
133
-		goto done;
134
-	}
135
-
136
-	ret = _tmb.t_append_branches(&contact);
137
-
138
-done:
139
-	/* unref the transaction which had been referred by t_lookup_ident() call.
140
-	 * Restore the original transaction (if any) */
141
-	if(t) _tmb.unref_cell(t);
142
-	_tmb.t_sett(orig_t, T_BR_UNDEFINED);
143
-
144
-	return ret;
145
-}
146
-
147
-int ts_append_by_contact(struct sip_msg* msg, str *ruri, str *contact, char *table) {
148
-	ts_urecord_t* _r;
149
-	ts_transaction_t* ptr;
150
-
151
-	struct sip_uri p_uri;
152
-	struct sip_uri c_uri;
153
-	str *t_uri;
154
-
155
-	int res;
156
-	int appended;
157
-
158
-	/* parse R-URI */
159
-	if (use_domain) {
160
-		t_uri = ruri;
161
-	} else {
162
-		if (parse_uri(ruri->s, ruri->len, &p_uri) < 0) {
163
-			LM_ERR("tsilo: failed to parse uri %.*s\n", ruri->len, ruri->s);
164
-			return -1;
165
-		}
166
-		t_uri = &p_uri.user;
167
-	}
168
-
169
-	/* parse contact */
170
-	if (parse_uri(contact->s, contact->len, &c_uri) < 0) {
171
-		LM_ERR("tsilo: failed to parse contact %.*s\n", ruri->len, ruri->s);
172
-		return -1;
173
-	}
174
-
175
-	/* find urecord in TSILO cache */
176
-	lock_entry_by_ruri(t_uri);
177
-	res = get_ts_urecord(t_uri, &_r);
178
-
179
-	if (res != 0) {
180
-		LM_ERR("tsilo: failed to retrieve record for %.*s\n", t_uri->len, t_uri->s);
181
-		unlock_entry_by_ruri(t_uri);
182
-		return -1;
183
-	}
184
-
185
-	/* cycle through existing transactions */
186
-	ptr = _r->transactions;
187
-	while(ptr) {
188
-		LM_DBG("tsilo: transaction %u:%u found for %.*s, going to append branches\n",
189
-						ptr->tindex, ptr->tlabel, t_uri->len, t_uri->s);
190
-		/* append only if the desired contact has been found in locations */
191
-		appended = ts_append_by_contact_to(msg, ptr->tindex, ptr->tlabel, table, ruri, contact);
192
-		if (appended > 0)
193
-			update_stat(added_branches, appended);
194
-		ptr = ptr->next;
195
-	}
196
-
197
-	unlock_entry_by_ruri(t_uri);
198
-
199
-	return 1;
200
-}
201
-
202
-int ts_append_by_contact_to(struct sip_msg* msg, int tindex, int tlabel, char *table, str *uri, str *contact) {
203
-	struct cell     *t=0;
204
-	struct cell     *orig_t;	/* a pointer to an existing transaction or 0 if lookup fails*/
205
-	struct sip_msg *orig_msg;
206
-	int ret;
207
-	str stable;
208
-
209
-	LM_DBG("tsilo: trying to append based on contact <%.*s>\n", contact->len, contact->s);
210
-
211
-	/* lookup a transaction based on its identifier (hash_index:label) */
212
-	orig_t = _tmb.t_gett();
213
-	if(_tmb.t_lookup_ident(&t, tindex, tlabel) < 0)
214
-	{
215
-		LM_ERR("tsilo: transaction [%u:%u] not found\n", tindex, tlabel);
216
-		ret = -1;
217
-		goto done;
218
-	}
219
-
220
-	/* check if the dialog is still in the early stage */
221
-	if (t->flags & T_CANCELED) {
222
-		LM_DBG("tsilo: trasaction [%u:%u] was cancelled\n", tindex, tlabel);
223
-		ret = -2;
224
-		goto done;
225
-	}
226
-	if (t->uas.status >= 200) {
227
-		LM_DBG("tsilo: trasaction [%u:%u] sent out a final response already - %d\n",
228
-					tindex, tlabel, t->uas.status);
229
-		ret = -3;
230
-		goto done;
231
-	}
232
-
233 133
 	/* get original (very first) request of the transaction */
234 134
 	orig_msg = t->uas.request;
235 135
 	stable.s = table;
... ...
@@ -242,13 +142,13 @@ int ts_append_by_contact_to(struct sip_msg* msg, int tindex, int tlabel, char *t
242 142
 	}
243 143
 
244 144
 	if(ret != 1) {
245
-		LM_ERR("tsilo: transaction %u:%u: error updating dset (%d)\n", tindex, tlabel, ret);
145
+		LM_ERR("transaction %u:%u: error updating dset (%d)\n", tindex, tlabel, ret);
246 146
 		ret = -4;
247 147
 		goto done;
248 148
 	}
249 149
 
250
-	/* start the transaction only for the desired contact
251
-		contact must be of syntax: sip:<user>@<host>:<port> with no parameters list*/
150
+	/* if the contact has been given previously
151
+		then do a new append only for the desired location */
252 152
 	ret = _tmb.t_append_branches(contact);
253 153
 
254 154
 done:
... ...
@@ -22,9 +22,7 @@
22 22
 #ifndef _TS_APPEND_H
23 23
 #define _TS_APPEND_H
24 24
 
25
-int ts_append(struct sip_msg* msg, str *ruri, char *table);
26
-int ts_append_to(struct sip_msg* msg, int tindex, int tlabel, char *table, str *uri);
27
-int ts_append_by_contact(struct sip_msg* msg, str *ruri, str *contact, char *table);
28
-int ts_append_by_contact_to(struct sip_msg* msg, int tindex, int tlabel, char *table, str *uri, str *contact);
25
+int ts_append(struct sip_msg* msg, str *ruri, str *contact, char *table);
26
+int ts_append_to(struct sip_msg* msg, int tindex, int tlabel, char *table, str *uri, str *contact);
29 27
 
30 28
 #endif
... ...
@@ -275,6 +275,9 @@ static int w_ts_append(struct sip_msg* _msg, char *_table, char *_ruri)
275 275
 	str ruri = STR_NULL;
276 276
 	int rc;
277 277
 
278
+	/* we do not want to do append by particular location */
279
+	str contact = STR_NULL;
280
+
278 281
 	if(_ruri==NULL || (fixup_get_svalue(_msg, (gparam_p)_ruri, &tmp)!=0 || tmp.len<=0)) {
279 282
 		LM_ERR("invalid ruri parameter\n");
280 283
 		return -1;
... ...
@@ -285,7 +288,7 @@ static int w_ts_append(struct sip_msg* _msg, char *_table, char *_ruri)
285 288
 	if (pkg_str_dup(&ruri, &tmp) < 0)
286 289
 		return -1;
287 290
 
288
-	rc = ts_append(_msg, &ruri, _table);
291
+	rc = ts_append(_msg, &ruri, &contact, _table);
289 292
 
290 293
 	pkg_free(ruri.s);
291 294
 
... ...
@@ -300,13 +303,16 @@ static int ki_ts_append(sip_msg_t* _msg, str *_table, str *_ruri)
300 303
 	str ruri = STR_NULL;
301 304
 	int rc;
302 305
 
306
+	/* we do not want to do append by particular location */
307
+	str contact = STR_NULL;
308
+
303 309
 	if(ts_check_uri(_ruri)<0)
304 310
 		return -1;
305 311
 
306 312
 	if (pkg_str_dup(&ruri, _ruri) < 0)
307 313
 		return -1;
308 314
 
309
-	rc = ts_append(_msg, &ruri, _table->s);
315
+	rc = ts_append(_msg, &ruri, &contact, _table->s);
310 316
 
311 317
 	pkg_free(ruri.s);
312 318
 
... ...
@@ -321,6 +327,9 @@ static int w_ts_append_to(struct sip_msg* msg, char *idx, char *lbl, char *table
321 327
 	unsigned int tindex;
322 328
 	unsigned int tlabel;
323 329
 
330
+	/* we do not want to do append by particular location */
331
+	str contact = STR_NULL;
332
+
324 333
 	if(fixup_get_ivalue(msg, (gparam_p)idx, (int*)&tindex)<0) {
325 334
 		LM_ERR("cannot get transaction index\n");
326 335
 		return -1;
... ...
@@ -331,7 +340,8 @@ static int w_ts_append_to(struct sip_msg* msg, char *idx, char *lbl, char *table
331 340
 		return -1;
332 341
 	}
333 342
 
334
-	return ts_append_to(msg, tindex, tlabel, table, 0);
343
+	/* we do not want to do append by particular location here */
344
+	return ts_append_to(msg, tindex, tlabel, table, 0, &contact);
335 345
 }
336 346
 
337 347
 /**
... ...
@@ -339,8 +349,12 @@ static int w_ts_append_to(struct sip_msg* msg, char *idx, char *lbl, char *table
339 349
  */
340 350
 static int ki_ts_append_to(sip_msg_t* _msg, int tindex, int tlabel, str *_table)
341 351
 {
352
+	/* we do not want to do append by particular location */
353
+	str contact = STR_NULL;
354
+
355
+	/* we do not want to do append by particular location here */
342 356
 	return ts_append_to(_msg, (unsigned int)tindex, (unsigned int)tlabel,
343
-			_table->s, 0);
357
+			_table->s, 0, &contact);
344 358
 }
345 359
 
346 360
 /**
... ...
@@ -352,6 +366,9 @@ static int w_ts_append_to2(struct sip_msg* msg, char *idx, char *lbl, char *tabl
352 366
 	unsigned int tlabel;
353 367
 	str suri;
354 368
 
369
+	/* we do not want to do append by particular location */
370
+	str contact = STR_NULL;
371
+
355 372
 	if(fixup_get_ivalue(msg, (gparam_p)idx, (int*)&tindex)<0) {
356 373
 		LM_ERR("cannot get transaction index\n");
357 374
 		return -1;
... ...
@@ -369,7 +386,8 @@ static int w_ts_append_to2(struct sip_msg* msg, char *idx, char *lbl, char *tabl
369 386
 	if(ts_check_uri(&suri)<0)
370 387
 		return -1;
371 388
 
372
-	return ts_append_to(msg, tindex, tlabel, table, &suri);
389
+	/* we do not want to do append by particular location here */
390
+	return ts_append_to(msg, tindex, tlabel, table, &suri, &contact);
373 391
 }
374 392
 
375 393
 /**
... ...
@@ -378,8 +396,12 @@ static int w_ts_append_to2(struct sip_msg* msg, char *idx, char *lbl, char *tabl
378 396
 static int ki_ts_append_to_uri(sip_msg_t* _msg, int tindex, int tlabel,
379 397
 		str *_table, str *_uri)
380 398
 {
399
+	/* we do not want to do append by particular location */
400
+	str contact = STR_NULL;
401
+
402
+	/* we do not want to do append by particular location here */
381 403
 	return ts_append_to(_msg, (unsigned int)tindex, (unsigned int)tlabel,
382
-			_table->s, _uri);
404
+			_table->s, _uri, &contact);
383 405
 }
384 406
 
385 407
 /**
... ...
@@ -455,7 +477,7 @@ static int w_ts_append_by_contact2(struct sip_msg* _msg, char *_table, char *_ru
455 477
 	}
456 478
 
457 479
 	/* contact must be of syntax: sip:<user>@<host>:<port> with no parameters list */
458
-	rc = ts_append_by_contact(_msg, &ruri, &contact, _table);
480
+	rc = ts_append(_msg, &ruri, &contact, _table);
459 481
 
460 482
 	/* free previously used memory */
461 483
 	pkg_free(ruri.s);
... ...
@@ -512,7 +534,7 @@ static int ki_ts_append_by_contact(sip_msg_t* _msg, str *_table, str *_ruri) {
512 534
 	}
513 535
 
514 536
 	/* contact must be of syntax: sip:<user>@<host>:<port> with no parameters list */
515
-	rc = ts_append_by_contact(_msg, &ruri, &contact, _table->s);
537
+	rc = ts_append(_msg, &ruri, &contact, _table->s);
516 538
 
517 539
 	pkg_free(ruri.s);
518 540
 	pkg_free(contact.s);
... ...
@@ -576,7 +598,7 @@ static int w_ts_append_by_contact3(struct sip_msg* _msg, char *_table, char *_ru
576 598
 	}
577 599
 
578 600
 	/* contact must be of syntax: sip:<user>@<host>:<port> with no parameters list */
579
-	rc = ts_append_by_contact(_msg, &ruri, &contact, _table);
601
+	rc = ts_append(_msg, &ruri, &contact, _table);
580 602
 
581 603
 	pkg_free(ruri.s);
582 604
 	pkg_free(contact.s);
... ...
@@ -606,7 +628,7 @@ static int ki_ts_append_by_contact_uri(sip_msg_t* _msg, str *_table, str *_ruri,
606 628
 		return -1;
607 629
 
608 630
 	/* contact must be of syntax: sip:<user>@<host>:<port> with no parameters list */
609
-	rc = ts_append_by_contact(_msg, &ruri, &contact, _table->s);
631
+	rc = ts_append(_msg, &ruri, &contact, _table->s);
610 632
 
611 633
 	pkg_free(ruri.s);
612 634
 	pkg_free(contact.s);