【opensips学习笔记】【二】core variables核心变量

注意: Opensips的版本为2.4.x
参考文献地址 https://www.opensips.org/Documentation/Script-CoreVar-2-4

变量语法

$(<context>name(subname)[index]{transformation})

name

其实是“type”,因为它分为几种,

  1. 标量/脚本变量var
  2. 键值对avpAttribute Value Pair
  3. 系统变量/伪变量PV (pseudo variables)
  4. 转移字符串

subname

就是对应type的变量name

index

如果是avp和PV,可以储存多个值,这里就可以使用index来索引。注意,它支持负数,表述从后往前

transformation

就是对取出的value做变换,如截取长度,取子字符串等

context

主要是区别request和reply两个方向的,如果不清楚的话,可以参考学习笔记一

标量/脚本变量

下面是官网的例子

$var(a) = 2;  # sets the value of variable 'a' to integer '2'
$var(a) = "2";  # sets the value of variable 'a' to string '2'
$var(a) = 3 + (7&(~2)); # arithmetic and bitwise operation
$var(a) = "sip:" + $au + "@" + $fd; # compose a value from authentication username and From URI domain

# using a script variable for tests
if( [ $var(a) & 4 ] ) {
  xlog("var a has third bit set\n");
}

AVP变量

TM模块有个参数onreply_avp_mode,这个参数如果设置为0(默认为0),则AVP的生命周期只有request route,branch route以及failure route阶段,reply route阶段没有;但是有意思的是,此时,var的生命周期是整个dialog。
如果设置为1,AVP的生命周期会包含request route,branch route以及failure route阶段,reply route阶段。但是只有一个transaction内的所有阶段。新的transaction是没有继承的;此时,var的生命周期只有一个transactin的equest route,branch route阶段。

modparam("tm", "onreply_avp_mode", 1)

官网的例子

例子1, 跨route阶段保存值

# enable avps in onreply route
modparam("tm", "onreply_avp_mode", 1)
...
route{
...
$avp(tmp) = $Ts ; # store the current time (at request processing)
...
t_onreply("1");
t_relay();
...
}

onreply_route[1] {
    if (t_check_status("200")) {
        # calculate the setup time
        $var(setup_time) = $Ts - $avp(tmp);
    }
}

例子2 多值操作

$avp(17) = "one";
# we have a single value
$avp(17) = "two";
# we have two values ("two","one")
$avp(17) = "three";
# we have three values ("three","two","one")

xlog("accessing values with no index: $avp(17)\n");
# this will print the first value, which is the last added value -> "three"

xlog("accessing values with no index: $(avp(17)[2])\n");
# this will print the index 2 value (third one), -> "one"

# remove the last value of the avp; if there is only one value, the AVP itself will be destroyed
$avp(17) = NULL;

# delete all values and destroy the AVP
avp_delete("$avp(17)/g");

# delete the value located at a certain index 
$(avp(17)[1]) = NULL;

#overwrite the value at a certain index
$(avp(17)[0]) = "zero";

PV 系统变量

PV Header Full Name Description
$ai URI in SIP Request's P-Asserted-Identity header reference to URI in request's P-Asserted-Identity header (see RFC 3325)
$adu Authentication Digest URI URI from Authorization or Proxy-Authorization header. This URI is used when calculating the HTTP Digest Response.
$ar Authentication realm realm from Authorization or Proxy-Authorization header
$au Auth username user user part of username from Authorization or Proxy-Authorization header
$ad Auth username domain domain part of username from Authorization or Proxy-Authorization header
$an Auth nonce the nonce from Authorization or Proxy-Authorization header
$auth.resp Auth response the authentication response from Authorization or Proxy-Authorization header
$auth.nonce Auth nonce the nonce string from Authorization or Proxy-Authorization header
$auth.opaque Auth opaque the opaque string from Authorization or Proxy-Authorization header
$auth.alg Auth algorithm the algorithm string from Authorization or Proxy-Authorization header
$auth.qop Auth QOP the value of qop parameter from Authorization or Proxy-Authorization header
$auth.nc Auth nonce count (nc) the value of nonce count parameter from Authorization or Proxy-Authorization header
$aU Auth whole username whole username from Authorization or Proxy-Authorization header
$Au Acc username username for accounting purposes. It's a selective pseudo variable (inherited from acc module). It returns $au if exits or From username otherwise.
$argv provides access to command line arguments specified with '-o' option
$af Address family The address family of the received SIP message. It's value is "INET" if the message was received over IPv4 or "INET6" if the message was received over IPv6
$bf Branch flags displays a list with the branch flags set for the current SIP request
$branch Branch this variable is used for creating new branches by writing into it the value of a SIP URI
$ci Call-Id reference to body of call-id header
$cl Content-Length reference to body of content-length header
$cs CSeq number reference to cseq number from cseq header
$ct Contact instance reference to contact instance/body from the contact header. A contact instance is display_name + URI + contact_params. As a Contact header may contain multiple Contact instances and a message may contain multiple Contact headers, an index was added to the $ct variable
$cT Content-Type reference to body of Content-Type header and also the content-type headers inside a multi-part body
$dd Domain of destination URI reference to domain of destination uri (read/write)
$di Diversion header URI reference to Diversion header URI
$dip Diversion "privacy" parameter reference to Diversion header "privacy" parameter value
$dir Diversion "reason" parameter reference to Diversion header "reason" parameter value
$dp Port of destination URI reference to port of destination uri(read/write)
$dP Transport protocol of destination URI reference to transport protocol of destination uri
$ds Destination set reference to destination set
$du Destination URI reference to destination uri (outbound proxy to be used for sending the request) If loose_route() returns TRUE a destination uri is set according to the first Route header. (read/write)
$err.class Error class the class of error (now is '1' for parsing errors)
$err.level Error level severity level for the error
$err.info Error info text describing the error
$err.rcode Error reply code recommended reply code
$err.rreason Error reply reason recommended reply reason phrase
$fd From URI domain reference to domain in URI of 'From' header
$fn From display name reference to display name of 'From' header
$fs Forced socket reference to the forced socket for message sending (if any) in the form proto:ip:port (read/write)
$ft From tag reference to tag parameter of 'From' header
$fu From URI reference to URI of 'From' header
$fU From URI username reference to username in URI of 'From' header
$log_level OpenSIPS Log level changes the log level for the current process (read/write)
$mb SIP message buffer reference to SIP message buffer
$mf Message Flags displays a list with the message/transaction flags set for the current SIP request
$mi SIP message ID reference to SIP message id
$ml SIP message length reference to SIP message length
$od Domain in SIP Request's original URI reference to domain in request's original R-URI
$op Port of SIP request's original URI reference to port of original R-URI
$oP Transport protocol of SIP request original URI reference to transport protocol of original R-URI
$ou SIP Request's original URI reference to request's original URI
$oU Username in SIP Request's original URI reference to username in request's original URI
$param(idx) Route parameter retrieves the parameters of the route. The index can be an integer, or a pseudo-variable (index starts at 1).
$pd Domain in SIP Request's P-Preferred-Identity header URI reference to domain in request's P-Preferred-Identity header URI (see RFC 3325)
$pn Display Name in SIP Request's P-Preferred-Identity header reference to Display Name in request's P-Preferred-Identity header (see RFC 3325)
$pp Process id reference to process id (pid)
$pr Protocol of received message protocol of received message (UDP, TCP, TLS, SCTP, WS)
$pU User in SIP Request's P-Preferred-Identity header URI reference to user in request's P-Preferred-Identity header URI (see RFC 3325)
$pu URI in SIP Request's P-Preferred-Identity header reference to URI in request's P-Preferred-Identity header (see RFC 3325)
$rd Domain in SIP Request's URI reference to domain in request's URI (read/write)
$rb Body of request/reply reference to the body or a body part of the SIP message
$rc Returned code reference to returned code by last invoked function
$re Remote-Party-ID header URI reference to Remote-Party-ID header URI
$rm SIP request's method reference to request's method
$rp SIP request's port reference to port of R-URI (read/write)
$rP Transport protocol of SIP request URI reference to transport protocol of R-URI
$rr SIP reply's reason reference to reply's reason
$rs SIP reply's status reference to reply's status
$rt Refer-to URI reference to URI of refer-to header
$ru SIP Request's URI reference to request's URI (read/write)
$rU Username in SIP Request's URI reference to username in request's URI (read/write)
$ru_q Q value of the SIP Request's URI reference to q value of the R-URI (read/write)
$Ri Received IP address reference to IP address of the interface where the request has been received
$Rp Received port reference to the port where the message was received
$si IP source address reference to IP source address of the message
$sp Source port reference to the source port of the message
$td To URI Domain reference to domain in URI of 'To' header
$tn To display name reference to display name of 'To' header
$tt To tag reference to tag parameter of 'To' header
$tu To URI reference to URI of 'To' header
$tU To URI Username reference to username in URI of 'To' header
$time(format) Formatted date and time returns the string formatted time according to UNIX date (see: man date).
$T_branch_idx Branch index the index (starting with 1 for the first branch) of the branch for which is executed the branch_route[]. If used outside of branch_route[] block, the value is '0'. This is exported by TM module
$Tf String formatted time
$Ts Current unix time stamp in seconds
$Tsm Current microseconds of the current second
$TS Startup unix time stamp
$ua User agent header reference to user agent header field
$(hdr(name)[N]) $(hdrcnt(name) SIP Headers represents the body of the N-th header identified by 'name'.
$rT Route Type Holds the current route type as a string
$cfg_line $cfg_file Current script line and file
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,951评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,606评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,601评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,478评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,565评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,587评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,590评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,337评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,785评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,096评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,273评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,935评论 5 339
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,578评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,199评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,440评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,163评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,133评论 2 352

推荐阅读更多精彩内容