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