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