731c87c75f6029d7d2ca9d1428af703edeb7d462
[sip-router] / select_core.c
1 /*
2  * $Id$
3  *
4  * Copyright (C) 2005-2006 iptelorg GmbH
5  *
6  * This file is part of ser, a free SIP server.
7  *
8  * ser 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  * For a license to use the ser software under conditions
14  * other than those described here, or to purchase support for this
15  * software, please contact iptel.org by e-mail at the following addresses:
16  *    info@iptel.org
17  *
18  * ser is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License 
24  * along with this program; if not, write to the Free Software 
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  *
27  * History:
28  * --------
29  *  2005-12-19  select framework, basic core functions (mma)
30  *  2006-01-19  multiple nested calls, IS_ALIAS -> NESTED flag renamed (mma)
31  *  2006-02-17  fixup call for select_anyhdr (mma)
32  *  2007-01-26  date, identity, identity_info support added (gergo)
33  */
34
35 #include <stdlib.h> 
36 #include "select.h"
37 #include "select_core.h"
38 #include "select_buf.h"
39 #include "dprint.h"
40 #include "trim.h"
41 #include "ut.h"
42 #include "globals.h"
43 #include "parser/parser_f.h"
44 #include "parser/hf.h"
45 #include "parser/parse_from.h"
46 #include "parser/parse_to.h"
47 #include "parser/contact/parse_contact.h"
48 #include "parser/contact/contact.h"
49 #include "parser/parse_via.h"
50 #include "parser/parse_uri.h"
51 #include "parser/parse_event.h"
52 #include "parser/parse_rr.h"
53 #include "parser/digest/digest.h"
54 #include "mem/mem.h"
55 #include "parser/parse_hname2.h"
56 #include "ip_addr.h"
57 #include "parser/parse_expires.h"
58 #include "parser/parse_refer_to.h"
59 #include "parser/parse_rpid.h"
60 #include "dset.h"
61 #include "sr_module.h"
62
63 #define RETURN0_res(x) {*res=(x);return 0;}
64 #define TRIM_RET0_res(x) {*res=(x);trim(res);return 0;} 
65 #define TEST_RET_res_body(x) if (x){*res=(x)->body;return 0;}else return 1;
66 #define TEST_RET_res_value(x) if (x){*res=(x)->value;return 0;}else return 1;
67
68 int select_ruri(str* res, select_t* s, struct sip_msg* msg)
69 {
70         if (msg->first_line.type==SIP_REQUEST) {
71                 if(msg->new_uri.s) {
72                         RETURN0_res(msg->new_uri);
73                 }
74                 else {
75                         RETURN0_res(msg->first_line.u.request.uri);
76                 }
77         }
78         return -1;
79 }
80
81 int select_dst_uri(str* res, select_t* s, struct sip_msg* msg)
82 {
83         if (msg->first_line.type!=SIP_REQUEST)
84                 return -1;
85         RETURN0_res(msg->dst_uri);
86 }
87
88 int select_next_hop(str* res, select_t* s, struct sip_msg* msg)
89 {
90         if (msg->first_line.type==SIP_REQUEST) {
91                 if(msg->dst_uri.s) {
92                         RETURN0_res(msg->dst_uri);
93                 }
94                 else if(msg->new_uri.s) {
95                         RETURN0_res(msg->new_uri);
96                 }
97                 else {
98                         RETURN0_res(msg->first_line.u.request.uri);
99                 }
100         }
101         return -1;
102 }
103
104 #define SELECT_uri_header(_name_) \
105 int select_##_name_(str* res, select_t* s, struct sip_msg* msg) \
106 { \
107         if (parse_##_name_##_header(msg)<0) \
108                 return -1; \
109         RETURN0_res(msg->_name_->body); \
110 } \
111 \
112 int select_##_name_##_uri(str* res, select_t* s, struct sip_msg* msg) \
113 { \
114         if (parse_##_name_##_header(msg)<0) \
115                 return -1; \
116         RETURN0_res(get_##_name_(msg)->uri); \
117 } \
118 \
119 int select_##_name_##_tag(str* res, select_t* s, struct sip_msg* msg) \
120 { \
121         if (parse_##_name_##_header(msg)<0) \
122                 return -1; \
123         RETURN0_res(get_##_name_(msg)->tag_value); \
124 } \
125 \
126 int select_##_name_##_name(str* res, select_t* s, struct sip_msg* msg) \
127 { \
128         if (parse_##_name_##_header(msg)<0) \
129                 return -1; \
130         RETURN0_res(get_##_name_(msg)->display); \
131 } \
132 \
133 int select_##_name_##_params(str* res, select_t* s, struct sip_msg* msg) \
134 { \
135         struct to_param* p; \
136         if (parse_##_name_##_header(msg)<0) \
137                 return -1; \
138         \
139         p = get_##_name_(msg)->param_lst; \
140         while (p) { \
141                 if ((p->name.len==s->params[s->n-1].v.s.len) \
142                     && !strncasecmp(p->name.s, s->params[s->n-1].v.s.s,p->name.len)) { \
143                         RETURN0_res(p->value); \
144                 } \
145                 p = p->next; \
146         } \
147         return 1; \
148
149
150 int parse_to_header(struct sip_msg *msg)
151 {
152         if ( !msg->to && ( parse_headers(msg,HDR_TO_F,0)==-1 || !msg->to)) {
153                 ERR("bad msg or missing TO header\n");
154                 return -1;
155         }
156
157         // HDR_TO_T is automatically parsed (get_hdr_field in parser/msg_parser.c)
158         // so check only ptr validity
159         if (msg->to->parsed)
160                 return 0;
161         else
162                 return -1;
163 }
164
165 SELECT_uri_header(to)
166 SELECT_uri_header(from)
167 SELECT_uri_header(refer_to)
168 SELECT_uri_header(rpid)
169
170 int parse_contact_header( struct sip_msg *msg)
171 {
172         if ( !msg->contact && ( parse_headers(msg,HDR_CONTACT_F,0)==-1 || !msg->contact)) {
173                 DBG("bad msg or missing CONTACT header\n");
174                 return -1;
175         }
176
177         if (msg->contact->parsed)
178                 return 0;
179
180         return parse_contact(msg->contact);
181 }
182
183 #define get_contact(msg) ((contact_body_t*)(msg->contact->parsed))
184
185 int select_contact(str* res, select_t* s, struct sip_msg* msg)
186 {
187         if (parse_contact_header(msg)<0)
188                 return -1;
189         RETURN0_res(msg->contact->body);
190 }
191
192 int select_contact_uri(str* res, select_t* s, struct sip_msg* msg)
193 {
194         contact_t* c;
195         if (parse_contact_header(msg)<0)
196                 return -1;
197         
198         c = get_contact(msg)->contacts;
199         if (!c)
200                 return 1;
201         RETURN0_res(c->uri);
202 }
203
204 int select_contact_name(str* res, select_t* s, struct sip_msg* msg)
205 {
206         contact_t* c;
207         if (parse_contact_header(msg)<0)
208                 return -1;
209         
210         c = get_contact(msg)->contacts;
211         if (!c)
212                 return 1;
213         RETURN0_res(c->name);
214 }
215
216 int select_contact_params_spec(str* res, select_t* s, struct sip_msg* msg)
217 {
218         contact_t* c;
219         
220         if (s->params[s->n-1].type != SEL_PARAM_DIV) {
221                 BUG("Last parameter should have type DIV (converted)\n");
222                 return -1;
223         }
224         
225         if (parse_contact_header(msg)<0)
226                 return -1;
227         
228         c = get_contact(msg)->contacts;
229         if (!c)
230                 return 1;
231         
232         switch (s->params[s->n-1].v.i) {
233         case SEL_PARAM_Q:
234                 TEST_RET_res_body(c->q);
235         case SEL_PARAM_EXPIRES:
236                 TEST_RET_res_body(c->expires);
237         case SEL_PARAM_METHOD:
238                 TEST_RET_res_body(c->method);
239         case SEL_PARAM_RECEIVED:
240                 TEST_RET_res_body(c->received);
241         case SEL_PARAM_INSTANCE:
242                 TEST_RET_res_body(c->instance);
243         default:
244                 BUG("Unexpected parameter value \"%d\"\n", s->params[s->n-1].v.i);
245                 return -1;
246         }
247         return -1;
248 }
249
250 int select_contact_params(str* res, select_t* s, struct sip_msg* msg)
251 {
252         contact_t* c;
253         param_t* p;
254         if (parse_contact_header(msg)<0)
255                 return -1;
256         
257         c = get_contact(msg)->contacts;
258         if (!c)
259                 return 1;
260         p = c->params;
261         while (p) {
262                 if ((p->name.len==s->params[s->n-1].v.s.len)
263                     && !strncasecmp(p->name.s, s->params[s->n-1].v.s.s,p->name.len)) {
264                         RETURN0_res(p->body)
265                 }
266                 p = p->next;
267         }
268         return 1;
269 }
270
271 int parse_via_header( struct sip_msg *msg, int n, struct via_body** q)
272 {
273         struct hdr_field *p;
274         struct via_body *pp = NULL;
275         int i;
276         
277         switch (n) {
278         case 1:
279         case 2:
280                 if (!msg->h_via1 && (parse_headers(msg,HDR_VIA_F,0)==-1 || !msg->h_via1)) {
281                         DBG("bad msg or missing VIA1 header \n");
282                         return -1;
283                 }
284                 pp = msg->h_via1->parsed;
285                 if (n==1) break;
286                 pp = pp->next;
287                 if (pp) break;
288                 
289                 if (!msg->h_via2 && (parse_headers(msg,HDR_VIA2_F,0)==-1 || !msg->h_via2)) {
290                         DBG("bad msg or missing VIA2 header \n");
291                         return -1;
292                 }
293                 pp = msg->h_via2->parsed;
294                 break;
295         default:        
296                 if (!msg->eoh && (parse_headers(msg,HDR_EOH_F,0)==-1 || !msg->eoh)) {
297                         ERR("bad msg while parsing to EOH \n");
298                         return -1;
299                 }
300                 p = msg->h_via1;
301                 i = n;
302                 while (i && p) {
303                         if (p->type == HDR_VIA_T) {
304                                 i--;
305                                 pp = p->parsed;
306                                 while (i && (pp->next)) {
307                                         i--;
308                                         pp = pp->next;
309                                 }
310                         }
311                         p = p->next;
312                 }
313                 if (i > 0) {
314                         DBG("missing VIA[%d] header\n", n);
315                         return -1;
316                 }
317         }
318         if (pp) {
319                 *q = pp;
320                 return 0;
321         } else
322                 return -1;
323 }
324
325 int select_via(str* res, select_t* s, struct sip_msg* msg)
326 {
327         struct via_body *p = NULL;
328         
329         if (((s->n == 1) || (s->params[1].type == SEL_PARAM_STR)) && (parse_via_header(msg, 1, &p)<0)) return -1;
330         else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
331         if (!p) return -1;
332         res->s=p->name.s;
333         res->len=p->bsize;
334         trim(res);
335         return 0;
336 }
337
338 int select_via_name(str* res, select_t* s, struct sip_msg* msg)
339 {
340         struct via_body *p = NULL;
341         
342         // it's not neccessary to test if (s->n > 1)
343         if ((s->params[1].type == SEL_PARAM_STR) && (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) && (parse_via_header(msg, 1, &p)<0)) return -1;
355         else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
356         if (!p) return -1;
357         RETURN0_res(p->version);
358 }
359
360 int select_via_transport(str* res, select_t* s, struct sip_msg* msg)
361 {
362         struct via_body *p = NULL;
363         
364         // it's not neccessary to test if (s->n > 1)
365         if ((s->params[1].type == SEL_PARAM_STR) && (parse_via_header(msg, 1, &p)<0)) return -1;
366         else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
367         if (!p) return -1;
368         RETURN0_res(p->transport);
369 }
370
371 int select_via_host(str* res, select_t* s, struct sip_msg* msg)
372 {
373         struct via_body *p = NULL;
374         
375         // it's not neccessary to test if (s->n > 1)
376         if ((s->params[1].type == SEL_PARAM_STR) && (parse_via_header(msg, 1, &p)<0)) return -1;
377         else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
378         if (!p) return -1;
379         RETURN0_res(p->host);
380 }
381
382 int select_via_port(str* res, select_t* s, struct sip_msg* msg)
383 {
384         struct via_body *p = NULL;
385         
386         // it's not neccessary to test if (s->n > 1)
387         if ((s->params[1].type == SEL_PARAM_STR) && (parse_via_header(msg, 1, &p)<0)) return -1;
388         else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
389         if (!p) return -1;
390         RETURN0_res(p->port_str);
391 }
392
393 int select_via_comment(str* res, select_t* s, struct sip_msg* msg)
394 {
395         struct via_body *p = NULL;
396         
397         // it's not neccessary to test if (s->n > 1)
398         if ((s->params[1].type == SEL_PARAM_STR) && (parse_via_header(msg, 1, &p)<0)) return -1;
399         else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
400         if (!p) return -1;
401         RETURN0_res(p->comment);
402 }
403
404 int select_via_params(str* res, select_t* s, struct sip_msg* msg)
405 {
406         struct via_body *p = NULL;
407         struct via_param *q;
408
409         // it's not neccessary to test if (s->n > 1)
410         if ((s->params[1].type == SEL_PARAM_STR) && (parse_via_header(msg, 1, &p)<0)) return -1;
411         else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
412         if (!p) return -1;
413         
414         for (q = p->param_lst;q;q=q->next) {
415                 if ((q->name.len==s->params[s->n-1].v.s.len)
416                     && !strncasecmp(q->name.s, s->params[s->n-1].v.s.s,q->name.len)) {
417                         RETURN0_res(q->value);
418                 }
419         }
420         return 1;
421 }
422
423 int select_via_params_spec(str* res, select_t* s, struct sip_msg* msg)
424 {
425         struct via_body *p = NULL;
426
427         if (s->params[s->n-1].type != SEL_PARAM_DIV) {
428                 BUG("Last parameter should have type DIV (converted)\n");
429                 return -1;
430         }
431         
432         // it's not neccessary to test if (s->n > 1)
433         if ((s->params[1].type == SEL_PARAM_STR) && (parse_via_header(msg, 1, &p)<0)) return -1;
434         else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
435         if (!p) return -1;
436         
437         switch (s->params[s->n-1].v.i) {
438         case SEL_PARAM_BRANCH:
439                 TEST_RET_res_value(p->branch);
440         case SEL_PARAM_RECEIVED:
441                 TEST_RET_res_value(p->received);
442         case SEL_PARAM_RPORT:
443                 TEST_RET_res_value(p->rport);
444         case SEL_PARAM_I:
445                 TEST_RET_res_value(p->i);
446         case SEL_PARAM_ALIAS:
447                 TEST_RET_res_value(p->alias);
448         default:
449                 BUG("Unexpected parameter value \"%d\"\n", s->params[s->n-1].v.i);
450                 return -1;
451         }
452         return -1;
453 }
454
455 int select_msg(str* res, select_t* s, struct sip_msg* msg)
456 {
457         res->s = msg->buf;
458         res->len = msg->len;
459         return 0;
460 }
461
462 int select_msg_first_line(str* res, select_t* s, struct sip_msg* msg) 
463 {
464         res->s=SIP_MSG_START(msg);
465         res->len=msg->first_line.len;
466         trim_trailing(res);
467         return 0;
468 }
469
470 int select_msg_type(str* res, select_t* s, struct sip_msg* msg) {
471         return uint_to_static_buffer(res, msg->first_line.type);
472
473
474 int select_msg_len(str* res, select_t* s, struct sip_msg* msg) {
475         return uint_to_static_buffer(res, msg->len);
476 }
477
478 int select_msg_id(str* res, select_t* s, struct sip_msg* msg) {
479         return uint_to_static_buffer(res, msg->id);
480 }
481
482 int select_msg_id_hex(str* res, select_t* s, struct sip_msg* msg) {
483         return uint_to_static_buffer_ex(res, msg->id, 16, 8);
484 }
485
486 int select_msg_flags(str* res, select_t* s, struct sip_msg* msg) { 
487         return uint_to_static_buffer(res, msg->flags);
488
489
490 int select_msg_body(str* res, select_t* s, struct sip_msg* msg)
491 {
492         res->s = get_body(msg);
493         res->len = msg->buf+msg->len - res->s;
494         return 0;       
495 }
496
497 int select_msg_header(str* res, select_t* s, struct sip_msg* msg)
498 {
499         /* get all headers */
500         char *c;
501         res->s = SIP_MSG_START(msg) + msg->first_line.len; 
502         c = get_body(msg);
503         res->len = c - res->s;
504         return 0;
505 }
506
507 int select_anyheader(str* res, select_t* s, struct sip_msg* msg)
508 {
509         struct hdr_field *hf, *hf0;
510         int hi;
511         char c;
512         struct hdr_field hdr;
513         
514         if(msg==NULL) {
515                 if (res!=NULL) return -1;
516
517                 /* "fixup" call, res & msg are NULL */
518                 if (s->n <3) return -1;
519
520                 if (s->params[2].type==SEL_PARAM_STR) {
521                                 /* replace _ with - (for P-xxx headers) */
522                         for (hi=s->params[2].v.s.len-1; hi>0; hi--)
523                                 if (s->params[2].v.s.s[hi]=='_')
524                                         s->params[2].v.s.s[hi]='-';
525                                 /* if header name is parseable, parse it and set SEL_PARAM_DIV */
526                         c=s->params[2].v.s.s[s->params[2].v.s.len];
527                         s->params[2].v.s.s[s->params[2].v.s.len]=':';
528                         if (parse_hname2(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),
529                                                 &hdr)==0) {
530                                 ERR("select_anyhdr:fixup_call:parse error\n");
531                                 return -1;
532                         }
533                         s->params[2].v.s.s[s->params[2].v.s.len]=c;
534                         
535                         if (hdr.type!=HDR_OTHER_T && hdr.type!=HDR_ERROR_T) {
536                                 /* pkg_free(s->params[1].v.s.s); */
537                                 /* don't free it (the mem can leak only once at startup)
538                                  * the parsed string can live inside larger string block
539                                  * e.g. when xlog's select is parsed
540                                  */
541                                 s->params[2].type = SEL_PARAM_DIV;
542                                 s->params[2].v.i = hdr.type;
543                         }
544                 }
545                 return 1;
546         }
547
548         hf0 = NULL;
549
550         /* extract header index if present */
551         if (s->param_offset[select_level+1] == 4) {
552                 if (s->params[3].type == SEL_PARAM_INT) {
553                         hi = s->params[3].v.i;
554                 } else {
555                         hi = -1;
556                 }
557         } else {
558                 hi = 1;
559         }
560
561         /* we need to be sure we have parsed all headers */
562         if (!msg->eoh && (parse_headers(msg,HDR_EOH_F,0)==-1 || !msg->eoh)) {
563                 ERR("bad msg while parsing to EOH \n");
564                 return -1;
565         }
566         for (hf=msg->headers; hf; hf=hf->next) {
567                 if(s->params[2].type==SEL_PARAM_DIV) {
568                         if (s->params[2].v.i!=hf->type) continue;
569                 } else if(s->params[2].type==SEL_PARAM_STR) {
570                         if (s->params[2].v.s.len!=hf->name.len) continue;
571                         if (strncasecmp(s->params[2].v.s.s, hf->name.s, hf->name.len)!=0) continue;
572                 }
573                 hf0 = hf;
574                 hi--;
575                 if (!hi) break;
576         }
577         if(hf0==NULL || hi>0)
578                 return 1;
579         res->s = hf0->body.s;
580         res->len = hf0->body.len;
581         trim(res);
582         return 0;
583 }
584
585 //ABSTRACT_F(select_anyheader_params)
586 // Instead of ABSTRACT_F(select_anyheader_params)
587 // use function which uses select_core_table
588 // to avoid gcc warning about not used
589  
590 int select_anyheader_params(str* res, select_t* s, struct sip_msg* msg)
591 {
592         if (select_core_table.next)
593                 return -1;
594         else
595                 return -1;
596 }
597
598 ABSTRACT_F(select_any_uri)
599
600 static struct sip_uri uri;
601
602 int select_uri_type(str* res, select_t* s, struct sip_msg* msg)
603 {
604         trim(res);
605         if (parse_uri(res->s, res->len, &uri)<0)
606                 return -1;
607
608         if (uri.type==ERROR_URI_T)
609                 return -1;
610
611         uri_type_to_str(uri.type, res);
612         return 0;
613 }
614
615 int select_uri_user(str* res, select_t* s, struct sip_msg* msg)
616 {
617         if (parse_uri(res->s, res->len, &uri)<0)
618                 return -1;
619
620         if (uri.flags & URI_USER_NORMALIZE) {
621                 if (!(res->s=get_static_buffer(uri.user.len)))
622                         return -1;
623                 if ((res->len=normalize_tel_user(res->s, (&uri.user)))==0)
624                         return 1;
625                 return 0;
626         }
627         RETURN0_res(uri.user);
628 }
629
630 int select_uri_pwd(str* res, select_t* s, struct sip_msg* msg)
631 {
632         if (parse_uri(res->s, res->len, &uri)<0)
633                 return -1;
634
635         RETURN0_res(uri.passwd);
636 }
637
638 int select_uri_host(str* res, select_t* s, struct sip_msg* msg)
639 {
640         if (parse_uri(res->s, res->len, &uri)<0)
641                 return -1;
642
643         RETURN0_res(uri.host);
644 }
645
646 int select_uri_port(str* res, select_t* s, struct sip_msg* msg)
647 {
648         if (parse_uri(res->s, res->len, &uri)<0)
649                 return -1;
650
651         RETURN0_res(uri.port);
652 }
653
654 int select_uri_hostport(str* res, select_t* s, struct sip_msg* msg)
655 {
656         char* p;
657         int size;
658         
659         if (parse_uri(res->s,res->len, &uri)<0)
660                 return -1;
661
662         if (!uri.host.len)
663                 return -1;
664         
665         if (uri.port.len) {
666                 res->s=uri.host.s;
667                 res->len=uri.host.len+uri.port.len+1;
668                 return 0;
669         }
670         
671         size=uri.host.len+5;
672         if (!(p = get_static_buffer(size)))
673                 return -1;
674                         
675         strncpy(p, uri.host.s, uri.host.len);
676         switch (uri.type) {
677                 case SIPS_URI_T:
678                 case TELS_URI_T:
679                         strncpy(p+uri.host.len, ":5061", 5); 
680                         break;
681                 case SIP_URI_T:
682                 case TEL_URI_T:
683                         strncpy(p+uri.host.len, ":5060", 5);
684                         break;
685                 case ERROR_URI_T:
686                         return -1;
687         }
688         res->s = p;
689         res->len = size;
690         return 0;
691 }
692
693 int select_uri_proto(str* res, select_t* s, struct sip_msg* msg)
694 {
695         if (parse_uri(res->s, res->len, &uri)<0)
696                 return -1;
697
698         if (uri.proto != PROTO_NONE) {
699                 proto_type_to_str(uri.proto, res);
700         } else {
701                 switch (uri.type) {
702                         case SIPS_URI_T:
703                         case TELS_URI_T:
704                                 proto_type_to_str(PROTO_TLS, res);
705                                 break;
706                         case SIP_URI_T:
707                         case TEL_URI_T:
708                                 proto_type_to_str(PROTO_UDP, res);
709                                 break;
710                         case ERROR_URI_T:
711                                 return -1;
712                 }
713         }
714         return 0;
715 }
716
717 int select_uri_params(str* res, select_t* s, struct sip_msg* msg)
718 {
719         if (!msg || !res) {
720                 return select_any_params(res, s, msg);
721         }
722
723         if (parse_uri(res->s, res->len, &uri)<0)
724                 return -1;
725         
726         if (s->param_offset[select_level+1]-s->param_offset[select_level]==1)
727                 RETURN0_res(uri.params);
728
729         *res=uri.params;
730         return select_any_params(res, s, msg);
731 }
732
733 int select_any_params(str* res, select_t* s, struct sip_msg* msg)
734 {
735         param_hooks_t h;
736         param_t *p, *list=NULL;
737         str* wanted;
738         int i;
739
740         if (!msg || !res) {
741                 if (s->param_offset[select_level+1]-s->param_offset[select_level]==1) return 0;
742                 if (s->params[s->param_offset[select_level]+1].type!=SEL_PARAM_STR) return -1;
743                 wanted=&s->params[s->param_offset[select_level]+1].v.s;
744                 for (i=0; i<wanted->len; i++) 
745                         if (wanted->s[i]=='_') 
746                                 wanted->s[i]='-';
747                 return 0;
748         }
749         
750         if (s->params[s->param_offset[select_level]+1].type!=SEL_PARAM_STR) return -1;
751         wanted=&s->params[s->param_offset[select_level]+1].v.s;
752         
753         if (!res->len) return -1;
754         if (parse_params(res, CLASS_ANY, &h, &list)<0) return -1;
755         
756         for (p = list; p; p=p->next) {
757                 if ((p->name.len==wanted->len) && 
758                          !strncasecmp(p->name.s, wanted->s,wanted->len)) {
759                         *res=p->body;
760                         free_params(list);
761                         return (res->len ? 0 : 1);
762                 }
763         }
764         free_params(list);
765
766         DBG("SELECT ...uri.params.%s NOT FOUND !\n", wanted->s);
767         return -1;
768 }
769
770 int select_event(str* res, select_t* s, struct sip_msg* msg)
771 {
772         if (!msg->event && parse_headers(msg, HDR_EVENT_F, 0) == -1) {
773                 ERR("Error while searching Event header field\n");
774                 return -1;
775         }
776
777         if (!msg->event) {
778                 DBG("Event header field not found\n");
779                 return -1;
780         }
781
782         if (parse_event(msg->event) < 0) {
783                 ERR("Error while parsing Event header field\n");
784                 return -1;
785         }
786
787         *res = ((event_t*)msg->event->parsed)->text;
788         return 0;
789 }
790
791
792
793 static int parse_rr_header(struct sip_msg *msg)
794 {
795         if ( !msg->record_route && ( parse_headers(msg,HDR_RECORDROUTE_F,0) == -1)) {
796                 ERR("bad msg or missing Record-Route header\n");
797                 return -1;
798         }
799
800         if (!msg->record_route) {
801                 DBG("No Record-Route header field found\n");
802                 return -1;
803         }
804
805         return parse_rr(msg->record_route);
806 }
807
808 #define get_rr(msg) ((rr_t*)(msg->record_route->parsed))
809
810
811 int select_rr(str* res, select_t* s, struct sip_msg* msg)
812 {
813         if (parse_rr_header(msg)<0)
814                 return -1;
815         RETURN0_res(msg->record_route->body);
816 }
817
818 int select_rr_uri(str* res, select_t* s, struct sip_msg* msg)
819 {
820         rr_t* r;
821         if (parse_rr_header(msg)<0)
822                 return -1;
823         
824         r = get_rr(msg);
825         if (!r)
826                 return 1;
827         RETURN0_res(r->nameaddr.uri);
828 }
829
830 int select_rr_name(str* res, select_t* s, struct sip_msg* msg)
831 {
832         rr_t* r;
833         if (parse_rr_header(msg)<0)
834                 return -1;
835         
836         r = get_rr(msg);
837         if (!r)
838                 return 1;
839         RETURN0_res(r->nameaddr.name);
840 }
841
842 int select_rr_params(str* res, select_t* s, struct sip_msg* msg)
843 {
844         rr_t* r;
845         param_t* p;
846         if (parse_rr_header(msg)<0)
847                 return -1;
848         
849         r = get_rr(msg);
850         if (!r)
851                 return 1;
852         p = r->params;
853         while (p) {
854                 if ((p->name.len==s->params[s->n-1].v.s.len)
855                     && !strncasecmp(p->name.s, s->params[s->n-1].v.s.s,p->name.len)) {
856                         RETURN0_res(p->body)
857                 }
858                 p = p->next;
859         }
860         return 0;
861 }
862
863
864 static inline struct cseq_body* sel_parse_cseq(struct sip_msg* msg)
865 {
866         if (!msg->cseq && (parse_headers(msg, HDR_CSEQ_F, 0) == -1)) {
867                 ERR("Unable to parse CSeq header\n");
868                 return 0;
869         }
870
871         if (!msg->cseq) {
872                 DBG("No CSeqheader field found\n");
873                 return 0;
874         }
875
876         return get_cseq(msg);
877 }
878
879
880
881 int select_cseq(str* res, select_t* s, struct sip_msg* msg)
882 {
883         struct cseq_body* cs;
884
885         cs = sel_parse_cseq(msg);
886         if (!cs) return -1;
887         *res = msg->cseq->body;
888         return 0;
889 }
890
891 int select_cseq_num(str* res, select_t* s, struct sip_msg* msg)
892 {
893         struct cseq_body* cs;
894
895         cs = sel_parse_cseq(msg);
896         if (!cs) return -1;
897         *res = cs->number;
898         return 0;
899 }
900
901 int select_cseq_method(str* res, select_t* s, struct sip_msg* msg)
902 {
903         struct cseq_body* cs;
904
905         cs = sel_parse_cseq(msg);
906         if (!cs) return -1;
907         *res = cs->method;
908         return 0;
909 }
910
911 int get_credentials(struct sip_msg* msg, select_t* s, struct hdr_field** hdr)
912 {
913         int ret;
914         str realm;
915         hdr_types_t hdr_type;
916
917         *hdr = NULL;
918
919         if (!msg) {
920                 /* fix-up call check domain for fparam conversion */
921                 void * ptr;
922                 char chr;
923                 ptr=(void *)(s->params[1].v.s.s);
924                 chr=s->params[1].v.s.s[s->params[1].v.s.len];
925                 s->params[1].v.s.s[s->params[1].v.s.len]=0;
926                 ret=fixup_var_str_12(&ptr,0);
927                 if (ret>=0) {
928                         s->params[1].v.s.s[s->params[1].v.s.len]=chr;
929                         s->params[1].v.p=ptr;
930                         s->params[1].type=SEL_PARAM_PTR;
931                 }
932                 return ret;
933         }
934         
935
936         /* Try to find credentials with corresponding realm
937          * in the message, parse them and return pointer to
938          * parsed structure
939          */
940         if (s->params[1].type==SEL_PARAM_PTR) {
941                 if (get_str_fparam(&realm, msg, s->params[1].v.p)<0)
942                         return -1;
943         } else {
944                 realm = s->params[1].v.s;
945         }
946
947         switch (s->params[0].v.i) {
948         case SEL_AUTH_WWW:
949                 hdr_type = HDR_AUTHORIZATION_T;
950                 break;
951
952         case SEL_AUTH_PROXY:
953                 hdr_type = HDR_PROXYAUTH_T;
954                 break;
955
956         default:
957                 BUG("Unexpected parameter value \"%d\"\n", s->params[0].v.i);
958                 return -1;
959         }
960
961         ret = find_credentials(msg, &realm, hdr_type, hdr);
962         return ret;
963 }
964
965
966 int select_auth(str* res, select_t* s, struct sip_msg* msg)
967 {
968         int ret;
969         struct hdr_field* hdr;
970
971         if (s->n != 2 && s->params[1].type != SEL_PARAM_STR
972                       && s->params[1].type != SEL_PARAM_PTR) return -1;
973
974         if (s->params[0].type != SEL_PARAM_DIV) {
975                 BUG("Last parameter should have type DIV (converted)\n");
976                 return -1;
977         }
978
979         ret = get_credentials(msg, s, &hdr);
980         if (!hdr) return ret;
981         RETURN0_res(hdr->body);
982 }
983
984 int select_auth_param(str* res, select_t* s, struct sip_msg* msg)
985 {
986         int ret;
987         struct hdr_field* hdr;
988         dig_cred_t* cred;
989
990         if ((s->n != 3 && s->n != 4) || (s->params[s->n - 1].type != SEL_PARAM_DIV)) return -1;
991
992         ret = get_credentials(msg, s, &hdr);
993         if (!hdr) return ret;
994         cred = &((auth_body_t*)hdr->parsed)->digest;
995
996         switch(s->params[s->n - 1].v.i) {
997         case SEL_AUTH_USER:     RETURN0_res(cred->username.user);
998         case SEL_AUTH_DOMAIN:   RETURN0_res(cred->username.domain);
999         case SEL_AUTH_USERNAME: RETURN0_res(cred->username.whole);
1000         case SEL_AUTH_REALM:    RETURN0_res(cred->realm);
1001         case SEL_AUTH_NONCE:    RETURN0_res(cred->nonce);
1002         case SEL_AUTH_URI:      RETURN0_res(cred->uri);
1003         case SEL_AUTH_CNONCE:   RETURN0_res(cred->cnonce);
1004         case SEL_AUTH_NC:       RETURN0_res(cred->nc);
1005         case SEL_AUTH_RESPONSE: RETURN0_res(cred->response);
1006         case SEL_AUTH_OPAQUE:   RETURN0_res(cred->opaque);
1007         case SEL_AUTH_ALG:      RETURN0_res(cred->alg.alg_str);
1008         case SEL_AUTH_QOP:      RETURN0_res(cred->qop.qop_str);
1009         default:
1010                 BUG("Unsupported digest credentials parameter in select\n");
1011                 return -1;
1012         }
1013 }
1014
1015 int select_auth_username(str* res, select_t* s, struct sip_msg* msg)
1016 {
1017         return select_auth_param(res, s, msg);
1018 }
1019
1020 int select_auth_username_comp(str* res, select_t* s, struct sip_msg* msg)
1021 {
1022         return select_auth_param(res, s, msg);
1023 }
1024
1025 ABSTRACT_F(select_any_nameaddr)
1026
1027 int select_nameaddr_name(str* res, select_t* s, struct sip_msg* msg)
1028 {
1029         char *p;
1030         
1031         p=find_not_quoted(res, '<');
1032         if (!p) {
1033                 DBG("select_nameaddr_name: no < found, whole string is uri\n");
1034                 res->len=0;
1035                 return 1;
1036         }
1037
1038         res->len=p-res->s;
1039         while (res->len && SP(res->s[res->len-1])) res->len--;
1040         return 0;
1041 }
1042
1043 int select_nameaddr_uri(str* res, select_t* s, struct sip_msg* msg)
1044 {
1045         char *p;
1046         
1047         p=find_not_quoted(res, '<');
1048         if (!p) {
1049                 DBG("select_nameaddr_uri: no < found, string up to first semicolon is uri\n");
1050                 p = q_memchr(res->s, ';', res->len);
1051                 if (p)
1052                         res->len = p-res->s;
1053                 return 0;
1054         }
1055
1056         res->len=res->len - (p-res->s) -1;
1057         res->s=p +1;
1058         
1059         p=find_not_quoted(res, '>');
1060         if (!p) {
1061                 ERR("select_nameaddr_uri: no > found, invalid nameaddr value\n");
1062                 return -1;
1063         }
1064
1065         res->len=p-res->s;
1066         return 0;
1067 }
1068
1069 int select_nameaddr_params(str* res, select_t* s, struct sip_msg* msg)
1070 {
1071         char *p;
1072         
1073         p=find_not_quoted(res, '<');
1074         if (!p) {
1075                 p=find_not_quoted(res, ';');
1076         } else {
1077                 res->len=res->len - (p-res->s) -1;
1078                 res->s=p +1;
1079                 p=find_not_quoted(res, '>');
1080                 if (!p) {
1081                         ERR("select_nameaddr_params: no > found, invalid nameaddr value\n");
1082                         return -1;
1083                 }
1084                 res->len=res->len - (p-res->s) -1;
1085                 res->s=p +1;
1086                 
1087                 p=find_not_quoted(res, ';');
1088         }
1089         if (!p) return 1;
1090         
1091         res->len=res->len - (p-res->s) -1;
1092         res->s=p +1;
1093         
1094         if (s->param_offset[select_level+1]-s->param_offset[select_level]==1)
1095                 return (res->len ? 0 : 1);
1096
1097         return select_any_params(res, s, msg);
1098 }
1099
1100 ABSTRACT_F(select_src)
1101 ABSTRACT_F(select_dst)
1102 ABSTRACT_F(select_rcv)
1103 int select_ip_port(str* res, select_t* s, struct sip_msg* msg)
1104 {
1105         str ip_str=STR_NULL, port_str=STR_NULL, proto_str=STR_NULL;
1106         int param, pos;
1107         
1108
1109         if ((s->n != 2) || (s->params[1].type != SEL_PARAM_DIV)) return -1;
1110         param=s->params[1].v.i;
1111
1112         if (param & SEL_SRC) {
1113                 if (param & SEL_IP) {
1114                         ip_str.s = ip_addr2a(&msg->rcv.src_ip);
1115                         ip_str.len = strlen(ip_str.s);
1116                 }
1117                 if (param & SEL_PORT) {
1118                         port_str.s = int2str(msg->rcv.src_port, &port_str.len);
1119                 }
1120         } else if (param & SEL_DST) {
1121                 if (param & SEL_IP) {
1122                         ip_str.s = ip_addr2a(&msg->rcv.dst_ip);
1123                         ip_str.len = strlen(ip_str.s);
1124                 }
1125                 if (param & SEL_PORT) {
1126                         port_str.s = int2str(msg->rcv.dst_port, &port_str.len);
1127                 }
1128         } else if (param & SEL_RCV) {
1129                 if (param & SEL_IP) {
1130                         ip_str = msg->rcv.bind_address->address_str;
1131                 }
1132                 if (param & SEL_PORT) {
1133                         port_str = msg->rcv.bind_address->port_no_str;
1134                 }
1135                 if (param & SEL_PROTO) {
1136                         switch (msg->rcv.proto) {
1137                         case PROTO_NONE:
1138                                 proto_str.s = 0;
1139                                 proto_str.len = 0;
1140                                 break;
1141
1142                         case PROTO_UDP:
1143                                 proto_str.s = "udp";
1144                                 proto_str.len = 3;
1145                                 break;
1146
1147                         case PROTO_TCP:
1148                                 proto_str.s = "tcp";
1149                                 proto_str.len = 3;
1150                                 break;
1151
1152                         case PROTO_TLS:
1153                                 proto_str.s = "tls";
1154                                 proto_str.len = 3;
1155                                 break;
1156
1157                         case PROTO_SCTP:
1158                                 proto_str.s = "sctp";
1159                                 proto_str.len = 4;
1160                                 break;
1161
1162                         default:
1163                                 ERR("BUG: select_ip_port: Unknown transport protocol\n");
1164                                 return -1;
1165                         }
1166                 }
1167         } else {
1168                 return -1;
1169         }
1170
1171         res->s = get_static_buffer(ip_str.len+port_str.len+proto_str.len+3);
1172         if (!res->s) return -1;
1173
1174         pos=0;
1175         if (param & SEL_PROTO) {
1176                 memcpy(res->s, proto_str.s, proto_str.len);
1177                 pos += proto_str.len;
1178         }
1179         if (param & SEL_IP) {
1180                 if (pos) res->s[pos++] = ':';
1181                 memcpy(res->s+pos, ip_str.s, ip_str.len);
1182                 pos += ip_str.len;
1183         }
1184         if (param & SEL_PORT) {
1185                 if (pos) res->s[pos++] = ':';
1186                 memcpy(res->s+pos, port_str.s, port_str.len);
1187                 pos += port_str.len;
1188         }
1189         res->s[pos] = 0;
1190         res->len = pos;
1191         return (pos==0 ? 1 : 0);
1192
1193 }
1194
1195
1196 int select_expires(str* res, select_t* s, struct sip_msg* msg)
1197 {
1198         if (!msg->expires && (parse_headers(msg, HDR_EXPIRES_F, 0) == -1)) {
1199                 return -1; /* error */
1200         }
1201
1202         if (!msg->expires) {
1203                 return 1;  /* null */
1204         }
1205
1206         if (msg->expires->parsed == NULL && parse_expires(msg->expires) < 0) {
1207                 return -1;
1208         }
1209
1210         RETURN0_res(((struct exp_body*)msg->expires->parsed)->text);
1211 }
1212
1213 #define SELECT_plain_header(_sel_name_,_fld_name_,_hdr_f_) \
1214 int select_##_sel_name_(str* res, select_t* s, struct sip_msg* msg) \
1215 { \
1216         if (!msg->_fld_name_ && (parse_headers(msg, _hdr_f_, 0) == -1)) { \
1217                 return -1; \
1218         } \
1219         if (!msg->_fld_name_) { \
1220                 return 1; \
1221         } \
1222         RETURN0_res(msg->_fld_name_->body); \
1223 }
1224
1225 SELECT_plain_header(call_id, callid, HDR_CALLID_F)
1226 SELECT_plain_header(max_forwards, maxforwards, HDR_MAXFORWARDS_F)
1227 SELECT_plain_header(content_type, content_type, HDR_CONTENTTYPE_F)
1228 SELECT_plain_header(content_length, content_length, HDR_CONTENTLENGTH_F)
1229 SELECT_plain_header(user_agent, user_agent, HDR_USERAGENT_F)
1230 SELECT_plain_header(subject, subject, HDR_SUBJECT_F)
1231 SELECT_plain_header(organization, organization, HDR_ORGANIZATION_F)
1232 SELECT_plain_header(priority, priority, HDR_PRIORITY_F)
1233 SELECT_plain_header(session_expires, session_expires, HDR_SESSIONEXPIRES_F)
1234 SELECT_plain_header(min_se, min_se, HDR_MIN_SE_F)
1235 SELECT_plain_header(sip_if_match, sipifmatch, HDR_SIPIFMATCH_F)
1236 SELECT_plain_header(date, date, HDR_DATE_F)
1237 SELECT_plain_header(identity, identity, HDR_IDENTITY_F)
1238 SELECT_plain_header(identity_info, identity_info, HDR_IDENTITY_INFO_F)
1239
1240 int select_msg_request(str* res, select_t* s, struct sip_msg* msg)
1241 {
1242         if (msg->first_line.type==SIP_REQUEST) { 
1243                 return select_msg(res, s, msg); 
1244         }
1245         else
1246                 return -1;      
1247 }
1248
1249 int select_msg_response(str* res, select_t* s, struct sip_msg* msg)
1250 {
1251         if (msg->first_line.type==SIP_REPLY) {
1252                 return select_msg(res, s, msg); 
1253         }
1254         else
1255                 return -1;      
1256 }
1257
1258 #define SELECT_first_line(_sel_name_,_type_,_fld_) \
1259 int select_msg_##_sel_name_(str* res, select_t* s, struct sip_msg* msg) {\
1260         if (msg->first_line.type==_type_) { \
1261                 RETURN0_res(msg->first_line._fld_); \
1262         } else return -1; \
1263 }
1264
1265 SELECT_first_line(request_method,SIP_REQUEST,u.request.method)
1266 SELECT_first_line(request_uri,SIP_REQUEST,u.request.uri)
1267 SELECT_first_line(request_version,SIP_REQUEST,u.request.version)
1268 SELECT_first_line(response_version,SIP_REPLY,u.reply.version)
1269 SELECT_first_line(response_status,SIP_REPLY,u.reply.status)
1270 SELECT_first_line(response_reason,SIP_REPLY,u.reply.reason)
1271
1272
1273 int select_version(str* res, select_t* s, struct sip_msg* msg)
1274 {
1275         switch (msg->first_line.type) { 
1276                 case SIP_REQUEST:
1277                         RETURN0_res(msg->first_line.u.request.version)
1278                         break;
1279                 case SIP_REPLY:
1280                         RETURN0_res(msg->first_line.u.reply.version)
1281                         break;
1282                 default:
1283                         return -1;
1284         }
1285 }
1286
1287 ABSTRACT_F(select_sys)
1288
1289 int select_sys_pid(str* res, select_t* s, struct sip_msg* msg) {
1290         return uint_to_static_buffer(res, getpid());
1291 }
1292
1293 int select_sys_server_id(str* res, select_t* s, struct sip_msg* msg) {
1294         return int_to_static_buffer(res, server_id);
1295 }
1296
1297 int select_sys_unique(str* res, select_t* s, struct sip_msg* msg) {
1298         #define UNIQUE_ID_PID_LEN 4
1299         #define UNIQUE_ID_TIME_LEN 8
1300         #define UNIQUE_ID_FIX_LEN (UNIQUE_ID_PID_LEN+1+UNIQUE_ID_TIME_LEN+1)
1301         #define UNIQUE_ID_RAND_LEN 8
1302         static char uniq_id[UNIQUE_ID_FIX_LEN+UNIQUE_ID_RAND_LEN];
1303         static int uniq_for_pid = -1;
1304         int i;
1305
1306         if (uniq_for_pid != getpid()) {
1307                 /* first call for this process */
1308                 int cb, rb, x, l;
1309                 char *c;
1310                 /* init gloabally uniq part */
1311                 c = int2str_base_0pad(getpid(), &l, 16, UNIQUE_ID_PID_LEN);
1312                 memcpy(uniq_id, c, UNIQUE_ID_PID_LEN);
1313                 uniq_id[UNIQUE_ID_PID_LEN] = '-';
1314                 c = int2str_base_0pad(time(NULL), &l, 16, UNIQUE_ID_TIME_LEN);
1315                 memcpy(uniq_id+UNIQUE_ID_PID_LEN+1, c, UNIQUE_ID_TIME_LEN);
1316                 uniq_id[UNIQUE_ID_PID_LEN+1+UNIQUE_ID_TIME_LEN] = '-';
1317
1318                 /* init random part */
1319                 for (i = RAND_MAX, rb=0; i; rb++, i>>=1);
1320                 for (i = UNIQUE_ID_FIX_LEN, cb = 0, x = 0; i < UNIQUE_ID_FIX_LEN+UNIQUE_ID_RAND_LEN; i++) {
1321                         if (!cb) {
1322                                 cb = rb;
1323                                 x = rand();
1324                         }
1325                         uniq_id[i] = fourbits2char[x & 0x0F];
1326                         x >>= rb;
1327                         cb -= rb;
1328                 }
1329                 uniq_for_pid = getpid();
1330         }
1331         for (i = UNIQUE_ID_FIX_LEN + UNIQUE_ID_RAND_LEN - 1; i >= UNIQUE_ID_FIX_LEN; i--) {
1332                 switch (uniq_id[i]) {
1333                         case '9':
1334                                 uniq_id[i]='a';
1335                                 i = 0;
1336                                 break;
1337                         case 'f':
1338                                 uniq_id[i]='0';
1339                                 /* go on */
1340                                 break;
1341                         default:
1342                                 uniq_id[i]++;
1343                                 i = 0;
1344                                 break;
1345                 }
1346         }
1347         res->s = uniq_id;   /* I think it's not worth copying at static buffer, I hope there is no real meaning of @sys.unique==@sys.unique */
1348         res->len = UNIQUE_ID_FIX_LEN+UNIQUE_ID_RAND_LEN;
1349         return 0;
1350 }
1351
1352
1353 int select_sys_now(str* res, select_t* s, struct sip_msg* msg) {
1354         return uint_to_static_buffer(res, time(NULL));
1355 }
1356
1357 int select_sys_now_fmt(str* res, select_t* s, struct sip_msg* msg)
1358 {
1359 #define SEL_POS 2
1360         time_t t;
1361         struct tm *tm;
1362         
1363         t = time(NULL);
1364         switch (s->params[SEL_POS].v.i) {
1365                 case SEL_NOW_GMT:
1366                         tm = gmtime(&t);
1367                         break;
1368         
1369                 case SEL_NOW_LOCAL:
1370                         tm = localtime(&t);
1371                         break;
1372                 default:
1373                         BUG("Unexpected parameter value 'now' \"%d\"\n", s->params[SEL_POS].v.i);
1374                         return -1;
1375         }
1376         if (s->n <= SEL_POS+1) {
1377                 char *c;
1378                 c = asctime(tm);
1379                 res->len = strlen(c);
1380                 while (res->len && c[res->len-1] < ' ') res->len--; /* rtrim */
1381                 res->s = get_static_buffer(res->len);
1382                 if (!res->s) return -1;
1383                 memcpy(res->s, c, res->len);
1384         }
1385         else {
1386                 char c, buff[80];
1387                 c = s->params[SEL_POS+1].v.s.s[s->params[SEL_POS+1].v.s.len];
1388                 s->params[SEL_POS+1].v.s.s[s->params[SEL_POS+1].v.s.len] = '\0';
1389                 res->len = strftime(buff, sizeof(buff)-1, s->params[SEL_POS+1].v.s.s, tm);
1390                 s->params[SEL_POS+1].v.s.s[s->params[SEL_POS+1].v.s.len] = c;
1391                 res->s = get_static_buffer(res->len);
1392                 if (!res->s) return -1;
1393                 memcpy(res->s, buff, res->len);
1394         }
1395         return 0;
1396 #undef SEL_POS
1397 }
1398
1399 ABSTRACT_F(select_branch)
1400
1401 int select_branch_count(str* res, select_t* s, struct sip_msg* msg) {
1402         return uint_to_static_buffer(res, nr_branches);
1403 }
1404
1405 int select_branch_uri(str* res, select_t* s, struct sip_msg* msg) {
1406 #define SEL_POS 1 
1407 #define Q_PARAM ">;q="
1408 #define Q_PARAM_LEN (sizeof(Q_PARAM) - 1)
1409
1410         qvalue_t q;
1411         int l, n;
1412         str dst_uri;
1413         if (s->n <= SEL_POS+1 && nr_branches > 1) { /* get all branches, if nr_branches==1 then use faster algorithm */
1414                 int len;
1415                 unsigned l2;
1416                 char *c;
1417                 init_branch_iterator();
1418                 len = 0;
1419                 while ((c = next_branch(&l, &q, &dst_uri.s, &dst_uri.len, 0))) {
1420
1421                         if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
1422                                 l = dst_uri.len;
1423                                 c = dst_uri.s;
1424                         }
1425                         if (s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI) ) {
1426                                 len += l;
1427                         }
1428                         if (q != Q_UNSPECIFIED && (s->params[SEL_POS].v.i & SEL_BRANCH_Q)) {
1429                                 len += len_q(q);
1430                                 if (s->params[SEL_POS].v.i & SEL_BRANCH_URI) {
1431                                         len += 1 + Q_PARAM_LEN;
1432                                 }
1433                         }
1434                         len += 1;
1435                 }
1436                 if (!len) return 1;
1437                 res->s = get_static_buffer(len);
1438                 if (!res->s) return -1;
1439                 
1440                 init_branch_iterator();
1441                 res->len = 0;
1442                 n = 0;
1443                 while ((c = next_branch(&l, &q, &dst_uri.s, &dst_uri.len, 0))) {
1444                         if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
1445                                 l = dst_uri.len;
1446                                 c = dst_uri.s;
1447                         }
1448                         if (n) {
1449                                 res->s[res->len] = ',';
1450                                 res->len++;
1451                         }
1452                         if ((s->params[SEL_POS].v.i & SEL_BRANCH_Q) == 0) {
1453                                 q = Q_UNSPECIFIED;
1454                         }
1455                         if ((s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI)) && q != Q_UNSPECIFIED) {
1456                                 res->s[res->len] = '<';
1457                                 res->len++;
1458                                 memcpy(res->s+res->len, c, l);
1459                                 res->len += l; 
1460                                 memcpy(res->s+res->len, Q_PARAM, Q_PARAM_LEN);
1461                                 res->len += Q_PARAM_LEN;
1462                                 c = q2str(q, &l2); l = l2;
1463                                 memcpy(res->s+res->len, c, l);
1464                                 res->len += l; 
1465                         }
1466                         else if (s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI)) {
1467                                 memcpy(res->s+res->len, c, l);
1468                                 res->len += l; 
1469                         }
1470                         else if (q != Q_UNSPECIFIED) {
1471                                 c = q2str(q, &l2); l = l2;
1472                                 memcpy(res->s+res->len, c, l);
1473                                 res->len += l; 
1474                         }
1475                         n++;
1476                 }
1477         }
1478         else {
1479                 unsigned l2;
1480                 char *c;
1481                 n = s->params[SEL_POS+1].v.i;
1482                 if (n < 0 || n >= nr_branches) 
1483                         return -1;
1484                 init_branch_iterator();
1485                 for (; (c = next_branch(&l, &q, &dst_uri.s, &dst_uri.len, 0)) && n; n--);
1486                 if (!c) return 1;
1487                 
1488                 if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
1489                         l = dst_uri.len;
1490                         c = dst_uri.s;
1491                 }
1492                 if ((s->params[SEL_POS].v.i & SEL_BRANCH_Q) == 0) {
1493                         q = Q_UNSPECIFIED;
1494                 }
1495                 if ((s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI)) && q != Q_UNSPECIFIED) {
1496
1497                         res->s = get_static_buffer(l + 1 + Q_PARAM_LEN + len_q(q));
1498                         if (!res->s) return -1;
1499                         res->len = 1;
1500                         res->s[0] = '<';
1501                         memcpy(res->s+res->len, c, l);
1502                         res->len += l; 
1503                         memcpy(res->s+res->len, Q_PARAM, Q_PARAM_LEN);
1504                         res->len += Q_PARAM_LEN;
1505                         c = q2str(q, &l2); l = l2;
1506                         memcpy(res->s+res->len, c, l);
1507                         res->len += l; 
1508                 }
1509                 else if (s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI)) {
1510                         res->s = c;  /* not necessary to copy to static buffer */
1511                         res->len = l;
1512                 } 
1513                 else if (q != Q_UNSPECIFIED) {
1514                         c = q2str(q, &l2);
1515                         res->len = l2;
1516                         res->s = get_static_buffer(res->len);
1517                         if (!res->s) return -1;
1518                         memcpy(res->s, c, res->len);
1519                 }
1520                 else {
1521                         res->len = 0;
1522                 }
1523         }
1524         return 0;
1525 #undef SEL_POS
1526 }
1527
1528 int select_branch_uriq(str* res, select_t* s, struct sip_msg* msg) {
1529         return select_branch_uri(res, s, msg);
1530 }
1531
1532 int select_branch_q(str* res, select_t* s, struct sip_msg* msg) {
1533         return select_branch_uri(res, s, msg);
1534 }
1535
1536 int select_branch_dst_uri(str* res, select_t* s, struct sip_msg* msg) {
1537         return select_branch_uri(res, s, msg);
1538 }