123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743 |
- /*
- * $Id$
- *
- * Copyright (C) 2005-2006 iptelorg GmbH
- *
- * This file is part of ser, a free SIP server.
- *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- * [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * History:
- * --------
- * 2005-12-19 select framework, basic core functions (mma)
- * 2006-01-19 multiple nested calls, IS_ALIAS -> NESTED flag renamed (mma)
- * 2006-02-17 fixup call for select_anyhdr (mma)
- * 2007-01-26 date, identity, identity_info support added (gergo)
- */
- /*!
- * \file
- * \brief SIP-router core ::
- * \ingroup core
- * Module: \ref core
- */
- #include <stdlib.h>
- #include "select.h"
- #include "select_core.h"
- #include "select_buf.h"
- #include "dprint.h"
- #include "trim.h"
- #include "ut.h"
- #include "globals.h"
- #include "parser/parser_f.h"
- #include "parser/hf.h"
- #include "parser/parse_from.h"
- #include "parser/parse_to.h"
- #include "parser/contact/parse_contact.h"
- #include "parser/contact/contact.h"
- #include "parser/parse_via.h"
- #include "parser/parse_uri.h"
- #include "parser/parse_event.h"
- #include "parser/parse_rr.h"
- #include "parser/digest/digest.h"
- #include "mem/mem.h"
- #include "parser/parse_hname2.h"
- #include "ip_addr.h"
- #include "parser/parse_expires.h"
- #include "parser/parse_refer_to.h"
- #include "parser/parse_rpid.h"
- #include "parser/parse_content.h"
- #include "parser/parse_body.h"
- #include "dset.h"
- #include "sr_module.h"
- #include "resolve.h"
- #include "forward.h"
- #define RETURN0_res(x) {*res=(x);return 0;}
- #define TRIM_RET0_res(x) {*res=(x);trim(res);return 0;}
- #define TEST_RET_res_body(x) if (x){*res=(x)->body;return 0;}else return 1;
- #define TEST_RET_res_value(x) if (x){*res=(x)->value;return 0;}else return 1;
- int select_ruri(str* res, select_t* s, struct sip_msg* msg)
- {
- /* Parse the RURI even if it is not needed right now
- * because the nested select calls can access the
- * parsed URI in this case.
- * Go ahead even if the parsing fails, so the
- * value of the broken RURI can be accessed at least.
- * Subsequent select calls will fail when they try to parse
- * the URI anyway. (Miklos)
- */
- parse_sip_msg_uri(msg);
- if (msg->parsed_uri_ok)
- select_uri_p = &msg->parsed_uri;
- if (msg->first_line.type==SIP_REQUEST) {
- if(msg->new_uri.s) {
- RETURN0_res(msg->new_uri);
- }
- else {
- RETURN0_res(msg->first_line.u.request.uri);
- }
- }
- return -1;
- }
- int select_dst_uri(str* res, select_t* s, struct sip_msg* msg)
- {
- if (msg->first_line.type!=SIP_REQUEST)
- return -1;
- RETURN0_res(msg->dst_uri);
- }
- int select_next_hop(str* res, select_t* s, struct sip_msg* msg)
- {
- if (msg->first_line.type==SIP_REQUEST) {
- if(msg->dst_uri.s) {
- RETURN0_res(msg->dst_uri);
- }
- else if(msg->new_uri.s) {
- if (msg->parsed_uri_ok)
- select_uri_p = &msg->parsed_uri;
- RETURN0_res(msg->new_uri);
- }
- else {
- if (msg->parsed_uri_ok)
- select_uri_p = &msg->parsed_uri;
- else if (msg->parsed_orig_ruri_ok)
- select_uri_p = &msg->parsed_orig_ruri;
- RETURN0_res(msg->first_line.u.request.uri);
- }
- }
- return -1;
- }
- int select_next_hop_src_ip(str* res, select_t* s, struct sip_msg* msg) {
- struct socket_info* socket_info;
- union sockaddr_union to;
- char proto;
- struct sip_uri *u, next_hop;
- str *dst_host;
- if (msg->first_line.type!=SIP_REQUEST)
- return -1;
- if (msg->force_send_socket) {
- *res = msg->force_send_socket->address_str;
- return 0;
- }
- if (msg->dst_uri.len) {
- if (parse_uri(msg->dst_uri.s, msg->dst_uri.len, &next_hop) < 0)
- return -1;
- u = &next_hop;
- }
- else {
- if (parse_sip_msg_uri(msg) < 0)
- return -1;
- u = &msg->parsed_uri;
- }
- #ifdef USE_TLS
- if (u->type==SIPS_URI_T)
- proto = PROTO_TLS;
- else
- #endif
- proto = u->proto;
- #ifdef HONOR_MADDR
- if (u->maddr_val.s && u->maddr_val.len)
- dst_host = &u->maddr_val;
- else
- #endif
- dst_host = &u->host;
- if (sip_hostport2su(&to, dst_host, u->port_no, &proto) < 0)
- return -1;
- socket_info = get_send_socket(msg, &to, proto);
- if (!socket_info)
- return -1;
- *res = socket_info->address_str;
- return 0;
- }
- #define SELECT_uri_header(_name_) \
- int select_##_name_(str* res, select_t* s, struct sip_msg* msg) \
- { \
- if (parse_##_name_##_header(msg)<0) \
- return -1; \
- RETURN0_res(msg->_name_->body); \
- } \
- \
- int select_##_name_##_uri(str* res, select_t* s, struct sip_msg* msg) \
- { \
- if (parse_##_name_##_header(msg)<0) \
- return -1; \
- RETURN0_res(get_##_name_(msg)->uri); \
- } \
- \
- int select_##_name_##_tag(str* res, select_t* s, struct sip_msg* msg) \
- { \
- if (parse_##_name_##_header(msg)<0) \
- return -1; \
- RETURN0_res(get_##_name_(msg)->tag_value); \
- } \
- \
- int select_##_name_##_name(str* res, select_t* s, struct sip_msg* msg) \
- { \
- if (parse_##_name_##_header(msg)<0) \
- return -1; \
- RETURN0_res(get_##_name_(msg)->display); \
- } \
- \
- int select_##_name_##_params(str* res, select_t* s, struct sip_msg* msg) \
- { \
- struct to_param* p; \
- if (parse_##_name_##_header(msg)<0) \
- return -1; \
- \
- p = get_##_name_(msg)->param_lst; \
- while (p) { \
- if ((p->name.len==s->params[s->n-1].v.s.len) \
- && !strncasecmp(p->name.s, s->params[s->n-1].v.s.s,p->name.len)) { \
- RETURN0_res(p->value); \
- } \
- p = p->next; \
- } \
- return 1; \
- }
- SELECT_uri_header(to)
- SELECT_uri_header(from)
- SELECT_uri_header(refer_to)
- SELECT_uri_header(rpid)
- int parse_contact_header( struct sip_msg *msg)
- {
- if ( !msg->contact && ( parse_headers(msg,HDR_CONTACT_F,0)==-1 || !msg->contact)) {
- DBG("bad msg or missing CONTACT header\n");
- return -1;
- }
- if (msg->contact->parsed)
- return 0;
- return parse_contact(msg->contact);
- }
- #define get_contact(msg) ((contact_body_t*)(msg->contact->parsed))
- int select_contact(str* res, select_t* s, struct sip_msg* msg)
- {
- if (parse_contact_header(msg)<0)
- return -1;
- RETURN0_res(msg->contact->body);
- }
- int select_contact_uri(str* res, select_t* s, struct sip_msg* msg)
- {
- contact_t* c;
- if (parse_contact_header(msg)<0)
- return -1;
-
- c = get_contact(msg)->contacts;
- if (!c)
- return 1;
- RETURN0_res(c->uri);
- }
- int select_contact_name(str* res, select_t* s, struct sip_msg* msg)
- {
- contact_t* c;
- if (parse_contact_header(msg)<0)
- return -1;
-
- c = get_contact(msg)->contacts;
- if (!c)
- return 1;
- RETURN0_res(c->name);
- }
- int select_contact_params_spec(str* res, select_t* s, struct sip_msg* msg)
- {
- contact_t* c;
-
- if (s->params[s->n-1].type != SEL_PARAM_DIV) {
- BUG("Last parameter should have type DIV (converted)\n");
- return -1;
- }
-
- if (parse_contact_header(msg)<0)
- return -1;
-
- c = get_contact(msg)->contacts;
- if (!c)
- return 1;
-
- switch (s->params[s->n-1].v.i) {
- case SEL_PARAM_Q:
- TEST_RET_res_body(c->q);
- case SEL_PARAM_EXPIRES:
- TEST_RET_res_body(c->expires);
- case SEL_PARAM_METHODS:
- TEST_RET_res_body(c->methods);
- case SEL_PARAM_RECEIVED:
- TEST_RET_res_body(c->received);
- case SEL_PARAM_INSTANCE:
- TEST_RET_res_body(c->instance);
- default:
- BUG("Unexpected parameter value \"%d\"\n", s->params[s->n-1].v.i);
- return -1;
- }
- return -1;
- }
- int select_contact_params(str* res, select_t* s, struct sip_msg* msg)
- {
- contact_t* c;
- param_t* p;
- if (parse_contact_header(msg)<0)
- return -1;
-
- c = get_contact(msg)->contacts;
- if (!c)
- return 1;
- p = c->params;
- while (p) {
- if ((p->name.len==s->params[s->n-1].v.s.len)
- && !strncasecmp(p->name.s, s->params[s->n-1].v.s.s,p->name.len)) {
- RETURN0_res(p->body)
- }
- p = p->next;
- }
- return 1;
- }
- int select_via(str* res, select_t* s, struct sip_msg* msg)
- {
- struct via_body *p = NULL;
-
- if ((s->n == 1) || (s->params[1].type == SEL_PARAM_STR)) {
- if (parse_via_header(msg, 1, &p)<0) return -1;
- } else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
- if (!p) return -1;
- res->s=p->name.s;
- res->len=p->bsize;
- trim(res);
- return 0;
- }
- int select_via_name(str* res, select_t* s, struct sip_msg* msg)
- {
- struct via_body *p = NULL;
-
- // it's not neccessary to test if (s->n > 1)
- if (s->params[1].type == SEL_PARAM_STR) {
- if(parse_via_header(msg, 1, &p)<0) return -1;
- } else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
- if (!p) return -1;
- RETURN0_res(p->name);
- }
- int select_via_version(str* res, select_t* s, struct sip_msg* msg)
- {
- struct via_body *p = NULL;
-
- // it's not neccessary to test if (s->n > 1)
- if (s->params[1].type == SEL_PARAM_STR) {
- if (parse_via_header(msg, 1, &p)<0) return -1;
- } else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
- if (!p) return -1;
- RETURN0_res(p->version);
- }
- int select_via_transport(str* res, select_t* s, struct sip_msg* msg)
- {
- struct via_body *p = NULL;
-
- // it's not neccessary to test if (s->n > 1)
- if (s->params[1].type == SEL_PARAM_STR) {
- if(parse_via_header(msg, 1, &p)<0) return -1;
- } else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
- if (!p) return -1;
- RETURN0_res(p->transport);
- }
- int select_via_host(str* res, select_t* s, struct sip_msg* msg)
- {
- struct via_body *p = NULL;
-
- // it's not neccessary to test if (s->n > 1)
- if (s->params[1].type == SEL_PARAM_STR) {
- if (parse_via_header(msg, 1, &p)<0) return -1;
- } else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
- if (!p) return -1;
- RETURN0_res(p->host);
- }
- int select_via_port(str* res, select_t* s, struct sip_msg* msg)
- {
- struct via_body *p = NULL;
-
- // it's not neccessary to test if (s->n > 1)
- if (s->params[1].type == SEL_PARAM_STR) {
- if (parse_via_header(msg, 1, &p)<0) return -1;
- } else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
- if (!p) return -1;
- RETURN0_res(p->port_str);
- }
- int select_via_comment(str* res, select_t* s, struct sip_msg* msg)
- {
- struct via_body *p = NULL;
-
- // it's not neccessary to test if (s->n > 1)
- if (s->params[1].type == SEL_PARAM_STR) {
- if(parse_via_header(msg, 1, &p)<0) return -1;
- } else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
- if (!p) return -1;
- RETURN0_res(p->comment);
- }
- int select_via_params(str* res, select_t* s, struct sip_msg* msg)
- {
- struct via_body *p = NULL;
- struct via_param *q;
- // it's not neccessary to test if (s->n > 1)
- if (s->params[1].type == SEL_PARAM_STR) {
- if (parse_via_header(msg, 1, &p)<0) return -1;
- } else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
- if (!p) return -1;
-
- for (q = p->param_lst;q;q=q->next) {
- if ((q->name.len==s->params[s->n-1].v.s.len)
- && !strncasecmp(q->name.s, s->params[s->n-1].v.s.s,q->name.len)) {
- RETURN0_res(q->value);
- }
- }
- return 1;
- }
- int select_via_params_spec(str* res, select_t* s, struct sip_msg* msg)
- {
- struct via_body *p = NULL;
- if (s->params[s->n-1].type != SEL_PARAM_DIV) {
- BUG("Last parameter should have type DIV (converted)\n");
- return -1;
- }
-
- // it's not neccessary to test if (s->n > 1)
- if (s->params[1].type != SEL_PARAM_INT) {
- if(parse_via_header(msg, 1, &p)<0) return -1;
- } else if (parse_via_header(msg, s->params[1].v.i, &p)<0) return -1;
- if (!p) return -1;
-
- switch (s->params[s->n-1].v.i) {
- case SEL_PARAM_BRANCH:
- TEST_RET_res_value(p->branch);
- case SEL_PARAM_RECEIVED:
- TEST_RET_res_value(p->received);
- case SEL_PARAM_RPORT:
- TEST_RET_res_value(p->rport);
- case SEL_PARAM_I:
- TEST_RET_res_value(p->i);
- case SEL_PARAM_ALIAS:
- TEST_RET_res_value(p->alias);
- default:
- BUG("Unexpected parameter value \"%d\"\n", s->params[s->n-1].v.i);
- return -1;
- }
- return -1;
- }
- int select_msg(str* res, select_t* s, struct sip_msg* msg)
- {
- res->s = msg->buf;
- res->len = msg->len;
- return 0;
- }
- int select_msg_first_line(str* res, select_t* s, struct sip_msg* msg)
- {
- res->s=SIP_MSG_START(msg);
- res->len=msg->first_line.len;
- trim_trailing(res);
- return 0;
- }
- int select_msg_type(str* res, select_t* s, struct sip_msg* msg) {
- return uint_to_static_buffer(res, msg->first_line.type);
- }
- int select_msg_len(str* res, select_t* s, struct sip_msg* msg) {
- return uint_to_static_buffer(res, msg->len);
- }
- int select_msg_id(str* res, select_t* s, struct sip_msg* msg) {
- return uint_to_static_buffer(res, msg->id);
- }
- int select_msg_id_hex(str* res, select_t* s, struct sip_msg* msg) {
- return uint_to_static_buffer_ex(res, msg->id, 16, 8);
- }
- int select_msg_flags(str* res, select_t* s, struct sip_msg* msg) {
- return uint_to_static_buffer(res, msg->flags);
- }
- int select_msg_body(str* res, select_t* s, struct sip_msg* msg)
- {
- res->s = get_body(msg);
- res->len = msg->buf+msg->len - res->s;
- return 0;
- }
- /* returns the sdp part of the message body */
- int select_msg_body_sdp(str* res, select_t* sel, struct sip_msg* msg)
- {
- /* try to get the body part with application/sdp */
- if ((res->s = get_body_part(msg,
- TYPE_APPLICATION, SUBTYPE_SDP,
- &res->len))
- )
- return 0;
- else
- return -1;
- }
- /* returns the value of the requested SDP line */
- int select_sdp_line(str* res, select_t* sel, struct sip_msg* msg)
- {
- int len;
- char *buf;
- char *buf_end, *line_end;
- char line;
- if (msg == NULL) {
- if (res!=NULL) return -1;
- if (sel->n < 5) return -1;
- if (sel->params[4].type != SEL_PARAM_STR) {
- ERR("wrong parameter type");
- return -1;
- }
- if ((sel->params[4].v.s.len < 1) ||
- (sel->params[4].v.s.len > 2) ||
- ((sel->params[4].v.s.len == 2) && (sel->params[4].v.s.s[1] != '='))
- ) {
- ERR("wrong sdp line format: %.*s\n",
- sel->params[4].v.s.len, sel->params[4].v.s.s);
- return -1;
- }
- return 0;
- }
- /* try to get the body part with application/sdp */
- if (!(buf = get_body_part(msg,
- TYPE_APPLICATION, SUBTYPE_SDP,
- &len))
- )
- return -1;
- buf_end = buf + len;
- line = *(sel->params[4].v.s.s);
- while (buf < buf_end) {
- if (*buf == line) {
- /* the requested SDP line is found, return its value */
- buf++;
- if ((buf >= buf_end) || (*buf != '=')) {
- ERR("wrong SDP line format\n");
- return -1;
- }
- buf++;
- line_end = buf;
- while ((line_end < buf_end) && (*line_end != '\n'))
- line_end++;
- if (line_end >= buf_end) {
- ERR("wrong SDP line format\n");
- return -1;
- }
- line_end--;
- if (*line_end == '\r') line_end--;
- if (line_end < buf) {
- ERR("wrong SDP line format\n");
- return -1;
- }
- res->s = buf;
- res->len = line_end - buf + 1;
- return 0;
- }
- while ((buf < buf_end) && (*buf != '\n'))
- buf++;
- buf++;
- }
- return -1;
- }
- int select_msg_header(str* res, select_t* s, struct sip_msg* msg)
- {
- /* get all headers */
- char *c;
- res->s = SIP_MSG_START(msg) + msg->first_line.len;
- c = get_body(msg);
- res->len = c - res->s;
- return 0;
- }
- int select_anyheader(str* res, select_t* s, struct sip_msg* msg)
- {
- struct hdr_field *hf, *hf0;
- int hi;
- char c;
- struct hdr_field hdr;
-
- if(msg==NULL) {
- if (res!=NULL) return -1;
- /* "fixup" call, res & msg are NULL */
- if (s->n <3) return -1;
- if (s->params[2].type==SEL_PARAM_STR) {
- /* replace _ with - (for P-xxx headers) */
- for (hi=s->params[2].v.s.len-1; hi>0; hi--)
- if (s->params[2].v.s.s[hi]=='_')
- s->params[2].v.s.s[hi]='-';
- /* if header name is parseable, parse it and set SEL_PARAM_DIV */
- c=s->params[2].v.s.s[s->params[2].v.s.len];
- s->params[2].v.s.s[s->params[2].v.s.len]=':';
- 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),
- &hdr)==0) {
- ERR("select_anyhdr:fixup_call:parse error\n");
- return -1;
- }
- s->params[2].v.s.s[s->params[2].v.s.len]=c;
-
- if (hdr.type!=HDR_OTHER_T && hdr.type!=HDR_ERROR_T) {
- /* pkg_free(s->params[1].v.s.s); */
- /* don't free it (the mem can leak only once at startup)
- * the parsed string can live inside larger string block
- * e.g. when xlog's select is parsed
- */
- s->params[2].type = SEL_PARAM_DIV;
- s->params[2].v.i = hdr.type;
- }
- }
- return 1;
- }
- hf0 = NULL;
- /* extract header index if present */
- if (s->param_offset[select_level+1] == 4) {
- if (s->params[3].type == SEL_PARAM_INT) {
- hi = s->params[3].v.i;
- } else {
- hi = -1;
- }
- } else {
- hi = 1;
- }
- /* we need to be sure we have parsed all headers */
- if (!msg->eoh && (parse_headers(msg,HDR_EOH_F,0)==-1 || !msg->eoh)) {
- ERR("bad msg while parsing to EOH \n");
- return -1;
- }
- for (hf=msg->headers; hf; hf=hf->next) {
- if(s->params[2].type==SEL_PARAM_DIV) {
- if (s->params[2].v.i!=hf->type) continue;
- } else if(s->params[2].type==SEL_PARAM_STR) {
- if (s->params[2].v.s.len!=hf->name.len) continue;
- if (strncasecmp(s->params[2].v.s.s, hf->name.s, hf->name.len)!=0) continue;
- }
- hf0 = hf;
- hi--;
- if (!hi) break;
- }
- if(hf0==NULL || hi>0)
- return 1;
- res->s = hf0->body.s;
- res->len = hf0->body.len;
- trim(res);
- return 0;
- }
- //ABSTRACT_F(select_anyheader_params)
- // Instead of ABSTRACT_F(select_anyheader_params)
- // use function which uses select_core_table
- // to avoid gcc warning about not used
-
- int select_anyheader_params(str* res, select_t* s, struct sip_msg* msg)
- {
- if (select_core_table.next)
- return -1;
- else
- return -1;
- }
- ABSTRACT_F(select_any_uri)
- static struct sip_uri uri;
- int select_uri_type(str* res, select_t* s, struct sip_msg* msg)
- {
- if (select_uri_p == NULL) {
- trim(res);
- if (parse_uri(res->s, res->len, &uri)<0)
- return -1;
- select_uri_p = &uri;
- }
- if (select_uri_p->type==ERROR_URI_T)
- return -1;
- uri_type_to_str(select_uri_p->type, res);
- return 0;
- }
- int select_uri_user(str* res, select_t* s, struct sip_msg* msg)
- {
- if (select_uri_p == NULL) {
- if (parse_uri(res->s, res->len, &uri)<0)
- return -1;
- select_uri_p = &uri;
- }
- if (select_uri_p->flags & URI_USER_NORMALIZE) {
- if (!(res->s=get_static_buffer(select_uri_p->user.len)))
- return -1;
- if ((res->len=normalize_tel_user(res->s, (&select_uri_p->user)))==0)
- return 1;
- return 0;
- }
- RETURN0_res(select_uri_p->user);
- }
- /* search for a parameter with "name"
- * Return value:
- * 0: not found
- * 1: found
- * -1: error
- *
- * val is set to the value of the parameter.
- */
- static inline int search_param(str params, char *name, int name_len,
- str *val)
- {
- param_hooks_t h;
- param_t *p, *list;
- if (params.s == NULL)
- return 0;
- if (parse_params(¶ms, CLASS_ANY, &h, &list) < 0)
- return -1;
- for (p = list; p; p=p->next) {
- if ((p->name.len == name_len)
- && (strncasecmp(p->name.s, name, name_len) == 0)
- ) {
- *val=p->body;
- free_params(list);
- return 1;
- }
- }
- free_params(list);
- return 0;
- }
- /* Return the value of the "rn" parameter if exists, otherwise the user name.
- * The user name is normalized if needed, i.e. visual separators are removed,
- * the "rn" param is always normalized. */
- int select_uri_rn_user(str* res, select_t* s, struct sip_msg* msg)
- {
- int ret;
- str val;
- if (select_uri_p == NULL) {
- if (parse_uri(res->s, res->len, &uri)<0)
- return -1;
- select_uri_p = &uri;
- }
- /* search for the "rn" parameter */
- if ((ret = search_param(select_uri_p->params, "rn", 2, &val)) != 0)
- goto done;
- if (select_uri_p->sip_params.s != select_uri_p->params.s) {
- /* check also the original sip: URI parameters */
- if ((ret = search_param(select_uri_p->sip_params, "rn", 2, &val)) != 0)
- goto done;
- }
- if ((select_uri_p->flags & URI_USER_NORMALIZE) == 0)
- RETURN0_res(select_uri_p->user);
- /* else normalize the user name */
- val = select_uri_p->user;
- done:
- if (ret < 0)
- return -1; /* error */
- if (!(res->s=get_static_buffer(val.len)))
- return -1;
- if ((res->len=normalize_tel_user(res->s, &val))==0)
- return 1;
- return 0;
- }
- int select_uri_pwd(str* res, select_t* s, struct sip_msg* msg)
- {
- if (select_uri_p == NULL) {
- if (parse_uri(res->s, res->len, &uri)<0)
- return -1;
- select_uri_p = &uri;
- }
- RETURN0_res(select_uri_p->passwd);
- }
- int select_uri_host(str* res, select_t* s, struct sip_msg* msg)
- {
- if (select_uri_p == NULL) {
- if (parse_uri(res->s, res->len, &uri)<0)
- return -1;
- select_uri_p = &uri;
- }
- RETURN0_res(select_uri_p->host);
- }
- int select_uri_port(str* res, select_t* s, struct sip_msg* msg)
- {
- if (select_uri_p == NULL) {
- if (parse_uri(res->s, res->len, &uri)<0)
- return -1;
- select_uri_p = &uri;
- }
- RETURN0_res(select_uri_p->port);
- }
- int select_uri_hostport(str* res, select_t* s, struct sip_msg* msg)
- {
- char* p;
- int size;
-
- if (select_uri_p == NULL) {
- if (parse_uri(res->s, res->len, &uri)<0)
- return -1;
- select_uri_p = &uri;
- }
- if (!select_uri_p->host.len)
- return -1;
-
- if (select_uri_p->port.len) {
- res->s=select_uri_p->host.s;
- res->len=select_uri_p->host.len+select_uri_p->port.len+1;
- return 0;
- }
-
- size=select_uri_p->host.len+5;
- if (!(p = get_static_buffer(size)))
- return -1;
-
- strncpy(p, select_uri_p->host.s, select_uri_p->host.len);
- switch (select_uri_p->type) {
- case SIPS_URI_T:
- case TELS_URI_T:
- strncpy(p+select_uri_p->host.len, ":5061", 5);
- break;
- case SIP_URI_T:
- case TEL_URI_T:
- case URN_URI_T:
- strncpy(p+select_uri_p->host.len, ":5060", 5);
- break;
- case ERROR_URI_T:
- return -1;
- }
- res->s = p;
- res->len = size;
- return 0;
- }
- int select_uri_proto(str* res, select_t* s, struct sip_msg* msg)
- {
- if (select_uri_p == NULL) {
- if (parse_uri(res->s, res->len, &uri)<0)
- return -1;
- select_uri_p = &uri;
- }
- if (select_uri_p->proto != PROTO_NONE) {
- proto_type_to_str(select_uri_p->proto, res);
- } else {
- switch (select_uri_p->type) {
- case SIPS_URI_T:
- case TELS_URI_T:
- proto_type_to_str(PROTO_TLS, res);
- break;
- case SIP_URI_T:
- case TEL_URI_T:
- case URN_URI_T:
- proto_type_to_str(PROTO_UDP, res);
- break;
- case ERROR_URI_T:
- return -1;
- }
- }
- return 0;
- }
- int select_uri_params(str* res, select_t* s, struct sip_msg* msg)
- {
- int ret;
- if (!msg || !res) {
- return select_any_params(res, s, msg);
- }
- if (select_uri_p == NULL) {
- if (parse_uri(res->s, res->len, &uri)<0)
- return -1;
- select_uri_p = &uri;
- }
-
- if (s->param_offset[select_level+1]-s->param_offset[select_level]==1)
- RETURN0_res(select_uri_p->params);
- *res=select_uri_p->params;
- ret = select_any_params(res, s, msg);
- if ((ret < 0)
- && (select_uri_p->sip_params.s != NULL)
- && (select_uri_p->sip_params.s != select_uri_p->params.s)
- ) {
- /* Search also in the original sip: uri parameters. */
- *res = select_uri_p->sip_params;
- ret = select_any_params(res, s, msg);
- }
- return ret;
- }
- int select_any_params(str* res, select_t* s, struct sip_msg* msg)
- {
- str* wanted;
- int i;
- if (!msg || !res) {
- if (s->param_offset[select_level+1]-s->param_offset[select_level]==1) return 0;
- if (s->params[s->param_offset[select_level]+1].type!=SEL_PARAM_STR) return -1;
- wanted=&s->params[s->param_offset[select_level]+1].v.s;
- for (i=0; i<wanted->len; i++)
- if (wanted->s[i]=='_')
- wanted->s[i]='-';
- return 0;
- }
-
- if (s->params[s->param_offset[select_level]+1].type!=SEL_PARAM_STR) return -1;
- wanted=&s->params[s->param_offset[select_level]+1].v.s;
-
- if (!res->len) return -1;
- if (search_param(*res, wanted->s, wanted->len, res) <= 0) {
- DBG("SELECT ...uri.params.%s NOT FOUND !\n", wanted->s);
- return -1;
- } else {
- return (res->len) ? 0 : 1;
- }
- }
- int select_event(str* res, select_t* s, struct sip_msg* msg)
- {
- if (!msg->event && parse_headers(msg, HDR_EVENT_F, 0) == -1) {
- ERR("Error while searching Event header field\n");
- return -1;
- }
- if (!msg->event) {
- DBG("Event header field not found\n");
- return -1;
- }
- if (parse_event(msg->event) < 0) {
- ERR("Error while parsing Event header field\n");
- return -1;
- }
- *res = ((event_t*)msg->event->parsed)->name;
- return 0;
- }
- static int parse_rr_header(struct sip_msg *msg)
- {
- if ( !msg->record_route && ( parse_headers(msg,HDR_RECORDROUTE_F,0) == -1)) {
- ERR("bad msg or missing Record-Route header\n");
- return -1;
- }
- if (!msg->record_route) {
- DBG("No Record-Route header field found\n");
- return -1;
- }
- return parse_rr(msg->record_route);
- }
- #define get_rr(msg) ((rr_t*)(msg->record_route->parsed))
- int select_rr(str* res, select_t* s, struct sip_msg* msg)
- {
- if (parse_rr_header(msg)<0)
- return -1;
- RETURN0_res(msg->record_route->body);
- }
- int select_rr_uri(str* res, select_t* s, struct sip_msg* msg)
- {
- rr_t* r;
- if (parse_rr_header(msg)<0)
- return -1;
-
- r = get_rr(msg);
- if (!r)
- return 1;
- RETURN0_res(r->nameaddr.uri);
- }
- int select_rr_name(str* res, select_t* s, struct sip_msg* msg)
- {
- rr_t* r;
- if (parse_rr_header(msg)<0)
- return -1;
-
- r = get_rr(msg);
- if (!r)
- return 1;
- RETURN0_res(r->nameaddr.name);
- }
- int select_rr_params(str* res, select_t* s, struct sip_msg* msg)
- {
- rr_t* r;
- param_t* p;
- if (parse_rr_header(msg)<0)
- return -1;
-
- r = get_rr(msg);
- if (!r)
- return 1;
- p = r->params;
- while (p) {
- if ((p->name.len==s->params[s->n-1].v.s.len)
- && !strncasecmp(p->name.s, s->params[s->n-1].v.s.s,p->name.len)) {
- RETURN0_res(p->body)
- }
- p = p->next;
- }
- return 0;
- }
- static inline struct cseq_body* sel_parse_cseq(struct sip_msg* msg)
- {
- if (!msg->cseq && (parse_headers(msg, HDR_CSEQ_F, 0) == -1)) {
- ERR("Unable to parse CSeq header\n");
- return 0;
- }
- if (!msg->cseq) {
- DBG("No CSeqheader field found\n");
- return 0;
- }
- return get_cseq(msg);
- }
- int select_cseq(str* res, select_t* s, struct sip_msg* msg)
- {
- struct cseq_body* cs;
- cs = sel_parse_cseq(msg);
- if (!cs) return -1;
- *res = msg->cseq->body;
- return 0;
- }
- int select_cseq_num(str* res, select_t* s, struct sip_msg* msg)
- {
- struct cseq_body* cs;
- cs = sel_parse_cseq(msg);
- if (!cs) return -1;
- *res = cs->number;
- return 0;
- }
- int select_cseq_method(str* res, select_t* s, struct sip_msg* msg)
- {
- struct cseq_body* cs;
- cs = sel_parse_cseq(msg);
- if (!cs) return -1;
- *res = cs->method;
- return 0;
- }
- int get_credentials(struct sip_msg* msg, select_t* s, struct hdr_field** hdr)
- {
- int ret;
- str realm;
- hdr_types_t hdr_type;
- *hdr = NULL;
- if (!msg) {
- /* fix-up call check domain for fparam conversion */
- void * ptr;
- char chr;
- ptr=(void *)(s->params[1].v.s.s);
- chr=s->params[1].v.s.s[s->params[1].v.s.len];
- s->params[1].v.s.s[s->params[1].v.s.len]=0;
- ret=fixup_var_str_12(&ptr,0);
- if (ret>=0) {
- s->params[1].v.s.s[s->params[1].v.s.len]=chr;
- s->params[1].v.p=ptr;
- s->params[1].type=SEL_PARAM_PTR;
- }
- return ret;
- }
-
- /* Try to find credentials with corresponding realm
- * in the message, parse them and return pointer to
- * parsed structure
- */
- if (s->params[1].type==SEL_PARAM_PTR) {
- if (get_str_fparam(&realm, msg, s->params[1].v.p)<0)
- return -1;
- } else {
- realm = s->params[1].v.s;
- }
- switch (s->params[0].v.i) {
- case SEL_AUTH_WWW:
- hdr_type = HDR_AUTHORIZATION_T;
- break;
- case SEL_AUTH_PROXY:
- hdr_type = HDR_PROXYAUTH_T;
- break;
- default:
- BUG("Unexpected parameter value \"%d\"\n", s->params[0].v.i);
- return -1;
- }
- ret = find_credentials(msg, &realm, hdr_type, hdr);
- return ret;
- }
- int select_auth(str* res, select_t* s, struct sip_msg* msg)
- {
- int ret;
- struct hdr_field* hdr;
- if (s->n != 2 && s->params[1].type != SEL_PARAM_STR
- && s->params[1].type != SEL_PARAM_PTR) return -1;
- if (s->params[0].type != SEL_PARAM_DIV) {
- BUG("Last parameter should have type DIV (converted)\n");
- return -1;
- }
- ret = get_credentials(msg, s, &hdr);
- if (!hdr) return ret;
- RETURN0_res(hdr->body);
- }
- int select_auth_param(str* res, select_t* s, struct sip_msg* msg)
- {
- int ret;
- struct hdr_field* hdr;
- dig_cred_t* cred;
- if ((s->n != 3 && s->n != 4) || (s->params[s->n - 1].type != SEL_PARAM_DIV)) return -1;
- ret = get_credentials(msg, s, &hdr);
- if (!hdr) return ret;
- cred = &((auth_body_t*)hdr->parsed)->digest;
- switch(s->params[s->n - 1].v.i) {
- case SEL_AUTH_USER: RETURN0_res(cred->username.user);
- case SEL_AUTH_DOMAIN: RETURN0_res(cred->username.domain);
- case SEL_AUTH_USERNAME: RETURN0_res(cred->username.whole);
- case SEL_AUTH_REALM: RETURN0_res(cred->realm);
- case SEL_AUTH_NONCE: RETURN0_res(cred->nonce);
- case SEL_AUTH_URI: RETURN0_res(cred->uri);
- case SEL_AUTH_CNONCE: RETURN0_res(cred->cnonce);
- case SEL_AUTH_NC: RETURN0_res(cred->nc);
- case SEL_AUTH_RESPONSE: RETURN0_res(cred->response);
- case SEL_AUTH_OPAQUE: RETURN0_res(cred->opaque);
- case SEL_AUTH_ALG: RETURN0_res(cred->alg.alg_str);
- case SEL_AUTH_QOP: RETURN0_res(cred->qop.qop_str);
- default:
- BUG("Unsupported digest credentials parameter in select\n");
- return -1;
- }
- }
- int select_auth_username(str* res, select_t* s, struct sip_msg* msg)
- {
- return select_auth_param(res, s, msg);
- }
- int select_auth_username_comp(str* res, select_t* s, struct sip_msg* msg)
- {
- return select_auth_param(res, s, msg);
- }
- ABSTRACT_F(select_any_nameaddr)
- int select_nameaddr_name(str* res, select_t* s, struct sip_msg* msg)
- {
- char *p;
-
- p=find_not_quoted(res, '<');
- if (!p) {
- DBG("select_nameaddr_name: no < found, whole string is uri\n");
- res->len=0;
- return 1;
- }
- res->len=p-res->s;
- while (res->len && SP(res->s[res->len-1])) res->len--;
- return 0;
- }
- int select_nameaddr_uri(str* res, select_t* s, struct sip_msg* msg)
- {
- char *p;
-
- p=find_not_quoted(res, '<');
- if (!p) {
- DBG("select_nameaddr_uri: no < found, string up to first semicolon is uri\n");
- p = q_memchr(res->s, ';', res->len);
- if (p)
- res->len = p-res->s;
- return 0;
- }
- res->len=res->len - (p-res->s) -1;
- res->s=p +1;
-
- p=find_not_quoted(res, '>');
- if (!p) {
- ERR("select_nameaddr_uri: no > found, invalid nameaddr value\n");
- return -1;
- }
- res->len=p-res->s;
- return 0;
- }
- int select_nameaddr_params(str* res, select_t* s, struct sip_msg* msg)
- {
- char *p;
-
- p=find_not_quoted(res, '<');
- if (!p) {
- p=find_not_quoted(res, ';');
- } else {
- res->len=res->len - (p-res->s) -1;
- res->s=p +1;
- p=find_not_quoted(res, '>');
- if (!p) {
- ERR("select_nameaddr_params: no > found, invalid nameaddr value\n");
- return -1;
- }
- res->len=res->len - (p-res->s) -1;
- res->s=p +1;
-
- p=find_not_quoted(res, ';');
- }
- if (!p) return 1;
-
- res->len=res->len - (p-res->s) -1;
- res->s=p +1;
-
- if (s->param_offset[select_level+1]-s->param_offset[select_level]==1)
- return (res->len ? 0 : 1);
- return select_any_params(res, s, msg);
- }
- ABSTRACT_F(select_src)
- ABSTRACT_F(select_dst)
- ABSTRACT_F(select_rcv)
- int select_ip_port(str* res, select_t* s, struct sip_msg* msg)
- {
- str ip_str=STR_NULL, port_str=STR_NULL, proto_str=STR_NULL;
- int param, pos;
-
- if ((s->n != 2) || (s->params[1].type != SEL_PARAM_DIV)) return -1;
- param=s->params[1].v.i;
- if (param & SEL_SRC) {
- if (param & SEL_IP) {
- ip_str.s = ip_addr2a(&msg->rcv.src_ip);
- ip_str.len = strlen(ip_str.s);
- }
- if (param & SEL_PORT) {
- port_str.s = int2str(msg->rcv.src_port, &port_str.len);
- }
- } else if (param & SEL_DST) {
- if (param & SEL_IP) {
- ip_str.s = ip_addr2a(&msg->rcv.dst_ip);
- ip_str.len = strlen(ip_str.s);
- }
- if (param & SEL_PORT) {
- port_str.s = int2str(msg->rcv.dst_port, &port_str.len);
- }
- } else if (param & SEL_RCV) {
- if (param & SEL_IP) {
- ip_str = msg->rcv.bind_address->address_str;
- }
- if (param & SEL_PORT) {
- port_str = msg->rcv.bind_address->port_no_str;
- }
- if (param & SEL_PROTO) {
- switch (msg->rcv.proto) {
- case PROTO_NONE:
- proto_str.s = 0;
- proto_str.len = 0;
- break;
- case PROTO_UDP:
- proto_str.s = "udp";
- proto_str.len = 3;
- break;
- case PROTO_TCP:
- proto_str.s = "tcp";
- proto_str.len = 3;
- break;
- case PROTO_TLS:
- proto_str.s = "tls";
- proto_str.len = 3;
- break;
- case PROTO_SCTP:
- proto_str.s = "sctp";
- proto_str.len = 4;
- break;
- default:
- ERR("BUG: select_ip_port: Unknown transport protocol\n");
- return -1;
- }
- }
- } else {
- return -1;
- }
- res->s = get_static_buffer(ip_str.len+port_str.len+proto_str.len+3);
- if (!res->s) return -1;
- pos=0;
- if (param & SEL_PROTO) {
- memcpy(res->s, proto_str.s, proto_str.len);
- pos += proto_str.len;
- }
- if (param & SEL_IP) {
- if (pos) res->s[pos++] = ':';
- memcpy(res->s+pos, ip_str.s, ip_str.len);
- pos += ip_str.len;
- }
- if (param & SEL_PORT) {
- if (pos) res->s[pos++] = ':';
- memcpy(res->s+pos, port_str.s, port_str.len);
- pos += port_str.len;
- }
- res->s[pos] = 0;
- res->len = pos;
- return (pos==0 ? 1 : 0);
- }
- int select_expires(str* res, select_t* s, struct sip_msg* msg)
- {
- if (!msg->expires && (parse_headers(msg, HDR_EXPIRES_F, 0) == -1)) {
- return -1; /* error */
- }
- if (!msg->expires) {
- return 1; /* null */
- }
- if (msg->expires->parsed == NULL && parse_expires(msg->expires) < 0) {
- return -1;
- }
- RETURN0_res(((struct exp_body*)msg->expires->parsed)->text);
- }
- #define SELECT_plain_header(_sel_name_,_fld_name_,_hdr_f_) \
- int select_##_sel_name_(str* res, select_t* s, struct sip_msg* msg) \
- { \
- if (!msg->_fld_name_ && (parse_headers(msg, _hdr_f_, 0) == -1)) { \
- return -1; \
- } \
- if (!msg->_fld_name_) { \
- return 1; \
- } \
- RETURN0_res(msg->_fld_name_->body); \
- }
- SELECT_plain_header(call_id, callid, HDR_CALLID_F)
- SELECT_plain_header(max_forwards, maxforwards, HDR_MAXFORWARDS_F)
- SELECT_plain_header(content_type, content_type, HDR_CONTENTTYPE_F)
- SELECT_plain_header(content_length, content_length, HDR_CONTENTLENGTH_F)
- SELECT_plain_header(user_agent, user_agent, HDR_USERAGENT_F)
- SELECT_plain_header(subject, subject, HDR_SUBJECT_F)
- SELECT_plain_header(organization, organization, HDR_ORGANIZATION_F)
- SELECT_plain_header(priority, priority, HDR_PRIORITY_F)
- SELECT_plain_header(session_expires, session_expires, HDR_SESSIONEXPIRES_F)
- SELECT_plain_header(min_se, min_se, HDR_MIN_SE_F)
- SELECT_plain_header(sip_if_match, sipifmatch, HDR_SIPIFMATCH_F)
- SELECT_plain_header(date, date, HDR_DATE_F)
- SELECT_plain_header(identity, identity, HDR_IDENTITY_F)
- SELECT_plain_header(identity_info, identity_info, HDR_IDENTITY_INFO_F)
- int select_msg_request(str* res, select_t* s, struct sip_msg* msg)
- {
- if (msg->first_line.type==SIP_REQUEST) {
- return select_msg(res, s, msg);
- }
- else
- return -1;
- }
- int select_msg_response(str* res, select_t* s, struct sip_msg* msg)
- {
- if (msg->first_line.type==SIP_REPLY) {
- return select_msg(res, s, msg);
- }
- else
- return -1;
- }
- #define SELECT_first_line(_sel_name_,_type_,_fld_) \
- int select_msg_##_sel_name_(str* res, select_t* s, struct sip_msg* msg) {\
- if (msg->first_line.type==_type_) { \
- RETURN0_res(msg->first_line._fld_); \
- } else return -1; \
- }
- SELECT_first_line(request_method,SIP_REQUEST,u.request.method)
- SELECT_first_line(request_uri,SIP_REQUEST,u.request.uri)
- SELECT_first_line(request_version,SIP_REQUEST,u.request.version)
- SELECT_first_line(response_version,SIP_REPLY,u.reply.version)
- SELECT_first_line(response_status,SIP_REPLY,u.reply.status)
- SELECT_first_line(response_reason,SIP_REPLY,u.reply.reason)
- int select_version(str* res, select_t* s, struct sip_msg* msg)
- {
- switch (msg->first_line.type) {
- case SIP_REQUEST:
- RETURN0_res(msg->first_line.u.request.version)
- break;
- case SIP_REPLY:
- RETURN0_res(msg->first_line.u.reply.version)
- break;
- default:
- return -1;
- }
- }
- ABSTRACT_F(select_sys)
- int select_sys_pid(str* res, select_t* s, struct sip_msg* msg) {
- return uint_to_static_buffer(res, getpid());
- }
- int select_sys_server_id(str* res, select_t* s, struct sip_msg* msg) {
- return int_to_static_buffer(res, server_id);
- }
- int select_sys_unique(str* res, select_t* s, struct sip_msg* msg) {
- #define UNIQUE_ID_PID_LEN 4
- #define UNIQUE_ID_TIME_LEN 8
- #define UNIQUE_ID_FIX_LEN (UNIQUE_ID_PID_LEN+1+UNIQUE_ID_TIME_LEN+1)
- #define UNIQUE_ID_RAND_LEN 8
- static char uniq_id[UNIQUE_ID_FIX_LEN+UNIQUE_ID_RAND_LEN];
- static int uniq_for_pid = -1;
- int i;
- if (uniq_for_pid != getpid()) {
- /* first call for this process */
- int cb, rb, x, l;
- char *c;
- /* init gloabally uniq part */
- c = int2str_base_0pad(getpid(), &l, 16, UNIQUE_ID_PID_LEN);
- memcpy(uniq_id, c, UNIQUE_ID_PID_LEN);
- uniq_id[UNIQUE_ID_PID_LEN] = '-';
- c = int2str_base_0pad(time(NULL), &l, 16, UNIQUE_ID_TIME_LEN);
- memcpy(uniq_id+UNIQUE_ID_PID_LEN+1, c, UNIQUE_ID_TIME_LEN);
- uniq_id[UNIQUE_ID_PID_LEN+1+UNIQUE_ID_TIME_LEN] = '-';
- /* init random part */
- for (i = RAND_MAX, rb=0; i; rb++, i>>=1);
- for (i = UNIQUE_ID_FIX_LEN, cb = 0, x = 0; i < UNIQUE_ID_FIX_LEN+UNIQUE_ID_RAND_LEN; i++) {
- if (!cb) {
- cb = rb;
- x = rand();
- }
- uniq_id[i] = fourbits2char[x & 0x0F];
- x >>= rb;
- cb -= rb;
- }
- uniq_for_pid = getpid();
- }
- for (i = UNIQUE_ID_FIX_LEN + UNIQUE_ID_RAND_LEN - 1; i >= UNIQUE_ID_FIX_LEN; i--) {
- switch (uniq_id[i]) {
- case '9':
- uniq_id[i]='a';
- i = 0;
- break;
- case 'f':
- uniq_id[i]='0';
- /* go on */
- break;
- default:
- uniq_id[i]++;
- i = 0;
- break;
- }
- }
- res->s = uniq_id; /* I think it's not worth copying at static buffer, I hope there is no real meaning of @[email protected] */
- res->len = UNIQUE_ID_FIX_LEN+UNIQUE_ID_RAND_LEN;
- return 0;
- }
- int select_sys_now(str* res, select_t* s, struct sip_msg* msg) {
- return uint_to_static_buffer(res, time(NULL));
- }
- int select_sys_now_fmt(str* res, select_t* s, struct sip_msg* msg)
- {
- #define SEL_POS 2
- time_t t;
- struct tm *tm;
-
- t = time(NULL);
- switch (s->params[SEL_POS].v.i) {
- case SEL_NOW_GMT:
- tm = gmtime(&t);
- break;
-
- case SEL_NOW_LOCAL:
- tm = localtime(&t);
- break;
- default:
- BUG("Unexpected parameter value 'now' \"%d\"\n", s->params[SEL_POS].v.i);
- return -1;
- }
- if (s->n <= SEL_POS+1) {
- char *c;
- c = asctime(tm);
- res->len = strlen(c);
- while (res->len && c[res->len-1] < ' ') res->len--; /* rtrim */
- res->s = get_static_buffer(res->len);
- if (!res->s) return -1;
- memcpy(res->s, c, res->len);
- }
- else {
- char c, buff[80];
- c = s->params[SEL_POS+1].v.s.s[s->params[SEL_POS+1].v.s.len];
- s->params[SEL_POS+1].v.s.s[s->params[SEL_POS+1].v.s.len] = '\0';
- res->len = strftime(buff, sizeof(buff)-1, s->params[SEL_POS+1].v.s.s, tm);
- s->params[SEL_POS+1].v.s.s[s->params[SEL_POS+1].v.s.len] = c;
- res->s = get_static_buffer(res->len);
- if (!res->s) return -1;
- memcpy(res->s, buff, res->len);
- }
- return 0;
- #undef SEL_POS
- }
- ABSTRACT_F(select_branch)
- int select_branch_count(str* res, select_t* s, struct sip_msg* msg) {
- return uint_to_static_buffer(res, nr_branches);
- }
- int select_branch_uri(str* res, select_t* s, struct sip_msg* msg) {
- #define SEL_POS 1
- #define Q_PARAM ">;q="
- #define Q_PARAM_LEN (sizeof(Q_PARAM) - 1)
- qvalue_t q;
- int l, n;
- str dst_uri;
- if (s->n <= SEL_POS+1 && nr_branches > 1) { /* get all branches, if nr_branches==1 then use faster algorithm */
- int len;
- unsigned l2;
- char *c;
- init_branch_iterator();
- len = 0;
- while ((c = next_branch(&l, &q, &dst_uri, 0, 0, 0, 0, 0, 0))) {
- if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
- l = dst_uri.len;
- c = dst_uri.s;
- }
- if (s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI) ) {
- len += l;
- }
- if (q != Q_UNSPECIFIED && (s->params[SEL_POS].v.i & SEL_BRANCH_Q)) {
- len += len_q(q);
- if (s->params[SEL_POS].v.i & SEL_BRANCH_URI) {
- len += 1 + Q_PARAM_LEN;
- }
- }
- len += 1;
- }
- if (!len) return 1;
- res->s = get_static_buffer(len);
- if (!res->s) return -1;
-
- init_branch_iterator();
- res->len = 0;
- n = 0;
- while ((c = next_branch(&l, &q, &dst_uri, 0, 0, 0, 0, 0, 0))) {
- if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
- l = dst_uri.len;
- c = dst_uri.s;
- }
- if (n) {
- res->s[res->len] = ',';
- res->len++;
- }
- if ((s->params[SEL_POS].v.i & SEL_BRANCH_Q) == 0) {
- q = Q_UNSPECIFIED;
- }
- if ((s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI)) && q != Q_UNSPECIFIED) {
- res->s[res->len] = '<';
- res->len++;
- memcpy(res->s+res->len, c, l);
- res->len += l;
- memcpy(res->s+res->len, Q_PARAM, Q_PARAM_LEN);
- res->len += Q_PARAM_LEN;
- c = q2str(q, &l2); l = l2;
- memcpy(res->s+res->len, c, l);
- res->len += l;
- }
- else if (s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI)) {
- memcpy(res->s+res->len, c, l);
- res->len += l;
- }
- else if (q != Q_UNSPECIFIED) {
- c = q2str(q, &l2); l = l2;
- memcpy(res->s+res->len, c, l);
- res->len += l;
- }
- n++;
- }
- }
- else {
- unsigned l2;
- char *c;
- n = s->params[SEL_POS+1].v.i;
- if (n < 0 || n >= nr_branches)
- return -1;
- init_branch_iterator();
- for (; (c = next_branch(&l, &q, &dst_uri, 0, 0, 0, 0, 0, 0)) && n; n--);
- if (!c) return 1;
-
- if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
- l = dst_uri.len;
- c = dst_uri.s;
- }
- if ((s->params[SEL_POS].v.i & SEL_BRANCH_Q) == 0) {
- q = Q_UNSPECIFIED;
- }
- if ((s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI)) && q != Q_UNSPECIFIED) {
- res->s = get_static_buffer(l + 1 + Q_PARAM_LEN + len_q(q));
- if (!res->s) return -1;
- res->len = 1;
- res->s[0] = '<';
- memcpy(res->s+res->len, c, l);
- res->len += l;
- memcpy(res->s+res->len, Q_PARAM, Q_PARAM_LEN);
- res->len += Q_PARAM_LEN;
- c = q2str(q, &l2); l = l2;
- memcpy(res->s+res->len, c, l);
- res->len += l;
- }
- else if (s->params[SEL_POS].v.i & (SEL_BRANCH_URI|SEL_BRANCH_DST_URI)) {
- res->s = c; /* not necessary to copy to static buffer */
- res->len = l;
- }
- else if (q != Q_UNSPECIFIED) {
- c = q2str(q, &l2);
- res->len = l2;
- res->s = get_static_buffer(res->len);
- if (!res->s) return -1;
- memcpy(res->s, c, res->len);
- }
- else {
- res->len = 0;
- }
- }
- return 0;
- #undef SEL_POS
- }
- int select_branch_uriq(str* res, select_t* s, struct sip_msg* msg) {
- return select_branch_uri(res, s, msg);
- }
- int select_branch_q(str* res, select_t* s, struct sip_msg* msg) {
- return select_branch_uri(res, s, msg);
- }
- int select_branch_dst_uri(str* res, select_t* s, struct sip_msg* msg) {
- return select_branch_uri(res, s, msg);
- }
|