美文网首页
【opensips学习笔记】【二】core variables核

【opensips学习笔记】【二】core variables核

作者: 安安爸Chris | 来源:发表于2021-02-19 17:03 被阅读0次

    注意: 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

    相关文章

      网友评论

          本文标题:【opensips学习笔记】【二】core variables核

          本文链接:https://www.haomeiwen.com/subject/jgraxltx.html