4 * Copyright (C) 2005-2006 iptelorg GmbH
6 * This file is part of ser, a free SIP server.
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
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:
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.
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
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)
37 #include "select_core.h"
38 #include "select_buf.h"
43 #include "parser/parser_f.h"
44 #include "parser/hf.h"
45 #include "parser/parse_from.h"
46 #include "parser/parse_to.h"
47 #include "parser/contact/parse_contact.h"
48 #include "parser/contact/contact.h"
49 #include "parser/parse_via.h"
50 #include "parser/parse_uri.h"
51 #include "parser/parse_event.h"
52 #include "parser/parse_rr.h"
53 #include "parser/digest/digest.h"
55 #include "parser/parse_hname2.h"
57 #include "parser/parse_expires.h"
58 #include "parser/parse_refer_to.h"
59 #include "parser/parse_rpid.h"
60 #include "parser/parse_content.h"
61 #include "parser/parse_body.h"
63 #include "sr_module.h"
65 #define RETURN0_res(x) {*res=(x);return 0;}
66 #define TRIM_RET0_res(x) {*res=(x);trim(res);return 0;}
67 #define TEST_RET_res_body(x) if (x){*res=(x)->body;return 0;}else return 1;
68 #define TEST_RET_res_value(x) if (x){*res=(x)->value;return 0;}else return 1;
70 int select_ruri(str* res, select_t* s, struct sip_msg* msg)
72 if (msg->first_line.type==SIP_REQUEST) {
74 RETURN0_res(msg->new_uri);
77 RETURN0_res(msg->first_line.u.request.uri);
83 int select_dst_uri(str* res, select_t* s, struct sip_msg* msg)
85 if (msg->first_line.type!=SIP_REQUEST)
87 RETURN0_res(msg->dst_uri);
90 int select_next_hop(str* res, select_t* s, struct sip_msg* msg)
92 if (msg->first_line.type==SIP_REQUEST) {
94 RETURN0_res(msg->dst_uri);
96 else if(msg->new_uri.s) {
97 RETURN0_res(msg->new_uri);
100 RETURN0_res(msg->first_line.u.request.uri);
106 #define SELECT_uri_header(_name_) \
107 int select_##_name_(str* res, select_t* s, struct sip_msg* msg) \
109 if (parse_##_name_##_header(msg)<0) \
111 RETURN0_res(msg->_name_->body); \
114 int select_##_name_##_uri(str* res, select_t* s, struct sip_msg* msg) \
116 if (parse_##_name_##_header(msg)<0) \
118 RETURN0_res(get_##_name_(msg)->uri); \
121 int select_##_name_##_tag(str* res, select_t* s, struct sip_msg* msg) \
123 if (parse_##_name_##_header(msg)<0) \
125 RETURN0_res(get_##_name_(msg)->tag_value); \
128 int select_##_name_##_name(str* res, select_t* s, struct sip_msg* msg) \
130 if (parse_##_name_##_header(msg)<0) \
132 RETURN0_res(get_##_name_(msg)->display); \
135 int select_##_name_##_params(str* res, select_t* s, struct sip_msg* msg) \
137 struct to_param* p; \
138 if (parse_##_name_##_header(msg)<0) \
141 p = get_##_name_(msg)->param_lst; \
143 if ((p->name.len==s->params[s->n-1].v.s.len) \
144 && !strncasecmp(p->name.s, s->params[s->n-1].v.s.s,p->name.len)) { \
145 RETURN0_res(p->value); \
152 int parse_to_header(struct sip_msg *msg)
154 if ( !msg->to && ( parse_headers(msg,HDR_TO_F,0)==-1 || !msg->to)) {
155 ERR("bad msg or missing TO header\n");
159 // HDR_TO_T is automatically parsed (get_hdr_field in parser/msg_parser.c)
160 // so check only ptr validity
167 SELECT_uri_header(to)
168 SELECT_uri_header(from)
169 SELECT_uri_header(refer_to)
170 SELECT_uri_header(rpid)
172 int parse_contact_header( struct sip_msg *msg)
174 if ( !msg->contact && ( parse_headers(msg,HDR_CONTACT_F,0)==-1 || !msg->contact)) {
175 DBG("bad msg or missing CONTACT header\n");
179 if (msg->contact->parsed)
182 return parse_contact(msg->contact);
185 #define get_contact(msg) ((contact_body_t*)(msg->contact->parsed))
187 int select_contact(str* res, select_t* s, struct sip_msg* msg)
189 if (parse_contact_header(msg)<0)
191 RETURN0_res(msg->contact->body);
194 int select_contact_uri(str* res, select_t* s, struct sip_msg* msg)
197 if (parse_contact_header(msg)<0)
200 c = get_contact(msg)->contacts;
206 int select_contact_name(str* res, select_t* s, struct sip_msg* msg)
209 if (parse_contact_header(msg)<0)
212 c = get_contact(msg)->contacts;
215 RETURN0_res(c->name);
218 int select_contact_params_spec(str* res, select_t* s, struct sip_msg* msg)
222 if (s->params[s->n-1].type != SEL_PARAM_DIV) {
223 BUG("Last parameter should have type DIV (converted)\n");
227 if (parse_contact_header(msg)<0)
230 c = get_contact(msg)->contacts;
234 switch (s->params[s->n-1].v.i) {
236 TEST_RET_res_body(c->q);
237 case SEL_PARAM_EXPIRES:
238 TEST_RET_res_body(c->expires);
239 case SEL_PARAM_METHOD:
240 TEST_RET_res_body(c->method);
241 case SEL_PARAM_RECEIVED:
242 TEST_RET_res_body(c->received);
243 case SEL_PARAM_INSTANCE:
244 TEST_RET_res_body(c->instance);
246 BUG("Unexpected parameter value \"%d\"\n", s->params[s->n-1].v.i);
252 int select_contact_params(str* res, select_t* s, struct sip_msg* msg)
256 if (parse_contact_header(msg)<0)
259 c = get_contact(msg)->contacts;
264 if ((p->name.len==s->params[s->n-1].v.s.len)
265 && !strncasecmp(p->name.s, s->params[s->n-1].v.s.s,p->name.len)) {
273 int parse_via_header( struct sip_msg *msg, int n, struct via_body** q)
276 struct via_body *pp = NULL;
282 if (!msg->h_via1 && (parse_headers(msg,HDR_VIA_F,0)==-1 || !msg->h_via1)) {
283 DBG("bad msg or missing VIA1 header \n");
286 pp = msg->h_via1->parsed;
291 if (!msg->h_via2 && (parse_headers(msg,HDR_VIA2_F,0)==-1 || !msg->h_via2)) {
292 DBG("bad msg or missing VIA2 header \n");
295 pp = msg->h_via2->parsed;
298 if (!msg->eoh && (parse_headers(msg,HDR_EOH_F,0)==-1 || !msg->eoh)) {
299 ERR("bad msg while parsing to EOH \n");
305 if (p->type == HDR_VIA_T) {
308 while (i && (pp->next)) {
316 DBG("missing VIA[%d] header\n", n);
327 int select_via(str* res, select_t* s, struct sip_msg* msg)
329 struct via_body *p = NULL;
331 if (((s->n == 1) || (s->params[1].type == SEL_PARAM_STR)) && (parse_via_header(msg, 1, &p)<0)) return -1;
332 else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
340 int select_via_name(str* res, select_t* s, struct sip_msg* msg)
342 struct via_body *p = NULL;
344 // it's not neccessary to test if (s->n > 1)
345 if ((s->params[1].type == SEL_PARAM_STR) && (parse_via_header(msg, 1, &p)<0)) return -1;
346 else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
348 RETURN0_res(p->name);
351 int select_via_version(str* res, select_t* s, struct sip_msg* msg)
353 struct via_body *p = NULL;
355 // it's not neccessary to test if (s->n > 1)
356 if ((s->params[1].type == SEL_PARAM_STR) && (parse_via_header(msg, 1, &p)<0)) return -1;
357 else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
359 RETURN0_res(p->version);
362 int select_via_transport(str* res, select_t* s, struct sip_msg* msg)
364 struct via_body *p = NULL;
366 // it's not neccessary to test if (s->n > 1)
367 if ((s->params[1].type == SEL_PARAM_STR) && (parse_via_header(msg, 1, &p)<0)) return -1;
368 else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
370 RETURN0_res(p->transport);
373 int select_via_host(str* res, select_t* s, struct sip_msg* msg)
375 struct via_body *p = NULL;
377 // it's not neccessary to test if (s->n > 1)
378 if ((s->params[1].type == SEL_PARAM_STR) && (parse_via_header(msg, 1, &p)<0)) return -1;
379 else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
381 RETURN0_res(p->host);
384 int select_via_port(str* res, select_t* s, struct sip_msg* msg)
386 struct via_body *p = NULL;
388 // it's not neccessary to test if (s->n > 1)
389 if ((s->params[1].type == SEL_PARAM_STR) && (parse_via_header(msg, 1, &p)<0)) return -1;
390 else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
392 RETURN0_res(p->port_str);
395 int select_via_comment(str* res, select_t* s, struct sip_msg* msg)
397 struct via_body *p = NULL;
399 // it's not neccessary to test if (s->n > 1)
400 if ((s->params[1].type == SEL_PARAM_STR) && (parse_via_header(msg, 1, &p)<0)) return -1;
401 else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
403 RETURN0_res(p->comment);
406 int select_via_params(str* res, select_t* s, struct sip_msg* msg)
408 struct via_body *p = NULL;
411 // it's not neccessary to test if (s->n > 1)
412 if ((s->params[1].type == SEL_PARAM_STR) && (parse_via_header(msg, 1, &p)<0)) return -1;
413 else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
416 for (q = p->param_lst;q;q=q->next) {
417 if ((q->name.len==s->params[s->n-1].v.s.len)
418 && !strncasecmp(q->name.s, s->params[s->n-1].v.s.s,q->name.len)) {
419 RETURN0_res(q->value);
425 int select_via_params_spec(str* res, select_t* s, struct sip_msg* msg)
427 struct via_body *p = NULL;
429 if (s->params[s->n-1].type != SEL_PARAM_DIV) {
430 BUG("Last parameter should have type DIV (converted)\n");
434 // it's not neccessary to test if (s->n > 1)
435 if ((s->params[1].type == SEL_PARAM_STR) && (parse_via_header(msg, 1, &p)<0)) return -1;
436 else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
439 switch (s->params[s->n-1].v.i) {
440 case SEL_PARAM_BRANCH:
441 TEST_RET_res_value(p->branch);
442 case SEL_PARAM_RECEIVED:
443 TEST_RET_res_value(p->received);
444 case SEL_PARAM_RPORT:
445 TEST_RET_res_value(p->rport);
447 TEST_RET_res_value(p->i);
448 case SEL_PARAM_ALIAS:
449 TEST_RET_res_value(p->alias);
451 BUG("Unexpected parameter value \"%d\"\n", s->params[s->n-1].v.i);
457 int select_msg(str* res, select_t* s, struct sip_msg* msg)
464 int select_msg_first_line(str* res, select_t* s, struct sip_msg* msg)
466 res->s=SIP_MSG_START(msg);
467 res->len=msg->first_line.len;
472 int select_msg_type(str* res, select_t* s, struct sip_msg* msg) {
473 return uint_to_static_buffer(res, msg->first_line.type);
476 int select_msg_len(str* res, select_t* s, struct sip_msg* msg) {
477 return uint_to_static_buffer(res, msg->len);
480 int select_msg_id(str* res, select_t* s, struct sip_msg* msg) {
481 return uint_to_static_buffer(res, msg->id);
484 int select_msg_id_hex(str* res, select_t* s, struct sip_msg* msg) {
485 return uint_to_static_buffer_ex(res, msg->id, 16, 8);
488 int select_msg_flags(str* res, select_t* s, struct sip_msg* msg) {
489 return uint_to_static_buffer(res, msg->flags);
492 int select_msg_body(str* res, select_t* s, struct sip_msg* msg)
494 res->s = get_body(msg);
495 res->len = msg->buf+msg->len - res->s;
499 int select_msg_body_sdp(str* res, select_t* sel, struct sip_msg* msg)
501 /* try to get the body part with application/sdp */
502 if ((res->s = get_body_part(msg,
503 TYPE_APPLICATION, SUBTYPE_SDP,
511 int select_msg_header(str* res, select_t* s, struct sip_msg* msg)
513 /* get all headers */
515 res->s = SIP_MSG_START(msg) + msg->first_line.len;
517 res->len = c - res->s;
521 int select_anyheader(str* res, select_t* s, struct sip_msg* msg)
523 struct hdr_field *hf, *hf0;
526 struct hdr_field hdr;
529 if (res!=NULL) return -1;
531 /* "fixup" call, res & msg are NULL */
532 if (s->n <3) return -1;
534 if (s->params[2].type==SEL_PARAM_STR) {
535 /* replace _ with - (for P-xxx headers) */
536 for (hi=s->params[2].v.s.len-1; hi>0; hi--)
537 if (s->params[2].v.s.s[hi]=='_')
538 s->params[2].v.s.s[hi]='-';
539 /* if header name is parseable, parse it and set SEL_PARAM_DIV */
540 c=s->params[2].v.s.s[s->params[2].v.s.len];
541 s->params[2].v.s.s[s->params[2].v.s.len]=':';
542 if (parse_hname2(s->params[2].v.s.s,s->params[2].v.s.s+(s->params[2].v.s.len<3?4:s->params[2].v.s.len+1),
544 ERR("select_anyhdr:fixup_call:parse error\n");
547 s->params[2].v.s.s[s->params[2].v.s.len]=c;
549 if (hdr.type!=HDR_OTHER_T && hdr.type!=HDR_ERROR_T) {
550 /* pkg_free(s->params[1].v.s.s); */
551 /* don't free it (the mem can leak only once at startup)
552 * the parsed string can live inside larger string block
553 * e.g. when xlog's select is parsed
555 s->params[2].type = SEL_PARAM_DIV;
556 s->params[2].v.i = hdr.type;
564 /* extract header index if present */
565 if (s->param_offset[select_level+1] == 4) {
566 if (s->params[3].type == SEL_PARAM_INT) {
567 hi = s->params[3].v.i;
575 /* we need to be sure we have parsed all headers */
576 if (!msg->eoh && (parse_headers(msg,HDR_EOH_F,0)==-1 || !msg->eoh)) {
577 ERR("bad msg while parsing to EOH \n");
580 for (hf=msg->headers; hf; hf=hf->next) {
581 if(s->params[2].type==SEL_PARAM_DIV) {
582 if (s->params[2].v.i!=hf->type) continue;
583 } else if(s->params[2].type==SEL_PARAM_STR) {
584 if (s->params[2].v.s.len!=hf->name.len) continue;
585 if (strncasecmp(s->params[2].v.s.s, hf->name.s, hf->name.len)!=0) continue;
591 if(hf0==NULL || hi>0)
593 res->s = hf0->body.s;
594 res->len = hf0->body.len;
599 //ABSTRACT_F(select_anyheader_params)
600 // Instead of ABSTRACT_F(select_anyheader_params)
601 // use function which uses select_core_table
602 // to avoid gcc warning about not used
604 int select_anyheader_params(str* res, select_t* s, struct sip_msg* msg)
606 if (select_core_table.next)
612 ABSTRACT_F(select_any_uri)
614 static struct sip_uri uri;
616 int select_uri_type(str* res, select_t* s, struct sip_msg* msg)
619 if (parse_uri(res->s, res->len, &uri)<0)
622 if (uri.type==ERROR_URI_T)
625 uri_type_to_str(uri.type, res);
629 int select_uri_user(str* res, select_t* s, struct sip_msg* msg)
631 if (parse_uri(res->s, res->len, &uri)<0)
634 if (uri.flags & URI_USER_NORMALIZE) {
635 if (!(res->s=get_static_buffer(uri.user.len)))
637 if ((res->len=normalize_tel_user(res->s, (&uri.user)))==0)
641 RETURN0_res(uri.user);
644 int select_uri_pwd(str* res, select_t* s, struct sip_msg* msg)
646 if (parse_uri(res->s, res->len, &uri)<0)
649 RETURN0_res(uri.passwd);
652 int select_uri_host(str* res, select_t* s, struct sip_msg* msg)
654 if (parse_uri(res->s, res->len, &uri)<0)
657 RETURN0_res(uri.host);
660 int select_uri_port(str* res, select_t* s, struct sip_msg* msg)
662 if (parse_uri(res->s, res->len, &uri)<0)
665 RETURN0_res(uri.port);
668 int select_uri_hostport(str* res, select_t* s, struct sip_msg* msg)
673 if (parse_uri(res->s,res->len, &uri)<0)
681 res->len=uri.host.len+uri.port.len+1;
686 if (!(p = get_static_buffer(size)))
689 strncpy(p, uri.host.s, uri.host.len);
693 strncpy(p+uri.host.len, ":5061", 5);
697 strncpy(p+uri.host.len, ":5060", 5);
707 int select_uri_proto(str* res, select_t* s, struct sip_msg* msg)
709 if (parse_uri(res->s, res->len, &uri)<0)
712 if (uri.proto != PROTO_NONE) {
713 proto_type_to_str(uri.proto, res);
718 proto_type_to_str(PROTO_TLS, res);
722 proto_type_to_str(PROTO_UDP, res);
731 int select_uri_params(str* res, select_t* s, struct sip_msg* msg)
734 return select_any_params(res, s, msg);
737 if (parse_uri(res->s, res->len, &uri)<0)
740 if (s->param_offset[select_level+1]-s->param_offset[select_level]==1)
741 RETURN0_res(uri.params);
744 return select_any_params(res, s, msg);
747 int select_any_params(str* res, select_t* s, struct sip_msg* msg)
750 param_t *p, *list=NULL;
755 if (s->param_offset[select_level+1]-s->param_offset[select_level]==1) return 0;
756 if (s->params[s->param_offset[select_level]+1].type!=SEL_PARAM_STR) return -1;
757 wanted=&s->params[s->param_offset[select_level]+1].v.s;
758 for (i=0; i<wanted->len; i++)
759 if (wanted->s[i]=='_')
764 if (s->params[s->param_offset[select_level]+1].type!=SEL_PARAM_STR) return -1;
765 wanted=&s->params[s->param_offset[select_level]+1].v.s;
767 if (!res->len) return -1;
768 if (parse_params(res, CLASS_ANY, &h, &list)<0) return -1;
770 for (p = list; p; p=p->next) {
771 if ((p->name.len==wanted->len) &&
772 !strncasecmp(p->name.s, wanted->s,wanted->len)) {
775 return (res->len ? 0 : 1);
780 DBG("SELECT ...uri.params.%s NOT FOUND !\n", wanted->s);
784 int select_event(str* res, select_t* s, struct sip_msg* msg)
786 if (!msg->event && parse_headers(msg, HDR_EVENT_F, 0) == -1) {
787 ERR("Error while searching Event header field\n");
792 DBG("Event header field not found\n");
796 if (parse_event(msg->event) < 0) {
797 ERR("Error while parsing Event header field\n");
801 *res = ((event_t*)msg->event->parsed)->text;
807 static int parse_rr_header(struct sip_msg *msg)
809 if ( !msg->record_route && ( parse_headers(msg,HDR_RECORDROUTE_F,0) == -1)) {
810 ERR("bad msg or missing Record-Route header\n");
814 if (!msg->record_route) {
815 DBG("No Record-Route header field found\n");
819 return parse_rr(msg->record_route);
822 #define get_rr(msg) ((rr_t*)(msg->record_route->parsed))
825 int select_rr(str* res, select_t* s, struct sip_msg* msg)
827 if (parse_rr_header(msg)<0)
829 RETURN0_res(msg->record_route->body);
832 int select_rr_uri(str* res, select_t* s, struct sip_msg* msg)
835 if (parse_rr_header(msg)<0)
841 RETURN0_res(r->nameaddr.uri);
844 int select_rr_name(str* res, select_t* s, struct sip_msg* msg)
847 if (parse_rr_header(msg)<0)
853 RETURN0_res(r->nameaddr.name);
856 int select_rr_params(str* res, select_t* s, struct sip_msg* msg)
860 if (parse_rr_header(msg)<0)
868 if ((p->name.len==s->params[s->n-1].v.s.len)
869 && !strncasecmp(p->name.s, s->params[s->n-1].v.s.s,p->name.len)) {
878 static inline struct cseq_body* sel_parse_cseq(struct sip_msg* msg)
880 if (!msg->cseq && (parse_headers(msg, HDR_CSEQ_F, 0) == -1)) {
881 ERR("Unable to parse CSeq header\n");
886 DBG("No CSeqheader field found\n");
890 return get_cseq(msg);
895 int select_cseq(str* res, select_t* s, struct sip_msg* msg)
897 struct cseq_body* cs;
899 cs = sel_parse_cseq(msg);
901 *res = msg->cseq->body;
905 int select_cseq_num(str* res, select_t* s, struct sip_msg* msg)
907 struct cseq_body* cs;
909 cs = sel_parse_cseq(msg);
915 int select_cseq_method(str* res, select_t* s, struct sip_msg* msg)
917 struct cseq_body* cs;
919 cs = sel_parse_cseq(msg);
925 int get_credentials(struct sip_msg* msg, select_t* s, struct hdr_field** hdr)
929 hdr_types_t hdr_type;
934 /* fix-up call check domain for fparam conversion */
937 ptr=(void *)(s->params[1].v.s.s);
938 chr=s->params[1].v.s.s[s->params[1].v.s.len];
939 s->params[1].v.s.s[s->params[1].v.s.len]=0;
940 ret=fixup_var_str_12(&ptr,0);
942 s->params[1].v.s.s[s->params[1].v.s.len]=chr;
943 s->params[1].v.p=ptr;
944 s->params[1].type=SEL_PARAM_PTR;
950 /* Try to find credentials with corresponding realm
951 * in the message, parse them and return pointer to
954 if (s->params[1].type==SEL_PARAM_PTR) {
955 if (get_str_fparam(&realm, msg, s->params[1].v.p)<0)
958 realm = s->params[1].v.s;
961 switch (s->params[0].v.i) {
963 hdr_type = HDR_AUTHORIZATION_T;
967 hdr_type = HDR_PROXYAUTH_T;
971 BUG("Unexpected parameter value \"%d\"\n", s->params[0].v.i);
975 ret = find_credentials(msg, &realm, hdr_type, hdr);
980 int select_auth(str* res, select_t* s, struct sip_msg* msg)
983 struct hdr_field* hdr;
985 if (s->n != 2 && s->params[1].type != SEL_PARAM_STR
986 && s->params[1].type != SEL_PARAM_PTR) return -1;
988 if (s->params[0].type != SEL_PARAM_DIV) {
989 BUG("Last parameter should have type DIV (converted)\n");
993 ret = get_credentials(msg, s, &hdr);
994 if (!hdr) return ret;
995 RETURN0_res(hdr->body);
998 int select_auth_param(str* res, select_t* s, struct sip_msg* msg)
1001 struct hdr_field* hdr;
1004 if ((s->n != 3 && s->n != 4) || (s->params[s->n - 1].type != SEL_PARAM_DIV)) return -1;
1006 ret = get_credentials(msg, s, &hdr);
1007 if (!hdr) return ret;
1008 cred = &((auth_body_t*)hdr->parsed)->digest;
1010 switch(s->params[s->n - 1].v.i) {
1011 case SEL_AUTH_USER: RETURN0_res(cred->username.user);
1012 case SEL_AUTH_DOMAIN: RETURN0_res(cred->username.domain);
1013 case SEL_AUTH_USERNAME: RETURN0_res(cred->username.whole);
1014 case SEL_AUTH_REALM: RETURN0_res(cred->realm);
1015 case SEL_AUTH_NONCE: RETURN0_res(cred->nonce);
1016 case SEL_AUTH_URI: RETURN0_res(cred->uri);
1017 case SEL_AUTH_CNONCE: RETURN0_res(cred->cnonce);
1018 case SEL_AUTH_NC: RETURN0_res(cred->nc);
1019 case SEL_AUTH_RESPONSE: RETURN0_res(cred->response);
1020 case SEL_AUTH_OPAQUE: RETURN0_res(cred->opaque);
1021 case SEL_AUTH_ALG: RETURN0_res(cred->alg.alg_str);
1022 case SEL_AUTH_QOP: RETURN0_res(cred->qop.qop_str);
1024 BUG("Unsupported digest credentials parameter in select\n");
1029 int select_auth_username(str* res, select_t* s, struct sip_msg* msg)
1031 return select_auth_param(res, s, msg);
1034 int select_auth_username_comp(str* res, select_t* s, struct sip_msg* msg)
1036 return select_auth_param(res, s, msg);
1039 ABSTRACT_F(select_any_nameaddr)
1041 int select_nameaddr_name(str* res, select_t* s, struct sip_msg* msg)
1045 p=find_not_quoted(res, '<');
1047 DBG("select_nameaddr_name: no < found, whole string is uri\n");
1053 while (res->len && SP(res->s[res->len-1])) res->len--;
1057 int select_nameaddr_uri(str* res, select_t* s, struct sip_msg* msg)
1061 p=find_not_quoted(res, '<');
1063 DBG("select_nameaddr_uri: no < found, string up to first semicolon is uri\n");
1064 p = q_memchr(res->s, ';', res->len);
1066 res->len = p-res->s;
1070 res->len=res->len - (p-res->s) -1;
1073 p=find_not_quoted(res, '>');
1075 ERR("select_nameaddr_uri: no > found, invalid nameaddr value\n");
1083 int select_nameaddr_params(str* res, select_t* s, struct sip_msg* msg)
1087 p=find_not_quoted(res, '<');
1089 p=find_not_quoted(res, ';');
1091 res->len=res->len - (p-res->s) -1;
1093 p=find_not_quoted(res, '>');
1095 ERR("select_nameaddr_params: no > found, invalid nameaddr value\n");
1098 res->len=res->len - (p-res->s) -1;
1101 p=find_not_quoted(res, ';');
1105 res->len=res->len - (p-res->s) -1;
1108 if (s->param_offset[select_level+1]-s->param_offset[select_level]==1)
1109 return (res->len ? 0 : 1);
1111 return select_any_params(res, s, msg);
1114 ABSTRACT_F(select_src)
1115 ABSTRACT_F(select_dst)
1116 ABSTRACT_F(select_rcv)
1117 int select_ip_port(str* res, select_t* s, struct sip_msg* msg)
1119 str ip_str=STR_NULL, port_str=STR_NULL, proto_str=STR_NULL;
1123 if ((s->n != 2) || (s->params[1].type != SEL_PARAM_DIV)) return -1;
1124 param=s->params[1].v.i;
1126 if (param & SEL_SRC) {
1127 if (param & SEL_IP) {
1128 ip_str.s = ip_addr2a(&msg->rcv.src_ip);
1129 ip_str.len = strlen(ip_str.s);
1131 if (param & SEL_PORT) {
1132 port_str.s = int2str(msg->rcv.src_port, &port_str.len);
1134 } else if (param & SEL_DST) {
1135 if (param & SEL_IP) {
1136 ip_str.s = ip_addr2a(&msg->rcv.dst_ip);
1137 ip_str.len = strlen(ip_str.s);
1139 if (param & SEL_PORT) {
1140 port_str.s = int2str(msg->rcv.dst_port, &port_str.len);
1142 } else if (param & SEL_RCV) {
1143 if (param & SEL_IP) {
1144 ip_str = msg->rcv.bind_address->address_str;
1146 if (param & SEL_PORT) {
1147 port_str = msg->rcv.bind_address->port_no_str;
1149 if (param & SEL_PROTO) {
1150 switch (msg->rcv.proto) {
1157 proto_str.s = "udp";
1162 proto_str.s = "tcp";
1167 proto_str.s = "tls";
1172 proto_str.s = "sctp";
1177 ERR("BUG: select_ip_port: Unknown transport protocol\n");
1185 res->s = get_static_buffer(ip_str.len+port_str.len+proto_str.len+3);
1186 if (!res->s) return -1;
1189 if (param & SEL_PROTO) {
1190 memcpy(res->s, proto_str.s, proto_str.len);
1191 pos += proto_str.len;
1193 if (param & SEL_IP) {
1194 if (pos) res->s[pos++] = ':';
1195 memcpy(res->s+pos, ip_str.s, ip_str.len);
1198 if (param & SEL_PORT) {
1199 if (pos) res->s[pos++] = ':';
1200 memcpy(res->s+pos, port_str.s, port_str.len);
1201 pos += port_str.len;
1205 return (pos==0 ? 1 : 0);
1210 int select_expires(str* res, select_t* s, struct sip_msg* msg)
1212 if (!msg->expires && (parse_headers(msg, HDR_EXPIRES_F, 0) == -1)) {
1213 return -1; /* error */
1216 if (!msg->expires) {
1217 return 1; /* null */
1220 if (msg->expires->parsed == NULL && parse_expires(msg->expires) < 0) {
1224 RETURN0_res(((struct exp_body*)msg->expires->parsed)->text);
1227 #define SELECT_plain_header(_sel_name_,_fld_name_,_hdr_f_) \
1228 int select_##_sel_name_(str* res, select_t* s, struct sip_msg* msg) \
1230 if (!msg->_fld_name_ && (parse_headers(msg, _hdr_f_, 0) == -1)) { \
1233 if (!msg->_fld_name_) { \
1236 RETURN0_res(msg->_fld_name_->body); \
1239 SELECT_plain_header(call_id, callid, HDR_CALLID_F)
1240 SELECT_plain_header(max_forwards, maxforwards, HDR_MAXFORWARDS_F)
1241 SELECT_plain_header(content_type, content_type, HDR_CONTENTTYPE_F)
1242 SELECT_plain_header(content_length, content_length, HDR_CONTENTLENGTH_F)
1243 SELECT_plain_header(user_agent, user_agent, HDR_USERAGENT_F)
1244 SELECT_plain_header(subject, subject, HDR_SUBJECT_F)
1245 SELECT_plain_header(organization, organization, HDR_ORGANIZATION_F)
1246 SELECT_plain_header(priority, priority, HDR_PRIORITY_F)
1247 SELECT_plain_header(session_expires, session_expires, HDR_SESSIONEXPIRES_F)
1248 SELECT_plain_header(min_se, min_se, HDR_MIN_SE_F)
1249 SELECT_plain_header(sip_if_match, sipifmatch, HDR_SIPIFMATCH_F)
1250 SELECT_plain_header(date, date, HDR_DATE_F)
1251 SELECT_plain_header(identity, identity, HDR_IDENTITY_F)
1252 SELECT_plain_header(identity_info, identity_info, HDR_IDENTITY_INFO_F)
1254 int select_msg_request(str* res, select_t* s, struct sip_msg* msg)
1256 if (msg->first_line.type==SIP_REQUEST) {
1257 return select_msg(res, s, msg);
1263 int select_msg_response(str* res, select_t* s, struct sip_msg* msg)
1265 if (msg->first_line.type==SIP_REPLY) {
1266 return select_msg(res, s, msg);
1272 #define SELECT_first_line(_sel_name_,_type_,_fld_) \
1273 int select_msg_##_sel_name_(str* res, select_t* s, struct sip_msg* msg) {\
1274 if (msg->first_line.type==_type_) { \
1275 RETURN0_res(msg->first_line._fld_); \
1279 SELECT_first_line(request_method,SIP_REQUEST,u.request.method)
1280 SELECT_first_line(request_uri,SIP_REQUEST,u.request.uri)
1281 SELECT_first_line(request_version,SIP_REQUEST,u.request.version)
1282 SELECT_first_line(response_version,SIP_REPLY,u.reply.version)
1283 SELECT_first_line(response_status,SIP_REPLY,u.reply.status)
1284 SELECT_first_line(response_reason,SIP_REPLY,u.reply.reason)
1287 int select_version(str* res, select_t* s, struct sip_msg* msg)
1289 switch (msg->first_line.type) {
1291 RETURN0_res(msg->first_line.u.request.version)
1294 RETURN0_res(msg->first_line.u.reply.version)
1301 ABSTRACT_F(select_sys)
1303 int select_sys_pid(str* res, select_t* s, struct sip_msg* msg) {
1304 return uint_to_static_buffer(res, getpid());
1307 int select_sys_server_id(str* res, select_t* s, struct sip_msg* msg) {
1308 return int_to_static_buffer(res, server_id);
1311 int select_sys_unique(str* res, select_t* s, struct sip_msg* msg) {
1312 #define UNIQUE_ID_PID_LEN 4
1313 #define UNIQUE_ID_TIME_LEN 8
1314 #define UNIQUE_ID_FIX_LEN (UNIQUE_ID_PID_LEN+1+UNIQUE_ID_TIME_LEN+1)
1315 #define UNIQUE_ID_RAND_LEN 8
1316 static char uniq_id[UNIQUE_ID_FIX_LEN+UNIQUE_ID_RAND_LEN];
1317 static int uniq_for_pid = -1;
1320 if (uniq_for_pid != getpid()) {
1321 /* first call for this process */
1324 /* init gloabally uniq part */
1325 c = int2str_base_0pad(getpid(), &l, 16, UNIQUE_ID_PID_LEN);
1326 memcpy(uniq_id, c, UNIQUE_ID_PID_LEN);
1327 uniq_id[UNIQUE_ID_PID_LEN] = '-';
1328 c = int2str_base_0pad(time(NULL), &l, 16, UNIQUE_ID_TIME_LEN);
1329 memcpy(uniq_id+UNIQUE_ID_PID_LEN+1, c, UNIQUE_ID_TIME_LEN);
1330 uniq_id[UNIQUE_ID_PID_LEN+1+UNIQUE_ID_TIME_LEN] = '-';
1332 /* init random part */
1333 for (i = RAND_MAX, rb=0; i; rb++, i>>=1);
1334 for (i = UNIQUE_ID_FIX_LEN, cb = 0, x = 0; i < UNIQUE_ID_FIX_LEN+UNIQUE_ID_RAND_LEN; i++) {
1339 uniq_id[i] = fourbits2char[x & 0x0F];
1343 uniq_for_pid = getpid();
1345 for (i = UNIQUE_ID_FIX_LEN + UNIQUE_ID_RAND_LEN - 1; i >= UNIQUE_ID_FIX_LEN; i--) {
1346 switch (uniq_id[i]) {
1361 res->s = uniq_id; /* I think it's not worth copying at static buffer, I hope there is no real meaning of @sys.unique==@sys.unique */
1362 res->len = UNIQUE_ID_FIX_LEN+UNIQUE_ID_RAND_LEN;
1367 int select_sys_now(str* res, select_t* s, struct sip_msg* msg) {
1368 return uint_to_static_buffer(res, time(NULL));
1371 int select_sys_now_fmt(str* res, select_t* s, struct sip_msg* msg)
1378 switch (s->params[SEL_POS].v.i) {
1387 BUG("Unexpected parameter value 'now' \"%d\"\n", s->params[SEL_POS].v.i);
1390 if (s->n <= SEL_POS+1) {
1393 res->len = strlen(c);
1394 while (res->len && c[res->len-1] < ' ') res->len--; /* rtrim */
1395 res->s = get_static_buffer(res->len);
1396 if (!res->s) return -1;
1397 memcpy(res->s, c, res->len);
1401 c = s->params[SEL_POS+1].v.s.s[s->params[SEL_POS+1].v.s.len];
1402 s->params[SEL_POS+1].v.s.s[s->params[SEL_POS+1].v.s.len] = '\0';
1403 res->len = strftime(buff, sizeof(buff)-1, s->params[SEL_POS+1].v.s.s, tm);
1404 s->params[SEL_POS+1].v.s.s[s->params[SEL_POS+1].v.s.len] = c;
1405 res->s = get_static_buffer(res->len);
1406 if (!res->s) return -1;
1407 memcpy(res->s, buff, res->len);
1413 ABSTRACT_F(select_branch)
1415 int select_branch_count(str* res, select_t* s, struct sip_msg* msg) {
1416 return uint_to_static_buffer(res, nr_branches);
1419 int select_branch_uri(str* res, select_t* s, struct sip_msg* msg) {
1421 #define Q_PARAM ">;q="
1422 #define Q_PARAM_LEN (sizeof(Q_PARAM) - 1)
1427 if (s->n <= SEL_POS+1 && nr_branches > 1) { /* get all branches, if nr_branches==1 then use faster algorithm */
1431 init_branch_iterator();
1433 while ((c = next_branch(&l, &q, &dst_uri.s, &dst_uri.len, 0))) {
1435 if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
1439 if (s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI) ) {
1442 if (q != Q_UNSPECIFIED && (s->params[SEL_POS].v.i & SEL_BRANCH_Q)) {
1444 if (s->params[SEL_POS].v.i & SEL_BRANCH_URI) {
1445 len += 1 + Q_PARAM_LEN;
1451 res->s = get_static_buffer(len);
1452 if (!res->s) return -1;
1454 init_branch_iterator();
1457 while ((c = next_branch(&l, &q, &dst_uri.s, &dst_uri.len, 0))) {
1458 if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
1463 res->s[res->len] = ',';
1466 if ((s->params[SEL_POS].v.i & SEL_BRANCH_Q) == 0) {
1469 if ((s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI)) && q != Q_UNSPECIFIED) {
1470 res->s[res->len] = '<';
1472 memcpy(res->s+res->len, c, l);
1474 memcpy(res->s+res->len, Q_PARAM, Q_PARAM_LEN);
1475 res->len += Q_PARAM_LEN;
1476 c = q2str(q, &l2); l = l2;
1477 memcpy(res->s+res->len, c, l);
1480 else if (s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI)) {
1481 memcpy(res->s+res->len, c, l);
1484 else if (q != Q_UNSPECIFIED) {
1485 c = q2str(q, &l2); l = l2;
1486 memcpy(res->s+res->len, c, l);
1495 n = s->params[SEL_POS+1].v.i;
1496 if (n < 0 || n >= nr_branches)
1498 init_branch_iterator();
1499 for (; (c = next_branch(&l, &q, &dst_uri.s, &dst_uri.len, 0)) && n; n--);
1502 if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
1506 if ((s->params[SEL_POS].v.i & SEL_BRANCH_Q) == 0) {
1509 if ((s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI)) && q != Q_UNSPECIFIED) {
1511 res->s = get_static_buffer(l + 1 + Q_PARAM_LEN + len_q(q));
1512 if (!res->s) return -1;
1515 memcpy(res->s+res->len, c, l);
1517 memcpy(res->s+res->len, Q_PARAM, Q_PARAM_LEN);
1518 res->len += Q_PARAM_LEN;
1519 c = q2str(q, &l2); l = l2;
1520 memcpy(res->s+res->len, c, l);
1523 else if (s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI)) {
1524 res->s = c; /* not necessary to copy to static buffer */
1527 else if (q != Q_UNSPECIFIED) {
1530 res->s = get_static_buffer(res->len);
1531 if (!res->s) return -1;
1532 memcpy(res->s, c, res->len);
1542 int select_branch_uriq(str* res, select_t* s, struct sip_msg* msg) {
1543 return select_branch_uri(res, s, msg);
1546 int select_branch_q(str* res, select_t* s, struct sip_msg* msg) {
1547 return select_branch_uri(res, s, msg);
1550 int select_branch_dst_uri(str* res, select_t* s, struct sip_msg* msg) {
1551 return select_branch_uri(res, s, msg);