dlg_var.c

Go to the documentation of this file.
00001 /**
00002  * $Id$
00003  *
00004  * Copyright (C) 2009 Daniel-Constantin Mierla (asipto.com)
00005  *
00006  * This file is part of kamailio, a free SIP server.
00007  *
00008  * openser is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * openser is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  */
00022              
00023 #include "dlg_var.h"
00024 
00025 dlg_ctx_t _dlg_ctx;
00026 
00027 int dlg_cfg_cb(struct sip_msg *foo, void *bar)
00028 {
00029    memset(&_dlg_ctx, 0, sizeof(dlg_ctx_t));
00030 
00031    return 1;
00032 }
00033 
00034 int pv_get_dlg_ctx(struct sip_msg *msg,  pv_param_t *param,
00035       pv_value_t *res)
00036 {
00037    if(param==NULL)
00038       return -1;
00039    switch(param->pvn.u.isname.name.n)
00040    {
00041       case 1:
00042          return pv_get_uintval(msg, param, res,
00043                (unsigned int)_dlg_ctx.flags);
00044       case 2:
00045          return pv_get_uintval(msg, param, res,
00046                (unsigned int)_dlg_ctx.timeout);
00047       case 3:
00048          return pv_get_uintval(msg, param, res,
00049                (unsigned int)_dlg_ctx.to_bye);
00050       case 4:
00051          return pv_get_uintval(msg, param, res,
00052                (unsigned int)_dlg_ctx.to_route);
00053       case 5:
00054          _dlg_ctx.set = (_dlg_ctx.dlg==NULL)?0:1;
00055          return pv_get_uintval(msg, param, res,
00056                (unsigned int)_dlg_ctx.set);
00057       case 6:
00058          return pv_get_uintval(msg, param, res,
00059                (unsigned int)_dlg_ctx.dir);
00060       default:
00061          return pv_get_uintval(msg, param, res,
00062                (unsigned int)_dlg_ctx.on);
00063    }
00064    return 0;
00065 }
00066 
00067 int pv_set_dlg_ctx(struct sip_msg* msg, pv_param_t *param,
00068       int op, pv_value_t *val)
00069 {
00070    int n;
00071    if(param==NULL)
00072       return -1;
00073 
00074    if(val==NULL)
00075       n = 0;
00076    else
00077       n = val->ri;
00078 
00079    switch(param->pvn.u.isname.name.n)
00080    {
00081       case 1:
00082          _dlg_ctx.flags = n;
00083       break;
00084       case 2:
00085          _dlg_ctx.timeout = n;
00086       break;
00087       case 3:
00088          _dlg_ctx.to_bye = n;
00089       break;
00090       case 4:
00091          _dlg_ctx.to_route = n;
00092       break;
00093       default:
00094          _dlg_ctx.on = n;
00095       break;
00096    }
00097    return 0;
00098 }
00099 
00100 int pv_parse_dlg_ctx_name(pv_spec_p sp, str *in)
00101 {
00102    if(sp==NULL || in==NULL || in->len<=0)
00103       return -1;
00104 
00105    switch(in->len)
00106    {
00107       case 2: 
00108          if(strncmp(in->s, "on", 2)==0)
00109             sp->pvp.pvn.u.isname.name.n = 0;
00110          else goto error;
00111       break;
00112       case 3: 
00113          if(strncmp(in->s, "set", 3)==0)
00114             sp->pvp.pvn.u.isname.name.n = 5;
00115          else if(strncmp(in->s, "dir", 3)==0)
00116             sp->pvp.pvn.u.isname.name.n = 6;
00117          else goto error;
00118       break;
00119       case 5: 
00120          if(strncmp(in->s, "flags", 6)==0)
00121             sp->pvp.pvn.u.isname.name.n = 1;
00122          else goto error;
00123       break;
00124       case 7: 
00125          if(strncmp(in->s, "timeout", 7)==0)
00126             sp->pvp.pvn.u.isname.name.n = 2;
00127          else goto error;
00128       break;
00129       case 11: 
00130          if(strncmp(in->s, "timeout_bye", 11)==0)
00131             sp->pvp.pvn.u.isname.name.n = 3;
00132          else goto error;
00133       break;
00134       case 13:
00135          if(strncmp(in->s, "timeout_route", 13)==0)
00136             sp->pvp.pvn.u.isname.name.n = 4;
00137          else goto error;
00138       break;
00139       default:
00140          goto error;
00141    }
00142    sp->pvp.pvn.type = PV_NAME_INTSTR;
00143    sp->pvp.pvn.u.isname.type = 0;
00144 
00145    return 0;
00146 
00147 error:
00148    LM_ERR("unknown PV name %.*s\n", in->len, in->s);
00149    return -1;
00150 }
00151 
00152 int pv_get_dlg(struct sip_msg *msg, pv_param_t *param,
00153       pv_value_t *res)
00154 {
00155    if(param==NULL)
00156       return -1;
00157    if(_dlg_ctx.dlg == NULL)
00158       return pv_get_null(msg, param, res);
00159    switch(param->pvn.u.isname.name.n)
00160    {
00161       case 1:
00162          return pv_get_uintval(msg, param, res,
00163                (unsigned int)_dlg_ctx.dlg->h_id);
00164       case 2:
00165          return pv_get_uintval(msg, param, res,
00166                (unsigned int)_dlg_ctx.dlg->state);
00167       case 3:
00168          if(_dlg_ctx.dlg->route_set[DLG_CALLEE_LEG].s==NULL
00169                || _dlg_ctx.dlg->route_set[DLG_CALLEE_LEG].len<=0)
00170             return pv_get_null(msg, param, res);
00171          return pv_get_strval(msg, param, res,
00172                &_dlg_ctx.dlg->route_set[DLG_CALLEE_LEG]);
00173       case 4:
00174          return pv_get_uintval(msg, param, res,
00175                (unsigned int)_dlg_ctx.dlg->dflags);
00176       case 5:
00177          return pv_get_uintval(msg, param, res,
00178                (unsigned int)_dlg_ctx.dlg->sflags);
00179       case 6:
00180          if(_dlg_ctx.dlg->callid.s==NULL
00181                || _dlg_ctx.dlg->callid.len<=0)
00182             return pv_get_null(msg, param, res);
00183          return pv_get_strval(msg, param, res,
00184                &_dlg_ctx.dlg->callid);
00185       case 7:
00186          if(_dlg_ctx.dlg->to_uri.s==NULL
00187                || _dlg_ctx.dlg->to_uri.len<=0)
00188             return pv_get_null(msg, param, res);
00189          return pv_get_strval(msg, param, res,
00190                &_dlg_ctx.dlg->to_uri);
00191       case 8:
00192          if(_dlg_ctx.dlg->tag[DLG_CALLEE_LEG].s==NULL
00193                || _dlg_ctx.dlg->tag[DLG_CALLEE_LEG].len<=0)
00194             return pv_get_null(msg, param, res);
00195          return pv_get_strval(msg, param, res,
00196                &_dlg_ctx.dlg->tag[DLG_CALLEE_LEG]);
00197       case 9:
00198          return pv_get_uintval(msg, param, res,
00199                (unsigned int)_dlg_ctx.dlg->toroute);
00200       case 10:
00201          if(_dlg_ctx.dlg->cseq[DLG_CALLEE_LEG].s==NULL
00202                || _dlg_ctx.dlg->cseq[DLG_CALLEE_LEG].len<=0)
00203             return pv_get_null(msg, param, res);
00204          return pv_get_strval(msg, param, res,
00205                &_dlg_ctx.dlg->cseq[DLG_CALLEE_LEG]);
00206       case 11:
00207          if(_dlg_ctx.dlg->route_set[DLG_CALLER_LEG].s==NULL
00208                || _dlg_ctx.dlg->route_set[DLG_CALLER_LEG].len<=0)
00209             return pv_get_null(msg, param, res);
00210          return pv_get_strval(msg, param, res,
00211                &_dlg_ctx.dlg->route_set[DLG_CALLER_LEG]);
00212       case 12:
00213          if(_dlg_ctx.dlg->from_uri.s==NULL
00214                || _dlg_ctx.dlg->from_uri.len<=0)
00215             return pv_get_null(msg, param, res);
00216          return pv_get_strval(msg, param, res,
00217                &_dlg_ctx.dlg->from_uri);
00218       case 13:
00219          if(_dlg_ctx.dlg->tag[DLG_CALLER_LEG].s==NULL
00220                || _dlg_ctx.dlg->tag[DLG_CALLER_LEG].len<=0)
00221             return pv_get_null(msg, param, res);
00222          return pv_get_strval(msg, param, res,
00223                &_dlg_ctx.dlg->tag[DLG_CALLER_LEG]);
00224       case 14:
00225          return pv_get_uintval(msg, param, res,
00226                (unsigned int)_dlg_ctx.dlg->lifetime);
00227       case 15:
00228          return pv_get_uintval(msg, param, res,
00229                (unsigned int)_dlg_ctx.dlg->start_ts);
00230       case 16:
00231          if(_dlg_ctx.dlg->cseq[DLG_CALLER_LEG].s==NULL
00232                || _dlg_ctx.dlg->cseq[DLG_CALLER_LEG].len<=0)
00233             return pv_get_null(msg, param, res);
00234          return pv_get_strval(msg, param, res,
00235                &_dlg_ctx.dlg->cseq[DLG_CALLER_LEG]);
00236       case 17:
00237          if(_dlg_ctx.dlg->contact[DLG_CALLEE_LEG].s==NULL
00238                || _dlg_ctx.dlg->contact[DLG_CALLEE_LEG].len<=0)
00239             return pv_get_null(msg, param, res);
00240          return pv_get_strval(msg, param, res,
00241                &_dlg_ctx.dlg->contact[DLG_CALLEE_LEG]);
00242       case 18:
00243          if(_dlg_ctx.dlg->bind_addr[DLG_CALLEE_LEG]==NULL)
00244             return pv_get_null(msg, param, res);
00245          return pv_get_strval(msg, param, res,
00246                &_dlg_ctx.dlg->bind_addr[DLG_CALLEE_LEG]->sock_str);
00247       case 19:
00248          if(_dlg_ctx.dlg->contact[DLG_CALLER_LEG].s==NULL
00249                || _dlg_ctx.dlg->contact[DLG_CALLER_LEG].len<=0)
00250             return pv_get_null(msg, param, res);
00251          return pv_get_strval(msg, param, res,
00252                &_dlg_ctx.dlg->contact[DLG_CALLER_LEG]);
00253       case 20:
00254          if(_dlg_ctx.dlg->bind_addr[DLG_CALLER_LEG]==NULL)
00255             return pv_get_null(msg, param, res);
00256          return pv_get_strval(msg, param, res,
00257                &_dlg_ctx.dlg->bind_addr[DLG_CALLER_LEG]->sock_str);
00258       case 21:
00259          return pv_get_uintval(msg, param, res,
00260                (unsigned int)_dlg_ctx.dlg->h_entry);
00261       default:
00262          return pv_get_uintval(msg, param, res,
00263                (unsigned int)_dlg_ctx.dlg->ref);
00264    }
00265    return 0;
00266 }
00267 
00268 int pv_parse_dlg_name(pv_spec_p sp, str *in)
00269 {
00270    if(sp==NULL || in==NULL || in->len<=0)
00271       return -1;
00272 
00273    switch(in->len)
00274    {
00275       case 3: 
00276          if(strncmp(in->s, "ref", 3)==0)
00277             sp->pvp.pvn.u.isname.name.n = 0;
00278          else goto error;
00279       break;
00280       case 4: 
00281          if(strncmp(in->s, "h_id", 4)==0)
00282             sp->pvp.pvn.u.isname.name.n = 1;
00283          else goto error;
00284       break;
00285       case 5: 
00286          if(strncmp(in->s, "state", 5)==0)
00287             sp->pvp.pvn.u.isname.name.n = 2;
00288          else if(strncmp(in->s, "to_rs", 5)==0)
00289             sp->pvp.pvn.u.isname.name.n = 3;
00290          else goto error;
00291       break;
00292       case 6: 
00293          if(strncmp(in->s, "dflags", 6)==0)
00294             sp->pvp.pvn.u.isname.name.n = 4;
00295          else if(strncmp(in->s, "sflags", 6)==0)
00296             sp->pvp.pvn.u.isname.name.n = 5;
00297          else if(strncmp(in->s, "callid", 6)==0)
00298             sp->pvp.pvn.u.isname.name.n = 6;
00299          else if(strncmp(in->s, "to_uri", 6)==0)
00300             sp->pvp.pvn.u.isname.name.n = 7;
00301          else if(strncmp(in->s, "to_tag", 6)==0)
00302             sp->pvp.pvn.u.isname.name.n = 8;
00303          else goto error;
00304       break;
00305       case 7: 
00306          if(strncmp(in->s, "toroute", 7)==0)
00307             sp->pvp.pvn.u.isname.name.n = 9;
00308          else if(strncmp(in->s, "to_cseq", 7)==0)
00309             sp->pvp.pvn.u.isname.name.n = 10;
00310          else if(strncmp(in->s, "from_rs", 7)==0)
00311             sp->pvp.pvn.u.isname.name.n = 11;
00312          else if(strncmp(in->s, "h_entry", 7)==0)
00313             sp->pvp.pvn.u.isname.name.n = 21;
00314          else goto error;
00315       break;
00316       case 8: 
00317          if(strncmp(in->s, "from_uri", 8)==0)
00318             sp->pvp.pvn.u.isname.name.n = 12;
00319          else if(strncmp(in->s, "from_tag", 8)==0)
00320             sp->pvp.pvn.u.isname.name.n = 13;
00321          else if(strncmp(in->s, "lifetime", 8)==0)
00322             sp->pvp.pvn.u.isname.name.n = 14;
00323          else if(strncmp(in->s, "start_ts", 8)==0)
00324             sp->pvp.pvn.u.isname.name.n = 15;
00325          else goto error;
00326       break;
00327       case 9: 
00328          if(strncmp(in->s, "from_cseq", 9)==0)
00329             sp->pvp.pvn.u.isname.name.n = 16;
00330          else goto error;
00331       break;
00332       case 10: 
00333          if(strncmp(in->s, "to_contact", 10)==0)
00334             sp->pvp.pvn.u.isname.name.n = 17;
00335          else goto error;
00336       break;
00337       case 11: 
00338          if(strncmp(in->s, "to_bindaddr", 11)==0)
00339             sp->pvp.pvn.u.isname.name.n = 18;
00340          else goto error;
00341       break;
00342       case 12: 
00343          if(strncmp(in->s, "from_contact", 12)==0)
00344             sp->pvp.pvn.u.isname.name.n = 19;
00345          else goto error;
00346       break;
00347       case 13: 
00348          if(strncmp(in->s, "from_bindaddr", 20)==0)
00349             sp->pvp.pvn.u.isname.name.n = 2;
00350          else goto error;
00351       break;
00352       default:
00353          goto error;
00354    }
00355    sp->pvp.pvn.type = PV_NAME_INTSTR;
00356    sp->pvp.pvn.u.isname.type = 0;
00357 
00358    return 0;
00359 
00360 error:
00361    LM_ERR("unknown PV name %.*s\n", in->len, in->s);
00362    return -1;
00363 }
00364 
00365 void dlg_set_ctx_dialog(struct dlg_cell *dlg)
00366 {
00367    _dlg_ctx.dlg = dlg;
00368 }
00369 
00370 struct dlg_cell* dlg_get_ctx_dialog(void)
00371 {
00372    return _dlg_ctx.dlg;
00373 }
00374 

Generated on Tue May 22 16:00:27 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6