Skip to content

Commit

Permalink
Address outbound/inbound BYE
Browse files Browse the repository at this point in the history
  • Loading branch information
Karn Saheb committed Jul 10, 2020
1 parent d25e39e commit 2d0a579
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 35 deletions.
80 changes: 47 additions & 33 deletions lib/sippy_cup/scenario.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Scenario
MSEC = 1_000
DEFAULT_RETRANS = 500

SIP_URI_HEADER_MATCH = '.*<?sip:(.*)>?.*;tag=([^;]*)'

#
# Build a scenario based on either a manifest string or a file handle. Manifests are supplied in YAML format.
# All manifest keys can be overridden by passing in a Hash of corresponding values.
Expand Down Expand Up @@ -297,17 +299,31 @@ def receive_invite(opts = { compact_header: false })
recv(opts.merge(request: 'INVITE', rrs: true)) do |recv|
action = doc.create_element('action') do |action|
action << doc.create_element('ereg') do |ereg|
ereg['regexp'] = '.*<sip:(.*)>.*;tag=([^;]*)'
ereg['regexp'] = SIP_URI_HEADER_MATCH
ereg['search_in'] = 'hdr'
ereg['header'] = 'f:'
ereg['assign_to'] = 'dummy,remote_addr,remote_tag'
end
action << doc.create_element('ereg') do |ereg|
ereg['regexp'] = SIP_URI_HEADER_MATCH
ereg['search_in'] = 'hdr'
ereg['header'] = opts[:compact_header] ? 'f:' : 'From:'
ereg['header'] = 'From:'
ereg['assign_to'] = 'dummy,remote_addr,remote_tag'
end

action << doc.create_element('ereg') do |ereg|
ereg['regexp'] = '<sip:(.*)>'
ereg['regexp'] = '<?sip:(.*)>?'
ereg['search_in'] = 'hdr'
ereg['header'] = opts[:compact_header] ? 't:' : 'To:'
ereg['header'] = 't:'
ereg['assign_to'] = 'dummy,local_addr'
end
action << doc.create_element('ereg') do |ereg|
ereg['regexp'] = '<?sip:(.*)>?'
ereg['search_in'] = 'hdr'
ereg['header'] = 'To:'
ereg['assign_to'] = 'dummy,local_addr'
end

action << doc.create_element('assignstr') do |assignstr|
assignstr['assign_to'] = "call_addr"
assignstr['value'] = "[$local_addr]"
Expand Down Expand Up @@ -507,18 +523,39 @@ def receive_answer(opts = { compact_header: false })
rtd: true # Response Time Duration: Record the response time
}

@direction = "outbound"

receive_200(options.merge(opts)) do |recv|
recv << doc.create_element('action') do |action|
action << doc.create_element('ereg') do |ereg|
ereg['regexp'] = '.*<sip:(.*)>.*;tag=([^;]*)'
ereg['regexp'] = SIP_URI_HEADER_MATCH
ereg['search_in'] = 'hdr'
ereg['header'] = 't:'
ereg['assign_to'] = 'dummy,remote_addr,remote_tag'
end
action << doc.create_element('ereg') do |ereg|
ereg['regexp'] = SIP_URI_HEADER_MATCH
ereg['search_in'] = 'hdr'
ereg['header'] = opts[:compact_header] ? 'f:' : 'From:'
ereg['header'] = 'To:'
ereg['assign_to'] = 'dummy,remote_addr,remote_tag'
end

action << doc.create_element('ereg') do |ereg|
ereg['regexp'] = SIP_URI_HEADER_MATCH
ereg['search_in'] = 'hdr'
ereg['header'] = 'f:'
ereg['assign_to'] = 'dummy,local_addr,local_tag'
end
action << doc.create_element('ereg') do |ereg|
ereg['regexp'] = SIP_URI_HEADER_MATCH
ereg['search_in'] = 'hdr'
ereg['header'] = 'From:'
ereg['assign_to'] = 'dummy,local_addr,local_tag'
end
end
end
# These variables will only be used if we initiate a hangup
@reference_variables += %w(dummy remote_addr remote_tag)
@reference_variables += %w(dummy remote_addr remote_tag local_addr local_tag)
end

#
Expand Down Expand Up @@ -796,10 +833,10 @@ def send_bye(opts = {})
msg = <<-MSG
BYE [next_url] SIP/2.0
Via: SIP/2.0/[transport] #{@adv_ip}:[local_port];rport;branch=[branch]
[last_Via:]
[routes]
To: "#{@from_user}" <sip:[$remote_addr]>;tag=[$remote_tag]
From: "#{@to_user}" <sip:#{@to_user}@stage.tncp.textnow.com>;tag=[call_number]
#{@direction == "outbound" ? "To:" : "From:" } sip:[$remote_addr];tag=[$remote_tag]
#{@direction == "outbound" ? "From:" : "To:" } sip:[$local_addr];tag=[call_number]
[last_Call-ID:]
CSeq: [cseq] BYE
Max-Forwards: 100
Expand All @@ -810,29 +847,6 @@ def send_bye(opts = {})
send msg, opts
end

#
# Send a BYE message using destination of previous messages Contact Header
#
# @param [Hash] opts A set of options to modify the message parameters
#
def send_bye_using_contact(opts = {})
msg = <<-MSG
BYE [next_url] SIP/2.0
Via: SIP/2.0/[transport] #{@adv_ip}:[local_port];rport;branch=[branch]
[routes]
To: "#{@from_user}" <sip:[$remote_addr]>;tag=[$remote_tag]
From: "#{@to_user}" <sip:#{@to_user}@stage.tncp.textnow.com>;tag=[call_number]
[last_Call-ID:]
Contact: <sip:#{@adv_ip};transport=[transport]>
Max-Forwards: 100
CSeq: [cseq] BYE
User-Agent: #{USER_AGENT}
Content-Length: 0
MSG
send msg, opts
end

#
# Expect to receive a BYE message
#
Expand Down
4 changes: 2 additions & 2 deletions spec/sippy_cup/scenario_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -503,9 +503,9 @@
subject.hangup opts
end

it 'calls send_by_using_contact and receive_ok when passed a use_contact option' do
it 'calls send_bye and receive_ok when passed a use_contact option' do
opts = { foo: 'bar', use_contact: true }
expect(subject).to receive(:send_bye_using_contact).with({ foo: 'bar' })
expect(subject).to receive(:send_bye).with({ foo: 'bar' })
expect(subject).to receive(:receive_ok).with({ foo: 'bar' })
subject.hangup opts
end
Expand Down

0 comments on commit 2d0a579

Please sign in to comment.