Using the code found here https://github.com/twisted/twisted/blob/trunk/src/twisted/test/test_sip.py
I have tried to send my local(127.0.0.1) asterisk server simple SIP options check.
From twisted.protocols import sip
From twisted.internet import defer, reactor
class Client(sip.Base):
def __init__(self):
sip.Base.__init__(self)
self.received = []
self.deferred = defer.Deferred()
def handle_response(self, response, addr):
self.received.append(response)
self.deferred.callback(self.received)
class OptionsC():
def setup(self):
self.client = Client()
self.clientPort = reactor.listenUDP(
5062, self.client, interface="127.0.1.1")
def testRegisterOPT(self):
p = self.clientPort.getHost().port
r = sip.Request("OPTIONS", "sip:127.0.0.1")
r.addHeader("via", sip.Via("127.0.1.1", port=5062, rport=5062, branch="test123").toString())
r.addHeader("to", "<sip:[email protected]>")
r.addHeader("From", "<sip:[email protected]>")
r.addHeader("Call-ID", "<[email protected]>")
r.addHeader("CSeq", "1 OPTIONS")
r.addHeader("contact", "<sip:[email protected]:5062;transport=UDP>")
r.addHeader("Accept", "application/sdp")
r.addHeader("Content-Length", "0")
print(r.toString())
self.client.sendMessage(
sip.URL(host="127.0.0.1", port=5060), r)
d = self.client.deferred
def check(received):
self.assertEqual(len(received), 1)
r = received[0]
print(r)
print(r.code)
print(dir(r))
self.assertEqual(r.code, 200)
d.addCallback(check)
return d
opc = OptionsC()
opc.setup()
res = opc.testRegisterOPT()
print("test")
This is what my environment looks like
$ pip freeze
constantly==15.1.0
incremental==16.10.1
Twisted==16.6.0
zope.interface==4.3.3
$ python -V
Python 2.7.5
On asterisk, I can see that the message arrives.
But asterisk never sends a response 200 ok, or error or anything else useful for me to understand what is wrong with my code.
*CLI> sip set debug on
SIP Debugging enabled
<--- SIP read from UDP:127.0.1.1:5062 --->
OPTIONS sip:127.0.0.1 SIP/2.0
Via: SIP/2.0/UDP 127.0.1.1:5062;branch=test123;rport=5062
To: <sip:[email protected]>
From: <sip:[email protected]>
Call-ID: <[email protected]>
CSeq: 1 OPTIONS
Contact: <sip:[email protected]:5062;transport=UDP>
Accept: application/sdp
Content-Length: 0
<------------->
--- (9 headers 0 lines) ---
Compare this with the following sipp command that does some thing very similar.
$ sipp -sf ./options.xml -m 1 -max_non_invite_retrans 1 127.0.0.1:5060
Resolving remote host '127.0.0.1'... Done.
$ cat ./options.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE scenario SYSTEM "sipp.dtd">
<scenario name="Options">
<send retrans="500" start_rtd="opt-timer">
<![CDATA[
OPTIONS sip:[remote_ip] SIP/2.0
Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
Max-Forwards: 70
To: <sip:[service]@[remote_ip]>
From: <sip:[service]@[remote_ip]>;tag=[call_number]
Call-ID: [call_id]
CSeq: [cseq] OPTIONS
Contact: <sip:[service]@[local_ip]:[local_port];transport=[transport]>
Accept: application/sdp
Content-Length: 0
]]>
</send>
<recv response="200" rrs="true" rtd="opt-timer"></recv>
<!-- definition of the response time repartition table (unit is ms) -->
<ResponseTimeRepartition value="10, 20, 30, 40, 50, 100, 150, 200"/>
<!-- definition of the call length repartition table (unit is ms) -->
<CallLengthRepartition value="10, 50, 100, 500, 1000, 5000, 10000"/>
</scenario>
Which asterisk correctly produces a response to
<--- SIP read from UDP:127.0.0.1:5061 --->
OPTIONS sip:127.0.0.1 SIP/2.0
Via: SIP/2.0/UDP 127.0.1.1:5061;branch=z9hG4bK-19903-1-0
Max-Forwards: 70
To: <sip:[email protected]>
From: <sip:[email protected]>;tag=1
Call-ID: [email protected]
CSeq: 1 OPTIONS
Contact: <sip:[email protected]:5061;transport=UDP>
Accept: application/sdp
Content-Length: 0
<------------->
--- (10 headers 0 lines) ---
Sending to 127.0.0.1:5061 (NAT)
Looking for s in public (domain 127.0.0.1)
<--- Transmitting (NAT) to 127.0.0.1:5061 --->
SIP/2.0 200 OK
Via: SIP/2.0/UDP 127.0.1.1:5061;branch=z9hG4bK-19903-1-0;received=127.0.0.1;rport=5061
From: <sip:[email protected]>;tag=1
To: <sip:[email protected]>;tag=as6e9328e8
Call-ID: [email protected]
CSeq: 1 OPTIONS
Server: Asterisk PBX 13.10.0~dfsg-1ubuntu2
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
Supported: replaces, timer
Contact: <sip:127.0.0.1:5060>
Accept: application/sdp
Content-Length: 0
<------------>
Scheduling destruction of SIP dialog '[email protected]' in 32000 ms (Method: OPTIONS)
Really destroying SIP dialog '[email protected]' Method: OPTIONS
What I can see is that SIP is doing some other work in the background to initialise a SIP dialog. I think that is what is missing in this example code above. As asterisk correctly starts Transmitting to a response to the client sipp is creating.
By removing r.addHeader("CSeq", "1 OPTIONS")
.
I am now getting my local asterisk to respond
def testRegisterOPT(self):
p = self.clientPort.getHost().port
r = sip.Request("OPTIONS", "sip:127.0.0.1")
r.addHeader("via", sip.Via("127.0.1.1", port=5062, rport=5062, branch="test123").toString())
r.addHeader("to", "<sip:[email protected]>")
r.addHeader("From", "<sip:[email protected]>")
r.addHeader("Call-ID", "<[email protected]>")
# r.addHeader("CSeq", "1 OPTIONS")
r.addHeader("contact", "<sip:[email protected]:5062;transport=UDP>")
r.addHeader("Accept", "application/sdp")
r.addHeader("Content-Length", "0")
print(r.toString())
self.client.sendMessage(
sip.URL(host="127.0.0.1", port=5060), r)
d = self.client.deferred
So I guess I need to build a way to work with the call sequence properly or just ignore it.
There is a warning that asterisk posts.
chan_sip.c:11681 copy_header: No field 'CSeq' present to copy
But at least my tests are now working
<--- SIP read from UDP:127.0.0.1:46947 --->
OPTIONS sip:127.0.0.1 SIP/2.0
Via: SIP/2.0/UDP 127.0.0.1:5062;rport=5062
Max-Forward: 3
To: <sip:[email protected]>
From: <sip:[email protected]>
Call-ID: <[email protected]>
Contact: <sip:[email protected];transport=UDP>
Accept: application/sdp
Content-Length: 0
<------------->
--- (9 headers 0 lines) ---
Sending to 127.0.0.1:5062 (no NAT)
[Jan 24 12:52:54] NOTICE[12752]: chan_sip.c:11681 copy_header: No field 'CSeq' present to copy
<--- Transmitting (no NAT) to 127.0.0.1:5062 --->
SIP/2.0 400 Bad Request
Via: SIP/2.0/UDP 127.0.0.1:5062;rport=5062;received=127.0.0.1
From: <sip:[email protected]>
To: <sip:[email protected]>;tag=as7d34b365
Call-ID: <[email protected]>
Server: Asterisk PBX 13.10.0~dfsg-1ubuntu2
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
Supported: replaces, timer
Content-Length: 0