check the '=' sign in the sdp line to be on the safe side
[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 /* returns the sdp part of the message body */
500 int select_msg_body_sdp(str* res, select_t* sel, struct sip_msg* msg)
501 {
502         /* try to get the body part with application/sdp */
503         if ((res->s = get_body_part(msg,
504                                 TYPE_APPLICATION, SUBTYPE_SDP,
505                                 &res->len))
506         )
507                 return 0;
508         else
509                 return -1;
510 }
511
512 /* returns the value of the requested SDP line */
513 int select_sdp_line(str* res, select_t* sel, struct sip_msg* msg)
514 {
515         int     len;
516         char    *buf;
517         char    *buf_end, *line_end;
518         char    line;
519
520         if (msg == NULL) {
521                 if (res!=NULL) return -1;
522                 if (sel->n < 5) return -1;
523
524                 if (sel->params[4].type != SEL_PARAM_STR) {
525                         ERR("wrong parameter type");
526                         return -1;
527                 }
528                 if ((sel->params[4].v.s.len < 1) ||
529                         (sel->params[4].v.s.len > 2) ||
530                         ((sel->params[4].v.s.len == 2) && (sel->params[4].v.s.s[1] != '='))
531                 ) {
532                         ERR("wrong sdp line format: %.*s\n",
533                                 sel->params[4].v.s.len, sel->params[4].v.s.s);
534                         return -1;
535                 }
536                 return 0;
537         }
538
539         /* try to get the body part with application/sdp */
540         if (!(buf = get_body_part(msg,
541                                 TYPE_APPLICATION, SUBTYPE_SDP,
542                                 &len))
543         )
544                 return -1;
545
546         buf_end = buf + len;
547         line = *(sel->params[4].v.s.s);
548
549         while (buf < buf_end) {
550                 if (*buf == line) {
551                         /* the requested SDP line is found, return its value */
552                         buf++;
553                         if ((buf >= buf_end) || (*buf != '=')) {
554                                 ERR("wrong SDP line format\n");
555                                 return -1;
556                         }
557                         buf++;
558
559                         line_end = buf;
560                         while ((line_end < buf_end) && (*line_end != '\n'))
561                                 line_end++;
562
563                         if (line_end >= buf_end) {
564                                 ERR("wrong SDP line format\n");
565                                 return -1;
566                         }
567                         line_end--;
568                         if (*line_end == '\r') line_end--;
569
570                         if (line_end < buf) {
571                                 ERR("wrong SDP line format\n");
572                                 return -1;
573                         }
574
575                         res->s = buf;
576                         res->len = line_end - buf + 1;
577                         return 0;
578                 }
579                 while ((buf < buf_end) && (*buf != '\n'))
580                         buf++;
581                 buf++;
582         }
583
584         return -1;
585 }
586
587 int select_msg_header(str* res, select_t* s, struct sip_msg* msg)
588 {
589         /* get all headers */
590         char *c;
591         res->s = SIP_MSG_START(msg) + msg->first_line.len; 
592         c = get_body(msg);
593         res->len = c - res->s;
594         return 0;
595 }
596
597 int select_anyheader(str* res, select_t* s, struct sip_msg* msg)
598 {
599         struct hdr_field *hf, *hf0;
600         int hi;
601         char c;
602         struct hdr_field hdr;
603         
604         if(msg==NULL) {
605                 if (res!=NULL) return -1;
606
607                 /* "fixup" call, res & msg are NULL */
608                 if (s->n <3) return -1;
609
610                 if (s->params[2].type==SEL_PARAM_STR) {
611                                 /* replace _ with - (for P-xxx headers) */
612                         for (hi=s->params[2].v.s.len-1; hi>0; hi--)
613                                 if (s->params[2].v.s.s[hi]=='_')
614                                         s->params[2].v.s.s[hi]='-';
615                                 /* if header name is parseable, parse it and set SEL_PARAM_DIV */
616                         c=s->params[2].v.s.s[s->params[2].v.s.len];
617                         s->params[2].v.s.s[s->params[2].v.s.len]=':';
618                         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),
619                                                 &hdr)==0) {
620                                 ERR("select_anyhdr:fixup_call:parse error\n");
621                                 return -1;
622                         }
623                         s->params[2].v.s.s[s->params[2].v.s.len]=c;
624                         
625                         if (hdr.type!=HDR_OTHER_T && hdr.type!=HDR_ERROR_T) {
626                                 /* pkg_free(s->params[1].v.s.s); */
627                                 /* don't free it (the mem can leak only once at startup)
628                                  * the parsed string can live inside larger string block
629                                  * e.g. when xlog's select is parsed
630                                  */
631                                 s->params[2].type = SEL_PARAM_DIV;
632                                 s->params[2].v.i = hdr.type;
633                         }
634                 }
635                 return 1;
636         }
637
638         hf0 = NULL;
639
640         /* extract header index if present */
641         if (s->param_offset[select_level+1] == 4) {
642                 if (s->params[3].type == SEL_PARAM_INT) {
643                         hi = s->params[3].v.i;
644                 } else {
645                         hi = -1;
646                 }
647         } else {
648                 hi = 1;
649         }
650
651         /* we need to be sure we have parsed all headers */
652         if (!msg->eoh && (parse_headers(msg,HDR_EOH_F,0)==-1 || !msg->eoh)) {
653                 ERR("bad msg while parsing to EOH \n");
654                 return -1;
655         }
656         for (hf=msg->headers; hf; hf=hf->next) {
657                 if(s->params[2].type==SEL_PARAM_DIV) {
658                         if (s->params[2].v.i!=hf->type) continue;
659                 } else if(s->params[2].type==SEL_PARAM_STR) {
660                         if (s->params[2].v.s.len!=hf->name.len) continue;
661                         if (strncasecmp(s->params[2].v.s.s, hf->name.s, hf->name.len)!=0) continue;
662                 }
663                 hf0 = hf;
664                 hi--;
665                 if (!hi) break;
666         }
667         if(hf0==NULL || hi>0)
668                 return 1;
669         res->s = hf0->body.s;
670         res->len = hf0->body.len;
671         trim(res);
672         return 0;
673 }
674
675 //ABSTRACT_F(select_anyheader_params)
676 // Instead of ABSTRACT_F(select_anyheader_params)
677 // use function which uses select_core_table
678 // to avoid gcc warning about not used
679  
680 int select_anyheader_params(str* res, select_t* s, struct sip_msg* msg)
681 {
682         if (select_core_table.next)
683                 return -1;
684         else
685                 return -1;
686 }
687
688 ABSTRACT_F(select_any_uri)
689
690 static struct sip_uri uri;
691
692 int select_uri_type(str* res, select_t* s, struct sip_msg* msg)
693 {
694         trim(res);
695         if (parse_uri(res->s, res->len, &uri)<0)
696                 return -1;
697
698         if (uri.type==ERROR_URI_T)
699                 return -1;
700
701         uri_type_to_str(uri.type, res);
702         return 0;
703 }
704
705 int select_uri_user(str* res, select_t* s, struct sip_msg* msg)
706 {
707         if (parse_uri(res->s, res->len, &uri)<0)
708                 return -1;
709
710         if (uri.flags & URI_USER_NORMALIZE) {
711                 if (!(res->s=get_static_buffer(uri.user.len)))
712                         return -1;
713                 if ((res->len=normalize_tel_user(res->s, (&uri.user)))==0)
714                         return 1;
715                 return 0;
716         }
717         RETURN0_res(uri.user);
718 }
719
720 int select_uri_pwd(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.passwd);
726 }
727
728 int select_uri_host(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.host);
734 }
735
736 int select_uri_port(str* res, select_t* s, struct sip_msg* msg)
737 {
738         if (parse_uri(res->s, res->len, &uri)<0)
739                 return -1;
740
741         RETURN0_res(uri.port);
742 }
743
744 int select_uri_hostport(str* res, select_t* s, struct sip_msg* msg)
745 {
746         char* p;
747         int size;
748         
749         if (parse_uri(res->s,res->len, &uri)<0)
750                 return -1;
751
752         if (!uri.host.len)
753                 return -1;
754         
755         if (uri.port.len) {
756                 res->s=uri.host.s;
757                 res->len=uri.host.len+uri.port.len+1;
758                 return 0;
759         }
760         
761         size=uri.host.len+5;
762         if (!(p = get_static_buffer(size)))
763                 return -1;
764                         
765         strncpy(p, uri.host.s, uri.host.len);
766         switch (uri.type) {
767                 case SIPS_URI_T:
768                 case TELS_URI_T:
769                         strncpy(p+uri.host.len, ":5061", 5); 
770                         break;
771                 case SIP_URI_T:
772                 case TEL_URI_T:
773                         strncpy(p+uri.host.len, ":5060", 5);
774                         break;
775                 case ERROR_URI_T:
776                         return -1;
777         }
778         res->s = p;
779         res->len = size;
780         return 0;
781 }
782
783 int select_uri_proto(str* res, select_t* s, struct sip_msg* msg)
784 {
785         if (parse_uri(res->s, res->len, &uri)<0)
786                 return -1;
787
788         if (uri.proto != PROTO_NONE) {
789                 proto_type_to_str(uri.proto, res);
790         } else {
791                 switch (uri.type) {
792                         case SIPS_URI_T:
793                         case TELS_URI_T:
794                                 proto_type_to_str(PROTO_TLS, res);
795                                 break;
796                         case SIP_URI_T:
797                         case TEL_URI_T:
798                                 proto_type_to_str(PROTO_UDP, res);
799                                 break;
800                         case ERROR_URI_T:
801                                 return -1;
802                 }
803         }
804         return 0;
805 }
806
807 int select_uri_params(str* res, select_t* s, struct sip_msg* msg)
808 {
809         if (!msg || !res) {
810                 return select_any_params(res, s, msg);
811         }
812
813         if (parse_uri(res->s, res->len, &uri)<0)
814                 return -1;
815         
816         if (s->param_offset[select_level+1]-s->param_offset[select_level]==1)
817                 RETURN0_res(uri.params);
818
819         *res=uri.params;
820         return select_any_params(res, s, msg);
821 }
822
823 int select_any_params(str* res, select_t* s, struct sip_msg* msg)
824 {
825         param_hooks_t h;
826         param_t *p, *list=NULL;
827         str* wanted;
828         int i;
829
830         if (!msg || !res) {
831                 if (s->param_offset[select_level+1]-s->param_offset[select_level]==1) return 0;
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                 for (i=0; i<wanted->len; i++) 
835                         if (wanted->s[i]=='_') 
836                                 wanted->s[i]='-';
837                 return 0;
838         }
839         
840         if (s->params[s->param_offset[select_level]+1].type!=SEL_PARAM_STR) return -1;
841         wanted=&s->params[s->param_offset[select_level]+1].v.s;
842         
843         if (!res->len) return -1;
844         if (parse_params(res, CLASS_ANY, &h, &list)<0) return -1;
845         
846         for (p = list; p; p=p->next) {
847                 if ((p->name.len==wanted->len) && 
848                          !strncasecmp(p->name.s, wanted->s,wanted->len)) {
849                         *res=p->body;
850                         free_params(list);
851                         return (res->len ? 0 : 1);
852                 }
853         }
854         free_params(list);
855
856         DBG("SELECT ...uri.params.%s NOT FOUND !\n", wanted->s);
857         return -1;
858 }
859
860 int select_event(str* res, select_t* s, struct sip_msg* msg)
861 {
862         if (!msg->event && parse_headers(msg, HDR_EVENT_F, 0) == -1) {
863                 ERR("Error while searching Event header field\n");
864                 return -1;
865         }
866
867         if (!msg->event) {
868                 DBG("Event header field not found\n");
869                 return -1;
870         }
871
872         if (parse_event(msg->event) < 0) {
873                 ERR("Error while parsing Event header field\n");
874                 return -1;
875         }
876
877         *res = ((event_t*)msg->event->parsed)->text;
878         return 0;
879 }
880
881
882
883 static int parse_rr_header(struct sip_msg *msg)
884 {
885         if ( !msg->record_route && ( parse_headers(msg,HDR_RECORDROUTE_F,0) == -1)) {
886                 ERR("bad msg or missing Record-Route header\n");
887                 return -1;
888         }
889
890         if (!msg->record_route) {
891                 DBG("No Record-Route header field found\n");
892                 return -1;
893         }
894
895         return parse_rr(msg->record_route);
896 }
897
898 #define get_rr(msg) ((rr_t*)(msg->record_route->parsed))
899
900
901 int select_rr(str* res, select_t* s, struct sip_msg* msg)
902 {
903         if (parse_rr_header(msg)<0)
904                 return -1;
905         RETURN0_res(msg->record_route->body);
906 }
907
908 int select_rr_uri(str* res, select_t* s, struct sip_msg* msg)
909 {
910         rr_t* r;
911         if (parse_rr_header(msg)<0)
912                 return -1;
913         
914         r = get_rr(msg);
915         if (!r)
916                 return 1;
917         RETURN0_res(r->nameaddr.uri);
918 }
919
920 int select_rr_name(str* res, select_t* s, struct sip_msg* msg)
921 {
922         rr_t* r;
923         if (parse_rr_header(msg)<0)
924                 return -1;
925         
926         r = get_rr(msg);
927         if (!r)
928                 return 1;
929         RETURN0_res(r->nameaddr.name);
930 }
931
932 int select_rr_params(str* res, select_t* s, struct sip_msg* msg)
933 {
934         rr_t* r;
935         param_t* p;
936         if (parse_rr_header(msg)<0)
937                 return -1;
938         
939         r = get_rr(msg);
940         if (!r)
941                 return 1;
942         p = r->params;
943         while (p) {
944                 if ((p->name.len==s->params[s->n-1].v.s.len)
945                     && !strncasecmp(p->name.s, s->params[s->n-1].v.s.s,p->name.len)) {
946                         RETURN0_res(p->body)
947                 }
948                 p = p->next;
949         }
950         return 0;
951 }
952
953
954 static inline struct cseq_body* sel_parse_cseq(struct sip_msg* msg)
955 {
956         if (!msg->cseq && (parse_headers(msg, HDR_CSEQ_F, 0) == -1)) {
957                 ERR("Unable to parse CSeq header\n");
958                 return 0;
959         }
960
961         if (!msg->cseq) {
962                 DBG("No CSeqheader field found\n");
963                 return 0;
964         }
965
966         return get_cseq(msg);
967 }
968
969
970
971 int select_cseq(str* res, select_t* s, struct sip_msg* msg)
972 {
973         struct cseq_body* cs;
974
975         cs = sel_parse_cseq(msg);
976         if (!cs) return -1;
977         *res = msg->cseq->body;
978         return 0;
979 }
980
981 int select_cseq_num(str* res, select_t* s, struct sip_msg* msg)
982 {
983         struct cseq_body* cs;
984
985         cs = sel_parse_cseq(msg);
986         if (!cs) return -1;
987         *res = cs->number;
988         return 0;
989 }
990
991 int select_cseq_method(str* res, select_t* s, struct sip_msg* msg)
992 {
993         struct cseq_body* cs;
994
995         cs = sel_parse_cseq(msg);
996         if (!cs) return -1;
997         *res = cs->method;
998         return 0;
999 }
1000
1001 int get_credentials(struct sip_msg* msg, select_t* s, struct hdr_field** hdr)
1002 {
1003         int ret;
1004         str realm;
1005         hdr_types_t hdr_type;
1006
1007         *hdr = NULL;
1008
1009         if (!msg) {
1010                 /* fix-up call check domain for fparam conversion */
1011                 void * ptr;
1012                 char chr;
1013                 ptr=(void *)(s->params[1].v.s.s);
1014                 chr=s->params[1].v.s.s[s->params[1].v.s.len];
1015                 s->params[1].v.s.s[s->params[1].v.s.len]=0;
1016                 ret=fixup_var_str_12(&ptr,0);
1017                 if (ret>=0) {
1018                         s->params[1].v.s.s[s->params[1].v.s.len]=chr;
1019                         s->params[1].v.p=ptr;
1020                         s->params[1].type=SEL_PARAM_PTR;
1021                 }
1022                 return ret;
1023         }
1024         
1025
1026         /* Try to find credentials with corresponding realm
1027          * in the message, parse them and return pointer to
1028          * parsed structure
1029          */
1030         if (s->params[1].type==SEL_PARAM_PTR) {
1031                 if (get_str_fparam(&realm, msg, s->params[1].v.p)<0)
1032                         return -1;
1033         } else {
1034                 realm = s->params[1].v.s;
1035         }
1036
1037         switch (s->params[0].v.i) {
1038         case SEL_AUTH_WWW:
1039                 hdr_type = HDR_AUTHORIZATION_T;
1040                 break;
1041
1042         case SEL_AUTH_PROXY:
1043                 hdr_type = HDR_PROXYAUTH_T;
1044                 break;
1045
1046         default:
1047                 BUG("Unexpected parameter value \"%d\"\n", s->params[0].v.i);
1048                 return -1;
1049         }
1050
1051         ret = find_credentials(msg, &realm, hdr_type, hdr);
1052         return ret;
1053 }
1054
1055
1056 int select_auth(str* res, select_t* s, struct sip_msg* msg)
1057 {
1058         int ret;
1059         struct hdr_field* hdr;
1060
1061         if (s->n != 2 && s->params[1].type != SEL_PARAM_STR
1062                       && s->params[1].type != SEL_PARAM_PTR) return -1;
1063
1064         if (s->params[0].type != SEL_PARAM_DIV) {
1065                 BUG("Last parameter should have type DIV (converted)\n");
1066                 return -1;
1067         }
1068
1069         ret = get_credentials(msg, s, &hdr);
1070         if (!hdr) return ret;
1071         RETURN0_res(hdr->body);
1072 }
1073
1074 int select_auth_param(str* res, select_t* s, struct sip_msg* msg)
1075 {
1076         int ret;
1077         struct hdr_field* hdr;
1078         dig_cred_t* cred;
1079
1080         if ((s->n != 3 && s->n != 4) || (s->params[s->n - 1].type != SEL_PARAM_DIV)) return -1;
1081
1082         ret = get_credentials(msg, s, &hdr);
1083         if (!hdr) return ret;
1084         cred = &((auth_body_t*)hdr->parsed)->digest;
1085
1086         switch(s->params[s->n - 1].v.i) {
1087         case SEL_AUTH_USER:     RETURN0_res(cred->username.user);
1088         case SEL_AUTH_DOMAIN:   RETURN0_res(cred->username.domain);
1089         case SEL_AUTH_USERNAME: RETURN0_res(cred->username.whole);
1090         case SEL_AUTH_REALM:    RETURN0_res(cred->realm);
1091         case SEL_AUTH_NONCE:    RETURN0_res(cred->nonce);
1092         case SEL_AUTH_URI:      RETURN0_res(cred->uri);
1093         case SEL_AUTH_CNONCE:   RETURN0_res(cred->cnonce);
1094         case SEL_AUTH_NC:       RETURN0_res(cred->nc);
1095         case SEL_AUTH_RESPONSE: RETURN0_res(cred->response);
1096         case SEL_AUTH_OPAQUE:   RETURN0_res(cred->opaque);
1097         case SEL_AUTH_ALG:      RETURN0_res(cred->alg.alg_str);
1098         case SEL_AUTH_QOP:      RETURN0_res(cred->qop.qop_str);
1099         default:
1100                 BUG("Unsupported digest credentials parameter in select\n");
1101                 return -1;
1102         }
1103 }
1104
1105 int select_auth_username(str* res, select_t* s, struct sip_msg* msg)
1106 {
1107         return select_auth_param(res, s, msg);
1108 }
1109
1110 int select_auth_username_comp(str* res, select_t* s, struct sip_msg* msg)
1111 {
1112         return select_auth_param(res, s, msg);
1113 }
1114
1115 ABSTRACT_F(select_any_nameaddr)
1116
1117 int select_nameaddr_name(str* res, select_t* s, struct sip_msg* msg)
1118 {
1119         char *p;
1120         
1121         p=find_not_quoted(res, '<');
1122         if (!p) {
1123                 DBG("select_nameaddr_name: no < found, whole string is uri\n");
1124                 res->len=0;
1125                 return 1;
1126         }
1127
1128         res->len=p-res->s;
1129         while (res->len && SP(res->s[res->len-1])) res->len--;
1130         return 0;
1131 }
1132
1133 int select_nameaddr_uri(str* res, select_t* s, struct sip_msg* msg)
1134 {
1135         char *p;
1136         
1137         p=find_not_quoted(res, '<');
1138         if (!p) {
1139                 DBG("select_nameaddr_uri: no < found, string up to first semicolon is uri\n");
1140                 p = q_memchr(res->s, ';', res->len);
1141                 if (p)
1142                         res->len = p-res->s;
1143                 return 0;
1144         }
1145
1146         res->len=res->len - (p-res->s) -1;
1147         res->s=p +1;
1148         
1149         p=find_not_quoted(res, '>');
1150         if (!p) {
1151                 ERR("select_nameaddr_uri: no > found, invalid nameaddr value\n");
1152                 return -1;
1153         }
1154
1155         res->len=p-res->s;
1156         return 0;
1157 }
1158
1159 int select_nameaddr_params(str* res, select_t* s, struct sip_msg* msg)
1160 {
1161         char *p;
1162         
1163         p=find_not_quoted(res, '<');
1164         if (!p) {
1165                 p=find_not_quoted(res, ';');
1166         } else {
1167                 res->len=res->len - (p-res->s) -1;
1168                 res->s=p +1;
1169                 p=find_not_quoted(res, '>');
1170                 if (!p) {
1171                         ERR("select_nameaddr_params: no > found, invalid nameaddr value\n");
1172                         return -1;
1173                 }
1174                 res->len=res->len - (p-res->s) -1;
1175                 res->s=p +1;
1176                 
1177                 p=find_not_quoted(res, ';');
1178         }
1179         if (!p) return 1;
1180         
1181         res->len=res->len - (p-res->s) -1;
1182         res->s=p +1;
1183         
1184         if (s->param_offset[select_level+1]-s->param_offset[select_level]==1)
1185                 return (res->len ? 0 : 1);
1186
1187         return select_any_params(res, s, msg);
1188 }
1189
1190 ABSTRACT_F(select_src)
1191 ABSTRACT_F(select_dst)
1192 ABSTRACT_F(select_rcv)
1193 int select_ip_port(str* res, select_t* s, struct sip_msg* msg)
1194 {
1195         str ip_str=STR_NULL, port_str=STR_NULL, proto_str=STR_NULL;
1196         int param, pos;
1197         
1198
1199         if ((s->n != 2) || (s->params[1].type != SEL_PARAM_DIV)) return -1;
1200         param=s->params[1].v.i;
1201
1202         if (param & SEL_SRC) {
1203                 if (param & SEL_IP) {
1204                         ip_str.s = ip_addr2a(&msg->rcv.src_ip);
1205                         ip_str.len = strlen(ip_str.s);
1206                 }
1207                 if (param & SEL_PORT) {
1208                         port_str.s = int2str(msg->rcv.src_port, &port_str.len);
1209                 }
1210         } else if (param & SEL_DST) {
1211                 if (param & SEL_IP) {
1212                         ip_str.s = ip_addr2a(&msg->rcv.dst_ip);
1213                         ip_str.len = strlen(ip_str.s);
1214                 }
1215                 if (param & SEL_PORT) {
1216                         port_str.s = int2str(msg->rcv.dst_port, &port_str.len);
1217                 }
1218         } else if (param & SEL_RCV) {
1219                 if (param & SEL_IP) {
1220                         ip_str = msg->rcv.bind_address->address_str;
1221                 }
1222                 if (param & SEL_PORT) {
1223                         port_str = msg->rcv.bind_address->port_no_str;
1224                 }
1225                 if (param & SEL_PROTO) {
1226                         switch (msg->rcv.proto) {
1227                         case PROTO_NONE:
1228                                 proto_str.s = 0;
1229                                 proto_str.len = 0;
1230                                 break;
1231
1232                         case PROTO_UDP:
1233                                 proto_str.s = "udp";
1234                                 proto_str.len = 3;
1235                                 break;
1236
1237                         case PROTO_TCP:
1238                                 proto_str.s = "tcp";
1239                                 proto_str.len = 3;
1240                                 break;
1241
1242                         case PROTO_TLS:
1243                                 proto_str.s = "tls";
1244                                 proto_str.len = 3;
1245                                 break;
1246
1247                         case PROTO_SCTP:
1248                                 proto_str.s = "sctp";
1249                                 proto_str.len = 4;
1250                                 break;
1251
1252                         default:
1253                                 ERR("BUG: select_ip_port: Unknown transport protocol\n");
1254                                 return -1;
1255                         }
1256                 }
1257         } else {
1258                 return -1;
1259         }
1260
1261         res->s = get_static_buffer(ip_str.len+port_str.len+proto_str.len+3);
1262         if (!res->s) return -1;
1263
1264         pos=0;
1265         if (param & SEL_PROTO) {
1266                 memcpy(res->s, proto_str.s, proto_str.len);
1267                 pos += proto_str.len;
1268         }
1269         if (param & SEL_IP) {
1270                 if (pos) res->s[pos++] = ':';
1271                 memcpy(res->s+pos, ip_str.s, ip_str.len);
1272                 pos += ip_str.len;
1273         }
1274         if (param & SEL_PORT) {
1275                 if (pos) res->s[pos++] = ':';
1276                 memcpy(res->s+pos, port_str.s, port_str.len);
1277                 pos += port_str.len;
1278         }
1279         res->s[pos] = 0;
1280         res->len = pos;
1281         return (pos==0 ? 1 : 0);
1282
1283 }
1284
1285
1286 int select_expires(str* res, select_t* s, struct sip_msg* msg)
1287 {
1288         if (!msg->expires && (parse_headers(msg, HDR_EXPIRES_F, 0) == -1)) {
1289                 return -1; /* error */
1290         }
1291
1292         if (!msg->expires) {
1293                 return 1;  /* null */
1294         }
1295
1296         if (msg->expires->parsed == NULL && parse_expires(msg->expires) < 0) {
1297                 return -1;
1298         }
1299
1300         RETURN0_res(((struct exp_body*)msg->expires->parsed)->text);
1301 }
1302
1303 #define SELECT_plain_header(_sel_name_,_fld_name_,_hdr_f_) \
1304 int select_##_sel_name_(str* res, select_t* s, struct sip_msg* msg) \
1305 { \
1306         if (!msg->_fld_name_ && (parse_headers(msg, _hdr_f_, 0) == -1)) { \
1307                 return -1; \
1308         } \
1309         if (!msg->_fld_name_) { \
1310                 return 1; \
1311         } \
1312         RETURN0_res(msg->_fld_name_->body); \
1313 }
1314
1315 SELECT_plain_header(call_id, callid, HDR_CALLID_F)
1316 SELECT_plain_header(max_forwards, maxforwards, HDR_MAXFORWARDS_F)
1317 SELECT_plain_header(content_type, content_type, HDR_CONTENTTYPE_F)
1318 SELECT_plain_header(content_length, content_length, HDR_CONTENTLENGTH_F)
1319 SELECT_plain_header(user_agent, user_agent, HDR_USERAGENT_F)
1320 SELECT_plain_header(subject, subject, HDR_SUBJECT_F)
1321 SELECT_plain_header(organization, organization, HDR_ORGANIZATION_F)
1322 SELECT_plain_header(priority, priority, HDR_PRIORITY_F)
1323 SELECT_plain_header(session_expires, session_expires, HDR_SESSIONEXPIRES_F)
1324 SELECT_plain_header(min_se, min_se, HDR_MIN_SE_F)
1325 SELECT_plain_header(sip_if_match, sipifmatch, HDR_SIPIFMATCH_F)
1326 SELECT_plain_header(date, date, HDR_DATE_F)
1327 SELECT_plain_header(identity, identity, HDR_IDENTITY_F)
1328 SELECT_plain_header(identity_info, identity_info, HDR_IDENTITY_INFO_F)
1329
1330 int select_msg_request(str* res, select_t* s, struct sip_msg* msg)
1331 {
1332         if (msg->first_line.type==SIP_REQUEST) { 
1333                 return select_msg(res, s, msg); 
1334         }
1335         else
1336                 return -1;      
1337 }
1338
1339 int select_msg_response(str* res, select_t* s, struct sip_msg* msg)
1340 {
1341         if (msg->first_line.type==SIP_REPLY) {
1342                 return select_msg(res, s, msg); 
1343         }
1344         else
1345                 return -1;      
1346 }
1347
1348 #define SELECT_first_line(_sel_name_,_type_,_fld_) \
1349 int select_msg_##_sel_name_(str* res, select_t* s, struct sip_msg* msg) {\
1350         if (msg->first_line.type==_type_) { \
1351                 RETURN0_res(msg->first_line._fld_); \
1352         } else return -1; \
1353 }
1354
1355 SELECT_first_line(request_method,SIP_REQUEST,u.request.method)
1356 SELECT_first_line(request_uri,SIP_REQUEST,u.request.uri)
1357 SELECT_first_line(request_version,SIP_REQUEST,u.request.version)
1358 SELECT_first_line(response_version,SIP_REPLY,u.reply.version)
1359 SELECT_first_line(response_status,SIP_REPLY,u.reply.status)
1360 SELECT_first_line(response_reason,SIP_REPLY,u.reply.reason)
1361
1362
1363 int select_version(str* res, select_t* s, struct sip_msg* msg)
1364 {
1365         switch (msg->first_line.type) { 
1366                 case SIP_REQUEST:
1367                         RETURN0_res(msg->first_line.u.request.version)
1368                         break;
1369                 case SIP_REPLY:
1370                         RETURN0_res(msg->first_line.u.reply.version)
1371                         break;
1372                 default:
1373                         return -1;
1374         }
1375 }
1376
1377 ABSTRACT_F(select_sys)
1378
1379 int select_sys_pid(str* res, select_t* s, struct sip_msg* msg) {
1380         return uint_to_static_buffer(res, getpid());
1381 }
1382
1383 int select_sys_server_id(str* res, select_t* s, struct sip_msg* msg) {
1384         return int_to_static_buffer(res, server_id);
1385 }
1386
1387 int select_sys_unique(str* res, select_t* s, struct sip_msg* msg) {
1388         #define UNIQUE_ID_PID_LEN 4
1389         #define UNIQUE_ID_TIME_LEN 8
1390         #define UNIQUE_ID_FIX_LEN (UNIQUE_ID_PID_LEN+1+UNIQUE_ID_TIME_LEN+1)
1391         #define UNIQUE_ID_RAND_LEN 8
1392         static char uniq_id[UNIQUE_ID_FIX_LEN+UNIQUE_ID_RAND_LEN];
1393         static int uniq_for_pid = -1;
1394         int i;
1395
1396         if (uniq_for_pid != getpid()) {
1397                 /* first call for this process */
1398                 int cb, rb, x, l;
1399                 char *c;
1400                 /* init gloabally uniq part */
1401                 c = int2str_base_0pad(getpid(), &l, 16, UNIQUE_ID_PID_LEN);
1402                 memcpy(uniq_id, c, UNIQUE_ID_PID_LEN);
1403                 uniq_id[UNIQUE_ID_PID_LEN] = '-';
1404                 c = int2str_base_0pad(time(NULL), &l, 16, UNIQUE_ID_TIME_LEN);
1405                 memcpy(uniq_id+UNIQUE_ID_PID_LEN+1, c, UNIQUE_ID_TIME_LEN);
1406                 uniq_id[UNIQUE_ID_PID_LEN+1+UNIQUE_ID_TIME_LEN] = '-';
1407
1408                 /* init random part */
1409                 for (i = RAND_MAX, rb=0; i; rb++, i>>=1);
1410                 for (i = UNIQUE_ID_FIX_LEN, cb = 0, x = 0; i < UNIQUE_ID_FIX_LEN+UNIQUE_ID_RAND_LEN; i++) {
1411                         if (!cb) {
1412                                 cb = rb;
1413                                 x = rand();
1414                         }
1415                         uniq_id[i] = fourbits2char[x & 0x0F];
1416                         x >>= rb;
1417                         cb -= rb;
1418                 }
1419                 uniq_for_pid = getpid();
1420         }
1421         for (i = UNIQUE_ID_FIX_LEN + UNIQUE_ID_RAND_LEN - 1; i >= UNIQUE_ID_FIX_LEN; i--) {
1422                 switch (uniq_id[i]) {
1423                         case '9':
1424                                 uniq_id[i]='a';
1425                                 i = 0;
1426                                 break;
1427                         case 'f':
1428                                 uniq_id[i]='0';
1429                                 /* go on */
1430                                 break;
1431                         default:
1432                                 uniq_id[i]++;
1433                                 i = 0;
1434                                 break;
1435                 }
1436         }
1437         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 */
1438         res->len = UNIQUE_ID_FIX_LEN+UNIQUE_ID_RAND_LEN;
1439         return 0;
1440 }
1441
1442
1443 int select_sys_now(str* res, select_t* s, struct sip_msg* msg) {
1444         return uint_to_static_buffer(res, time(NULL));
1445 }
1446
1447 int select_sys_now_fmt(str* res, select_t* s, struct sip_msg* msg)
1448 {
1449 #define SEL_POS 2
1450         time_t t;
1451         struct tm *tm;
1452         
1453         t = time(NULL);
1454         switch (s->params[SEL_POS].v.i) {
1455                 case SEL_NOW_GMT:
1456                         tm = gmtime(&t);
1457                         break;
1458         
1459                 case SEL_NOW_LOCAL:
1460                         tm = localtime(&t);
1461                         break;
1462                 default:
1463                         BUG("Unexpected parameter value 'now' \"%d\"\n", s->params[SEL_POS].v.i);
1464                         return -1;
1465         }
1466         if (s->n <= SEL_POS+1) {
1467                 char *c;
1468                 c = asctime(tm);
1469                 res->len = strlen(c);
1470                 while (res->len && c[res->len-1] < ' ') res->len--; /* rtrim */
1471                 res->s = get_static_buffer(res->len);
1472                 if (!res->s) return -1;
1473                 memcpy(res->s, c, res->len);
1474         }
1475         else {
1476                 char c, buff[80];
1477                 c = s->params[SEL_POS+1].v.s.s[s->params[SEL_POS+1].v.s.len];
1478                 s->params[SEL_POS+1].v.s.s[s->params[SEL_POS+1].v.s.len] = '\0';
1479                 res->len = strftime(buff, sizeof(buff)-1, s->params[SEL_POS+1].v.s.s, tm);
1480                 s->params[SEL_POS+1].v.s.s[s->params[SEL_POS+1].v.s.len] = c;
1481                 res->s = get_static_buffer(res->len);
1482                 if (!res->s) return -1;
1483                 memcpy(res->s, buff, res->len);
1484         }
1485         return 0;
1486 #undef SEL_POS
1487 }
1488
1489 ABSTRACT_F(select_branch)
1490
1491 int select_branch_count(str* res, select_t* s, struct sip_msg* msg) {
1492         return uint_to_static_buffer(res, nr_branches);
1493 }
1494
1495 int select_branch_uri(str* res, select_t* s, struct sip_msg* msg) {
1496 #define SEL_POS 1 
1497 #define Q_PARAM ">;q="
1498 #define Q_PARAM_LEN (sizeof(Q_PARAM) - 1)
1499
1500         qvalue_t q;
1501         int l, n;
1502         str dst_uri;
1503         if (s->n <= SEL_POS+1 && nr_branches > 1) { /* get all branches, if nr_branches==1 then use faster algorithm */
1504                 int len;
1505                 unsigned l2;
1506                 char *c;
1507                 init_branch_iterator();
1508                 len = 0;
1509                 while ((c = next_branch(&l, &q, &dst_uri.s, &dst_uri.len, 0))) {
1510
1511                         if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
1512                                 l = dst_uri.len;
1513                                 c = dst_uri.s;
1514                         }
1515                         if (s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI) ) {
1516                                 len += l;
1517                         }
1518                         if (q != Q_UNSPECIFIED && (s->params[SEL_POS].v.i & SEL_BRANCH_Q)) {
1519                                 len += len_q(q);
1520                                 if (s->params[SEL_POS].v.i & SEL_BRANCH_URI) {
1521                                         len += 1 + Q_PARAM_LEN;
1522                                 }
1523                         }
1524                         len += 1;
1525                 }
1526                 if (!len) return 1;
1527                 res->s = get_static_buffer(len);
1528                 if (!res->s) return -1;
1529                 
1530                 init_branch_iterator();
1531                 res->len = 0;
1532                 n = 0;
1533                 while ((c = next_branch(&l, &q, &dst_uri.s, &dst_uri.len, 0))) {
1534                         if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
1535                                 l = dst_uri.len;
1536                                 c = dst_uri.s;
1537                         }
1538                         if (n) {
1539                                 res->s[res->len] = ',';
1540                                 res->len++;
1541                         }
1542                         if ((s->params[SEL_POS].v.i & SEL_BRANCH_Q) == 0) {
1543                                 q = Q_UNSPECIFIED;
1544                         }
1545                         if ((s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI)) && q != Q_UNSPECIFIED) {
1546                                 res->s[res->len] = '<';
1547                                 res->len++;
1548                                 memcpy(res->s+res->len, c, l);
1549                                 res->len += l; 
1550                                 memcpy(res->s+res->len, Q_PARAM, Q_PARAM_LEN);
1551                                 res->len += Q_PARAM_LEN;
1552                                 c = q2str(q, &l2); l = l2;
1553                                 memcpy(res->s+res->len, c, l);
1554                                 res->len += l; 
1555                         }
1556                         else if (s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI)) {
1557                                 memcpy(res->s+res->len, c, l);
1558                                 res->len += l; 
1559                         }
1560                         else if (q != Q_UNSPECIFIED) {
1561                                 c = q2str(q, &l2); l = l2;
1562                                 memcpy(res->s+res->len, c, l);
1563                                 res->len += l; 
1564                         }
1565                         n++;
1566                 }
1567         }
1568         else {
1569                 unsigned l2;
1570                 char *c;
1571                 n = s->params[SEL_POS+1].v.i;
1572                 if (n < 0 || n >= nr_branches) 
1573                         return -1;
1574                 init_branch_iterator();
1575                 for (; (c = next_branch(&l, &q, &dst_uri.s, &dst_uri.len, 0)) && n; n--);
1576                 if (!c) return 1;
1577                 
1578                 if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
1579                         l = dst_uri.len;
1580                         c = dst_uri.s;
1581                 }
1582                 if ((s->params[SEL_POS].v.i & SEL_BRANCH_Q) == 0) {
1583                         q = Q_UNSPECIFIED;
1584                 }
1585                 if ((s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI)) && q != Q_UNSPECIFIED) {
1586
1587                         res->s = get_static_buffer(l + 1 + Q_PARAM_LEN + len_q(q));
1588                         if (!res->s) return -1;
1589                         res->len = 1;
1590                         res->s[0] = '<';
1591                         memcpy(res->s+res->len, c, l);
1592                         res->len += l; 
1593                         memcpy(res->s+res->len, Q_PARAM, Q_PARAM_LEN);
1594                         res->len += Q_PARAM_LEN;
1595                         c = q2str(q, &l2); l = l2;
1596                         memcpy(res->s+res->len, c, l);
1597                         res->len += l; 
1598                 }
1599                 else if (s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI)) {
1600                         res->s = c;  /* not necessary to copy to static buffer */
1601                         res->len = l;
1602                 } 
1603                 else if (q != Q_UNSPECIFIED) {
1604                         c = q2str(q, &l2);
1605                         res->len = l2;
1606                         res->s = get_static_buffer(res->len);
1607                         if (!res->s) return -1;
1608                         memcpy(res->s, c, res->len);
1609                 }
1610                 else {
1611                         res->len = 0;
1612                 }
1613         }
1614         return 0;
1615 #undef SEL_POS
1616 }
1617
1618 int select_branch_uriq(str* res, select_t* s, struct sip_msg* msg) {
1619         return select_branch_uri(res, s, msg);
1620 }
1621
1622 int select_branch_q(str* res, select_t* s, struct sip_msg* msg) {
1623         return select_branch_uri(res, s, msg);
1624 }
1625
1626 int select_branch_dst_uri(str* res, select_t* s, struct sip_msg* msg) {
1627         return select_branch_uri(res, s, msg);
1628 }