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