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