cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
3082
Views
0
Helpful
2
Replies
Highlighted
Contributor

Modify Request-URI, To Header, From Header and Contact Header

Hello all,

first of all i am very new to SIP Normalization Scripts and LUA programming.

I am trying to modify the Request-URI, To Header, From Header and Contact Header with a SIP Normalization script. But, without much luck so far and i am looking for any help or tip to get my script working.

I am able to modify the Requst-URI and the To Header one at a time but not all of them in one go.

This is my script which i am working on:

M = {}

trace.enable()

function M.inbound_INVITE(msg)

    local mwi_number_length = scriptParameters.getValue("MWI-Number-Length") -- MWI-Number-Length equals to 7

  

-- Get the SIP Request

  

    local method, ruri, version = msg:getRequestLine()  

  

-- Adjust SIP Request URI

    -- Get the first digits of the Request URI to form the new Called number

  

        local dnis_start = string.find(ruri, "%d")

        local dnis_end = dnis_start+mwi_number_length-1

        local dnis = string.sub(ruri, dnis_start, dnis_end)

  

    -- Get the Right Hand Side from the Request URI

  

        local host_start = string.find(ruri, "\@")

        local host_end = string.find(ruri, "%d", -1)

        local host = string.sub(ruri, host_start, host_end)

        local ruri_new = "sip:" .. dnis .. host

  

    -- Tracing

        trace.format("New DNIS is %s:%s", dnis)

        trace.format("New RURI is %s:%s", ruri_new)

  

    -- Set the new Request URI

  

        msg:setRequestUri(ruri_new)

      

      

end

trace.enable()

function M.inbound_INVITE(msg)

    local mwi_number_length = scriptParameters.getValue("MWI-Number-Length")  

          

-- Adjust SIP To Header

    -- Get the SIP To Header

  

        local to = msg:getHeader("To")

  

    -- Extract DNIS from SIP To Header

      

        local to_dnis_start = string.find(to, "%d")

        local to_dnis_end = to_dnis_start+mwi_number_length-1

        local to_dnis_new = string.sub(to, to_dnis_start, to_dnis_end)

    -- Extract RHS from SIP To Header

  

        local to_rhs = string.match(to, "\@.*")

    -- Create New SIP To Header

  

        local to_new = "<sip:" .. to_dnis_new .. to_rhs

      

    -- Tracing

        trace.format("New To DNIS is %s:%s", to_dnis_new)

        trace.format("New To Header is %s:%s", to_new)      

          

    -- Set the new SIP To Header

      

        msg:modifyHeader("To", to_new)

      

end

return M

This is the original SIP Invite:

INVITE sip:91249874955566677788@10.123.255.145:5060;transport=udp SIP/2.0

Via: SIP/2.0/UDP 10.122.185.5:5060;branch=z9hG4bKQv9ZAYlmXawDkxKenpWp9g~~21

Max-Forwards: 70

To: <sip:91249874955566677788@10.123.255.145:5060>

From: <sip:9124980@10.122.185.5>;tag=ds2f74cdd

Call-ID: 143871259893526@10.122.185.5

CSeq: 100 INVITE

Content-Length: 190

Contact: <sip:9124980@10.122.185.5:5060;transport=udp>

Content-Type: application/sdp

Allow: INVITE, BYE, CANCEL, ACK, REFER, SUBSCRIBE, NOTIFY, INFO

Cisco-Gcid: F9F41197-014E-1000-5EE4-BDDE9F0E867D

The modified SIP Invite should look like this:

INVITE sip:9124987@10.123.255.145:5060;transport=udp SIP/2.0

Via: SIP/2.0/UDP 10.122.185.5:5060;branch=z9hG4bKQv9ZAYlmXawDkxKenpWp9g~~21

Max-Forwards: 70

To: <sip:9124987@10.123.255.145:5060>

From: <sip:4955566677788@10.122.185.5>;tag=ds2f74cdd

Call-ID: 143871259893526@10.122.185.5

CSeq: 100 INVITE

Content-Length: 190

Contact: <sip:4955566677788@10.122.185.5:5060;transport=udp>

Content-Type: application/sdp

Allow: INVITE, BYE, CANCEL, ACK, REFER, SUBSCRIBE, NOTIFY, INFO

Cisco-Gcid: F9F41197-014E-1000-5EE4-BDDE9F0E867D

Any help or tip is welcome!

/Robert

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Cisco Employee

The script only gets invoked once per message. This is different from how CUBE handles a list of Normalization rules.

What you have to do is combine both 'functions' in a single M.inbound_INVITE() procedure. You can probably reuse a lot of code since you are trying to set the Request URI and TO headers to essentially the same thing.

Mark

View solution in original post

2 REPLIES 2
Highlighted
Cisco Employee

The script only gets invoked once per message. This is different from how CUBE handles a list of Normalization rules.

What you have to do is combine both 'functions' in a single M.inbound_INVITE() procedure. You can probably reuse a lot of code since you are trying to set the Request URI and TO headers to essentially the same thing.

Mark

View solution in original post

Highlighted

Hi mark,

I got my script working now that i am using only one 'function'. Thanks for your help!

Here is my working script:

M = {}

function M.inbound_INVITE(msg)

-- Get the MWI On/Off Numbers as Parameters from the CUCM Trunk Page

    mwiOnNumber = scriptParameters.getValue("MWI-On-Number")

    mwiOffNumberNumber = scriptParameters.getValue("MWI-Off-Number")

   

-- Get the SIP Request-URI

   

    method, ruri, version = msg:getRequestLine()   

   

-- SIP Request-URI: INVITE sip:91249884955566677788@10.123.255.145:5060 SIP/2.0

        -- Extract the Protocol Handler from SIP Request-URI

        ruriProtHandlerStart = string.find(ruri, "%a")

        ruriProtHandlerEnd = string.find(ruri,"\:")

        -- Set the Protocol Handler from SIP Request-URI

        ruriProtHandler = string.sub(ruri, ruriProtHandlerStart, ruriProtHandlerEnd)

   

-- Extract DNIS from SIP Request-URI

        dnisStart = string.find(ruri, "%d")

        mwiNumberLength = string.len(mwiOnNumber)

        dnisEnd = dnisStart+mwiNumberLength-1

        -- Set the New DNIS

        dnis = string.sub(ruri, dnisStart, dnisEnd)

-- Extract ANI from SIP Request-URI

        aniStart = dnisStart+mwiNumberLength

        aniEnd = string.find(ruri, "\@")

        ani = string.sub(ruri, aniStart, aniEnd-1)

        -- Set the New ANI

        aniNew = "\+" .. ani

       

-- Extract the Right Hand Side from SIP Request-URI

        ruriRhs = string.match(ruri, "\@.*")

        -- Set the new Right Hand Side from SIP Request-URI

        ruriNew = ruriProtHandler .. dnis .. ruriRhs

   

--  Get the SIP To Header: To: <sip:91249884955566677788@10.123.255.145>

    to = msg:getHeader("To")

        -- Extract the Protocol Handler from SIP To Header

        toProtHandlerStart = string.find(to, "\<")

        toProtHandlerEnd = string.find(to, "\:")

        toProtHandler = string.sub(to, toProtHandlerStart, toProtHandlerEnd)

        -- Extract the Right Hand Side from SIP To Header

        toRhsTag = string.match(to, "\@.*")

        -- Set the new SIP To Header

        toNew = toProtHandler .. dnis .. ruriRhs .. ">"

   

-- Get the SIP From Header: From: <sip:9124980@10.122.185.4>;tag=D6F709E8-144

    from = msg:getHeader("From")

        -- Extract the Protocol Handler from SIP From Header

        fromProtHandlerStart = string.find(from, "\<")

        fromProtHandlerEnd = string.find(from, "\:")

        fromProtHandler = string.sub(from, fromProtHandlerStart, fromProtHandlerEnd)

        -- Extract the Right Hand Side from SIP From Header

        fromRhsTag = string.match(from, "\@.*")

        -- Set the new SIP From Header

        fromNew =  fromProtHandler .. aniNew .. fromRhsTag

       

   

-- Get the SIP Contact Header: Contact: <sip:9124980@10.122.185.4:5060>

    contact = msg:getHeader("Contact")

        -- Extract the Protocol Handler from SIP Contact Header

        contProtHandlerStart = string.find(contact, "\<")

        contProtHandlerEnd = string.find(contact, "\:")

        contProtHandler = string.sub(contact, contProtHandlerStart, contProtHandlerEnd)

        -- Extract the Right Hand Side from SIP Contact Header

        contRhsTrans = string.match(contact, "\@.*")

        -- Set the new SIP Contact Header

        contNew =  contProtHandler .. aniNew .. contRhsTrans

-- Get the SIP Remote-Party-ID Header: Remote-Party-ID: <sip:9124980@10.122.185.4>;party=calling;screen=no;privacy=off

-- Only needed when the CUE is connecteed to CUCM through an IP-IP Gateway

    rpid = msg:getHeader("Remote-Party-ID")

        -- Extract the Protocol Handler from SIP Remote-Party-ID Header

        rpidProtHandlerStart = string.find(rpid, "\<")

        rpidProtHandlerEnd = string.find(rpid, "\:")

        rpidProtHandler = string.sub(rpid, contProtHandlerStart, contProtHandlerEnd)

        -- Extract the Right Hand Side from SIP Remote-Party-ID Header

        rpidRhsTrans = string.match(rpid, "\@.*")

        -- Set the new SIP Remote-Party-ID Header

        rpidNew =  rpidProtHandler .. aniNew .. rpidRhsTrans

-- Check if the DNIS equals to the MWI On or Off Number

if dnis == mwiOnNumber then

    -- If DNIS equals to the MWI On Number then return the new Headers

    msg:setRequestUri(ruriNew) -- Return New Request URI

    msg:modifyHeader("To", toNew) -- Return New To Header

    msg:modifyHeader("From", fromNew) -- Return New From Header

    msg:modifyHeader("Contact", contNew) -- Return New Contact Header

    msg:modifyHeader("Remote-Party-ID", rpidNew) -- Return New Remote-Party-ID Header, Only needed when the CUE is connecteed to CUCM through an IP-IP Gateway

elseif dnis == mwiOffNumberNumber then

    -- If DNIS equals to the MWI Off Number then return the new Headers

    msg:setRequestUri(ruriNew) -- Return New Request URI

    msg:modifyHeader("To", toNew) -- Return New To Header

    msg:modifyHeader("From", fromNew) -- Return New From Header

    msg:modifyHeader("Contact", contNew) -- Return New Contact Header

    msg:modifyHeader("Remote-Party-ID", rpidNew) -- Return New Remote-Party-ID Header, Only needed when the CUE is connecteed to CUCM through an IP-IP Gateway

else

    -- If DNIS is something other than the MWI Numbers leave the Headers as is

end

end

return M

This widget could not be displayed.