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,1959 +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
-#include "defs.h"
22
-
23
-
24
-#include "../../dprint.h"
25
-#include "../../config.h"
26
-#include "../../parser/parser_f.h"
27
-#include "../../ut.h"
28
-#include "../../timer.h"
29
-#include "../../hash_func.h"
30
-#include "../../globals.h"
31
-#include "../../cfg_core.h"
32
-#include "../../mem/mem.h"
33
-#include "../../dset.h"
34
-#include "../../action.h"
35
-#include "../../data_lump.h"
36
-#include "../../onsend.h"
37
-#include "../../compiler_opt.h"
38
-#include "../../route.h"
39
-#include "../../script_cb.h"
40
-#include "t_funcs.h"
41
-#include "t_hooks.h"
42
-#include "t_msgbuilder.h"
43
-#include "ut.h"
44
-#include "t_cancel.h"
45
-#include "t_lookup.h"
46
-#include "t_fwd.h"
47
-#include "t_reply.h"
48
-#include "h_table.h"
49
-#include "../../fix_lumps.h"
50
-#include "config.h"
51
-#ifdef USE_DNS_FAILOVER
52
-#include "../../dns_cache.h"
53
-#include "../../cfg_core.h" /* cfg_get(core, core_cfg, use_dns_failover) */
54
-#include "../../msg_translator.h"
55
-#include "lw_parser.h"
56
-#endif
57
-#ifdef USE_DST_BLACKLIST
58
-#include "../../dst_blacklist.h"
59
-#endif
60
-#include "../../atomic_ops.h" /* membar_depends() */
61
-#include "../../kemi.h"
62
-
63
-
64
-extern int tm_failure_exec_mode;
65
-extern int tm_dns_reuse_rcv_socket;
66
-
67
-static int goto_on_branch = 0, branch_route = 0;
68
-
69
-void t_on_branch( unsigned int go_to )
70
-{
71
-	struct cell *t = get_t();
72
-
73
-	/* in REPLY_ROUTE and FAILURE_ROUTE T will be set to current transaction;
74
-	 * in REQUEST_ROUTE T will be set only if the transaction was already
75
-	 * created; if not -> use the static variable */
76
-	if (!t || t==T_UNDEFINED ) {
77
-		goto_on_branch=go_to;
78
-	} else {
79
-		t->on_branch = go_to;
80
-	}
81
-}
82
-
83
-unsigned int get_on_branch(void)
84
-{
85
-	return goto_on_branch;
86
-}
87
-
88
-void set_branch_route( unsigned int on_branch)
89
-{
90
-	branch_route = on_branch;
91
-}
92
-
93
-
94
-
95
-
96
-/** prepares a new branch "buffer".
97
- * Creates the buffer used in the branch rb, fills everything needed (
98
- * the sending information: t->uac[branch].request.dst, branch buffer, uri
99
- * path vector a.s.o.) and runs the on_branch route.
100
- * t->uac[branch].request.dst will be filled if next_hop !=0 with the result
101
- * of the DNS resolution (next_hop, fproto and fsocket).
102
- * If next_hop is 0 all the dst members except the send_flags are read-only
103
- * (send_flags it's updated) and are supposed to be pre-filled.
104
- *
105
- * @param t  - transaction
106
- * @param i_req - corresponding sip_msg, must be non-null, flags might be
107
- *                be modified (on_branch route)
108
- * @param branch - branch no
109
- * @param uri
110
- * @param path  - path vector (list of route like destination in text form,
111
- *                 e.g.: "<sip:1.2.3.4;lr>, <sip:5.6.7.8;lr>")
112
- * @param next_hop - uri of the next hop. If non 0 it will be used
113
- *              for DNS resolution and the branch request.dst structure will
114
- *              be filled. If 0 the branch must already have
115
- *              a pre-filled valid request.dst.
116
- * @param fsocket - forced send socket for forwarding.
117
- * @param send_flags - special flags for sending (see SND_F_* / snd_flags_t).
118
- * @param fproto - forced proto for forwarding. Used only if next_hop!=0.
119
- * @param flags - 0, UAC_DNS_FAILOVER_F or UAC_SKIP_BR_DST_F for now.
120
- *
121
- * @return  0 on success, < 0 (ser_errror E***) on failure.
122
- */
123
-static int prepare_new_uac( struct cell *t, struct sip_msg *i_req,
124
-		int branch, str *uri, str* path,
125
-		str* next_hop,
126
-		struct socket_info* fsocket,
127
-		snd_flags_t snd_flags,
128
-		int fproto, int flags,
129
-		str *instance, str *ruid,
130
-		str *location_ua)
131
-{
132
-	char *shbuf;
133
-	struct lump* add_rm_backup, *body_lumps_backup;
134
-	struct sip_uri parsed_uri_bak;
135
-	int ret;
136
-	unsigned int len;
137
-	int parsed_uri_ok_bak, free_new_uri;
138
-	str msg_uri_bak;
139
-	str dst_uri_bak;
140
-	int dst_uri_backed_up;
141
-	str path_bak;
142
-	int free_path;
143
-	str instance_bak;
144
-	int free_instance;
145
-	str ruid_bak;
146
-	int free_ruid;
147
-	str ua_bak;
148
-	int free_ua;
149
-	int backup_route_type;
150
-	int test_dst;
151
-	snd_flags_t fwd_snd_flags_bak;
152
-	snd_flags_t rpl_snd_flags_bak;
153
-	struct socket_info *force_send_socket_bak;
154
-	struct dest_info *dst;
155
-	struct run_act_ctx ctx;
156
-	struct run_act_ctx *bctx;
157
-	sr_kemi_eng_t *keng;
158
-
159
-	shbuf=0;
160
-	ret=E_UNSPEC;
161
-	msg_uri_bak.s=0; /* kill warnings */
162
-	msg_uri_bak.len=0;
163
-	parsed_uri_ok_bak=0;
164
-	free_new_uri=0;
165
-	dst_uri_bak.s=0;
166
-	dst_uri_bak.len=0;
167
-	dst_uri_backed_up=0;
168
-	path_bak.s=0;
169
-	path_bak.len=0;
170
-	free_path=0;
171
-	instance_bak.s=0;
172
-	instance_bak.len=0;
173
-	free_instance=0;
174
-	ruid_bak.s=0;
175
-	ruid_bak.len=0;
176
-	free_ruid=0;
177
-	ua_bak.s=0;
178
-	ua_bak.len=0;
179
-	free_ua=0;
180
-	dst=&t->uac[branch].request.dst;
181
-
182
-	/* ... we calculate branch ... */
183
-	if (!t_calc_branch(t, branch, i_req->add_to_branch_s,
184
-				&i_req->add_to_branch_len ))
185
-	{
186
-		LM_ERR("branch computation failed\n");
187
-		ret=E_UNSPEC;
188
-		goto error00;
189
-	}
190
-
191
-	/* dup lumps
192
-	 * TODO: clone lumps only if needed */
193
-	/* lumps can be set outside of the lock, make sure that we read
194
-	 * the up-to-date values */
195
-	membar_depends();
196
-	add_rm_backup = i_req->add_rm;
197
-	body_lumps_backup = i_req->body_lumps;
198
-	if (unlikely(i_req->add_rm)){
199
-		i_req->add_rm = dup_lump_list(i_req->add_rm);
200
-		if (unlikely(i_req->add_rm==0)){
201
-			ret=E_OUT_OF_MEM;
202
-			goto error04;
203
-		}
204
-	}
205
-	if (unlikely(i_req->body_lumps)){
206
-		i_req->body_lumps = dup_lump_list(i_req->body_lumps);
207
-		if (unlikely(i_req->body_lumps==0)){
208
-			ret=E_OUT_OF_MEM;
209
-			goto error04;
210
-		}
211
-	}
212
-	/* backup uri & path: we need to change them so that build_req...()
213
-	 * will use uri & path and not the ones in the original msg (i_req)
214
-	 * => we must back them up so that we can restore them to the original
215
-	 * value after building the send buffer */
216
-	msg_uri_bak=i_req->new_uri;
217
-	parsed_uri_bak=i_req->parsed_uri;
218
-	parsed_uri_ok_bak=i_req->parsed_uri_ok;
219
-	path_bak=i_req->path_vec;
220
-	instance_bak=i_req->instance;
221
-	ruid_bak=i_req->ruid;
222
-	ua_bak=i_req->location_ua;
223
-
224
-	if (unlikely(branch_route || has_tran_tmcbs(t, TMCB_REQUEST_FWDED))){
225
-		/* dup uris, path a.s.o. if we have a branch route or callback */
226
-		/* ... set ruri ... */
227
-		/* if uri points to new_uri, it needs to be "fixed" so that we can
228
-		 * change msg->new_uri */
229
-		if (uri==&i_req->new_uri)
230
-			uri=&msg_uri_bak;
231
-		i_req->parsed_uri_ok=0;
232
-		i_req->new_uri.s=pkg_malloc(uri->len);
233
-		if (unlikely(i_req->new_uri.s==0)){
234
-			ret=E_OUT_OF_MEM;
235
-			goto error03;
236
-		}
237
-		free_new_uri=1;
238
-		memcpy(i_req->new_uri.s, uri->s, uri->len);
239
-		i_req->new_uri.len=uri->len;
240
-
241
-		/* update path_vec */
242
-		/* if path points to msg path_vec, it needs to be "fixed" so that we
243
-		 * can change/update msg->path_vec */
244
-		if (path==&i_req->path_vec)
245
-			path=&path_bak;
246
-		/* zero it first so that set_path_vector will work */
247
-		i_req->path_vec.s=0;
248
-		i_req->path_vec.len=0;
249
-		if (unlikely(path)){
250
-			if (unlikely(set_path_vector(i_req, path)<0)){
251
-				ret=E_OUT_OF_MEM;
252
-				goto error03;
253
-			}
254
-			free_path=1;
255
-		}
256
-		/* update instance */
257
-		/* if instance points to msg instance, it needs to be "fixed" so that we
258
-		 * can change/update msg->instance */
259
-		if (instance==&i_req->instance)
260
-			instance=&instance_bak;
261
-		/* zero it first so that set_instance will work */
262
-		i_req->instance.s=0;
263
-		i_req->instance.len=0;
264
-		if (unlikely(instance)){
265
-			if (unlikely(set_instance(i_req, instance)<0)){
266
-				ret=E_OUT_OF_MEM;
267
-				goto error03;
268
-			}
269
-			free_instance=1;
270
-		}
271
-
272
-		/* update ruid */
273
-		/* if ruid points to msg ruid, it needs to be "fixed" so that we
274
-		 * can change/update msg->ruid */
275
-		if (ruid==&i_req->ruid)
276
-			ruid=&ruid_bak;
277
-		/* zero it first so that set_ruid will work */
278
-		i_req->ruid.s=0;
279
-		i_req->ruid.len=0;
280
-		if (unlikely(ruid)){
281
-			if (unlikely(set_ruid(i_req, ruid)<0)){
282
-				ret=E_OUT_OF_MEM;
283
-				goto error03;
284
-			}
285
-			free_ruid=1;
286
-		}
287
-
288
-		/* update location_ua */
289
-		/* if location_ua points to msg location_ua, it needs to be "fixed"
290
-		 * so that we can change/update msg->location_ua */
291
-		if (location_ua==&i_req->location_ua)
292
-			location_ua=&ua_bak;
293
-		/* zero it first so that set_ua will work */
294
-		i_req->location_ua.s=0;
295
-		i_req->location_ua.len=0;
296
-		if (unlikely(location_ua)){
297
-			if (unlikely(set_ua(i_req, location_ua)<0)){
298
-				ret=E_OUT_OF_MEM;
299
-				goto error03;
300
-			}
301
-			free_ua=1;
302
-		}
303
-
304
-		/* backup dst uri  & zero it*/
305
-		dst_uri_bak=i_req->dst_uri;
306
-		dst_uri_backed_up=1;
307
-		/* if next_hop points to dst_uri, it needs to be "fixed" so that we
308
-		 * can change msg->dst_uri */
309
-		if (next_hop==&i_req->dst_uri)
310
-			next_hop=&dst_uri_bak;
311
-		/* zero it first so that set_dst_uri will work */
312
-		i_req->dst_uri.s=0;
313
-		i_req->dst_uri.len=0;
314
-		if (likely(next_hop)){
315
-			if(unlikely((flags & UAC_SKIP_BR_DST_F)==0)){
316
-				/* set dst uri to next_hop for the on_branch route */
317
-				if (unlikely(set_dst_uri(i_req, next_hop)<0)){
318
-					ret=E_OUT_OF_MEM;
319
-					goto error03;
320
-				}
321
-			}
322
-		}
323
-
324
-		if (likely(branch_route)) {
325
-			/* run branch_route actions if provided */
326
-			backup_route_type = get_route_type();
327
-			set_route_type(BRANCH_ROUTE);
328
-			tm_ctx_set_branch_index(branch);
329
-			/* no need to backup/set avp lists: the on_branch route is run
330
-			 * only in the main route context (e.g. t_relay() in the main
331
-			 * route) or in the failure route context (e.g. append_branch &
332
-			 * t_relay()) and in both cases the avp lists are properly set
333
-			 * Note: the branch route is not run on delayed dns failover
334
-			 * (for that to work one would have to set branch_route prior to
335
-			 * calling add_uac(...) and then reset it afterwards).
336
-			 */
337
-			if (exec_pre_script_cb(i_req, BRANCH_CB_TYPE)>0) {
338
-				/* backup ireq msg send flags and force_send_socket*/
339
-				fwd_snd_flags_bak=i_req->fwd_send_flags;;
340
-				rpl_snd_flags_bak=i_req->rpl_send_flags;
341
-				force_send_socket_bak=i_req->force_send_socket;
342
-				/* set the new values */
343
-				i_req->fwd_send_flags=snd_flags /* intial value  */;
344
-				set_force_socket(i_req, fsocket);
345
-				keng = sr_kemi_eng_get();
346
-				if(unlikely(keng!=NULL)) {
347
-					bctx = sr_kemi_act_ctx_get();
348
-					init_run_actions_ctx(&ctx);
349
-					sr_kemi_act_ctx_set(&ctx);
350
-					if(keng->froute(i_req, BRANCH_ROUTE,
351
-							sr_kemi_cbname_lookup_idx(branch_route), NULL)<0) {
352
-						LM_ERR("error running branch route kemi callback\n");
353
-					}
354
-					sr_kemi_act_ctx_set(bctx);
355
-				} else {
356
-					if (run_top_route(branch_rt.rlist[branch_route],
357
-								i_req, &ctx) < 0) {
358
-						LM_DBG("negative return code in run_top_route\n");
359
-					}
360
-				}
361
-				/* update dst send_flags  and send socket*/
362
-				snd_flags=i_req->fwd_send_flags;
363
-				fsocket=i_req->force_send_socket;
364
-				/* restore ireq_msg force_send_socket & flags */
365
-				set_force_socket(i_req, force_send_socket_bak);
366
-				i_req->fwd_send_flags=fwd_snd_flags_bak;
367
-				i_req->rpl_send_flags=rpl_snd_flags_bak;
368
-				exec_post_script_cb(i_req, BRANCH_CB_TYPE);
369
-				/* if DROP was called in cfg, don't forward, jump to end */
370
-				if (unlikely(ctx.run_flags&DROP_R_F))
371
-				{
372
-					tm_ctx_set_branch_index(T_BR_UNDEFINED);
373
-					set_route_type(backup_route_type);
374
-					/* triggered by drop in CFG */
375
-					ret=E_CFG;
376
-					goto error03;
377
-				}
378
-			}
379
-			tm_ctx_set_branch_index(T_BR_UNDEFINED);
380
-			set_route_type(backup_route_type);
381
-		}
382
-
383
-		/* run the specific callbacks for this transaction */
384
-		if (unlikely(has_tran_tmcbs(t, TMCB_REQUEST_FWDED)))
385
-			run_trans_callbacks( TMCB_REQUEST_FWDED , t, i_req, 0,
386
-					-i_req->REQ_METHOD);
387
-
388
-		if (likely( !(flags & UAC_DNS_FAILOVER_F) && i_req->dst_uri.s &&
389
-					i_req->dst_uri.len)){
390
-			/* no dns failover and non-empty dst_uri => use it as dst
391
-			 * (on dns failover dns_h will be non-empty => next_hop will be
392
-			 * ignored) */
393
-			next_hop=&i_req->dst_uri;
394
-		}
395
-		/* no path vector initially, but now is set after branch route and
396
-		 * callbacks execution */
397
-		if(i_req->path_vec.s!=0 && free_path==0)
398
-			free_path=1;
399
-	}else{
400
-		/* no branch route and no TMCB_REQUEST_FWDED callback => set
401
-		 * msg uri and path to the new values (if needed) */
402
-		if (unlikely((uri->s!=i_req->new_uri.s || uri->len!=i_req->new_uri.len)
403
-					&& (i_req->new_uri.s!=0 ||
404
-						uri->s!=i_req->first_line.u.request.uri.s ||
405
-						uri->len!=i_req->first_line.u.request.uri.len) )){
406
-			/* uri is different from i_req uri => replace i_req uri and force
407
-			 * uri re-parsing */
408
-			i_req->new_uri=*uri;
409
-			i_req->parsed_uri_ok=0;
410
-		}
411
-		if (unlikely(path && (i_req->path_vec.s!=path->s ||
412
-						i_req->path_vec.len!=path->len))){
413
-			i_req->path_vec=*path;
414
-		}else if (unlikely(path==0 && i_req->path_vec.len!=0)){
415
-			i_req->path_vec.s=0;
416
-			i_req->path_vec.len=0;
417
-		}
418
-		if (unlikely(instance && (i_req->instance.s!=instance->s ||
419
-						i_req->instance.len!=instance->len))){
420
-			i_req->instance=*instance;
421
-		}else if (unlikely(instance==0 && i_req->instance.len!=0)){
422
-			i_req->instance.s=0;
423
-			i_req->instance.len=0;
424
-		}
425
-		if (unlikely(ruid && (i_req->ruid.s!=ruid->s ||
426
-						i_req->ruid.len!=ruid->len))){
427
-			i_req->ruid=*ruid;
428
-		}else if (unlikely(ruid==0 && i_req->ruid.len!=0)){
429
-			i_req->ruid.s=0;
430
-			i_req->ruid.len=0;
431
-		}
432
-		if (unlikely(location_ua && (i_req->location_ua.s!=location_ua->s ||
433
-						i_req->location_ua.len!=location_ua->len))){
434
-			i_req->location_ua=*location_ua;
435
-		}else if (unlikely(location_ua==0 && i_req->location_ua.len!=0)){
436
-			i_req->location_ua.s=0;
437
-			i_req->location_ua.len=0;
438
-		}
439
-	}
440
-
441
-	if (likely(next_hop!=0 || (flags & UAC_DNS_FAILOVER_F))){
442
-		/* next_hop present => use it for dns resolution */
443
-#ifdef USE_DNS_FAILOVER
444
-		test_dst = (uri2dst2(&t->uac[branch].dns_h, dst, fsocket, snd_flags,
445
-					next_hop?next_hop:uri, fproto) == 0);
446
-#else
447
-		/* dst filled from the uri & request (send_socket) */
448
-		test_dst = (uri2dst2(dst, fsocket, snd_flags,
449
-					next_hop?next_hop:uri, fproto)==0);
450
-#endif
451
-		if (test_dst){
452
-			ret=E_BAD_ADDRESS;
453
-			goto error01;
454
-		}
455
-	}
456
-	/* else next_hop==0 =>
457
-	 * no dst_uri / empty dst_uri and initial next_hop==0 =>
458
-	 * dst is pre-filled with a valid dst => use the pre-filled dst */
459
-
460
-	/* Set on_reply and on_negative handlers for this branch to the handlers in the transaction */
461
-	t->uac[branch].on_reply = t->on_reply;
462
-	t->uac[branch].on_failure = t->on_failure;
463
-	t->uac[branch].on_branch_failure = t->on_branch_failure;
464
-
465
-	/* check if send_sock is ok */
466
-	if (t->uac[branch].request.dst.send_sock==0) {
467
-		LM_ERR("can't fwd to af %d, proto %d "
468
-				" (no corresponding listening socket)\n",
469
-				dst->to.s.sa_family, dst->proto );
470
-		ret=E_NO_SOCKET;
471
-		goto error01;
472
-	}
473
-	/* ... and build it now */
474
-	shbuf=build_req_buf_from_sip_req( i_req, &len, dst, BUILD_IN_SHM);
475
-	if (!shbuf) {
476
-		LM_ERR("could not build request\n");
477
-		ret=E_OUT_OF_MEM;
478
-		goto error01;
479
-	}
480
-#ifdef DBG_MSG_QA
481
-	if (shbuf[len-1]==0) {
482
-		LM_ERR("sanity check failed\n");
483
-		abort();
484
-	}
485
-#endif
486
-	/* things went well, move ahead and install new buffer! */
487
-	t->uac[branch].request.buffer=shbuf;
488
-	t->uac[branch].request.buffer_len=len;
489
-	t->uac[branch].uri.s=t->uac[branch].request.buffer+
490
-		i_req->first_line.u.request.method.len+1;
491
-	t->uac[branch].uri.len=GET_RURI(i_req)->len;
492
-	if (unlikely(i_req->path_vec.s && i_req->path_vec.len)){
493
-		t->uac[branch].path.s=shm_malloc(i_req->path_vec.len+1);
494
-		if (unlikely(t->uac[branch].path.s==0)) {
495
-			shm_free(shbuf);
496
-			t->uac[branch].request.buffer=0;
497
-			t->uac[branch].request.buffer_len=0;
498
-			t->uac[branch].uri.s=0;
499
-			t->uac[branch].uri.len=0;
500
-			ret=E_OUT_OF_MEM;
501
-			goto error01;
502
-		}
503
-		t->uac[branch].path.len=i_req->path_vec.len;
504
-		t->uac[branch].path.s[i_req->path_vec.len]=0;
505
-		memcpy( t->uac[branch].path.s, i_req->path_vec.s, i_req->path_vec.len);
506
-	}
507
-	if (unlikely(i_req->instance.s && i_req->instance.len)){
508
-		t->uac[branch].instance.s=shm_malloc(i_req->instance.len+1);
509
-		if (unlikely(t->uac[branch].instance.s==0)) {
510
-			shm_free(shbuf);
511
-			t->uac[branch].request.buffer=0;
512
-			t->uac[branch].request.buffer_len=0;
513
-			t->uac[branch].uri.s=0;
514
-			t->uac[branch].uri.len=0;
515
-			ret=E_OUT_OF_MEM;
516
-			goto error01;
517
-		}
518
-		t->uac[branch].instance.len=i_req->instance.len;
519
-		t->uac[branch].instance.s[i_req->instance.len]=0;
520
-		memcpy( t->uac[branch].instance.s, i_req->instance.s, i_req->instance.len);
521
-	}
522
-	if (unlikely(i_req->ruid.s && i_req->ruid.len)){
523
-		t->uac[branch].ruid.s=shm_malloc(i_req->ruid.len+1);
524
-		if (unlikely(t->uac[branch].ruid.s==0)) {
525
-			shm_free(shbuf);
526
-			t->uac[branch].request.buffer=0;
527
-			t->uac[branch].request.buffer_len=0;
528
-			t->uac[branch].uri.s=0;
529
-			t->uac[branch].uri.len=0;
530
-			ret=E_OUT_OF_MEM;
531
-			goto error01;
532
-		}
533
-		t->uac[branch].ruid.len=i_req->ruid.len;
534
-		t->uac[branch].ruid.s[i_req->ruid.len]=0;
535
-		memcpy( t->uac[branch].ruid.s, i_req->ruid.s, i_req->ruid.len);
536
-	}
537
-	if (unlikely(i_req->location_ua.s && i_req->location_ua.len)){
538
-		t->uac[branch].location_ua.s=shm_malloc(i_req->location_ua.len+1);
539
-		if (unlikely(t->uac[branch].location_ua.s==0)) {
540
-			shm_free(shbuf);
541
-			t->uac[branch].request.buffer=0;
542
-			t->uac[branch].request.buffer_len=0;
543
-			t->uac[branch].uri.s=0;
544
-			t->uac[branch].uri.len=0;
545
-			ret=E_OUT_OF_MEM;
546
-			goto error01;
547
-		}
548
-		t->uac[branch].location_ua.len=i_req->location_ua.len;
549
-		t->uac[branch].location_ua.s[i_req->location_ua.len]=0;
550
-		memcpy( t->uac[branch].location_ua.s, i_req->location_ua.s,
551
-				i_req->location_ua.len);
552
-	}
553
-
554
-	len = count_applied_lumps(i_req->add_rm, HDR_RECORDROUTE_T);
555
-	if(len==1)
556
-		t->uac[branch].flags = TM_UAC_FLAG_RR;
557
-	else if(len==2)
558
-		t->uac[branch].flags = TM_UAC_FLAG_RR|TM_UAC_FLAG_R2;
559
-
560
-	ret=0;
561
-
562
-error01:
563
-error03:
564
-	/* restore the new_uri & path from the backup */
565
-	if (unlikely(free_new_uri && i_req->new_uri.s)){
566
-		pkg_free(i_req->new_uri.s);
567
-	}
568
-	if (unlikely(free_path)){
569
-		reset_path_vector(i_req);
570
-	}
571
-	if (unlikely(free_instance)){
572
-		reset_instance(i_req);
573
-	}
574
-	if (unlikely(free_ruid)){
575
-		reset_ruid(i_req);
576
-	}
577
-	if (unlikely(free_ua)){
578
-		reset_ua(i_req);
579
-	}
580
-	if (dst_uri_backed_up){
581
-		reset_dst_uri(i_req); /* free dst_uri */
582
-		i_req->dst_uri=dst_uri_bak;
583
-	}
584
-	/* restore original new_uri and path values */
585
-	i_req->new_uri=msg_uri_bak;
586
-	i_req->parsed_uri=parsed_uri_bak;
587
-	i_req->parsed_uri_ok=parsed_uri_ok_bak;
588
-	i_req->path_vec=path_bak;
589
-	i_req->instance=instance_bak;
590
-	i_req->ruid=ruid_bak;
591
-	i_req->location_ua=ua_bak;
592
-
593
-	/* Delete the duplicated lump lists, this will also delete
594
-	 * all lumps created here, such as lumps created in per-branch
595
-	 * routing sections, Via, and Content-Length headers created in
596
-	 * build_req_buf_from_sip_req
597
-	 */
598
-error04:
599
-	free_duped_lump_list(i_req->add_rm);
600
-	free_duped_lump_list(i_req->body_lumps);
601
-	/* Restore the lists from backups */
602
-	i_req->add_rm = add_rm_backup;
603
-	i_req->body_lumps = body_lumps_backup;
604
-
605
-error00:
606
-	return ret;
607
-}
608
-
609
-#ifdef USE_DNS_FAILOVER
610
-/* Similar to print_uac_request(), but this function uses the outgoing message
611
- * buffer of the failed branch to construct the new message in case of DNS
612
- * failover.
613
- *
614
- * WARNING: only the first VIA header is replaced in the buffer, the rest
615
- * of the message is untouched, thus, the send socket is corrected only in the
616
- * VIA HF.
617
- */
618
-static char *print_uac_request_from_buf( struct cell *t, struct sip_msg *i_req,
619
-		int branch, str *uri, unsigned int *len, struct dest_info* dst,
620
-		char *buf, short buf_len)
621
-{
622
-	char *shbuf;
623
-	str branch_str;
624
-	char *via, *old_via_begin, *old_via_end;
625
-	unsigned int via_len;
626
-
627
-	shbuf=0;
628
-
629
-	/* ... we calculate branch ... */
630
-	if (!t_calc_branch(t, branch, i_req->add_to_branch_s,
631
-				&i_req->add_to_branch_len ))
632
-	{
633
-		LM_ERR("branch computation failed\n");
634
-		goto error00;
635
-	}
636
-	branch_str.s = i_req->add_to_branch_s;
637
-	branch_str.len = i_req->add_to_branch_len;
638
-
639
-	/* find the beginning of the first via header in the buffer */
640
-	old_via_begin = lw_find_via(buf, buf+buf_len);
641
-	if (!old_via_begin) {
642
-		LM_ERR("beginning of via header not found\n");
643
-		goto error00;
644
-	}
645
-	/* find the end of the first via header in the buffer */
646
-	old_via_end = lw_next_line(old_via_begin, buf+buf_len);
647
-	if (!old_via_end) {
648
-		LM_ERR("end of via header not found\n");
649
-		goto error00;
650
-	}
651
-
652
-	/* create the new VIA HF */
653
-	via = create_via_hf(&via_len, i_req, dst, &branch_str);
654
-	if (!via) {
655
-		LM_ERR("via building failed\n");
656
-		goto error00;
657
-	}
658
-
659
-	/* allocate memory for the new buffer */
660
-	*len = buf_len + via_len - (old_via_end - old_via_begin);
661
-	shbuf=(char *)shm_malloc(*len);
662
-	if (!shbuf) {
663
-		ser_error=E_OUT_OF_MEM;
664
-		LM_ERR("no shmem\n");
665
-		goto error01;
666
-	}
667
-
668
-	/* construct the new buffer */
669
-	memcpy(shbuf, buf, old_via_begin-buf);
670
-	memcpy(shbuf+(old_via_begin-buf), via, via_len);
671
-	memcpy(shbuf+(old_via_begin-buf)+via_len, old_via_end, (buf+buf_len)-old_via_end);
672
-
673
-#ifdef DBG_MSG_QA
674
-	if (shbuf[*len-1]==0) {
675
-		LM_ERR("sanity check failed\n");
676
-		abort();
677
-	}
678
-#endif
679
-
680
-error01:
681
-	pkg_free(via);
682
-error00:
683
-	return shbuf;
684
-}
685
-#endif
686
-
687
-/* introduce a new uac, which is blind -- it only creates the
688
- * data structures and starts FR timer, but that's it; it does
689
- * not print messages and send anything anywhere; that is good
690
- * for FIFO apps -- the transaction must look operationally
691
- * and FR must be ticking, whereas the request is "forwarded"
692
- * using a non-SIP way and will be replied the same way
693
- */
694
-int add_blind_uac( /*struct cell *t*/ )
695
-{
696
-	unsigned short branch;
697
-	struct cell *t;
698
-
699
-	t=get_t();
700
-	if (t==T_UNDEFINED || !t ) {
701
-		LM_ERR("no transaction context\n");
702
-		return -1;
703
-	}
704
-
705
-	branch=t->nr_of_outgoings;
706
-	if (branch==sr_dst_max_branches) {
707
-		LM_ERR("maximum number of branches exceeded\n");
708
-		return -1;
709
-	}
710
-	/* make sure it will be replied */
711
-	t->flags |= T_NOISY_CTIMER_FLAG;
712
-	membar_write(); /* to allow lockless prepare_to_cancel() we want to be sure
713
-					 * all the writes finished before updating branch number*/
714
-
715
-	t->uac[branch].flags |= TM_UAC_FLAG_BLIND;
716
-	t->nr_of_outgoings=(branch+1);
717
-	t->async_backup.blind_uac = branch;
718
-	/* ^^^ whenever we create a blind UAC, lets save the current branch
719
-	 * this is used in async tm processing specifically to be able to route replies
720
-	 * that were possibly in response to a request forwarded on this blind UAC......
721
-	 * we still want replies to be processed as if it were a normal UAC */
722
-
723
-	/* start FR timer -- protocol set by default to PROTO_NONE,
724
-	 * which means retransmission timer will not be started
725
-	 */
726
-	if (start_retr(&t->uac[branch].request)!=0)
727
-		LM_CRIT("start retr failed for %p\n", &t->uac[branch].request);
728
-	/* we are on a timer -- don't need to put on wait on script clean-up */
729
-	set_kr(REQ_FWDED);
730
-
731
-	return 1; /* success */
732
-}
733
-
734
-/** introduce a new uac to transaction.
735
- *  It doesn't send a message yet -- a reply to it might interfere with the
736
- *  processes of adding multiple branches; On error returns <0 & sets ser_error
737
- *  to the same value.
738
- *  @param t - transaction
739
- *  @param request - corresponding sip_mst, must be non-null, flags might be
740
- *                   modified (on_branch route).
741
- *  @param uri - uri used for the branch (must be non-null).
742
- *  @param next_hop - next_hop in sip uri format. If null and proxy is null
743
- *                    too, the uri will be used
744
- *  @param path     - path vector (list of route like destinations in sip
745
- *                     uri format, e.g.: "<sip:1.2.3.4;lr>, <sip:5.6.7.8;lr>").
746
- *  @param proxy    - proxy structure. If non-null it takes precedence over
747
- *                    next_hop/uri and it will be used for forwarding.
748
- *  @param fsocket  - forced forward send socket (can be 0).
749
- *  @param snd_flags - special send flags (see SND_F_* / snd_flags_t)
750
- *  @param proto    - forced protocol for forwarding (overrides the protocol
751
- *                    in next_hop/uri or proxy if != PROTO_NONE).
752
- *  @param flags    - special flags passed to prepare_new_uac().
753
- *                    @see prepare_new_uac().
754
- *  @returns branch id (>=0) or error (<0)
755
- */
756
-int add_uac( struct cell *t, struct sip_msg *request, str *uri,
757
-		str* next_hop, str* path, struct proxy_l *proxy,
758
-		struct socket_info* fsocket, snd_flags_t snd_flags,
759
-		int proto, int flags, str *instance, str *ruid,
760
-		str *location_ua)
761
-{
762
-
763
-	int ret;
764
-	unsigned short branch;
765
-
766
-	branch=t->nr_of_outgoings;
767
-	if (branch==sr_dst_max_branches) {
768
-		LM_ERR("maximum number of branches exceeded\n");
769
-		ret=ser_error=E_TOO_MANY_BRANCHES;
770
-		goto error;
771
-	}
772
-
773
-	/* check existing buffer -- rewriting should never occur */
774
-	if (t->uac[branch].request.buffer) {
775
-		LM_CRIT("buffer rewrite attempt\n");
776
-		ret=ser_error=E_BUG;
777
-		goto error;
778
-	}
779
-
780
-	/* check DNS resolution */
781
-	if (proxy){
782
-		/* dst filled from the proxy */
783
-		init_dest_info(&t->uac[branch].request.dst);
784
-		t->uac[branch].request.dst.proto=get_proto(proto, proxy->proto);
785
-		proxy2su(&t->uac[branch].request.dst.to, proxy);
786
-		/* fill dst send_sock */
787
-		t->uac[branch].request.dst.send_sock =
788
-			get_send_socket( request, &t->uac[branch].request.dst.to,
789
-					t->uac[branch].request.dst.proto);
790
-		if (request)
791
-			t->uac[branch].request.dst.send_flags=request->fwd_send_flags;
792
-		else
793
-			SND_FLAGS_INIT(&t->uac[branch].request.dst.send_flags);
794
-		next_hop=0;
795
-	}else {
796
-		next_hop= next_hop?next_hop:uri;
797
-	}
798
-
799
-	/* now message printing starts ... */
800
-	if (unlikely( (ret=prepare_new_uac(t, request, branch, uri, path,
801
-						next_hop, fsocket, snd_flags,
802
-						proto, flags, instance, ruid,
803
-						location_ua)) < 0)){
804
-		ser_error=ret;
805
-		goto error01;
806
-	}
807
-	getbflagsval(0, &t->uac[branch].branch_flags);
808
-	membar_write(); /* to allow lockless ops (e.g. prepare_to_cancel()) we want
809
-					 * to be sure everything above is fully written before
810
-					 * updating branches no. */
811
-	t->nr_of_outgoings=(branch+1);
812
-
813
-	/* update stats */
814
-	if (proxy){
815
-		proxy_mark(proxy, 1);
816
-	}
817
-	/* done! */
818
-	ret=branch;
819
-
820
-error01:
821
-error:
822
-	return ret;
823
-}
824
-
825
-
826
-
827
-#ifdef USE_DNS_FAILOVER
828
-/* Similar to add_uac(), but this function uses the outgoing message buffer
829
- * of the failed branch to construct the new message in case of DNS failover.
830
- */
831
-static int add_uac_from_buf( struct cell *t, struct sip_msg *request,
832
-		str *uri, str* path,
833
-		struct socket_info* fsocket,
834
-		snd_flags_t send_flags,
835
-		int proto,
836
-		char *buf, short buf_len,
837
-		str *instance, str *ruid,
838
-		str *location_ua)
839
-{
840
-
841
-	int ret;
842
-	unsigned short branch;
843
-	char *shbuf;
844
-	unsigned int len;
845
-
846
-	branch=t->nr_of_outgoings;
847
-	if (branch==sr_dst_max_branches) {
848
-		LM_ERR("maximum number of branches exceeded\n");
849
-		ret=ser_error=E_TOO_MANY_BRANCHES;
850
-		goto error;
851
-	}
852
-
853
-	/* check existing buffer -- rewriting should never occur */
854
-	if (t->uac[branch].request.buffer) {
855
-		LM_CRIT("buffer rewrite attempt\n");
856
-		ret=ser_error=E_BUG;
857
-		goto error;
858
-	}
859
-
860
-	if (uri2dst2(&t->uac[branch].dns_h, &t->uac[branch].request.dst,
861
-				fsocket, send_flags, uri, proto) == 0)
862
-	{
863
-		ret=ser_error=E_BAD_ADDRESS;
864
-		goto error;
865
-	}
866
-
867
-	/* check if send_sock is ok */
868
-	if (t->uac[branch].request.dst.send_sock==0) {
869
-		LM_ERR("can't fwd to af %d, proto %d"
870
-				" (no corresponding listening socket)\n",
871
-				t->uac[branch].request.dst.to.s.sa_family,
872
-				t->uac[branch].request.dst.proto );
873
-		ret=ser_error=E_NO_SOCKET;
874
-		goto error;
875
-	}
876
-
877
-	/* now message printing starts ... */
878
-	shbuf=print_uac_request_from_buf( t, request, branch, uri,
879
-			&len, &t->uac[branch].request.dst,
880
-			buf, buf_len);
881
-	if (!shbuf) {
882
-		ret=ser_error=E_OUT_OF_MEM;
883
-		goto error;
884
-	}
885
-
886
-	/* things went well, move ahead and install new buffer! */
887
-	t->uac[branch].request.buffer=shbuf;
888
-	t->uac[branch].request.buffer_len=len;
889
-	t->uac[branch].uri.s=t->uac[branch].request.buffer+
890
-		request->first_line.u.request.method.len+1;
891
-	t->uac[branch].uri.len=uri->len;
892
-	/* copy the path */
893
-	if (unlikely(path && path->s)){
894
-		t->uac[branch].path.s=shm_malloc(path->len+1);
895
-		if (unlikely(t->uac[branch].path.s==0)) {
896
-			shm_free(shbuf);
897
-			t->uac[branch].request.buffer=0;
898
-			t->uac[branch].request.buffer_len=0;
899
-			t->uac[branch].uri.s=0;
900
-			t->uac[branch].uri.len=0;