Lua Script Solves Call Forward Masking

Had to implement an Auto Attendant in Unity today.  The client had a requirement to send after-hours calls out to the PSTN, but had to be routed in to their Call Centre, which had it’s own IVR which matched on calling number, that needed to be the switchboard number for their site.

Call Flow was as follows:

  1. Inbound call from PSTN
  2. CTI RP re-direct to Unity
  3. Unity Call Handler, with Close and Holiday transfer to 2nd Call Handler
  4. After-Hours Call Handler transfers back to After-Hours CTI RP
  5. After-Hours CTI RP as Call Forward to a PSTN number

 

The behavior that we see is controlled by SIP Trunk Calling Party Selection parameter, which is set to “Originator” by default:

pic1

 

The SIP INVITE sent to the CUBE looked like this:

INVITE sip:0112223344@mygateway.mydomain.com:5060 SIP/2.0
Via: SIP/2.0/UDP 192.168.5.5:5060;branch=z9hG4bK55cd222ad94e
From: <sip:0255667787@myhostname.mydomain.com>;tag=336868~0d10edf8-0553-4009-a728-c065d8394e46-24136542
To: <sip:0112223344@mygateway.mydomain.com>
Date: Mon, 07 Dec 2015 10:38:00 GMT
Call-ID: 94dfb980-66516188-5120-247410ac@192.168.5.5
Supported: 100rel,timer,resource-priority,replaces
Min-SE: 1800
User-Agent: Cisco-CUCM11.0
Allow: INVITE, OPTIONS, INFO, BYE, CANCEL, ACK, PRACK, UPDATE, REFER, SUBSCRIBE, NOTIFY
CSeq: 101 INVITE
Expires: 180
Allow-Events: presence, kpml
Supported: X-cisco-srtp-fallback
Supported: Geolocation
Call-Info: <sip:192.168.5.5:5060>;method=”NOTIFY;Event=telephone-event;Duration=500″
Call-Info: <urn:x-cisco-remotecc:callinfo>;x-cisco-video-traffic-class=DESKTOP
Session-ID: 97632eb36d62419cbdc5fe71aa336867;remote=00000000000000000000000000000000
Cisco-Guid: 2497689984-0000065536-0000000110-0611586220
Session-Expires: 1800
Diversion: “After-Hours Re-Direct” <sip:**999@mydomain.com>;reason=unconditional;privacy=off;screen=yes
P-Asserted-Identity: <sip:0255667787@myhostname.mydomain.com>
Remote-Party-ID: <sip:0255667787@myhostname.mydomain.com>;party=calling;screen=yes;privacy=off
Contact: <sip:0255667787@192.168.5.5:5060>
Max-Forwards: 69
Content-Type: application/sdp
Content-Length: 227

v=0
o=CiscoSystemsCCM-SIP 336868 1 IN IP4 192.168.5.5
s=SIP Call
c=IN IP4 172.16.115.23
b=TIAS:8000
b=AS:8
t=0 0
m=audio 26842 RTP/AVP 18 101
a=rtpmap:18 G729/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-15

 

  • In this case the calling party is sent out at 0255667787.  We want this sent out as 0255667000 to me the client’s requirement.
  • To do this, we need to correct the From, PAI, RPID, and Contact headers
  • The Diversion Header contains the Internal DN of the CTI RP – not very pretty, and something that our ITSP may bark at!  We must change it.
  • The headers MUST ONLY be modified when the call is forwarded FROM THIS CTI RP – my “hook” here will be the offending Diversion header.

 

I opted for a Lua script.  I find complex SIP Profiles unwieldly, which could have been another option.  Vomit.

 

So, here’s the script that achieved it.  Pretty simple in fact:

M = {}

trace.enable()

function M.outbound_INVITE(msg)

— Input for script
local ctirpdn = scriptParameters.getValue(“ctirpdn”)
local mainnumber = scriptParameters.getValue(“mainnumber”)
local rdnis = scriptParameters.getValue(“rdnis”)

— Get Diversion header
local diversion = msg:getHeader(“Diversion”)

— Check if Diversion Header exists and if the CTI RP DN is matched
if diversion and diversion:find(ctirpdn) then
trace.format(“Successful match on diversion – applying masking”)

trace.format(“CTI RP DN is : %s”, ctirpdn)
trace.format(“Main Number is : %s”, mainnumber)

— Apply masking to affected headers – From, PAI, RPID, Contact
msg:applyNumberMask(“From”, mainnumber)
msg:applyNumberMask(“P-Asserted-Identity”, mainnumber)
msg:applyNumberMask(“Remote-Party-ID”, mainnumber)
msg:applyNumberMask(“Contact”, mainnumber)
— Apply masking to Diversion to mask internal CTI RP DN
msg:applyNumberMask(“Diversion”, rdnis)

end
end

return M

The bold script params were applied on the SIP Trunk.  I decided to use Unity as the forwarding station, as set this as the “rdnis”.  The key is the IF STATEMENT, that only applied changes if the CTI RP DN is matched in the header.  Otherwise, the script is skipped.

 

Now, for testing… Let’s open up RTMT and trace this in SDL:

 

 

03149762.001 |13:00:37.979 |AppInfo |//SIP/SIPUdp/wait_SdlSPISignal: Outgoing SIP UDP message to 192.168.1.3:[5060]:
[990360,NET]
INVITE sip:0112223344@mygateway.@mydomain.com:5060 SIP/2.0
Via: SIP/2.0/UDP 192.168.5.5:5060;branch=z9hG4bK5634188efa43
From: <sip:0255667000@myhostname.@mydomain.com>;tag=337995~0d10edf8-0553-4009-a728-c065d8394e46-24136584
To: <sip:0112223344@mygateway.@mydomain.com>
Date: Mon, 07 Dec 2015 11:00:37 GMT
Call-ID: bdb57e00-665166d5-516d-247410ac@192.168.5.5
Supported: 100rel,timer,resource-priority,replaces
Min-SE: 1800
User-Agent: Cisco-CUCM11.0
Allow: INVITE, OPTIONS, INFO, BYE, CANCEL, ACK, PRACK, UPDATE, REFER, SUBSCRIBE, NOTIFY
CSeq: 101 INVITE
Expires: 180
Allow-Events: presence, kpml
Supported: X-cisco-srtp-fallback
Supported: Geolocation
Call-Info: <sip:192.168.5.5:5060>;method=”NOTIFY;Event=telephone-event;Duration=500″
Call-Info: <urn:x-cisco-remotecc:callinfo>;x-cisco-video-traffic-class=DESKTOP
Session-ID: 97632eb36d62419cbdc5fe71aa337994;remote=00000000000000000000000000000000
Cisco-Guid: 3182788096-0000065536-0000000119-0611586220
Session-Expires: 1800
Diversion: “After-Hours Re-Direct” <sip:0255667800@@mydomain.com>;reason=unconditional;privacy=off;screen=yes
P-Asserted-Identity: <sip:0255667000@myhostname.@mydomain.com>
Remote-Party-ID: <sip:0255667000@myhostname.@mydomain.com>;party=calling;screen=yes;privacy=off
Contact: <sip:0255667000@192.168.5.5:5060>
Max-Forwards: 69
Content-Type: application/sdp
Content-Length: 227

v=0
o=CiscoSystemsCCM-SIP 337995 1 IN IP4 192.168.5.5
s=SIP Call
c=IN IP4 172.16.115.23
b=TIAS:8000
b=AS:8
t=0 0
m=audio 22640 RTP/AVP 18 101
a=rtpmap:18 G729/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-15

 

Working perfectly! 🙂

 

#dontcalltac

 

 

 

 

5 thoughts on “Lua Script Solves Call Forward Masking

  1. Hi
    Its veryusefull article.I need to remove following line on sdp/body messages.Can you please give me exaple.

    b=TIAS:8000
    b=AS:8

    Like

    1. Hi Kemal

      Thanks, I’m glad that this has helped you. Sure, I can assist. Please advise on:

      – In what SIP messages must this be applied? INVITE/200 OK/ACK/183 etc.
      – In what direction wrt CUCM? Inbound/Outbound/Both?

      SDP is a bit more tricky that SIP Headers, but this is still quite easy to do.

      – Jonathan

      Like

      1. Hi Jonathan

        Its a sip trunk,it should be aplly firs invite it’s a outbound direction calls.
        For above scnerio can I forward calling party number when call forwarded using with UCCX .

        In my scenerio;

        itsp-sip-cube–sip trunk- cucm- ctip rp- uccx- ip phone, call forwarding – pstn number.
        When I use above call flow calling party can not seen on the mobile phone.
        I can seen pilot nunber on the mobile phone how can see callling party number on the gsm mobile.

        on the sip trunk for calling party type selected: originater.

        Thanks

        Like

  2. Hi Jonathan,

    This was very informative. I have been looking at your blogs and they are awesome.

    I have been struggling to get one of the LUA scripts working but I couldn’t. There is a need to modify the RPID for all the incoming PSTN calls to a specific name “FROM PROVIDER” so that the enterprise phones could see the Calling Name as FROM PROVIDER and the Calling Number in the +E164 format. We have 2 vCUBE’s in HA mode using RG technique (Active/Standby). The LUA works just fine before the failover but as soon as the failover to the vCUBE2 happens and if there is an UPDATE request(HOLD/RESUME) from the PSTN side, the Calling Name remains unchanged, but the Calling number is sent as received by the vCUBE to the CUCM (10 digits instead of E164). And this way the user’s see the 10 digit calling number instead of +e164 number after the vCUBE failover.

    Here is my LUA

    M = {}
    function M.inbound_INVITE(msg)
    if msg:isInitialInviteRequest() or msg:isReInviteRequest() then
    local rpid = msg:getHeader(“Remote-Party-ID”)
    local temp = string.match(rpid, “(<.+)")
    local pstn = "FROM Provider"
    local final = pstn .. temp
    msg:modifyHeader("Remote-Party-ID", final)
    end
    end
    function M.inbound_UPDATE(msg)
    local rpid = msg:getHeader("Remote-Party-ID")
    local temp = string.match(rpid, "(<.+)")
    local pstn = "FROM Provider"
    local final = pstn .. temp
    msg:modifyHeader("Remote-Party-ID", final)
    end
    return M

    Thanks

    Liked by 1 person

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.