cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1362
Views
0
Helpful
3
Replies

Normalization Script - remove UPDATE in ALLOW header in INVITE

fei he
Level 2
Level 2

Hi 

 

I tried to remove UPDATE in ALLOW header in OUTBOUND INVITE (CM to carriage) after went through developer guide but could not really get it works after add below in my existing script. CM traces shows it did not remove UPDATE at all, any wrong with my script?

 

============

M = {}

 

function M.outbound_INVITE(msg)
msg:removeHeaderValue("Allow", "UPDATE")
end

 

local function getDisplayName (i_header)
local position_of_uri=string.find(i_header, "<")
if position_of_uri <= 2
then
display_name=nil
else
-- save display name which arrives in quotes
local display_name_tmp = string.sub(i_header,1, (position_of_uri - 1))
-- now remove the quotes
display_name_tmp = string.gsub(display_name_tmp,'"','')
-- now remove the space
display_name = string.gsub(display_name_tmp,' ','')
end
return display_name
end


local function splitURI(i_uri)
local position_of_semi = string.find(i_uri, ";")
local position_of_sip = string.find(i_uri, "sip")
if position_of_semi then
i_uri_id = string.sub(i_uri, position_of_sip, (position_of_semi -1 ))
r_uri_user = string.sub(i_uri, (position_of_semi + 1 ))
else
-- no user
i_uri_id = string.sub(i_uri, position_of_sip)
r_uri_user=nil
end

i_uri_id = string.gsub(i_uri_id,"sip:","")
local position_of_ampersand = string.find(i_uri_id, "@")
local r_uri_host=string.sub(i_uri_id,1,(position_of_ampersand-1))
local r_uri_domain=string.sub(i_uri_id,(position_of_ampersand+1))
local port_position = string.find(r_uri_domain, ":", 2)
if port_position then
r_uri_domain_port = string.sub(r_uri_domain,(port_position+1))
r_uri_domain = string.sub(r_uri_domain,1,(port_position-1))
else
-- no port
r_uri_domain_port = nil
end
return r_uri_host, r_uri_domain, r_uri_domain_port, r_uri_user
end

local function add_plus_to_string(uri_user)
-- check whether string begins with "+"
-- if it does, do nothing
-- if it does not, add plus

local firstchar=string.sub(uri_user, 1, 1)
local foundplus=string.match(firstchar, "+")

if foundplus then
newuri = uri_user

else
newuri='+' .. uri_user

end
return newuri, foundplus

end

local function remove_plus_from_string(uri_user)

-- check whether string begins with "+"
-- if it does not, do nothing
-- if it does, remove plus

local firstchar=string.sub(uri_user, 1, 1)
local foundplus=string.match(firstchar, "+")

if foundplus then
newuri = string.sub(uri_user, 2)

else
newuri= uri_user

end
return newuri

end


local function add_plus_to_from(msg)

local From_header = msg:getHeader("From")
local From_uri = msg:getUri("From")
local From_tag = msg:getHeaderValueParameter("From", "tag")
From_uri_host, From_uri_domain, From_uri_domain_port, From_uri_user=splitURI(From_uri)
displayname=getDisplayName(From_header)

From_uri_host, foundplus=add_plus_to_string(From_uri_host)

if foundplus then

else
local context = msg:getContext()
if context
then

context["foundplusonfrom"]="+"
end
end

-- Create new Header
if displayname == nil then
-- new_from_header = '"' .. From_uri_host .. '" <sip:' .. From_uri_host .. '@' .. From_uri_domain
new_from_header = '<sip:' .. From_uri_host .. '@' .. From_uri_domain
else
new_from_header = '"' .. displayname .. '" <sip:' .. From_uri_host .. '@' .. From_uri_domain
end

if From_uri_domain_port then
new_from_header = new_from_header .. ':' .. From_uri_domain_port
end
if From_uri_user then
new_from_header = new_from_header .. ';' .. From_uri_user
end
new_from_header = new_from_header .. '>'
if From_tag then
new_from_header = new_from_header .. ';tag=' .. From_tag
end

msg:modifyHeader("From", new_from_header)

end


local function add_plus_to_paid(msg)

local Pai_header = msg:getHeader("P-Asserted-Identity")
local Pai_uri = msg:getUri("P-Asserted-Identity")
Pai_uri_host, Pai_uri_domain, Pai_uri_domain_port, Pai_uri_user=splitURI(Pai_uri)
displayname=getDisplayName(Pai_header)

Pai_uri_host, foundplus=add_plus_to_string(Pai_uri_host)


-- Create new Header
if displayname == nil then
-- new_from_header = '"' .. From_uri_host .. '" <sip:' .. From_uri_host .. '@' .. From_uri_domain
new_pai_header = '<sip:' .. Pai_uri_host .. '@' .. Pai_uri_domain
else
new_pai_header = '"' .. displayname .. '" <sip:' .. Pai_uri_host .. '@' .. Pai_uri_domain
end

if Pai_uri_domain_port then
new_pai_header = new_pai_header .. ':' .. Pai_uri_domain_port
end
if Pai_uri_user then
new_pai_header = new_pai_header .. ';' .. Pai_uri_user
end
new_pai_header = new_pai_header .. '>'


msg:modifyHeader("P-Asserted-Identity", new_pai_header)

end


local function add_plus_to_diversion(msg)

local Diversion_header = msg:getHeader("Diversion")
local position_of_sip = string.find(Diversion_header, "sip:")
if position_of_sip
then
first_chunk = string.sub(Diversion_header, 1, (position_of_sip + 3))
second_chunk = string.sub(Diversion_header, (position_of_sip +4))
firstchar = string.sub(second_chunk, 1, 1)
local foundplus=string.match(firstchar, "+")
if foundplus then

else
Diversion_header = first_chunk .. "+" .. second_chunk
msg:modifyHeader("Diversion", Diversion_header)
end

end
end

local function add_plus_to_rpid(msg)

local Rpid_header = msg:getHeader("Remote-Party-ID")
local position_of_sip = string.find(Rpid_header, "sip:")
if position_of_sip
then
first_chunk = string.sub(Rpid_header, 1, (position_of_sip + 3))
second_chunk = string.sub(Rpid_header, (position_of_sip +4))
firstchar = string.sub(second_chunk, 1, 1)
local foundplus=string.match(firstchar, "+")
if foundplus then

else
Rpid_header = first_chunk .. "+" .. second_chunk
msg:modifyHeader("Remote-Party-ID", Rpid_header)
end

end
end


local function remove_plus_from_from(msg)

local context = msg:getContext()
if context
then
local foundplus = context["foundplusonfrom"]
if foundplus then

local From_header = msg:getHeader("From")
local From_uri = msg:getUri("From")
local From_tag = msg:getHeaderValueParameter("From", "tag")
From_uri_host, From_uri_domain, From_uri_domain_port, From_uri_user=splitURI(From_uri)
displayname=getDisplayName(From_header)

From_uri_host=remove_plus_from_string(From_uri_host)

-- Create new Header
if displayname == nil then
-- new_from_header = '"' .. From_uri_host .. '" <sip:' .. From_uri_host .. '@' .. From_uri_domain
new_from_header = '<sip:' .. From_uri_host .. '@' .. From_uri_domain
else
new_from_header = '"' .. displayname .. '" <sip:' .. From_uri_host .. '@' .. From_uri_domain
end

if From_uri_domain_port then
new_from_header = new_from_header .. ':' .. From_uri_domain_port
end
if From_uri_user then
new_from_header = new_from_header .. ';' .. From_uri_user
end
new_from_header = new_from_header .. '>'
if From_tag then
new_from_header = new_from_header .. ';tag=' .. From_tag
end

msg:modifyHeader("From", new_from_header)
end
end
end

 

 

local function handle_outbound_message(msg)

-- need to check outbound from, PAID, diversion and RPID headers for username beginning in "+"
-- if "+" is found do nothing
-- if "+" is not found, add "+" to the username and create tag to record that this header has been modified


-- save From
local From_header = msg:getHeader("From")

if From_header
then
add_plus_to_from(msg)
end

local Pai_header = msg:getHeader("P-Asserted-Identity")

if Pai_header
then
add_plus_to_paid(msg)
end

local Diversion_header = msg:getHeader("Diversion")

if Diversion_header
then
add_plus_to_diversion(msg)
end

local Rpid_header = msg:getHeader("Remote-Party-ID")

if Rpid_header
then
add_plus_to_rpid(msg)
end

end


local function handle_inbound_message(msg)

-- for header (from, PAID, diversion, RPID) check tag to find whether it was modified in the outbound INVITE
-- if it was not modified do nothing
-- if it was modified, then remove then check for "+" on the

-- save From
local From_header = msg:getHeader("From")

if From_header
then
remove_plus_from_from(msg)

end

end

 

-- Request messages <direction>_<method>
-- M.inbound_INVITE = handle_inbound_invite
-- M.inbound_18X_INVITE = handle_inbound_18x
-- M.inbound_ACK = handle_inbound_ack
-- M.inbound_UPDATE = handle_inbound_message
-- M.inbound_200_INVITE = handle_inbound_message
-- M.outbound_200_INVITE = add_term_ioi
-- M.outbound_INVITE = handle_outbound_invite
M.outbound_INVITE = handle_outbound_message

-- M.outbound_18X_INVITE = handle_outbound_18x
-- M.outbound_ANY_INVITE = handle_outbound_any_invite
-- M.inbound_ANY_INVITE = handle_inbound_any_invite
M.inbound_ANY_INVITE = handle_inbound_message

-- M.outbound_ACK = handle_outbound_ack
M.outbound_ACK = handle_outbound_message

-- M.inbound_UPDATE = retrieveSNRContext
-- Response messages <direction>_<response-code>_<method>
-- M.inbound_200_INVITE = retrieveSNRContext
-- Response messages <direction>_<response-code>_<method>
-- M.outbound_ANY_INVITE = retrieveSNRBackwardContext
-- M.outbound_CANCEL = retrieveSNRForwardContext
M.outbound_CANCEL = handle_outbound_message

-- M.outbound_UPDATE = handle_outbound_update
-- M.outbound_BYE = retrieveSNRForwardContext
-- Request messages <direction>_<method>
M.outbound_BYE = handle_outbound_message
M.inbound_BYE = handle_inbound_message
M = {}

local function getDisplayName (i_header)
local position_of_uri=string.find(i_header, "<")
if position_of_uri <= 2
then
display_name=nil
else
-- save display name which arrives in quotes
local display_name_tmp = string.sub(i_header,1, (position_of_uri - 1))
-- now remove the quotes
display_name_tmp = string.gsub(display_name_tmp,'"','')
-- now remove the space
display_name = string.gsub(display_name_tmp,' ','')
end
return display_name
end


local function splitURI(i_uri)
local position_of_semi = string.find(i_uri, ";")
local position_of_sip = string.find(i_uri, "sip")
if position_of_semi then
i_uri_id = string.sub(i_uri, position_of_sip, (position_of_semi -1 ))
r_uri_user = string.sub(i_uri, (position_of_semi + 1 ))
else
-- no user
i_uri_id = string.sub(i_uri, position_of_sip)
r_uri_user=nil
end

i_uri_id = string.gsub(i_uri_id,"sip:","")
local position_of_ampersand = string.find(i_uri_id, "@")
local r_uri_host=string.sub(i_uri_id,1,(position_of_ampersand-1))
local r_uri_domain=string.sub(i_uri_id,(position_of_ampersand+1))
local port_position = string.find(r_uri_domain, ":", 2)
if port_position then
r_uri_domain_port = string.sub(r_uri_domain,(port_position+1))
r_uri_domain = string.sub(r_uri_domain,1,(port_position-1))
else
-- no port
r_uri_domain_port = nil
end
return r_uri_host, r_uri_domain, r_uri_domain_port, r_uri_user
end

local function add_plus_to_string(uri_user)
-- check whether string begins with "+"
-- if it does, do nothing
-- if it does not, add plus

local firstchar=string.sub(uri_user, 1, 1)
local foundplus=string.match(firstchar, "+")

if foundplus then
newuri = uri_user

else
newuri='+' .. uri_user

end
return newuri, foundplus

end

local function remove_plus_from_string(uri_user)

-- check whether string begins with "+"
-- if it does not, do nothing
-- if it does, remove plus

local firstchar=string.sub(uri_user, 1, 1)
local foundplus=string.match(firstchar, "+")

if foundplus then
newuri = string.sub(uri_user, 2)

else
newuri= uri_user

end
return newuri

end


local function add_plus_to_from(msg)

local From_header = msg:getHeader("From")
local From_uri = msg:getUri("From")
local From_tag = msg:getHeaderValueParameter("From", "tag")
From_uri_host, From_uri_domain, From_uri_domain_port, From_uri_user=splitURI(From_uri)
displayname=getDisplayName(From_header)

From_uri_host, foundplus=add_plus_to_string(From_uri_host)

if foundplus then

else
local context = msg:getContext()
if context
then

context["foundplusonfrom"]="+"
end
end

-- Create new Header
if displayname == nil then
-- new_from_header = '"' .. From_uri_host .. '" <sip:' .. From_uri_host .. '@' .. From_uri_domain
new_from_header = '<sip:' .. From_uri_host .. '@' .. From_uri_domain
else
new_from_header = '"' .. displayname .. '" <sip:' .. From_uri_host .. '@' .. From_uri_domain
end

if From_uri_domain_port then
new_from_header = new_from_header .. ':' .. From_uri_domain_port
end
if From_uri_user then
new_from_header = new_from_header .. ';' .. From_uri_user
end
new_from_header = new_from_header .. '>'
if From_tag then
new_from_header = new_from_header .. ';tag=' .. From_tag
end

msg:modifyHeader("From", new_from_header)

end


local function add_plus_to_paid(msg)

local Pai_header = msg:getHeader("P-Asserted-Identity")
local Pai_uri = msg:getUri("P-Asserted-Identity")
Pai_uri_host, Pai_uri_domain, Pai_uri_domain_port, Pai_uri_user=splitURI(Pai_uri)
displayname=getDisplayName(Pai_header)

Pai_uri_host, foundplus=add_plus_to_string(Pai_uri_host)


-- Create new Header
if displayname == nil then
-- new_from_header = '"' .. From_uri_host .. '" <sip:' .. From_uri_host .. '@' .. From_uri_domain
new_pai_header = '<sip:' .. Pai_uri_host .. '@' .. Pai_uri_domain
else
new_pai_header = '"' .. displayname .. '" <sip:' .. Pai_uri_host .. '@' .. Pai_uri_domain
end

if Pai_uri_domain_port then
new_pai_header = new_pai_header .. ':' .. Pai_uri_domain_port
end
if Pai_uri_user then
new_pai_header = new_pai_header .. ';' .. Pai_uri_user
end
new_pai_header = new_pai_header .. '>'


msg:modifyHeader("P-Asserted-Identity", new_pai_header)

end


local function add_plus_to_diversion(msg)

local Diversion_header = msg:getHeader("Diversion")
local position_of_sip = string.find(Diversion_header, "sip:")
if position_of_sip
then
first_chunk = string.sub(Diversion_header, 1, (position_of_sip + 3))
second_chunk = string.sub(Diversion_header, (position_of_sip +4))
firstchar = string.sub(second_chunk, 1, 1)
local foundplus=string.match(firstchar, "+")
if foundplus then

else
Diversion_header = first_chunk .. "+" .. second_chunk
msg:modifyHeader("Diversion", Diversion_header)
end

end
end

local function add_plus_to_rpid(msg)

local Rpid_header = msg:getHeader("Remote-Party-ID")
local position_of_sip = string.find(Rpid_header, "sip:")
if position_of_sip
then
first_chunk = string.sub(Rpid_header, 1, (position_of_sip + 3))
second_chunk = string.sub(Rpid_header, (position_of_sip +4))
firstchar = string.sub(second_chunk, 1, 1)
local foundplus=string.match(firstchar, "+")
if foundplus then

else
Rpid_header = first_chunk .. "+" .. second_chunk
msg:modifyHeader("Remote-Party-ID", Rpid_header)
end

end
end


local function remove_plus_from_from(msg)

local context = msg:getContext()
if context
then
local foundplus = context["foundplusonfrom"]
if foundplus then

local From_header = msg:getHeader("From")
local From_uri = msg:getUri("From")
local From_tag = msg:getHeaderValueParameter("From", "tag")
From_uri_host, From_uri_domain, From_uri_domain_port, From_uri_user=splitURI(From_uri)
displayname=getDisplayName(From_header)

From_uri_host=remove_plus_from_string(From_uri_host)

-- Create new Header
if displayname == nil then
-- new_from_header = '"' .. From_uri_host .. '" <sip:' .. From_uri_host .. '@' .. From_uri_domain
new_from_header = '<sip:' .. From_uri_host .. '@' .. From_uri_domain
else
new_from_header = '"' .. displayname ..'" <sip:' .. From_uri_host .. '@' .. From_uri_domain
end

if From_uri_domain_port then
new_from_header = new_from_header .. ':' .. From_uri_domain_port
end
if From_uri_user then
new_from_header = new_from_header .. ';' .. From_uri_user
end
new_from_header = new_from_header .. '>'
if From_tag then
new_from_header = new_from_header .. ';tag=' .. From_tag
end

msg:modifyHeader("From", new_from_header)
end
end
end

 

 

local function handle_outbound_message(msg)

-- need to check outbound from, PAID, diversion and RPID headers for username beginning in "+"
-- if "+" is found do nothing
-- if "+" is not found, add "+" to the username and create tag to record that this header has been modified


-- save From
local From_header = msg:getHeader("From")

if From_header
then
add_plus_to_from(msg)
end

local Pai_header = msg:getHeader("P-Asserted-Identity")

if Pai_header
then
add_plus_to_paid(msg)
end

local Diversion_header = msg:getHeader("Diversion")

if Diversion_header
then
add_plus_to_diversion(msg)
end

local Rpid_header = msg:getHeader("Remote-Party-ID")

if Rpid_header
then
add_plus_to_rpid(msg)
end

end


local function handle_inbound_message(msg)

-- for header (from, PAID, diversion, RPID) check tag to find whether it was modified in the outbound INVITE
-- if it was not modified do nothing
-- if it was modified, then remove then check for "+" on the

-- save From
local From_header = msg:getHeader("From")

if From_header
then
remove_plus_from_from(msg)

end

end

 

-- Request messages <direction>_<method>
-- M.inbound_INVITE = handle_inbound_invite
-- M.inbound_18X_INVITE = handle_inbound_18x
-- M.inbound_ACK = handle_inbound_ack
-- M.inbound_UPDATE = handle_inbound_message
-- M.inbound_200_INVITE = handle_inbound_message
-- M.outbound_200_INVITE = add_term_ioi
-- M.outbound_INVITE = handle_outbound_invite
M.outbound_INVITE = handle_outbound_message

-- M.outbound_18X_INVITE = handle_outbound_18x
-- M.outbound_ANY_INVITE = handle_outbound_any_invite
-- M.inbound_ANY_INVITE = handle_inbound_any_invite
M.inbound_ANY_INVITE = handle_inbound_message

-- M.outbound_ACK = handle_outbound_ack
M.outbound_ACK = handle_outbound_message

-- M.inbound_UPDATE = retrieveSNRContext
-- Response messages <direction>_<response-code>_<method>
-- M.inbound_200_INVITE = retrieveSNRContext
-- Response messages <direction>_<response-code>_<method>
-- M.outbound_ANY_INVITE = retrieveSNRBackwardContext
-- M.outbound_CANCEL = retrieveSNRForwardContext
M.outbound_CANCEL = handle_outbound_message

-- M.outbound_UPDATE = handle_outbound_update
-- M.outbound_BYE = retrieveSNRForwardContext
-- Request messages <direction>_<method>
M.outbound_BYE = handle_outbound_message
M.inbound_BYE = handle_inbound_message
M.outbound_UPDATE = handle_outbound_message
M.inbound_UPDATE = handle_inbound_message
M.inbound_100_ANY = handle_inbound_message

M.inbound_UPDATE = handle_inbound_message
M.inbound_100_ANY = handle_inbound_message

 

return M

============

 

3 Replies 3

fei he
Level 2
Level 2
And CM traces output:

//Pre Normalized Message//
INVITE sip:+61XXXXXXXXX@x.x.x.x:5060 SIP/2.0
Via: SIP/2.0/TCP x.x.x.x:5060;branch=z9hG4bK1ee735fcb88ea
From: < sip:+61XXXXXXXXX@x.x.x.x> ;tag=246599~e4f81d89-c8d9-4cdb-8808-27ab08bbd6ae-69271786
To: < sip:+61XXXXXXXXX@y.y.y.y>
Date: Mon, 28 Aug 2017 07:00:29 GMT
Call-ID: 921daa00-9a31bf8d-1edb2-601a8c0@x.x.x.x
Supported: 100rel,timer,resource-priority,replaces
Min-SE: 7200
User-Agent: Cisco-CUCM10.5
Allow: INVITE, OPTIONS, INFO, BYE, CANCEL, ACK, PRACK, UPDATE, REFER, SUBSCRIBE, NOTIFY

//Post Normalized Message: //
INVITE sip:+61XXXXXXXXX@y.y.y.y:5060 SIP/2.0
Via: SIP/2.0/TCP x.x.x.x:5060;branch=z9hG4bK1ee735fcb88ea
From: < sip:+61XXXXXXXXX@x.x.x.x> ;tag=246599~e4f81d89-c8d9-4cdb-8808-27ab08bbd6ae-69271786
To: < sip:+61XXXXXXXXX@y.y.y.y>
Date: Mon, 28 Aug 2017 07:00:29 GMT
Call-ID: 921daa00-9a31bf8d-1edb2-601a8c0@x.x.x.x
Supported: 100rel,timer,resource-priority,replaces
Min-SE: 7200
User-Agent: Cisco-CUCM10.5
Allow: INVITE, OPTIONS, INFO, BYE, CANCEL, ACK, PRACK, UPDATE, REFER, SUBSCRIBE, NOTIFY

Anthony Holloway
Cisco Employee
Cisco Employee

Is there some reason your script is so long?  I can't really follow it, and I'm pretty sure it could either be a lot smaller to achieve what you want, or you can just post the relevant section.

 

Also, if you're doing it to the carrier, you might want to do it on the CUBE.  It would be as easy as this:

 

voice class sip-profiles 1
 request INVITE sip-header Allow-Header modify ", UPDATE" ""
 request INVITE sip-header Allow-Header modify "UPDATE, " ""
 request INVITE sip-header Allow-Header modify "UPDATE" "INVITE"
!

 

Then apply that profile to your dial-peer(s) facing the ITSP:

voice-class sip profiles 1

Anthony

The script was provided by Cisco intially after they build the solution for us and we have been used that script since. I am not a script guy hence does not understand why the script is so long.

 

As i mentioned, I have tried to add that function into current script but seems it does not remove UPDATE in ALLOW but when I only run this section the UPDATE is removed as desire. I reckon the script itself somehow is messed up but i cannot tell. Neither Cisco is supporting SIP Norm script.

 

Anyway, in our solution, we are using CUBE-SP instead of CUBE hence could not use what you suggest.