Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,2772 +0,0 @@
1
-/*
2
- * Copyright (C) 2001-2003 FhG Fokus
3
- *
4
- * This file is part of Kamailio, a free SIP server.
5
- *
6
- * Kamailio is free software; you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License as published by
8
- * the Free Software Foundation; either version 2 of the License, or
9
- * (at your option) any later version
10
- *
11
- * Kamailio is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
- * GNU General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU General Public License
17
- * along with this program; if not, write to the Free Software
18
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19
- *
20
- */
21
-
22
-/** Defines:
23
- *           TM_ONREPLY_FINAL_DROP_OK - allows dropping the final reply
24
- *            from the tm onreply_routes, but comes with a small performance
25
- *            hit (extra unlock()/lock() for each final reply when a onreply
26
- *            route is set).
27
- */
28
-
29
-#ifdef EXTRA_DEBUG
30
-#include <assert.h>
31
-#endif
32
-#include "../../comp_defs.h"
33
-#include "../../hash_func.h"
34
-#include "../../dprint.h"
35
-#include "../../config.h"
36
-#include "../../parser/parser_f.h"
37
-#include "../../parser/parse_to.h"
38
-#include "../../ut.h"
39
-#include "../../timer.h"
40
-#include "../../error.h"
41
-#include "../../action.h"
42
-#include "../../script_cb.h"
43
-#include "../../dset.h"
44
-#include "../../tags.h"
45
-#include "../../route.h"
46
-#include "../../data_lump.h"
47
-#include "../../data_lump_rpl.h"
48
-#include "../../usr_avp.h"
49
-#ifdef WITH_XAVP
50
-#include "../../usr_avp.h"
51
-#endif
52
-#include "../../atomic_ops.h" /* membar_write() */
53
-#include "../../compiler_opt.h"
54
-#ifdef USE_DST_BLACKLIST
55
-#include "../../dst_blacklist.h"
56
-#endif
57
-#ifdef USE_DNS_FAILOVER
58
-#include "../../dns_cache.h"
59
-#include "../../cfg_core.h" /* cfg_get(core, core_cfg, use_dns_failover) */
60
-#endif
61
-
62
-#include "defs.h"
63
-#include "config.h"
64
-#include "h_table.h"
65
-#include "t_hooks.h"
66
-#include "t_funcs.h"
67
-#include "t_reply.h"
68
-#include "t_cancel.h"
69
-#include "t_msgbuilder.h"
70
-#include "t_lookup.h"
71
-#include "t_fwd.h"
72
-#include "../../fix_lumps.h"
73
-#include "../../sr_compat.h"
74
-#include "../../receive.h"
75
-#include "../../onsend.h"
76
-#include "../../kemi.h"
77
-#include "t_stats.h"
78
-#include "uac.h"
79
-
80
-
81
-#ifdef NO_TM_ONREPLY_FINAL_DROP_OK
82
-#undef TM_ONREPLY_FINAL_DROP_OK
83
-#endif
84
-
85
-/* private place where we create to-tags for replies */
86
-/* janakj: made public, I need to access this value to store it in dialogs */
87
-char tm_tags[TOTAG_VALUE_LEN];
88
-/* bogdan: pack tm_tag buffer and len into a str to pass them to
89
- * build_res_buf_from_sip_req() */
90
-static str  tm_tag = {tm_tags,TOTAG_VALUE_LEN};
91
-char *tm_tag_suffix;
92
-
93
-/* where to go if there is no positive reply (>=300) */
94
-static int goto_on_failure=0;
95
-/* where to go if a failure is returned on a branch */
96
-static int goto_on_branch_failure=0;
97
-/* where to go on receipt of reply */
98
-static int goto_on_reply=0;
99
-/* where to go on receipt of reply without transaction context */
100
-int goto_on_sl_reply=0;
101
-
102
-/* remap 503 response code to 500 */
103
-extern int tm_remap_503_500;
104
-
105
-/* how to deal with winning branch reply selection in failure_route
106
- * can be overwritten per transaction with t_drop_replies(...)
107
- * Values:
108
- * - 0 - all branches are kept (default, and default ser 2.1.x behaviour)
109
- * - 1 - all branches are discarded
110
- * - 2 - braches of last step of serial forking are discarded
111
- * - 3 - all branches are discarded if a new leg of serial forking
112
- *       is started (default kamailio 1.5.x behaviour)
113
- */
114
-int failure_reply_mode = 3;
115
-
116
-/* responses priority (used by t_pick_branch)
117
- *  0xx is used only for the initial value (=> should have no chance to be
118
- *  selected => the highest value); 1xx is not used */
119
-static unsigned short resp_class_prio[]={
120
-			32000, /* 0-99, special */
121
-			11000, /* 1xx, special, should never be used */
122
-				0,  /* 2xx, high priority (not used, 2xx are immediately
123
-					 * forwarded and t_pick_branch will never be called if
124
-					 * a 2xx was received) */
125
-			3000,  /* 3xx */
126
-			4000,  /* 4xx */
127
-			5000,  /* 5xx */
128
-			1000   /* 6xx, highest priority */
129
-};
130
-
131
-/* How to prioritize faked replies
132
- * The value will be added to the default prio
133
- * - 0 disabled
134
- * - < 0 increase prio
135
- * - > 0 decrease prio
136
- */
137
-int faked_reply_prio = 0;
138
-
139
-
140
-int t_get_reply_totag(struct sip_msg *msg, str *totag)
141
-{
142
-	if(msg==NULL || totag==NULL) {
143
-		return -1;
144
-	}
145
-
146
-	calc_crc_suffix(msg, tm_tag_suffix);
147
-	*totag = tm_tag;
148
-
149
-	return 1;
150
-}
151
-
152
-static int picked_branch = -1;
153
-
154
-/*! \brief returns the picked branch */
155
-int t_get_picked_branch(void)
156
-{
157
-	return picked_branch;
158
-}
159
-
160
-
161
-
162
-
163
-/* we store the reply_route # in private memory which is
164
- * then processed during t_relay; we cannot set this value
165
- * before t_relay creates transaction context or after
166
- * t_relay when a reply may arrive after we set this
167
- * value; that's why we do it how we do it, i.e.,
168
- * *inside*  t_relay using hints stored in private memory
169
- * before t_relay is called
170
-*/
171
-
172
-
173
-void t_on_failure( unsigned int go_to )
174
-{
175
-	struct cell *t = get_t();
176
-
177
-	/* in REPLY_ROUTE and FAILURE_ROUTE T will be set to current transaction;
178
-	 * in REQUEST_ROUTE T will be set only if the transaction was already
179
-	 * created; if not -> use the static variable */
180
-	if (!t || t==T_UNDEFINED )
181
-		goto_on_failure=go_to;
182
-	else
183
-		t->on_failure = go_to;
184
-}
185
-
186
-
187
-void t_on_branch_failure( unsigned int go_to )
188
-{
189
-	struct cell *t = get_t();
190
-
191
-	/* in REPLY_ROUTE and FAILURE_ROUTE T will be set to current transaction;
192
-	 * in REQUEST_ROUTE T will be set only if the transaction was already
193
-	 * created; if not -> use the static variable */
194
-	if (!t || t==T_UNDEFINED )
195
-		goto_on_branch_failure=go_to;
196
-	else
197
-		t->on_branch_failure = go_to;
198
-}
199
-
200
-
201
-void t_on_reply( unsigned int go_to )
202
-{
203
-	struct cell *t = get_t();
204
-
205
-	/* in REPLY_ROUTE and FAILURE_ROUTE T will be set to current transaction;
206
-	 * in REQUEST_ROUTE T will be set only if the transaction was already
207
-	 * created; if not -> use the static variable */
208
-	if (!t || t==T_UNDEFINED )
209
-		goto_on_reply=go_to;
210
-	else
211
-		t->on_reply = go_to;
212
-}
213
-
214
-
215
-unsigned int get_on_failure()
216
-{
217
-	return goto_on_failure;
218
-}
219
-
220
-unsigned int get_on_branch_failure()
221
-{
222
-	return goto_on_branch_failure;
223
-}
224
-
225
-unsigned int get_on_reply()
226
-{
227
-	return goto_on_reply;
228
-}
229
-
230
-void tm_init_tags()
231
-{
232
-	init_tags(tm_tags, &tm_tag_suffix,
233
-		"SER-TM/tags", TM_TAG_SEPARATOR );
234
-}
235
-
236
-/* returns 0 if the message was previously acknowledged
237
- * (i.e., no E2EACK callback is needed) and one if the
238
- * callback shall be executed */
239
-int unmatched_totag(struct cell *t, struct sip_msg *ack)
240
-{
241
-	struct totag_elem *i;
242
-	str *tag;
243
-
244
-	if (parse_headers(ack, HDR_TO_F,0)==-1 ||
245
-				!ack->to ) {
246
-		LM_ERR("To header is invalid\n");
247
-		return 1;
248
-	}
249
-	tag=&get_to(ack)->tag_value;
250
-	i=t->fwded_totags;
251
-	while(i){
252
-		membar_depends(); /* make sure we don't see some old i content
253
-							(needed on CPUs like Alpha) */
254
-		if (i->tag.len==tag->len
255
-				&& memcmp(i->tag.s, tag->s, tag->len)==0) {
256
-			LM_DBG("totag for e2e ACK found: %d\n", i->acked);
257
-			/* mark totag as acked and return 1 if this was the first ack
258
-			 * and 0 otherwise */
259
-			return (atomic_get_and_set_int(&i->acked, 1)==0);
260
-		}
261
-		i=i->next;
262
-	}
263
-	/* surprising: to-tag never sighted before */
264
-	return 1;
265
-}
266
-
267
-static inline void update_local_tags(struct cell *trans,
268
-				struct bookmark *bm, char *dst_buffer,
269
-				char *src_buffer /* to which bm refers */)
270
-{
271
-	if (bm->to_tag_val.s) {
272
-		trans->uas.local_totag.s=bm->to_tag_val.s-src_buffer+dst_buffer;
273
-		trans->uas.local_totag.len=bm->to_tag_val.len;
274
-	}
275
-}
276
-
277
-
278
-/* append a newly received tag from a 200/INVITE to
279
- * transaction's set; (only safe if called from within
280
- * a REPLY_LOCK); it returns 1 if such a to tag already
281
- * exists
282
- */
283
-inline static int update_totag_set(struct cell *t, struct sip_msg *ok)
284
-{
285
-	struct totag_elem *i, *n;
286
-	str *tag;
287
-	char *s;
288
-
289
-	if (!ok->to || !ok->to->parsed) {
290
-		LM_ERR("to header not parsed\n");
291
-		return 0;
292
-	}
293
-	tag=&get_to(ok)->tag_value;
294
-	if (!tag->s) {
295
-		LM_DBG("no tag in to header\n");
296
-		return 0;
297
-	}
298
-
299
-	for (i=t->fwded_totags; i; i=i->next) {
300
-		if (i->tag.len==tag->len
301
-				&& memcmp(i->tag.s, tag->s, tag->len) ==0 ){
302
-			/* to tag already recorded */
303
-			LM_DBG("to-tag retransmission\n");
304
-			return 1;
305
-		}
306
-	}
307
-	/* that's a new to-tag -- record it */
308
-	shm_lock();
309
-	n=(struct totag_elem*) shm_malloc_unsafe(sizeof(struct totag_elem));
310
-	s=(char *)shm_malloc_unsafe(tag->len);
311
-	shm_unlock();
312
-	if (!s || !n) {
313
-		LM_ERR("no more shm memory \n");
314
-		if (n) shm_free(n);
315
-		if (s) shm_free(s);
316
-		return 0;
317
-	}
318
-	memset(n, 0, sizeof(struct totag_elem));
319
-	memcpy(s, tag->s, tag->len );
320
-	n->tag.s=s;n->tag.len=tag->len;
321
-	n->next=t->fwded_totags;
322
-	membar_write(); /* make sure all the changes to n are visible on all cpus
323
-					 * before we update t->fwded_totags. This is needed for
324
-					 * three reasons: the compiler might reorder some of the
325
-					 * writes, the cpu/cache could also reorder them with
326
-					 * respect to the visibility on other cpus
327
-					 * (e.g. some of the changes to n could be visible on
328
-					 * another cpu _after_ seeing t->fwded_totags=n) and
329
-					 * the "readers" (unmatched_tags()) do not use locks and
330
-					 * can be called simultaneously on another cpu.*/
331
-	t->fwded_totags=n;
332
-	LM_DBG("new totag \n");
333
-	return 0;
334
-}
335
-
336
-
337
-/*
338
- * Build an ACK to a negative reply
339
- */
340
-static char *build_ack(struct sip_msg* rpl,struct cell *trans,int branch,
341
-	unsigned int *ret_len)
342
-{
343
-	str to;
344
-
345
-	if (parse_headers(rpl,HDR_TO_F, 0)==-1 || !rpl->to ) {
346
-		LM_ERR("cannot generate a HBH ACK if key HFs in reply missing\n");
347
-		return NULL;
348
-	}
349
-	to.s=rpl->to->name.s;
350
-	to.len=rpl->to->len;
351
-
352
-	if (cfg_get(tm, tm_cfg, reparse_invite)) {
353
-		/* build the ACK from the INVITE which was sent out */
354
-		return build_local_reparse( trans, branch, ret_len,
355
-					ACK, ACK_LEN, &to
356
-	#ifdef CANCEL_REASON_SUPPORT
357
-					, 0
358
-	#endif /* CANCEL_REASON_SUPPORT */
359
-					);
360
-	} else {
361
-		/* build the ACK from the reveived INVITE */
362
-		return build_local( trans, branch, ret_len,
363
-					ACK, ACK_LEN, &to
364
-	#ifdef CANCEL_REASON_SUPPORT
365
-					, 0
366
-	#endif /* CANCEL_REASON_SUPPORT */
367
-					);
368
-	}
369
-}
370
-
371
-
372
-/*
373
- * The function builds an ACK to 200 OK of local transactions, honoring the
374
- * route set.
375
- * The destination to which the message should be sent will be returned
376
- * in the dst parameter.
377
- * returns 0 on error and a pkg_malloc'ed buffer with length in ret_len
378
- *  and intended destination in dst on success.
379
- */
380
-static char *build_local_ack(struct sip_msg* rpl, struct cell *trans,
381
-								int branch, unsigned int *ret_len,
382
-								struct dest_info*  dst)
383
-{
384
-#ifdef WITH_AS_SUPPORT
385
-	struct retr_buf *local_ack, *old_lack;
386
-
387
-	/* do we have the ACK cache, previously build? */
388
-	if ((local_ack = trans->uac[0].local_ack) && local_ack->buffer_len) {
389
-		LM_DBG("reusing ACK retr. buffer.\n");
390
-		*ret_len = local_ack->buffer_len;
391
-		*dst = local_ack->dst;
392
-		return local_ack->buffer;
393
-	}
394
-
395
-	/* the ACK will be built (and cached) by the AS (ack_local_uac()) */
396
-	if (trans->flags & T_NO_AUTO_ACK)
397
-		return NULL;
398
-
399
-	if (! (local_ack = local_ack_rb(rpl, trans, branch, /*hdrs*/NULL,
400
-			/*body*/NULL))) {
401
-		LM_ERR("failed to build local ACK retransmission buffer (T@%p).\n",
402
-				trans);
403
-		return NULL;
404
-	}
405
-
406
-	/* set the new buffer, but only if not already set (concurrent 2xx) */
407
-	/* a memory write barrier is needed to make sure the local_ack
408
-	 * content is fully written, before we try to add it to the transaction
409
-	 * -- andrei */
410
-	membar_write_atomic_op();
411
-	if ((old_lack = (struct retr_buf *)atomic_cmpxchg_long(
412
-			(void *)&trans->uac[0].local_ack, 0, (long)local_ack))) {
413
-		/* buffer already set: trash current and use the winning one */
414
-		LM_INFO("concurrent 2xx to local INVITE detected (T@%p).\n", trans);
415
-		free_local_ack(local_ack);
416
-		local_ack = old_lack;
417
-	}
418
-
419
-	*ret_len = local_ack->buffer_len;
420
-	*dst = local_ack->dst;
421
-	return local_ack->buffer;
422
-#else /* ! WITH_AS_SUPPORT */
423
-	return build_dlg_ack(rpl, trans, branch, /*hdrs*/NULL, /*body*/NULL,
424
-			ret_len, dst);
425
-#endif /* WITH_AS_SUPPORT */
426
-}
427
-
428
-
429
-inline static void start_final_repl_retr( struct cell *t )
430
-{
431
-	if (unlikely(!is_local(t) && t->uas.request->REQ_METHOD==METHOD_INVITE )){
432
-		/* crank timers for negative replies */
433
-		if (t->uas.status>=300) {
434
-			if (start_retr(&t->uas.response)!=0)
435
-				LM_CRIT("BUG: start retr failed for %p\n", &t->uas.response);
436
-			return;
437
-		}
438
-		/* local UAS retransmits too */
439
-		if (t->relayed_reply_branch==-2 && t->uas.status>=200) {
440
-			/* we retransmit 200/INVs regardless of transport --
441
-			 * even if TCP used, UDP could be used upstream and
442
-			 * loose the 200, which is not retransmitted by proxies
443
-			*/
444
-			if (force_retr( &t->uas.response )!=0)
445
-				LM_CRIT("BUG: force retr failed for %p\n", &t->uas.response);
446
-			return;
447
-		}
448
-	}
449
-}
450
-
451
-
452
-
453
-static int _reply_light( struct cell *trans, char* buf, unsigned int len,
454
-				unsigned int code,
455
-				char *to_tag, unsigned int to_tag_len, int lock,
456
-				struct bookmark *bm	)
457
-{
458
-	struct retr_buf *rb;
459
-	unsigned int buf_len;
460
-	struct cancel_info cancel_data;
461
-	struct tmcb_params onsend_params;
462
-	int rt, backup_rt;
463
-	struct run_act_ctx ctx;
464
-	struct sip_msg pmsg;
465
-
466
-	init_cancel_info(&cancel_data);
467
-	if (!buf)
468
-	{
469
-		LM_ERR("response building failed\n");
470
-		/* determine if there are some branches to be canceled */
471
-		if ( is_invite(trans) ) {
472
-			prepare_to_cancel(trans, &cancel_data.cancel_bitmap, 0);
473
-		}
474
-		/* and clean-up, including cancellations, if needed */
475
-		goto error;
476
-	}
477
-
478
-	if (lock) LOCK_REPLIES( trans );
479
-	if (trans->uas.status>=200) {
480
-		LM_ERR("can't generate %d reply when a final %d was sent out\n",
481
-				code, trans->uas.status);
482
-		goto error2;
483
-	}
484
-
485
-	rb = & trans->uas.response;
486
-	rb->activ_type=code;
487
-
488
-	trans->uas.status = code;
489
-	buf_len = rb->buffer ? len : len + REPLY_OVERBUFFER_LEN;
490
-	rb->buffer = (char*)shm_resize( rb->buffer, buf_len );
491
-	/* puts the reply's buffer to uas.response */
492
-	if (! rb->buffer ) {
493
-			LM_ERR("cannot allocate shmem buffer\n");
494
-			goto error3;
495
-	}
496
-	update_local_tags(trans, bm, rb->buffer, buf);
497
-
498
-	rb->buffer_len = len ;
499
-	memcpy( rb->buffer , buf , len );
500
-	/* needs to be protected too because what timers are set depends
501
-	 * on current transactions status */
502
-	/* t_update_timers_after_sending_reply( rb ); */
503
-	update_reply_stats( code );
504
-	trans->relayed_reply_branch=-2;
505
-	t_stats_rpl_generated();
506
-	t_stats_rpl_sent();
507
-	if (lock) UNLOCK_REPLIES( trans );
508
-
509
-	/* do UAC cleanup procedures in case we generated
510
-	 * a final answer whereas there are pending UACs */
511
-	if (code>=200) {
512
-		if (unlikely(is_local(trans))) {
513
-			if(unlikely(has_tran_tmcbs(trans, TMCB_LOCAL_COMPLETED)))
514
-				run_trans_callbacks(TMCB_LOCAL_COMPLETED, trans,
515
-									0, FAKED_REPLY, code);
516
-		} else {
517
-			if(unlikely(has_tran_tmcbs(trans, TMCB_RESPONSE_READY))) {
518
-				run_trans_callbacks_with_buf(TMCB_RESPONSE_READY, rb,
519
-					trans->uas.request, FAKED_REPLY, code);
520
-			}
521
-		}
522
-		cleanup_uac_timers( trans );
523
-		if (is_invite(trans)){
524
-			prepare_to_cancel(trans, &cancel_data.cancel_bitmap, 0);
525
-#ifdef CANCEL_REASON_SUPPORT
526
-			cancel_data.reason.cause=code;
527
-#endif /* CANCEL_REASON_SUPPORT */
528
-			cancel_uacs( trans, &cancel_data, F_CANCEL_B_KILL );
529
-		}
530
-		start_final_repl_retr(  trans );
531
-	}
532
-
533
-	if (code==100) {
534
-		if(unlikely(has_tran_tmcbs(trans, TMCB_REQUEST_PENDING)))
535
-			run_trans_callbacks_with_buf(TMCB_REQUEST_PENDING, rb,
536
-					trans->uas.request, FAKED_REPLY, code);
537
-	}
538
-
539
-	/* send it out */
540
-	/* first check if we managed to resolve topmost Via
541
-	 * -- if not yet, don't try to retransmit
542
-	*/
543
-	/*
544
-	 * response.dst.send_sock might be unset if the process that created
545
-	 * the original transaction has not finished initialising the
546
-	 * retransmission buffer (see t_newtran/ init_rb).
547
-	 * If reply_to_via is set and via contains a host name (and not an ip)
548
-	 * the chances for this increase a lot.
549
-	 */
550
-	if (unlikely(!trans->uas.response.dst.send_sock)) {
551
-		LM_ERR("no resolved dst to send reply to\n");
552
-	} else {
553
-		if (likely(SEND_PR_BUFFER( rb, buf, len )>=0)){
554
-			if (unlikely(code>=200 && !is_local(trans) &&
555
-						has_tran_tmcbs(trans, TMCB_RESPONSE_OUT)) ){
556
-				INIT_TMCB_ONSEND_PARAMS(onsend_params, trans->uas.request,
557
-								FAKED_REPLY, rb, &rb->dst,
558
-								buf, len, TMCB_LOCAL_F, rb->branch, code);
559
-				run_trans_callbacks_off_params(TMCB_RESPONSE_OUT, trans,
560
-						&onsend_params);
561
-			}
562
-			if (unlikely(has_tran_tmcbs(trans, TMCB_RESPONSE_SENT))){
563
-				INIT_TMCB_ONSEND_PARAMS(onsend_params, trans->uas.request,
564
-								FAKED_REPLY, rb, &rb->dst,
565
-								buf, len, TMCB_LOCAL_F, rb->branch, code);
566
-				run_trans_callbacks_off_params(TMCB_RESPONSE_SENT, trans,
567
-						&onsend_params);
568
-			}
569
-
570
-			rt = route_lookup(&event_rt, "tm:local-response");
571
-			if (unlikely(rt >= 0 && event_rt.rlist[rt] != NULL))
572
-			{
573
-				if (likely(build_sip_msg_from_buf(&pmsg, buf, len,
574
-								inc_msg_no()) == 0))
575
-				{
576
-					struct onsend_info onsnd_info;
577
-
578
-					onsnd_info.to=&(trans->uas.response.dst.to);
579
-					onsnd_info.send_sock=trans->uas.response.dst.send_sock;
580
-					onsnd_info.buf=buf;
581
-					onsnd_info.len=len;
582
-					p_onsend=&onsnd_info;
583
-
584
-					backup_rt = get_route_type();
585
-					set_route_type(LOCAL_ROUTE);
586
-					init_run_actions_ctx(&ctx);
587
-					run_top_route(event_rt.rlist[rt], &pmsg, 0);
588
-					set_route_type(backup_rt);
589
-					p_onsend=0;
590
-
591
-					free_sip_msg(&pmsg);
592
-				}
593
-			}
594
-
595
-		}
596
-		LM_DBG("reply sent out. buf=%p: %.20s..., shmem=%p: %.20s\n",
597
-			buf, buf, rb->buffer, rb->buffer );
598
-	}
599
-	if (code>=200) {
600
-		/* start wait timer after finishing with t so that this function can
601
-		 * be safely called from a fr_timer which allows quick timer dels
602
-		 * (timer_allow_del()) (there's no chance of having the wait handler
603
-		 *  executed while we still need t) --andrei */
604
-		put_on_wait(trans);
605
-	}
606
-	pkg_free( buf ) ;
607
-	LM_DBG("finished\n");
608
-	return 1;
609
-
610
-error3:
611
-	prepare_to_cancel(trans, &cancel_data.cancel_bitmap, 0);
612
-error2:
613
-	if (lock) UNLOCK_REPLIES( trans );
614
-	pkg_free ( buf );
615
-error:
616
-	/* do UAC cleanup */
617
-	cleanup_uac_timers( trans );
618
-	if ( is_invite(trans) && cancel_data.cancel_bitmap )
619
-		cancel_uacs( trans, &cancel_data, F_CANCEL_B_KILL);
620
-	/* we did not succeed -- put the transaction on wait */
621
-	put_on_wait(trans);
622
-	return -1;
623
-}
624
-
625
-
626
-/* send a UAS reply
627
- * returns 1 if everything was OK or -1 for error
628
- */
629
-static int _reply( struct cell *trans, struct sip_msg* p_msg,
630
-	unsigned int code, char * text, int lock )
631
-{
632
-	unsigned int len;
633
-	char * buf, *dset;
634
-	struct bookmark bm;
635
-	int dset_len;
636
-	str reason;
637
-
638
-	if (code>=200) set_kr(REQ_RPLD);
639
-	/* compute the buffer in private memory prior to entering lock;
640
-	 * create to-tag if needed */
641
-
642
-	/* if that is a redirection message, dump current message set to it */
643
-	if (code>=300 && code<400) {
644
-		dset=print_dset(p_msg, &dset_len);
645
-		if (dset) {
646
-			add_lump_rpl(p_msg, dset, dset_len, LUMP_RPL_HDR);
647
-		}
648
-	}
649
-
650
-	reason.s = text;
651
-	reason.len = strlen(text);
652
-	if (code>=180 && p_msg->to
653
-				&& (get_to(p_msg)->tag_value.s==0
654
-					|| get_to(p_msg)->tag_value.len==0)) {
655
-		calc_crc_suffix( p_msg, tm_tag_suffix );
656
-		buf = build_res_buf_from_sip_req(code, &reason, &tm_tag, p_msg,
657
-				&len, &bm);
658
-		return _reply_light( trans, buf, len, code,
659
-			tm_tag.s, TOTAG_VALUE_LEN, lock, &bm);
660
-	} else {
661
-		buf = build_res_buf_from_sip_req(code, &reason, 0 /*no to-tag*/,
662
-			p_msg, &len, &bm);
663
-		return _reply_light(trans,buf,len,code,
664
-			0, 0, /* no to-tag */lock, &bm);
665
-	}
666
-}
667
-
668
-/** create or restore a "fake environment" for running a failure_route,
669
- * OR an "async environment" depending on is_async_value (0=std failure-faked, 1=async)
670
- * if msg is set -> it will fake the env. vars conforming with the msg; if NULL
671
- * the env. will be restore to original.
672
- * Side-effect: mark_ruri_consumed() for faked env only.
673
- */
674
-void faked_env(struct cell *t, struct sip_msg *msg, int is_async_env) {
675
-	static int backup_route_type;
676
-	static struct cell *backup_t;
677
-	static int backup_branch;
678
-	static unsigned int backup_msgid;
679
-	static avp_list_t* backup_user_from, *backup_user_to;
680
-	static avp_list_t* backup_domain_from, *backup_domain_to;
681
-	static avp_list_t* backup_uri_from, *backup_uri_to;
682
-#ifdef WITH_XAVP
683
-	static sr_xavp_t **backup_xavps;
684
-#endif
685
-	static struct socket_info* backup_si;
686
-
687
-	static struct lump *backup_add_rm;
688
-	static struct lump *backup_body_lumps;
689
-	static struct lump_rpl *backup_reply_lump;
690
-
691
-
692
-	if (msg) {
693
-		/* remember we are back in request processing, but process
694
-		 * a shmem-ed replica of the request; advertise it in route type;
695
-		 * for example t_reply needs to know that
696
-		 */
697
-		backup_route_type = get_route_type();
698
-
699
-		if (is_async_env) {
700
-			set_route_type(t->async_backup.backup_route);
701
-			if (t->async_backup.ruri_new) {
702
-				ruri_mark_new();
703
-			}
704
-		} else {
705
-			set_route_type(FAILURE_ROUTE);
706
-			/* don't bother backing up ruri state, since failure route
707
-			 * is called either on reply or on timer and in both cases
708
-			 * the ruri should not be used again for forking */
709
-			ruri_mark_consumed();  /* in failure route we assume ruri
710
-									* should not be used again for forking */
711
-		}
712
-		/* also, tm actions look in beginning whether transaction is
713
-		 * set -- whether we are called from a reply-processing
714
-		 * or a timer process, we need to set current transaction;
715
-		 * otherwise the actions would attempt to look the transaction
716
-		 * up (unnecessary overhead, refcounting)
717
-		 */
718
-
719
-		/* backup */
720
-		backup_t = get_t();
721
-		backup_branch = get_t_branch();
722
-		backup_msgid = global_msg_id;
723
-		/* fake transaction and message id */
724
-		global_msg_id = msg->id;
725
-
726
-		if (is_async_env) {
727
-			set_t(t, t->async_backup.backup_branch);
728
-		} else {
729
-			set_t(t, T_BR_UNDEFINED);
730
-		}
731
-
732
-		/* make available the avp list from transaction */
733
-		backup_uri_from = set_avp_list(AVP_TRACK_FROM | AVP_CLASS_URI,
734
-				&t->uri_avps_from);
735
-		backup_uri_to = set_avp_list(AVP_TRACK_TO | AVP_CLASS_URI,
736
-				&t->uri_avps_to);
737
-		backup_user_from = set_avp_list(AVP_TRACK_FROM | AVP_CLASS_USER,
738
-				&t->user_avps_from);
739
-		backup_user_to = set_avp_list(AVP_TRACK_TO | AVP_CLASS_USER,
740
-				&t->user_avps_to);
741
-		backup_domain_from = set_avp_list(AVP_TRACK_FROM | AVP_CLASS_DOMAIN,
742
-				&t->domain_avps_from);
743
-		backup_domain_to = set_avp_list(AVP_TRACK_TO | AVP_CLASS_DOMAIN,
744
-				&t->domain_avps_to);
745
-#ifdef WITH_XAVP
746
-		backup_xavps = xavp_set_list(&t->xavps_list);
747
-#endif
748
-		/* set default send address to the saved value */
749
-		backup_si = bind_address;
750
-		bind_address = t->uac[0].request.dst.send_sock;
751
-		/* backup lump lists */
752
-		backup_add_rm = t->uas.request->add_rm;
753
-		backup_body_lumps = t->uas.request->body_lumps;
754
-		backup_reply_lump = t->uas.request->reply_lump;
755
-	} else {
756
-		/* restore original environment */
757
-		set_t(backup_t, backup_branch);
758
-		global_msg_id = backup_msgid;
759
-		set_route_type(backup_route_type);
760
-		/* restore original avp list */
761
-		set_avp_list(AVP_TRACK_FROM | AVP_CLASS_USER, backup_user_from);
762
-		set_avp_list(AVP_TRACK_TO | AVP_CLASS_USER, backup_user_to);
763
-		set_avp_list(AVP_TRACK_FROM | AVP_CLASS_DOMAIN, backup_domain_from);
764
-		set_avp_list(AVP_TRACK_TO | AVP_CLASS_DOMAIN, backup_domain_to);
765
-		set_avp_list(AVP_TRACK_FROM | AVP_CLASS_URI, backup_uri_from);
766
-		set_avp_list(AVP_TRACK_TO | AVP_CLASS_URI, backup_uri_to);
767
-#ifdef WITH_XAVP
768
-		xavp_set_list(backup_xavps);
769
-#endif
770
-		bind_address = backup_si;
771
-		/* restore lump lists */
772
-		t->uas.request->add_rm = backup_add_rm;
773
-		t->uas.request->body_lumps = backup_body_lumps;
774
-		t->uas.request->reply_lump = backup_reply_lump;
775
-	}
776
-}
777
-
778
-/**
779
- * helper function to clone back to pkg fields that can change in fake_req
780
- */
781
-int fake_req_clone_str_helper(str *src, str *dst, char *txt)
782
-{
783
-	/* src string can change -- make a private copy */
784
-	if (src->s!=0 && src->len!=0) {
785
-		dst->s=pkg_malloc(src->len+1);
786
-		if (!dst->s) {
787
-			LM_ERR("no pkg mem to clone %s back to faked msg\n", txt);
788
-			return -1;
789
-		}
790
-		dst->len=src->len;
791
-		memcpy(dst->s, src->s, dst->len);
792
-		dst->s[dst->len]=0;
793
-	}else{
794
-		/* in case src->len==0, but src->s!=0 (extra safety) */
795
-		dst->s = 0;
796
-	}
797
-	return 0;
798
-}
799
-
800
-/**
801
- * fake a semi-private sip message using transaction's shared memory message
802
- */
803
-int fake_req(struct sip_msg *faked_req,
804
-		struct sip_msg *shmem_msg, int extra_flags, struct ua_client *uac)
805
-{
806
-	/* on_failure_reply faked msg now copied from shmem msg (as opposed
807
-	 * to zero-ing) -- more "read-only" actions (exec in particular) will
808
-	 * work from reply_route as they will see msg->from, etc.; caution,
809
-	 * rw actions may append some pkg stuff to msg, which will possibly be
810
-	 * never released (shmem is released in a single block) */
811
-	memcpy( faked_req, shmem_msg, sizeof(struct sip_msg));
812
-
813
-	/* if we set msg_id to something different from current's message
814
-	 * id, the first t_fork will properly clean new branch URIs */
815
-	faked_req->id=shmem_msg->id-1;
816
-	/* msg->parsed_uri_ok must be reset since msg_parsed_uri is
817
-	 * not cloned (and cannot be cloned) */
818
-	faked_req->parsed_uri_ok = 0;
819
-
820
-	faked_req->msg_flags|=extra_flags; /* set the extra tm flags */
821
-
822
-	/* path_vec was cloned in shm and can change -- make a private copy */
823
-	if(fake_req_clone_str_helper(&shmem_msg->path_vec, &faked_req->path_vec,
824
-				"path_vec")<0) {
825
-		goto error00;
826
-	}
827
-	/* dst_uri was cloned in shm and can change -- make a private copy */
828
-	if(fake_req_clone_str_helper(&shmem_msg->dst_uri, &faked_req->dst_uri,
829
-				"dst_uri")<0) {
830
-		goto error01;
831
-	}
832
-	/* new_uri was cloned in shm and can change -- make a private copy */
833
-	if(fake_req_clone_str_helper(&shmem_msg->new_uri, &faked_req->new_uri,
834
-				"new_uri")<0) {
835
-		goto error02;
836
-	}
837
-
838
-	if(uac) setbflagsval(0, uac->branch_flags);
839
-	else setbflagsval(0, 0);
840
-
841
-	return 1;
842
-
843
-error02:
844
-	if (faked_req->dst_uri.s) {
845
-		pkg_free(faked_req->dst_uri.s);
846
-		faked_req->dst_uri.s = 0;
847
-		faked_req->dst_uri.len = 0;
848
-	}
849
-error01:
850
-	if (faked_req->path_vec.s) {
851
-		pkg_free(faked_req->path_vec.s);
852
-		faked_req->path_vec.s = 0;
853
-		faked_req->path_vec.len = 0;
854
-	}
855
-error00:
856
-	return 0;
857
-}
858
-
859
-void free_faked_req(struct sip_msg *faked_req, struct cell *t)
860
-{
861
-	struct hdr_field *hdr;
862
-
863
-	reset_new_uri(faked_req);
864
-	reset_dst_uri(faked_req);
865
-
866
-	/* free all types of lump that were added in failure handlers */
867
-	del_nonshm_lump( &(faked_req->add_rm) );
868
-	del_nonshm_lump( &(faked_req->body_lumps) );
869
-	del_nonshm_lump_rpl( &(faked_req->reply_lump) );
870
-
871
-	/* free header's parsed structures that were added by failure handlers */
872
-	for( hdr=faked_req->headers ; hdr ; hdr=hdr->next ) {
873
-		if ( hdr->parsed && hdr_allocs_parse(hdr) &&
874
-		(hdr->parsed<(void*)t->uas.request ||
875
-		hdr->parsed>=(void*)t->uas.end_request)) {
876
-			/* header parsed filed doesn't point inside uas.request memory
877
-			 * chunck -> it was added by failure funcs.-> free it as pkg */
878
-			LM_DBG("removing hdr->parsed %d\n",
879
-					hdr->type);
880
-			clean_hdr_field(hdr);
881
-			hdr->parsed = 0;
882
-		}
883
-	}
884
-	/* free parsed body added by failure handlers */
885
-	if (faked_req->body) {
886
-		if(faked_req->body->free)
887
-			faked_req->body->free(&faked_req->body);
888
-		faked_req->body = 0;
889
-	}
890
-
891
-	/* free sip_msg_t fileds that can be set in pkg */
892
-	reset_path_vector(faked_req);
893
-	reset_instance(faked_req);
894
-	reset_ruid(faked_req);
895
-	reset_ua(faked_req);
896
-	msg_ldata_reset(faked_req);
897
-}
898
-
899
-
900
-/* return 1 if a failure_route processes */
901
-int run_failure_handlers(struct cell *t, struct sip_msg *rpl,