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,1731 +0,0 @@
1
-/*
2
- * Copyright (C) 2005-2006 iptelorg GmbH
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
-/*!
23
- * \file
24
- * \brief Kamailio core :: select framework, basic core functions (mma)
25
- * \ingroup core
26
- * Module: \ref core
27
- */
28
-
29
-#include <stdlib.h> 
30
-#include "select.h"
31
-#include "select_core.h"
32
-#include "select_buf.h"
33
-#include "dprint.h"
34
-#include "trim.h"
35
-#include "ut.h"
36
-#include "globals.h"
37
-#include "parser/parser_f.h"
38
-#include "parser/hf.h"
39
-#include "parser/parse_from.h"
40
-#include "parser/parse_to.h"
41
-#include "parser/contact/parse_contact.h"
42
-#include "parser/contact/contact.h"
43
-#include "parser/parse_via.h"
44
-#include "parser/parse_uri.h"
45
-#include "parser/parse_event.h"
46
-#include "parser/parse_rr.h"
47
-#include "parser/digest/digest.h"
48
-#include "mem/mem.h"
49
-#include "parser/parse_hname2.h"
50
-#include "ip_addr.h"
51
-#include "parser/parse_expires.h"
52
-#include "parser/parse_refer_to.h"
53
-#include "parser/parse_rpid.h"
54
-#include "parser/parse_content.h"
55
-#include "parser/parse_body.h"
56
-#include "dset.h"
57
-#include "sr_module.h"
58
-#include "resolve.h"
59
-#include "forward.h"
60
-#include "rand/kam_rand.h"
61
-
62
-#define RETURN0_res(x) {*res=(x);return 0;}
63
-#define TRIM_RET0_res(x) {*res=(x);trim(res);return 0;} 
64
-#define TEST_RET_res_body(x) if (x){*res=(x)->body;return 0;}else return 1;
65
-#define TEST_RET_res_value(x) if (x){*res=(x)->value;return 0;}else return 1;
66
-
67
-int select_ruri(str* res, select_t* s, struct sip_msg* msg)
68
-{
69
-	/* Parse the RURI even if it is not needed right now
70
-	 * because the nested select calls can access the
71
-	 * parsed URI in this case.
72
-	 * Go ahead even if the parsing fails, so the
73
-	 * value of the broken RURI can be accessed at least.
74
-	 * Subsequent select calls will fail when they try to parse
75
-	 * the URI anyway. (Miklos)
76
-	 */
77
-	parse_sip_msg_uri(msg);
78
-
79
-	if (msg->parsed_uri_ok)
80
-		select_uri_p = &msg->parsed_uri;
81
-
82
-	if (msg->first_line.type==SIP_REQUEST) {
83
-		if(msg->new_uri.s) {
84
-			RETURN0_res(msg->new_uri);
85
-		}
86
-		else {
87
-			RETURN0_res(msg->first_line.u.request.uri);
88
-		}
89
-	}
90
-	return -1;
91
-}
92
-
93
-int select_dst_uri(str* res, select_t* s, struct sip_msg* msg)
94
-{
95
-	if (msg->first_line.type!=SIP_REQUEST)
96
-		return -1;
97
-	RETURN0_res(msg->dst_uri);
98
-}
99
-
100
-int select_next_hop(str* res, select_t* s, struct sip_msg* msg)
101
-{
102
-	if (msg->first_line.type==SIP_REQUEST) {
103
-		if(msg->dst_uri.s) {
104
-			RETURN0_res(msg->dst_uri);
105
-		}
106
-		else if(msg->new_uri.s) {
107
-			if (msg->parsed_uri_ok)
108
-				select_uri_p = &msg->parsed_uri;
109
-			RETURN0_res(msg->new_uri);
110
-		}
111
-		else {
112
-			if (msg->parsed_uri_ok)
113
-				select_uri_p = &msg->parsed_uri;
114
-			else if (msg->parsed_orig_ruri_ok)
115
-				select_uri_p = &msg->parsed_orig_ruri;
116
-			RETURN0_res(msg->first_line.u.request.uri);
117
-		}
118
-	}
119
-	return -1;
120
-}
121
-
122
-int select_next_hop_src_ip(str* res, select_t* s, struct sip_msg* msg) {
123
-	struct socket_info* socket_info;
124
-	union sockaddr_union to;
125
-	char proto;
126
-	struct sip_uri *u, next_hop;
127
-	str *dst_host;
128
-
129
-	if (msg->first_line.type!=SIP_REQUEST) 
130
-		return -1;
131
-
132
-	if (msg->force_send_socket) {
133
-		*res = msg->force_send_socket->address_str;
134
-		return 0;
135
-	}
136
-
137
-	if (msg->dst_uri.len) {
138
-		if (parse_uri(msg->dst_uri.s, msg->dst_uri.len, &next_hop) < 0)
139
-			return -1;
140
-		u = &next_hop;
141
-	}
142
-	else {
143
-		if (parse_sip_msg_uri(msg) < 0)
144
-			return -1;
145
-		u = &msg->parsed_uri;
146
-	}
147
-#ifdef USE_TLS
148
-	if (u->type==SIPS_URI_T)
149
-		proto = PROTO_TLS;
150
-	else
151
-#endif
152
-		proto = u->proto;
153
-
154
-#ifdef HONOR_MADDR
155
-	if (u->maddr_val.s && u->maddr_val.len)
156
-		dst_host = &u->maddr_val;
157
-	else
158
-#endif
159
-		dst_host = &u->host;
160
-
161
-	if (sip_hostport2su(&to, dst_host, u->port_no, &proto) < 0)
162
-		return -1;
163
-	socket_info = get_send_socket(msg, &to, proto);
164
-	if (!socket_info)
165
-		return -1;
166
-
167
-	*res = socket_info->address_str;
168
-	return 0;
169
-}
170
-
171
-#define SELECT_uri_header(_name_) \
172
-int select_##_name_(str* res, select_t* s, struct sip_msg* msg) \
173
-{ \
174
-	if (parse_##_name_##_header(msg)<0) \
175
-		return -1; \
176
-	RETURN0_res(msg->_name_->body); \
177
-} \
178
-\
179
-int select_##_name_##_uri(str* res, select_t* s, struct sip_msg* msg) \
180
-{ \
181
-	if (parse_##_name_##_header(msg)<0) \
182
-		return -1; \
183
-	RETURN0_res(get_##_name_(msg)->uri); \
184
-} \
185
-\
186
-int select_##_name_##_tag(str* res, select_t* s, struct sip_msg* msg) \
187
-{ \
188
-	if (parse_##_name_##_header(msg)<0) \
189
-		return -1; \
190
-	RETURN0_res(get_##_name_(msg)->tag_value); \
191
-} \
192
-\
193
-int select_##_name_##_name(str* res, select_t* s, struct sip_msg* msg) \
194
-{ \
195
-	if (parse_##_name_##_header(msg)<0) \
196
-		return -1; \
197
-	RETURN0_res(get_##_name_(msg)->display); \
198
-} \
199
-\
200
-int select_##_name_##_params(str* res, select_t* s, struct sip_msg* msg) \
201
-{ \
202
-	struct to_param* p; \
203
-	if (parse_##_name_##_header(msg)<0) \
204
-		return -1; \
205
-	\
206
-	p = get_##_name_(msg)->param_lst; \
207
-	while (p) { \
208
-		if ((p->name.len==s->params[s->n-1].v.s.len) \
209
-		    && !strncasecmp(p->name.s, s->params[s->n-1].v.s.s,p->name.len)) { \
210
-			RETURN0_res(p->value); \
211
-		} \
212
-		p = p->next; \
213
-	} \
214
-	return 1; \
215
-} 
216
-
217
-SELECT_uri_header(to)
218
-SELECT_uri_header(from)
219
-SELECT_uri_header(refer_to)
220
-SELECT_uri_header(rpid)
221
-
222
-int parse_contact_header( struct sip_msg *msg)
223
-{
224
-        if ( !msg->contact && ( parse_headers(msg,HDR_CONTACT_F,0)==-1 || !msg->contact)) {
225
-                LM_DBG("bad msg or missing CONTACT header\n");
226
-                return -1;
227
-        }
228
-
229
-        if (msg->contact->parsed)
230
-                return 0;
231
-
232
-	return parse_contact(msg->contact);
233
-}
234
-
235
-#define get_contact(msg) ((contact_body_t*)(msg->contact->parsed))
236
-
237
-int select_contact(str* res, select_t* s, struct sip_msg* msg)
238
-{
239
-	if (parse_contact_header(msg)<0)
240
-		return -1;
241
-	RETURN0_res(msg->contact->body);
242
-}
243
-
244
-int select_contact_uri(str* res, select_t* s, struct sip_msg* msg)
245
-{
246
-	contact_t* c;
247
-	if (parse_contact_header(msg)<0)
248
-		return -1;
249
-	
250
-	c = get_contact(msg)->contacts;
251
-	if (!c)
252
-		return 1;
253
-	RETURN0_res(c->uri);
254
-}
255
-
256
-int select_contact_name(str* res, select_t* s, struct sip_msg* msg)
257
-{
258
-	contact_t* c;
259
-	if (parse_contact_header(msg)<0)
260
-		return -1;
261
-	
262
-	c = get_contact(msg)->contacts;
263
-	if (!c)
264
-		return 1;
265
-	RETURN0_res(c->name);
266
-}
267
-
268
-int select_contact_params_spec(str* res, select_t* s, struct sip_msg* msg)
269
-{
270
-	contact_t* c;
271
-	
272
-	if (s->params[s->n-1].type != SEL_PARAM_DIV) {
273
-		BUG("Last parameter should have type DIV (converted)\n");
274
-		return -1;
275
-	}
276
-	
277
-	if (parse_contact_header(msg)<0)
278
-		return -1;
279
-	
280
-	c = get_contact(msg)->contacts;
281
-	if (!c)
282
-		return 1;
283
-	
284
-	switch (s->params[s->n-1].v.i) {
285
-	case SEL_PARAM_Q:
286
-		TEST_RET_res_body(c->q);
287
-	case SEL_PARAM_EXPIRES:
288
-		TEST_RET_res_body(c->expires);
289
-	case SEL_PARAM_METHODS:
290
-		TEST_RET_res_body(c->methods);
291
-	case SEL_PARAM_RECEIVED:
292
-		TEST_RET_res_body(c->received);
293
-	case SEL_PARAM_INSTANCE:
294
-		TEST_RET_res_body(c->instance);
295
-	default:
296
-		BUG("Unexpected parameter value \"%d\"\n", s->params[s->n-1].v.i);
297
-		return -1;
298
-	}
299
-	return -1;
300
-}
301
-
302
-int select_contact_params(str* res, select_t* s, struct sip_msg* msg)
303
-{
304
-	contact_t* c;
305
-	param_t* p;
306
-	if (parse_contact_header(msg)<0)
307
-		return -1;
308
-	
309
-	c = get_contact(msg)->contacts;
310
-	if (!c)
311
-		return 1;
312
-	p = c->params;
313
-	while (p) {
314
-		if ((p->name.len==s->params[s->n-1].v.s.len)
315
-		    && !strncasecmp(p->name.s, s->params[s->n-1].v.s.s,p->name.len)) {
316
-			RETURN0_res(p->body)
317
-		}
318
-		p = p->next;
319
-	}
320
-	return 1;
321
-}
322
-
323
-int select_via(str* res, select_t* s, struct sip_msg* msg)
324
-{
325
-	struct via_body *p = NULL;
326
-	
327
-	if ((s->n == 1) || (s->params[1].type == SEL_PARAM_STR)) {
328
-		if (parse_via_header(msg, 1, &p)<0) return -1;
329
-	} else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
330
-	if (!p) return -1;
331
-	res->s=p->name.s;
332
-	res->len=p->bsize;
333
-	trim(res);
334
-	return 0;
335
-}
336
-
337
-int select_via_name(str* res, select_t* s, struct sip_msg* msg)
338
-{
339
-	struct via_body *p = NULL;
340
-	
341
-	// it's not neccessary to test if (s->n > 1)
342
-	if (s->params[1].type == SEL_PARAM_STR) {
343
-		if(parse_via_header(msg, 1, &p)<0) return -1;
344
-	} else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
345
-	if (!p) return -1;
346
-	RETURN0_res(p->name);
347
-}
348
-
349
-int select_via_version(str* res, select_t* s, struct sip_msg* msg)
350
-{
351
-	struct via_body *p = NULL;
352
-	
353
-	// it's not neccessary to test if (s->n > 1)
354
-	if (s->params[1].type == SEL_PARAM_STR) {
355
-		if (parse_via_header(msg, 1, &p)<0) return -1;
356
-	} else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
357
-	if (!p) return -1;
358
-	RETURN0_res(p->version);
359
-}
360
-
361
-int select_via_transport(str* res, select_t* s, struct sip_msg* msg)
362
-{
363
-	struct via_body *p = NULL;
364
-	
365
-	// it's not neccessary to test if (s->n > 1)
366
-	if (s->params[1].type == SEL_PARAM_STR) {
367
-		if(parse_via_header(msg, 1, &p)<0) return -1;
368
-	} else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
369
-	if (!p) return -1;
370
-	RETURN0_res(p->transport);
371
-}
372
-
373
-int select_via_host(str* res, select_t* s, struct sip_msg* msg)
374
-{
375
-	struct via_body *p = NULL;
376
-	
377
-	// it's not neccessary to test if (s->n > 1)
378
-	if (s->params[1].type == SEL_PARAM_STR) {
379
-		if (parse_via_header(msg, 1, &p)<0) return -1;
380
-	} else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
381
-	if (!p) return -1;
382
-	RETURN0_res(p->host);
383
-}
384
-
385
-int select_via_port(str* res, select_t* s, struct sip_msg* msg)
386
-{
387
-	struct via_body *p = NULL;
388
-	
389
-	// it's not neccessary to test if (s->n > 1)
390
-	if (s->params[1].type == SEL_PARAM_STR) {
391
-		if (parse_via_header(msg, 1, &p)<0) return -1;
392
-	} else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
393
-	if (!p) return -1;
394
-	RETURN0_res(p->port_str);
395
-}
396
-
397
-int select_via_comment(str* res, select_t* s, struct sip_msg* msg)
398
-{
399
-	struct via_body *p = NULL;
400
-	
401
-	// it's not neccessary to test if (s->n > 1)
402
-	if (s->params[1].type == SEL_PARAM_STR) {
403
-		if(parse_via_header(msg, 1, &p)<0) return -1;
404
-	} else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
405
-	if (!p) return -1;
406
-	RETURN0_res(p->comment);
407
-}
408
-
409
-int select_via_params(str* res, select_t* s, struct sip_msg* msg)
410
-{
411
-	struct via_body *p = NULL;
412
-	struct via_param *q;
413
-
414
-	// it's not neccessary to test if (s->n > 1)
415
-	if (s->params[1].type == SEL_PARAM_STR) {
416
-		if (parse_via_header(msg, 1, &p)<0) return -1;
417
-	} else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
418
-	if (!p) return -1;
419
-	
420
-	for (q = p->param_lst;q;q=q->next) {
421
-		if ((q->name.len==s->params[s->n-1].v.s.len)
422
-		    && !strncasecmp(q->name.s, s->params[s->n-1].v.s.s,q->name.len)) {
423
-			RETURN0_res(q->value);
424
-		}
425
-	}
426
-	return 1;
427
-}
428
-
429
-int select_via_params_spec(str* res, select_t* s, struct sip_msg* msg)
430
-{
431
-	struct via_body *p = NULL;
432
-
433
-	if (s->params[s->n-1].type != SEL_PARAM_DIV) {
434
-		BUG("Last parameter should have type DIV (converted)\n");
435
-		return -1;
436
-	}
437
-	
438
-	// it's not neccessary to test if (s->n > 1)
439
-	if (s->params[1].type != SEL_PARAM_INT) {
440
-		if(parse_via_header(msg, 1, &p)<0) return -1;
441
-	} else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
442
-	if (!p) return -1;
443
-	
444
-	switch (s->params[s->n-1].v.i) {
445
-	case SEL_PARAM_BRANCH:
446
-		TEST_RET_res_value(p->branch);
447
-	case SEL_PARAM_RECEIVED:
448
-		TEST_RET_res_value(p->received);
449
-	case SEL_PARAM_RPORT:
450
-		TEST_RET_res_value(p->rport);
451
-	case SEL_PARAM_I:
452
-		TEST_RET_res_value(p->i);
453
-	case SEL_PARAM_ALIAS:
454
-		TEST_RET_res_value(p->alias);
455
-	default:
456
-		BUG("Unexpected parameter value \"%d\"\n", s->params[s->n-1].v.i);
457
-		return -1;
458
-	}
459
-	return -1;
460
-}
461
-
462
-int select_msg(str* res, select_t* s, struct sip_msg* msg)
463
-{
464
-	res->s = msg->buf;
465
-	res->len = msg->len;
466
-	return 0;
467
-}
468
-
469
-int select_msg_first_line(str* res, select_t* s, struct sip_msg* msg) 
470
-{
471
-	res->s=SIP_MSG_START(msg);
472
-	res->len=msg->first_line.len;
473
-	trim_trailing(res);
474
-	return 0;
475
-}
476
-
477
-int select_msg_type(str* res, select_t* s, struct sip_msg* msg) {
478
-	return uint_to_static_buffer(res, msg->first_line.type);
479
-} 
480
-
481
-int select_msg_len(str* res, select_t* s, struct sip_msg* msg) {
482
-	return uint_to_static_buffer(res, msg->len);
483
-}
484
-
485
-int select_msg_id(str* res, select_t* s, struct sip_msg* msg) {
486
-	return uint_to_static_buffer(res, msg->id);
487
-}
488
-
489
-int select_msg_id_hex(str* res, select_t* s, struct sip_msg* msg) {
490
-	return uint_to_static_buffer_ex(res, msg->id, 16, 8);
491
-}
492
-
493
-int select_msg_flags(str* res, select_t* s, struct sip_msg* msg) { 
494
-	return uint_to_static_buffer(res, msg->flags);
495
-} 
496
-
497
-int select_msg_body(str* res, select_t* s, struct sip_msg* msg)
498
-{
499
-	res->s = get_body(msg);
500
-	res->len = msg->buf+msg->len - res->s;
501
-	return 0;	
502
-}
503
-
504
-/* returns the sdp part of the message body */
505
-int select_msg_body_sdp(str* res, select_t* sel, struct sip_msg* msg)
506
-{
507
-	/* try to get the body part with application/sdp */
508
-	if ((res->s = get_body_part(msg,
509
-				TYPE_APPLICATION, SUBTYPE_SDP,
510
-				&res->len))
511
-	)
512
-		return 0;
513
-	else
514
-		return -1;
515
-}
516
-
517
-/* returns the value of the requested SDP line */
518
-int select_sdp_line(str* res, select_t* sel, struct sip_msg* msg)
519
-{
520
-	int	len;
521
-	char	*buf;
522
-	char	*buf_end, *line_end;
523
-	char	line;
524
-
525
-	if (msg == NULL) {
526
-		if (res!=NULL) return -1;
527
-		if (sel->n < 5) return -1;
528
-
529
-		if (sel->params[4].type != SEL_PARAM_STR) {
530
-			LM_ERR("wrong parameter type");
531
-			return -1;
532
-		}
533
-		if ((sel->params[4].v.s.len < 1) ||
534
-			(sel->params[4].v.s.len > 2) ||
535
-			((sel->params[4].v.s.len == 2) && (sel->params[4].v.s.s[1] != '='))
536
-		) {
537
-			LM_ERR("wrong sdp line format: %.*s\n",
538
-				sel->params[4].v.s.len, sel->params[4].v.s.s);
539
-			return -1;
540
-		}
541
-		return 0;
542
-	}
543
-
544
-	/* try to get the body part with application/sdp */
545
-	if (!(buf = get_body_part(msg,
546
-				TYPE_APPLICATION, SUBTYPE_SDP,
547
-				&len))
548
-	)
549
-		return -1;
550
-
551
-	buf_end = buf + len;
552
-	line = *(sel->params[4].v.s.s);
553
-
554
-	while (buf < buf_end) {
555
-		if (*buf == line) {
556
-			/* the requested SDP line is found, return its value */
557
-			buf++;
558
-			if ((buf >= buf_end) || (*buf != '=')) {
559
-				LM_ERR("wrong SDP line format\n");
560
-				return -1;
561
-			}
562
-			buf++;
563
-
564
-			line_end = buf;
565
-			while ((line_end < buf_end) && (*line_end != '\n'))
566
-				line_end++;
567
-
568
-			if (line_end >= buf_end) {
569
-				LM_ERR("wrong SDP line format\n");
570
-				return -1;
571
-			}
572
-			line_end--;
573
-			if (*line_end == '\r') line_end--;
574
-
575
-			if (line_end < buf) {
576
-				LM_ERR("wrong SDP line format\n");
577
-				return -1;
578
-			}
579
-
580
-			res->s = buf;
581
-			res->len = line_end - buf + 1;
582
-			return 0;
583
-		}
584
-		while ((buf < buf_end) && (*buf != '\n'))
585
-			buf++;
586
-		buf++;
587
-	}
588
-
589
-	return -1;
590
-}
591
-
592
-int select_msg_header(str* res, select_t* s, struct sip_msg* msg)
593
-{
594
-	/* get all headers */
595
-	char *c;
596
-	res->s = SIP_MSG_START(msg) + msg->first_line.len; 
597
-	c = get_body(msg);
598
-	res->len = c - res->s;
599
-	return 0;
600
-}
601
-
602
-int select_anyheader(str* res, select_t* s, struct sip_msg* msg)
603
-{
604
-	struct hdr_field *hf, *hf0;
605
-	int hi;
606
-	char c;
607
-	struct hdr_field hdr;
608
-	
609
-	if(msg==NULL) {
610
-		if (res!=NULL) return -1;
611
-
612
-		/* "fixup" call, res & msg are NULL */
613
-		if (s->n <3) return -1;
614
-
615
-		if (s->params[2].type==SEL_PARAM_STR) {
616
-				/* replace _ with - (for P-xxx headers) */
617
-			for (hi=s->params[2].v.s.len-1; hi>0; hi--)
618
-				if (s->params[2].v.s.s[hi]=='_')
619
-					s->params[2].v.s.s[hi]='-';
620
-				/* if header name is parseable, parse it and set SEL_PARAM_DIV */
621
-			c=s->params[2].v.s.s[s->params[2].v.s.len];
622
-			s->params[2].v.s.s[s->params[2].v.s.len]=':';
623
-			if (parse_hname2_short(s->params[2].v.s.s,s->params[2].v.s.s+(s->params[2].v.s.len<3?4:s->params[2].v.s.len+1),
624
-						&hdr)==0) {
625
-				LM_ERR("fixup_call:parse error\n");
626
-				return -1;
627
-			}
628
-			s->params[2].v.s.s[s->params[2].v.s.len]=c;
629
-			
630
-			if (hdr.type!=HDR_OTHER_T && hdr.type!=HDR_ERROR_T) {
631
-				/* pkg_free(s->params[1].v.s.s); */
632
-				/* don't free it (the mem can leak only once at startup)
633
-				 * the parsed string can live inside larger string block
634
-				 * e.g. when xlog's select is parsed
635
-				 */
636
-				s->params[2].type = SEL_PARAM_DIV;
637
-				s->params[2].v.i = hdr.type;
638
-			}
639
-		}
640
-		return 1;
641
-	}
642
-
643
-	hf0 = NULL;
644
-
645
-	/* extract header index if present */
646
-	if (s->param_offset[select_level+1] == 4) {
647
-		if (s->params[3].type == SEL_PARAM_INT) {
648
-			hi = s->params[3].v.i;
649
-		} else {
650
-			hi = -1;
651
-		}
652
-	} else {
653
-		hi = 1;
654
-	}
655
-
656
-	/* we need to be sure we have parsed all headers */
657
-	if (!msg->eoh && (parse_headers(msg,HDR_EOH_F,0)==-1 || !msg->eoh)) {
658
-		LM_ERR("bad msg while parsing to EOH \n");
659
-		return -1;
660
-	}
661
-	for (hf=msg->headers; hf; hf=hf->next) {
662
-		if(s->params[2].type==SEL_PARAM_DIV) {
663
-			if (s->params[2].v.i!=hf->type)	continue;
664
-		} else if(s->params[2].type==SEL_PARAM_STR) {
665
-			if (s->params[2].v.s.len!=hf->name.len)	continue;
666
-			if (strncasecmp(s->params[2].v.s.s, hf->name.s, hf->name.len)!=0) continue;
667
-		}
668
-		hf0 = hf;
669
-		hi--;
670
-		if (!hi) break;
671
-	}
672
-	if(hf0==NULL || hi>0)
673
-		return 1;
674
-	res->s = hf0->body.s;
675
-	res->len = hf0->body.len;
676
-	trim(res);
677
-	return 0;
678
-}
679
-
680
-//ABSTRACT_F(select_anyheader_params)
681
-// Instead of ABSTRACT_F(select_anyheader_params)
682
-// use function which uses select_core_table
683
-// to avoid gcc warning about not used
684
- 
685
-int select_anyheader_params(str* res, select_t* s, struct sip_msg* msg)
686
-{
687
-	if (select_core_table.next)
688
-		return -1;
689
-	else
690
-		return -1;
691
-}
692
-
693
-ABSTRACT_F(select_any_uri)
694
-
695
-static struct sip_uri uri;
696
-
697
-int select_uri_type(str* res, select_t* s, struct sip_msg* msg)
698
-{
699
-	if (select_uri_p == NULL) {
700
-		trim(res);
701
-		if (parse_uri(res->s, res->len, &uri)<0)
702
-			return -1;
703
-		select_uri_p = &uri;
704
-	}
705
-
706
-	if (select_uri_p->type==ERROR_URI_T)
707
-		return -1;
708
-
709
-	uri_type_to_str(select_uri_p->type, res);
710
-	return 0;
711
-}
712
-
713
-int select_uri_user(str* res, select_t* s, struct sip_msg* msg)
714
-{
715
-	if (select_uri_p == NULL) {
716
-		if (parse_uri(res->s, res->len, &uri)<0)
717
-			return -1;
718
-		select_uri_p = &uri;
719
-	}
720
-
721
-	if (select_uri_p->flags & URI_USER_NORMALIZE) {
722
-		if (!(res->s=get_static_buffer(select_uri_p->user.len)))
723
-			return -1;
724
-		if ((res->len=normalize_tel_user(res->s, (&select_uri_p->user)))==0)
725
-			return 1;
726
-		return 0;
727
-	}
728
-	RETURN0_res(select_uri_p->user);
729
-}
730
-
731
-/* search for a parameter with "name"
732
- * Return value:
733
- *	0: not found
734
- *	1: found
735
- *	-1: error
736
- *
737
- * val is set to the value of the parameter.
738
- */
739
-static inline int search_param(str params, char *name, int name_len,
740
-				str *val)
741
-{
742
-	param_hooks_t h;
743
-	param_t *p, *list;
744
-
745
-	if (params.s == NULL)
746
-		return 0;
747
-
748
-	if (parse_params(&params, CLASS_ANY, &h, &list) < 0)
749
-		return -1;
750
-	for (p = list; p; p=p->next) {
751
-		if ((p->name.len == name_len)
752
-			&& (strncasecmp(p->name.s, name, name_len) == 0)
753
-		) {
754
-			*val=p->body;
755
-			free_params(list);
756
-			return 1;
757
-		}
758
-	}
759
-	free_params(list);
760
-	return 0;
761
-}
762
-
763
-/* Return the value of the "rn" parameter if exists, otherwise the user name.
764
- * The user name is normalized if needed, i.e. visual separators are removed,
765
- * the "rn" param is always normalized. */
766
-int select_uri_rn_user(str* res, select_t* s, struct sip_msg* msg)
767
-{
768
-	int	ret;
769
-	str	val;
770
-
771
-	if (select_uri_p == NULL) {
772
-		if (parse_uri(res->s, res->len, &uri)<0)
773
-			return -1;
774
-		select_uri_p = &uri;
775
-	}
776
-
777
-	/* search for the "rn" parameter */
778
-	if ((ret = search_param(select_uri_p->params, "rn", 2, &val)) != 0)
779
-		goto done;
780
-
781
-	if (select_uri_p->sip_params.s != select_uri_p->params.s) {
782
-		/* check also the original sip: URI parameters */
783
-		if ((ret = search_param(select_uri_p->sip_params, "rn", 2, &val)) != 0)
784
-			goto done;
785
-	}
786
-
787
-	if ((select_uri_p->flags & URI_USER_NORMALIZE) == 0)
788
-		RETURN0_res(select_uri_p->user);
789
-	/* else normalize the user name */
790
-	val = select_uri_p->user;
791
-done:
792
-	if (ret < 0)
793
-		return -1; /* error */
794
-
795
-	if (!(res->s=get_static_buffer(val.len)))
796
-		return -1;
797
-	if ((res->len=normalize_tel_user(res->s, &val))==0)
798
-		return 1;
799
-	return 0;
800
-}
801
-
802
-int select_uri_pwd(str* res, select_t* s, struct sip_msg* msg)
803
-{
804
-	if (select_uri_p == NULL) {
805
-		if (parse_uri(res->s, res->len, &uri)<0)
806
-			return -1;
807
-		select_uri_p = &uri;
808
-	}
809
-
810
-	RETURN0_res(select_uri_p->passwd);
811
-}
812
-
813
-int select_uri_host(str* res, select_t* s, struct sip_msg* msg)
814
-{
815
-	if (select_uri_p == NULL) {
816
-		if (parse_uri(res->s, res->len, &uri)<0)
817
-			return -1;
818
-		select_uri_p = &uri;
819
-	}
820
-
821
-	RETURN0_res(select_uri_p->host);
822
-}
823
-
824
-int select_uri_port(str* res, select_t* s, struct sip_msg* msg)
825
-{
826
-	if (select_uri_p == NULL) {
827
-		if (parse_uri(res->s, res->len, &uri)<0)
828
-			return -1;
829
-		select_uri_p = &uri;
830
-	}
831
-
832
-	RETURN0_res(select_uri_p->port);
833
-}
834
-
835
-int select_uri_hostport(str* res, select_t* s, struct sip_msg* msg)
836
-{
837
-	char* p;
838
-	int size;
839
-	
840
-	if (select_uri_p == NULL) {
841
-		if (parse_uri(res->s, res->len, &uri)<0)
842
-			return -1;
843
-		select_uri_p = &uri;
844
-	}
845
-
846
-	if (!select_uri_p->host.len)
847
-		return -1;
848
-	
849
-	if (select_uri_p->port.len) {
850
-		res->s=select_uri_p->host.s;
851
-		res->len=select_uri_p->host.len+select_uri_p->port.len+1;
852
-		return 0;
853
-	}
854
-	
855
-	size=select_uri_p->host.len+5;
856
-	if (!(p = get_static_buffer(size)))
857
-		return -1;
858
-			
859
-	strncpy(p, select_uri_p->host.s, select_uri_p->host.len);
860
-	switch (select_uri_p->type) {
861
-		case SIPS_URI_T:
862
-		case TELS_URI_T:
863
-			strncpy(p+select_uri_p->host.len, ":5061", 5); 
864
-			break;
865
-		case SIP_URI_T:
866
-		case TEL_URI_T:
867
-		case URN_URI_T:
868
-			strncpy(p+select_uri_p->host.len, ":5060", 5);
869
-			break;
870
-		case ERROR_URI_T:
871
-			return -1;
872
-	}
873
-	res->s = p;
874
-	res->len = size;
875
-	return 0;
876
-}
877
-
878
-int select_uri_proto(str* res, select_t* s, struct sip_msg* msg)
879
-{
880
-	if (select_uri_p == NULL) {
881
-		if (parse_uri(res->s, res->len, &uri)<0)
882
-			return -1;
883
-		select_uri_p = &uri;
884
-	}
885
-
886
-	if (select_uri_p->proto != PROTO_NONE) {
887
-		proto_type_to_str(select_uri_p->proto, res);
888
-	} else {
889
-		switch (select_uri_p->type) {
890
-			case SIPS_URI_T:
891
-			case TELS_URI_T:
892
-				proto_type_to_str(PROTO_TLS, res);
893
-				break;
894
-			case SIP_URI_T:
895
-			case TEL_URI_T:
896
-			case URN_URI_T:
897
-				proto_type_to_str(PROTO_UDP, res);
898
-				break;
899
-			case ERROR_URI_T:
900
-				return -1;
901
-		}
902
-	}
903
-	return 0;
904
-}
905
-
906