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,914 +0,0 @@
1
-/*
2
- * destination set
3
- *
4
- * Copyright (C) 2001-2004 FhG FOKUS
5
- *
6
- * This file is part of Kamailio, a free SIP server.
7
- *
8
- * Kamailio is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation; either version 2 of the License, or
11
- * (at your option) any later version
12
- *
13
- * Kamailio is distributed in the hope that it will be useful,
14
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
- * GNU General Public License for more details.
17
- *
18
- * You should have received a copy of the GNU General Public License 
19
- * along with this program; if not, write to the Free Software 
20
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21
- */
22
-
23
-/** Kamailio core :: destination set / branches support.
24
- * @file dset.c
25
- * @ingroup core
26
- * Module: @ref core
27
- */
28
-
29
-#include <string.h>
30
-#include "dprint.h"
31
-#include "config.h"
32
-#include "parser/parser_f.h"
33
-#include "parser/parse_uri.h"
34
-#include "parser/msg_parser.h"
35
-#include "globals.h"
36
-#include "ut.h"
37
-#include "hash_func.h"
38
-#include "error.h"
39
-#include "dset.h"
40
-#include "mem/mem.h"
41
-#include "ip_addr.h"
42
-
43
-#define CONTACT "Contact: "
44
-#define CONTACT_LEN (sizeof(CONTACT) - 1)
45
-
46
-#define CONTACT_DELIM ", "
47
-#define CONTACT_DELIM_LEN (sizeof(CONTACT_DELIM) - 1)
48
-
49
-#define Q_PARAM ">;q="
50
-#define Q_PARAM_LEN (sizeof(Q_PARAM) - 1)
51
-
52
-
53
-/* 
54
- * Where we store URIs of additional transaction branches
55
- * (sr_dst_max_branches - 1 : because of the default branch for r-uri, #0 in tm)
56
- */
57
-static struct branch *branches = NULL;
58
-
59
-/* how many of them we have */
60
-unsigned int nr_branches = 0;
61
-
62
-/* branch iterator */
63
-static int branch_iterator = 0;
64
-
65
-/* used to mark ruris "consumed" when branching (1 new, 0 consumed) */
66
-int ruri_is_new = 0;
67
-
68
-/* The q parameter of the Request-URI */
69
-static qvalue_t ruri_q = Q_UNSPECIFIED;
70
-
71
-/* Branch flags of the Request-URI */
72
-static flag_t ruri_bflags;
73
-
74
-
75
-int init_dst_set(void)
76
-{
77
-	if(sr_dst_max_branches<=0 || sr_dst_max_branches>=MAX_BRANCHES_LIMIT) {
78
-		LM_ERR("invalid value for max branches parameter: %u\n",
79
-				sr_dst_max_branches);
80
-		return -1;
81
-	}
82
-	/* sr_dst_max_branches - 1 : because of the default branch for r-uri, #0 in tm */
83
-	branches = (branch_t*)pkg_malloc((sr_dst_max_branches-1)*sizeof(branch_t));
84
-	if(branches==NULL) {
85
-		LM_ERR("not enough memory to initialize destination branches\n");
86
-		return -1;
87
-	}
88
-	memset(branches, 0, (sr_dst_max_branches-1)*sizeof(branch_t));
89
-	return 0;
90
-}
91
-
92
-/*! \brief
93
- * Return pointer to branch[idx] structure
94
- * @param idx - branch index
95
- *
96
- * @return  pointer to branch or NULL if invalid branch
97
- */
98
-branch_t *get_sip_branch(int idx)
99
-{
100
-	if(nr_branches==0)
101
-		return NULL;
102
-	if(idx<0)
103
-	{
104
-		if((int)nr_branches + idx >= 0)
105
-			return &branches[nr_branches+idx];
106
-		return NULL;
107
-	}
108
-	if(idx < nr_branches)
109
-		return &branches[idx];
110
-	return 0;
111
-}
112
-
113
-/*! \brief
114
- * Drop branch[idx]
115
- * @param idx - branch index
116
- *
117
- * @return  0 on success, -1 on error
118
- */
119
-int drop_sip_branch(int idx)
120
-{
121
-	if(nr_branches==0 || idx>=nr_branches)
122
-		return 0;
123
-	if(idx<0 && (int)nr_branches+idx<0)
124
-		return 0;
125
-	if(idx<0)
126
-		idx += nr_branches;
127
-	/* last branch */
128
-	if(idx==nr_branches-1)
129
-	{
130
-		nr_branches--;
131
-		return 0;
132
-	}
133
-	/* shift back one position */
134
-	for(; idx<nr_branches-1; idx++)
135
-		memcpy(&branches[idx], &branches[idx+1], sizeof(branch_t));
136
-	nr_branches--;
137
-	return 0;
138
-}
139
-
140
-static inline flag_t* get_bflags_ptr(unsigned int branch)
141
-{
142
-	if (branch == 0) return &ruri_bflags;
143
-	if (branch - 1 < nr_branches) return &branches[branch - 1].flags;
144
-	return NULL;
145
-}
146
-
147
-
148
-int setbflag(unsigned int branch, flag_t flag)
149
-{
150
-	flag_t* flags;
151
-
152
-	if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
153
-	(*flags) |= 1 << flag;
154
-	return 1;
155
-}
156
-
157
-
158
-int isbflagset(unsigned int branch, flag_t flag)
159
-{
160
-	flag_t* flags;
161
-
162
-	if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
163
-	return ((*flags) & (1 << flag)) ? 1 : -1;
164
-}
165
-
166
-
167
-int resetbflag(unsigned int branch, flag_t flag)
168
-{
169
-	flag_t* flags;
170
-
171
-	if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
172
-	(*flags) &= ~ (1 << flag);
173
-	return 1;
174
-}
175
-
176
-
177
-int getbflagsval(unsigned int branch, flag_t* res)
178
-{
179
-	flag_t* flags;
180
-	if (res == NULL) return -1;
181
-	if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
182
-	*res = *flags;
183
-	return 1;
184
-}
185
-
186
-
187
-int setbflagsval(unsigned int branch, flag_t val)
188
-{
189
-	flag_t* flags;
190
-	if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
191
-	*flags = val;
192
-	return 1;
193
-}
194
-
195
-
196
-/*
197
- * Initialize the branch iterator, the next
198
- * call to next_branch will return the first
199
- * contact from the dset array
200
- */
201
-void init_branch_iterator(void)
202
-{
203
-	branch_iterator = 0;
204
-}
205
-
206
-/**
207
- * return the value of current branch iterator
208
- */
209
-int get_branch_iterator(void)
210
-{
211
-	return branch_iterator;
212
-}
213
-
214
-/**
215
- * set the value of current branch interator
216
- */
217
-void set_branch_iterator(int n)
218
-{
219
-	branch_iterator = n;
220
-}
221
-
222
-
223
-/** \brief Get a branch from the destination set
224
- * \return Return the 'i' branch from the dset
225
- * array, 0 is returned if there are no
226
- * more branches
227
- */
228
-char* get_branch(unsigned int i, int* len, qvalue_t* q, str* dst_uri,
229
-		 str* path, unsigned int *flags,
230
-		 struct socket_info** force_socket,
231
-		 str *ruid, str *instance, str *location_ua)
232
-{
233
-	if (i < nr_branches) {
234
-		*len = branches[i].len;
235
-		*q = branches[i].q;
236
-		if (dst_uri) {
237
-			dst_uri->len = branches[i].dst_uri_len;
238
-			dst_uri->s = (dst_uri->len)?branches[i].dst_uri:0;
239
-		}
240
-		if (path) {
241
-			path->len = branches[i].path_len;
242
-			path->s = (path->len)?branches[i].path:0;
243
-		}
244
-		if (force_socket)
245
-			*force_socket = branches[i].force_send_socket;
246
-		if (flags)
247
-			*flags = branches[i].flags;
248
-		if (ruid) {
249
-			ruid->len = branches[i].ruid_len;
250
-			ruid->s = (ruid->len)?branches[i].ruid:0;
251
-		}
252
-		if (instance) {
253
-			instance->len = branches[i].instance_len;
254
-			instance->s = (instance->len)?branches[i].instance:0;
255
-		}
256
-		if (location_ua) {
257
-			location_ua->len = branches[i].location_ua_len;
258
-			location_ua->s
259
-				= (location_ua->len)?branches[i].location_ua:0;
260
-		}
261
-		return branches[i].uri;
262
-	} else {
263
-		*len = 0;
264
-		*q = Q_UNSPECIFIED;
265
-		if (dst_uri) {
266
-			dst_uri->s = 0;
267
-			dst_uri->len = 0;
268
-		}
269
-		if (path) {
270
-			path->s = 0;
271
-			path->len = 0;
272
-		}
273
-		if (force_socket)
274
-			*force_socket = 0;
275
-		if (flags)
276
-			*flags = 0;
277
-		if (ruid) {
278
-			ruid->s = 0;
279
-			ruid->len = 0;
280
-		}
281
-		if (instance) {
282
-			instance->s = 0;
283
-			instance->len = 0;
284
-		}
285
-		if (location_ua) {
286
-			location_ua->s = 0;
287
-			location_ua->len = 0;
288
-		}
289
-		return 0;
290
-	}
291
-}
292
-
293
-
294
-
295
-/** Return the next branch from the dset array.
296
- * 0 is returned if there are no more branches
297
- */
298
-char* next_branch(int* len, qvalue_t* q, str* dst_uri, str* path,
299
-		  unsigned int* flags, struct socket_info** force_socket,
300
-		  str* ruid, str *instance, str *location_ua)
301
-{
302
-	char* ret;
303
-	
304
-	ret=get_branch(branch_iterator, len, q, dst_uri, path, flags,
305
-		       force_socket, ruid, instance, location_ua);
306
-	if (likely(ret))
307
-		branch_iterator++;
308
-	return ret;
309
-}
310
-
311
-
312
-/*
313
- * Empty the dset array
314
- */
315
-void clear_branches(void)
316
-{
317
-	nr_branches = 0;
318
-	ruri_q = Q_UNSPECIFIED;
319
-	ruri_bflags = 0;
320
-	ruri_mark_consumed();
321
-}
322
-
323
-
324
-
325
-/**  Add a new branch to the current transaction.
326
- * @param msg - sip message, used for getting the uri if not specified (0).
327
- * @param uri - uri, can be 0 (in which case the uri is taken from msg)
328
- * @param dst_uri - destination uri, can be 0.
329
- * @param path - path vector (passed in a string), can be 0.
330
- * @param q  - q value.
331
- * @param flags - per branch flags.
332
- * @param force_socket - socket that should be used when sending.
333
- *
334
- * @return  <0 (-1) on failure, 1 on success (script convention).
335
- */
336
-int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
337
-		  qvalue_t q, unsigned int flags,
338
-		  struct socket_info* force_socket,
339
-		  str* instance, unsigned int reg_id,
340
-		  str* ruid, str* location_ua)
341
-{
342
-	str luri;
343
-
344
-	/* if we have already set up the maximum number
345
-	 * of branches, don't try new ones 
346
-	 */
347
-	if (unlikely(nr_branches == sr_dst_max_branches - 1)) {
348
-		LM_ERR("max nr of branches exceeded\n");
349
-		ser_error = E_TOO_MANY_BRANCHES;
350
-		return -1;
351
-	}
352
-
353
-	/* if not parameterized, take current uri */
354
-	if (uri==0 || uri->len==0 || uri->s==0) {
355
-		if(msg==NULL) {
356
-			LM_ERR("no new uri and no msg to take r-uri\n");
357
-			ser_error = E_INVALID_PARAMS;
358
-			return -1;
359
-		}
360
-		if (msg->new_uri.s)
361
-			luri = msg->new_uri;
362
-		else
363
-			luri = msg->first_line.u.request.uri;
364
-	} else {
365
-		luri = *uri;
366
-	}
367
-
368
-	if (unlikely(luri.len > MAX_URI_SIZE - 1)) {
369
-		LM_ERR("too long uri: %.*s\n", luri.len, luri.s);
370
-		return -1;
371
-	}
372
-
373
-	/* copy the dst_uri */
374
-	if (dst_uri && dst_uri->len && dst_uri->s) {
375
-		if (unlikely(dst_uri->len > MAX_URI_SIZE - 1)) {
376
-			LM_ERR("too long dst_uri: %.*s\n", dst_uri->len, dst_uri->s);
377
-			return -1;
378
-		}
379
-		memcpy(branches[nr_branches].dst_uri, dst_uri->s, dst_uri->len);
380
-		branches[nr_branches].dst_uri[dst_uri->len] = 0;
381
-		branches[nr_branches].dst_uri_len = dst_uri->len;
382
-	} else {
383
-		branches[nr_branches].dst_uri[0] = '\0';
384
-		branches[nr_branches].dst_uri_len = 0;
385
-	}
386
-
387
-	/* copy the path string */
388
-	if (unlikely(path && path->len && path->s)) {
389
-		if (unlikely(path->len > MAX_PATH_SIZE - 1)) {
390
-			LM_ERR("too long path: %.*s\n", path->len, path->s);
391
-			return -1;
392
-		}
393
-		memcpy(branches[nr_branches].path, path->s, path->len);
394
-		branches[nr_branches].path[path->len] = 0;
395
-		branches[nr_branches].path_len = path->len;
396
-	} else {
397
-		branches[nr_branches].path[0] = '\0';
398
-		branches[nr_branches].path_len = 0;
399
-	}
400
-
401
-	/* copy the ruri */
402
-	memcpy(branches[nr_branches].uri, luri.s, luri.len);
403
-	branches[nr_branches].uri[luri.len] = 0;
404
-	branches[nr_branches].len = luri.len;
405
-	branches[nr_branches].q = q;
406
-
407
-	branches[nr_branches].force_send_socket = force_socket;
408
-	branches[nr_branches].flags = flags;
409
-
410
-	/* copy instance string */
411
-	if (unlikely(instance && instance->len && instance->s)) {
412
-		if (unlikely(instance->len > MAX_INSTANCE_SIZE - 1)) {
413
-			LM_ERR("too long instance: %.*s\n",
414
-			    instance->len, instance->s);
415
-			return -1;
416
-		}
417
-		memcpy(branches[nr_branches].instance, instance->s,
418
-		       instance->len);
419
-		branches[nr_branches].instance[instance->len] = 0;
420
-		branches[nr_branches].instance_len = instance->len;
421
-	} else {
422
-		branches[nr_branches].instance[0] = '\0';
423
-		branches[nr_branches].instance_len = 0;
424
-	}
425
-
426
-	/* copy reg_id */
427
-	branches[nr_branches].reg_id = reg_id;
428
-
429
-	/* copy ruid string */
430
-	if (unlikely(ruid && ruid->len && ruid->s)) {
431
-		if (unlikely(ruid->len > MAX_RUID_SIZE - 1)) {
432
-			LM_ERR("too long ruid: %.*s\n",
433
-			    ruid->len, ruid->s);
434
-			return -1;
435
-		}
436
-		memcpy(branches[nr_branches].ruid, ruid->s,
437
-		       ruid->len);
438
-		branches[nr_branches].ruid[ruid->len] = 0;
439
-		branches[nr_branches].ruid_len = ruid->len;
440
-	} else {
441
-		branches[nr_branches].ruid[0] = '\0';
442
-		branches[nr_branches].ruid_len = 0;
443
-	}
444
-
445
-	if (unlikely(location_ua && location_ua->len && location_ua->s)) {
446
-		if (unlikely(location_ua->len > MAX_UA_SIZE)) {
447
-			LM_ERR("too long location_ua: %.*s\n",
448
-			    location_ua->len, location_ua->s);
449
-			return -1;
450
-		}
451
-		memcpy(branches[nr_branches].location_ua, location_ua->s,
452
-		       location_ua->len);
453
-		branches[nr_branches].location_ua[location_ua->len] = 0;
454
-		branches[nr_branches].location_ua_len = location_ua->len;
455
-	} else {
456
-		branches[nr_branches].location_ua[0] = '\0';
457
-		branches[nr_branches].location_ua_len = 0;
458
-	}
459
-	
460
-	nr_branches++;
461
-	return 1;
462
-}
463
-
464
-
465
-/*
466
- * Create a Contact header field from the dset
467
- * array
468
- */
469
-char* print_dset(struct sip_msg* msg, int* len) 
470
-{
471
-	int cnt, i;
472
-	unsigned int qlen;
473
-	qvalue_t q;
474
-	str uri;
475
-	char* p, *qbuf;
476
-	int crt_branch;
477
-	static char dset[MAX_REDIRECTION_LEN];
478
-
479
-	if (msg->new_uri.s) {
480
-		cnt = 1;
481
-		*len = msg->new_uri.len + 1 /*'<'*/;
482
-		if (ruri_q != Q_UNSPECIFIED) {
483
-			*len += Q_PARAM_LEN + len_q(ruri_q);
484
-		} else {
485
-			*len += 1 /*'>'*/;
486
-		}
487
-	} else {
488
-		cnt = 0;
489
-		*len = 0;
490
-	}
491
-
492
-	/* backup current branch index to restore it later */
493
-	crt_branch = get_branch_iterator();
494
-
495
-	init_branch_iterator();
496
-	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0, 0, 0, 0))) {
497
-		cnt++;
498
-		*len += uri.len + 1 /*'<'*/;
499
-		if (q != Q_UNSPECIFIED) {
500
-			*len += Q_PARAM_LEN + len_q(q);
501
-		} else {
502
-			*len += 1 /*'>'*/;
503
-		}
504
-	}
505
-
506
-	if (cnt == 0) return 0;	
507
-
508
-	*len += CONTACT_LEN + CRLF_LEN + (cnt - 1) * CONTACT_DELIM_LEN;
509
-
510
-	if (*len + 1 > MAX_REDIRECTION_LEN) {
511
-		LM_ERR("redirection buffer length exceed\n");
512
-		goto error;
513
-	}
514
-
515
-	memcpy(dset, CONTACT, CONTACT_LEN);
516
-	p = dset + CONTACT_LEN;
517
-	if (msg->new_uri.s) {
518
-		*p++ = '<';
519
-
520
-		memcpy(p, msg->new_uri.s, msg->new_uri.len);
521
-		p += msg->new_uri.len;
522
-
523
-		if (ruri_q != Q_UNSPECIFIED) {
524
-			memcpy(p, Q_PARAM, Q_PARAM_LEN);
525
-			p += Q_PARAM_LEN;
526
-
527
-			qbuf = q2str(ruri_q, &qlen);
528
-			memcpy(p, qbuf, qlen);
529
-			p += qlen;
530
-		} else {
531
-			*p++ = '>';
532
-		}
533
-		i = 1;
534
-	} else {
535
-		i = 0;
536
-	}
537
-
538
-	init_branch_iterator();
539
-	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0, 0, 0, 0))) {
540
-		if (i) {
541
-			memcpy(p, CONTACT_DELIM, CONTACT_DELIM_LEN);
542
-			p += CONTACT_DELIM_LEN;
543
-		}
544
-
545
-		*p++ = '<';
546
-
547
-		memcpy(p, uri.s, uri.len);
548
-		p += uri.len;
549
-		if (q != Q_UNSPECIFIED) {
550
-			memcpy(p, Q_PARAM, Q_PARAM_LEN);
551
-			p += Q_PARAM_LEN;
552
-
553
-			qbuf = q2str(q, &qlen);
554
-			memcpy(p, qbuf, qlen);
555
-			p += qlen;
556
-		} else {
557
-			*p++ = '>';
558
-		}
559
-		i++;
560
-	}
561
-
562
-	memcpy(p, CRLF " ", CRLF_LEN + 1);
563
-	set_branch_iterator(crt_branch);
564
-	return dset;
565
-
566
-error:
567
-	set_branch_iterator(crt_branch);
568
-	return 0;
569
-}
570
-
571
-
572
-/*
573
- * Sets the q parameter of the Request-URI
574
- */
575
-void set_ruri_q(qvalue_t q)
576
-{
577
-	ruri_q = q;
578
-}
579
-
580
-
581
-/*
582
- * Return the q value of the Request-URI
583
- */
584
-qvalue_t get_ruri_q(void)
585
-{
586
-	return ruri_q;
587
-}
588
-
589
-
590
-
591
-/*
592
- * Rewrite Request-URI
593
- */
594
-int rewrite_uri(struct sip_msg* _m, str* _s)
595
-{
596
-	char *buf = NULL;
597
-
598
-	if(_m->new_uri.s==NULL || _m->new_uri.len<_s->len) {
599
-		buf = (char*)pkg_malloc(_s->len + 1);
600
-		if (!buf) {
601
-			LM_ERR("No memory left to rewrite r-uri\n");
602
-			return -1;
603
-		}
604
-	}
605
-	if(buf!=NULL) {
606
-		if(_m->new_uri.s)
607
-			pkg_free(_m->new_uri.s);
608
-	} else {
609
-		buf = _m->new_uri.s;
610
-	}
611
-
612
-	memcpy(buf, _s->s, _s->len);
613
-	buf[_s->len] = '\0';
614
-
615
-	_m->parsed_uri_ok = 0;
616
-
617
-	_m->new_uri.s = buf;
618
-	_m->new_uri.len = _s->len;
619
-	/* mark ruri as new and available for forking */
620
-	ruri_mark_new();
621
-
622
-	return 1;
623
-}
624
-
625
-/**
626
- * return src ip, port and proto as a SIP uri or proxy address
627
- * - value stored in a static buffer
628
- * - mode=0 return uri, mode=1 return proxy address
629
- */
630
-int msg_get_src_addr(sip_msg_t *msg, str *uri, int mode)
631
-{
632
-	static char buf[80];
633
-	char* p;
634
-	str ip, port;
635
-	int len;
636
-	str proto;
637
-
638
-	if (msg==NULL || uri==NULL) {
639
-		LM_ERR("invalid parameter value\n");
640
-		return -1;
641
-	}
642
-
643
-	ip.s = ip_addr2a(&msg->rcv.src_ip);
644
-	ip.len = strlen(ip.s);
645
-
646
-	port.s = int2str(msg->rcv.src_port, &port.len);
647
-
648
-	switch(msg->rcv.proto) {
649
-		case PROTO_NONE:
650
-		case PROTO_UDP:
651
-			if(mode==0) {
652
-				proto.s = 0; /* Do not add transport parameter, UDP is default */
653
-				proto.len = 0;
654
-			} else {
655
-				proto.s = "udp";
656
-				proto.len = 3;
657
-			}
658
-		break;
659
-
660
-		case PROTO_TCP:
661
-			proto.s = "tcp";
662
-			proto.len = 3;
663
-		break;
664
-
665
-		case PROTO_TLS:
666
-			proto.s = "tls";
667
-			proto.len = 3;
668
-		break;
669
-
670
-		case PROTO_SCTP:
671
-			proto.s = "sctp";
672
-			proto.len = 4;
673
-		break;
674
-
675
-		case PROTO_WS:
676
-		case PROTO_WSS:
677
-			proto.s = "ws";
678
-			proto.len = 2;
679
-		break;
680
-
681
-		default:
682
-			LM_ERR("unknown transport protocol\n");
683
-		return -1;
684
-	}
685
-
686
-	len = ip.len + 2*(msg->rcv.src_ip.af==AF_INET6)+ 1 + port.len;
687
-	if (mode==0) {
688
-		len += 4;
689
-		if(proto.s) {
690
-			len += TRANSPORT_PARAM_LEN;
691
-			len += proto.len;
692
-		}
693
-	} else {
694
-		len += proto.len + 1;
695
-	}
696
-
697
-	if (len > 79) {
698
-		LM_ERR("buffer too small\n");
699
-		return -1;
700
-	}
701
-
702
-	p = buf;
703
-	if(mode==0) {
704
-		memcpy(p, "sip:", 4);
705
-		p += 4;
706
-	} else {
707
-		memcpy(p, proto.s, proto.len);
708
-		p += proto.len;
709
-		*p++ = ':';
710
-	}
711
-
712
-	if (msg->rcv.src_ip.af==AF_INET6)
713
-		*p++ = '[';
714
-	memcpy(p, ip.s, ip.len);
715
-	p += ip.len;
716
-	if (msg->rcv.src_ip.af==AF_INET6)
717
-		*p++ = ']';
718
-
719
-	*p++ = ':';
720
-
721
-	memcpy(p, port.s, port.len);
722
-	p += port.len;
723
-
724
-	if (mode==0 && proto.s) {
725
-		memcpy(p, TRANSPORT_PARAM, TRANSPORT_PARAM_LEN);
726
-		p += TRANSPORT_PARAM_LEN;
727
-
728
-		memcpy(p, proto.s, proto.len);
729
-		p += proto.len;
730
-	}
731
-
732
-	uri->s = buf;
733
-	uri->len = len;
734
-	uri->s[uri->len] = '\0';
735
-
736
-	return 0;
737
-}
738
-
739
-/**
740
- * add alias parameter with encoding of source address
741
- * - nuri->s must point to a buffer of nuri->len size
742
- */
743
-int uri_add_rcv_alias(sip_msg_t *msg, str *uri, str *nuri)
744
-{
745
-	char* p;
746
-	str ip, port;
747
-	int len;
748
-
749
-	if (msg==NULL || uri==NULL || nuri==NULL) {
750
-		LM_ERR("invalid parameter value\n");
751
-		return -1;
752
-	}
753
-
754
-	ip.s = ip_addr2a(&msg->rcv.src_ip);
755
-	ip.len = strlen(ip.s);
756
-
757
-	port.s = int2str(msg->rcv.src_port, &port.len);
758
-
759
-	/*uri;alias=[ip]~port~proto*/
760
-	len = uri->len+ip.len+port.len+12;
761
-	if(len>=nuri->len) {
762
-		LM_ERR("not enough space for new uri: %d\n", len);
763
-		return -1;
764
-	}
765
-	p = nuri->s;
766
-	memcpy(p, uri->s, uri->len);
767
-	p += uri->len;
768
-	memcpy(p, ";alias=", 7);
769
-	p += 7;
770
-	if (msg->rcv.src_ip.af == AF_INET6)
771
-		*p++ = '[';
772
-	memcpy(p, ip.s, ip.len);
773
-	p += ip.len;
774
-	if (msg->rcv.src_ip.af == AF_INET6)
775
-		*p++ = ']';
776
-	*p++ = '~';
777
-	memcpy(p, port.s, port.len);
778
-	p += port.len;
779
-	*p++ = '~';
780
-	*p++ = msg->rcv.proto + '0';
781
-	nuri->len = p - nuri->s;
782
-	nuri->s[nuri->len] = '\0';
783
-
784
-	LM_DBG("encoded <%.*s> => [%.*s]\n",
785
-			uri->len, uri->s, nuri->len, nuri->s);
786
-	return 0;
787
-}
788
-
789
-/**
790
- * restore from alias parameter with encoding of source address
791
- * - nuri->s must point to a buffer of nuri->len size
792
- * - suri->s must point to a buffer of suri->len size
793
- */
794
-int uri_restore_rcv_alias(str *uri, str *nuri, str *suri)
795
-{
796
-	char* p;
797
-	str skip;
798
-	str ip, port, sproto;
799
-	int proto;
800
-
801
-	if (uri==NULL || nuri==NULL || suri==NULL) {
802
-		LM_ERR("invalid parameter value\n");
803
-		return -1;
804
-	}
805
-
806
-	/* sip:x;alias=1.1.1.1~0~0 */
807
-	if(uri->len < 23) {
808
-		/* no alias possible */
809
-		return -2;
810
-	}
811
-	p = uri->s + uri->len-18;
812
-	skip.s = 0;
813
-	while(p>uri->s+5) {
814
-		if(strncmp(p, ";alias=", 7)==0) {
815
-			skip.s = p;
816
-			break;
817
-		}
818
-		p--;
819
-	}
820
-	if(skip.s==0) {
821
-		/* alias parameter not found */
822
-		return -2;
823
-	}
824
-	p += 7;
825
-	ip.s = p;
826
-	p = (char*)memchr(ip.s, '~', (size_t)(uri->s+uri->len-ip.s));
827
-	if(p==NULL) {
828
-		/* proper alias parameter not found */
829
-		return -2;
830
-	}
831
-	ip.len = p - ip.s;
832
-	p++;
833
-	if(p>=uri->s+uri->len) {
834
-		/* proper alias parameter not found */
835
-		return -2;
836
-	}
837
-	port.s = p;
838
-	p = (char*)memchr(port.s, '~', (size_t)(uri->s+uri->len-port.s));
839
-	if(p==NULL) {
840
-		/* proper alias parameter not found */
841
-		return -2;
842
-	}
843
-	port.len = p - port.s;
844
-	p++;
845
-	if(p>=uri->s+uri->len) {
846
-		/* proper alias parameter not found */
847
-		return -2;
848
-	}
849
-	proto = (int)(*p - '0');
850
-	p++;
851
-
852
-	if(p!=uri->s+uri->len && *p!=';') {
853
-		/* proper alias parameter not found */
854
-		return -2;
855
-	}
856
-	skip.len = (int)(p - skip.s);
857
-
858
-	if(suri->len<=4+ip.len+1+port.len+11/*;transport=*/+4) {
859
-		LM_ERR("address buffer too small\n");
860
-		return -1;
861
-	}
862
-	if(nuri->len<=uri->len - skip.len) {
863
-		LM_ERR("uri buffer too small\n");
864
-		return -1;
865
-	}
866
-
867
-	p = nuri->s;
868
-	memcpy(p, uri->s, (size_t)(skip.s-uri->s));
869
-	p += skip.s-uri->s;
870
-	memcpy(p, skip.s+skip.len, (size_t)(uri->s+uri->len - skip.s - skip.len));
871
-	p += uri->s+uri->len - skip.s - skip.len;
872
-	nuri->len = p - nuri->s;
873
-
874
-	p = suri->s;
875
-	strncpy(p, "sip:", 4);
876
-	p += 4;
877
-	strncpy(p, ip.s, ip.len);
878
-	p += ip.len;
879
-	*p++ = ':';
880
-	strncpy(p, port.s, port.len);
881
-	p += port.len;
882
-	proto_type_to_str((unsigned short)proto, &sproto);
883
-	if(sproto.len>0 && proto!=PROTO_UDP) {
884
-		strncpy(p, ";transport=", 11);
885
-		p += 11;
886
-		strncpy(p, sproto.s, sproto.len);
887
-		p += sproto.len;
888
-	}
889
-	suri->len = p - suri->s;
890
-
891
-	LM_DBG("decoded <%.*s> => [%.*s] [%.*s]\n",
892
-			uri->len, uri->s, nuri->len, nuri->s, suri->len, suri->s);
893
-
894
-	return 0;
895
-}
896
-
897
-/* address of record (aor) management */
898
-
899
-/* address of record considered case sensitive
900
- * - 0 = no; 1 = yes */
901
-static int aor_case_sensitive=0;
902
-
903
-int set_aor_case_sensitive(int mode)
904
-{
905
-	int r;
906
-	r = aor_case_sensitive;
907
-	aor_case_sensitive = mode;
908
-	return r;