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