I decided to write a short manual on how to replace end-call codes (they are hangupcause Q.931 ISDN) in Asterisk.
Sometimes we need to give the client a clear-back code, regardless of what came to us at the other end of the line. By default, at the specified extent (exten => s, n, HangUp), the asterisk sends 34 a release code (in the sip specification, this is the error code 503). The task comes down to the following - to catch the code that came from shoulder B, and to transfer shoulder A already what we need, according to our conditions. All this can fit in 2 contexts:
[outgoing] exten => _X.,1,Dial(<SIP|IAX2>/${EXTEN}@trunk,60,g) same => n,GoTo(hangup,s,${HANGUPCAUSE}) [hangup] exten => s,<1...127>,HangUp(< Q.931>)
The context logic is simple - after the end of the Dial call, the HANGUPCAUSE variable catches the value flown from the B side, and by means of the GoTo function it goes into the hangup context at a priority with a value from 1 to 127. We indicate directly in it whether we need a replacement or whether we leave the value that came to us. The list of matches for Q931 -> SIP can be viewed. For example,
here
Example
Let's look at a working example with detailed logs. There are 2 Asterisk servers, one of which will be “our”, the second one will give arbitrary hang-up codes that we need to adjust.
')
To begin, consider the correspondence between the Asterisk SIP codes and Q.931, which we will substitute into our context:
- 403 (21 as AST_CAUSE_CALL_REJECTED)
- 404 (1 as AST_CAUSE_UNALLOCATED)
- 410 (22 as AST_CAUSE_NUMBER_CHANGED)
- 484 (28 as AST_CAUSE_INVALID_NUMBER_FORMAT)
- 486 (17 as AST_CAUSE_USER_BUSY)
- 502 (27 as AST_CAUSE_DESTINATION_OUT_OF_ORDER)
- 503 (34 as AST_CAUSE_NORMAL_CIRCUIT_CONGESTION, there are more options for using it, but we take this as an example)
And we will correct the following codes:
- 403 503 (21--->34) - 486 410 (17--->22) - 484 404 (28--->1) - 502 503(27--->34) - 503 (34--->34) - 404 (1--->1)
Connection diagram:
(I will not do the pre-setting of
sip peers , let's say it has already been done)
Now in the outgoing context [outgoing] for user 101 (sip client) on asterisk in the extensions.conf file we add our dialplan:
dialplan[outgoing]
exten => _X., 1, Dial (SIP / $ {EXTEN} @ 192.168.1.100,60, g)
same => n, GoTo (hangup, s, $ {HANGUPCAUSE}) ;; the value of HANGUPCAUSE is a priority in the context of hangup
[hangup]
exten => s, 1, hangup (1)
same => 17, HangUp (22)
same => 21, HangUp (34)
same => 27, HangUp (34)
same => 28, HangUp (1)
same => 34, HangUp (34)
It's time to test this dialplan in practice. We will initiate the return by the sip-provider of the codes we need, in order to check how asterisk substitutes them, according to the specified rules. The number of the called subscriber does not matter (the pattern _X is indicated.), Therefore we will call 102. We denote sip-client
A , sip-provider
B , and our asterisk -
T.
So, let's begin:
403 to 503
B ---> T2016/12/07 14: 31: 47.864287 192.168.1.100:5060 -> 192.168.1.59:5060
SIP / 2.0 403 Forbidden
Via: SIP / 2.0 / UDP 192.168.1.59:5060;branch=z9hG4bK5d05bcde;received=192.168.1.59
From: <sip: 101@192.168.1.59>; tag = as5eed27bb
To: <sip: 102@192.168.1.100>; tag = as64678c0b
Call ID: 228c0daa2ce496dd10d46e9478757bdc@192.168.1.59: 5060
CSeq: 102 INVITE
Server: Asterisk PBX 11.25.0
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
Supported: replaces, timer
Session-Expires: 1800; refresher = uas
Content-Length: 0
T ---> A2016/12/07 14: 31: 47.864536 172.16.1.1:5060 -> 172.16.1.10:45416
SIP / 2.0 503 Service Unavailable
Via: SIP / 2.0 / UDP 172.16.1.10:45416;branch=z9hG4bK-524287-1---3b6420a969f2c3a5;received=172.16.1.10;rport=45416
From: <sip: 101@172.16.1.1; transport = UDP>; tag = a946bd32
To: <sip: 102@172.16.1.1; transport = UDP>; tag = as558ab11d
Call ID: t_H-Dcu8ceIhISoGT7dI9w ...
CSeq: 2 INVITE
Server: Asterisk PBX 11.25.0
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
Supported: replaces, timer
X-Asterisk-HangupCause: Call Rejected
X-Asterisk-HangupCauseCode: 21
Content-Length: 0
486 to 410
B ---> T2016/12/07 15: 01: 06.232282 192.168.1.100:5060 -> 192.168.1.59:5060
SIP / 2.0 486 Busy here
Via: SIP / 2.0 / UDP 192.168.1.59:5060;branch=z9hG4bK49adb904;received=192.168.1.59
From: <sip: 101@192.168.1.59>; tag = as274a0aaa
To: <sip: 102@192.168.1.100>; tag = as0865c714
Call ID: 133710b7018b6f003e461b7366dc071d@192.168.1.59: 5060
CSeq: 102 INVITE
Server: Asterisk PBX 11.25.0
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
Supported: replaces, timer
Session-Expires: 1800; refresher = uas
Content-Length: 0
T ---> A2016/12/07 15: 01: 06.232644 172.16.1.1:5060 -> 172.16.1.10:45416
SIP / 2.0 410 Gone
Via: SIP / 2.0 / UDP 172.16.1.10:45416;branch=z9hG4bK-524287-1---ffcd5e1a73cb2616;received=172.16.1.10;rport=45416
From: <sip: 101@172.16.1.1; transport = UDP>; tag = 769e0625
To: <sip: 102@172.16.1.1; transport = UDP>; tag = as4a312af7
Call ID: TJ49AuSsPy15GbKbIn3KTw ...
CSeq: 2 INVITE
Server: Asterisk PBX 11.25.0
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
Supported: replaces, timer
Content-Length: 0
484 to 404
B ---> T2016/12/07 15: 03: 42.757761 192.168.1.100:5060 -> 192.168.1.59:5060
SIP / 2.0 484 Address incomplete
Via: SIP / 2.0 / UDP 192.168.1.59:5060;branch=z9hG4bK329498e8;received=192.168.1.59
From: <sip: 101@192.168.1.59>; tag = as7feed819
To: <sip: 102@192.168.1.100>; tag = as31afa634
Call ID: 4c129a6b7680fc2f6940c77525a58e48@192.168.1.59: 5060
CSeq: 102 INVITE
Server: Asterisk PBX 11.25.0
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
Supported: replaces, timer
Session-Expires: 1800; refresher = uas
Content-Length: 0
T ---> A2016/12/07 15: 03: 42.758083 172.16.1.1:5060 -> 172.16.1.10:45416
SIP / 2.0 404 Not Found
Via: SIP / 2.0 / UDP 172.16.1.10:45416;branch=z9hG4bK-524287-1---60687a681de37faa;received=172.16.1.10;rport=45416
From: <sip: 101@172.16.1.1; transport = UDP>; tag = 2303fd45
To: <sip: 102@172.16.1.1; transport = UDP>; tag = as6f3c2e31
Call ID: VPDPD8Wtelh7GfRX5OSnig ...
CSeq: 2 INVITE
Server: Asterisk PBX 11.25.0
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
Supported: replaces, timer
Content-Length: 0
502 to 503
B ---> T2016/12/07 14: 46: 12.941218 192.168.1.100:5060 -> 192.168.1.59:5060
SIP / 2.0 502 Bad Gateway
Via: SIP / 2.0 / UDP 192.168.1.59:5060;branch=z9hG4bK39ae3e4a;received=192.168.1.59
From: <sip: 101@192.168.1.59>; tag = as0d0f21e7
To: <sip: 102@192.168.1.100>; tag = as501e30d3
Call ID: 50cc1fca1b0c8d4807c110ab2621de88@192.168.1.59: 5060
CSeq: 102 INVITE
Server: Asterisk PBX 11.25.0
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
Supported: replaces, timer
Session-Expires: 1800; refresher = uas
Content-Length: 0
T -> A2016/12/07 14: 46: 12.941529 172.16.1.1:5060 -> 172.16.1.10:45416
SIP / 2.0 503 Service Unavailable
Via: SIP / 2.0 / UDP 172.16.1.10:45416;branch=z9hG4bK-524287-1---34f25329bb23215d;received=172.16.1.10;rport=45416
From: <sip: 101@172.16.1.1; transport = UDP>; tag = 0aabc036
To: <sip: 102@172.16.1.1; transport = UDP>; tag = as61430d18
Call ID: pH7WSH9iuRXDNJ0lyRnlGQ ...
CSeq: 2 INVITE
Server: Asterisk PBX 11.25.0
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
Supported: replaces, timer
X-Asterisk-HangupCause: Destination out of order
X-Asterisk-HangupCauseCode: 27
Content-Length: 0
503
B ---> T2016/12/07 15: 05: 17.966997 192.168.1.100:5060 -> 192.168.1.59:5060
SIP / 2.0 503 Service Unavailable
Via: SIP / 2.0 / UDP 192.168.1.59:5060;branch=z9hG4bK0c901ae6;received=192.168.1.59
From: <sip: 101@192.168.1.59>; tag = as049eed53
To: <sip: 102@192.168.1.100>; tag = as5b5775fa
Call ID: 115e76677306210f6841df276dba9084@192.168.1.59: 5060
CSeq: 102 INVITE
Server: Asterisk PBX 11.25.0
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
Supported: replaces, timer
Session-Expires: 1800; refresher = uas
Content-Length: 0
T ---> A2016/12/07 15: 05: 17.967354 172.16.1.1:5060 -> 172.16.1.10:45416
SIP / 2.0 503 Service Unavailable
Via: SIP / 2.0 / UDP 172.16.1.10:45416;branch=z9hG4bK-524287-1---a5d67aaf0d62c772;received=172.16.1.10;rport=45416
From: <sip: 101@172.16.1.1; transport = UDP>; tag = 886e8753
To: <sip: 102@172.16.1.1; transport = UDP>; tag = as515af7fd
Call ID: rd_vI3FHvTGECqLBPqu_mQ ...
CSeq: 2 INVITE
Server: Asterisk PBX 11.25.0
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
Supported: replaces, timer
Content-Length: 0
404
B --- T2016/12/07 15: 07: 10.270648 192.168.1.100:5060 -> 192.168.1.59:5060
SIP / 2.0 404 Not Found
Via: SIP / 2.0 / UDP 192.168.1.59:5060;branch=z9hG4bK70bbffe3;received=192.168.1.59
From: <sip: 101@192.168.1.59>; tag = as42b18ba7
To: <sip: 102@192.168.1.100>; tag = as4bbccb03
Call ID: 681fa91b5d0b43d56125561a0a128540@192.168.1.59: 5060
CSeq: 102 INVITE
Server: Asterisk PBX 11.25.0
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
Supported: replaces, timer
Session-Expires: 1800; refresher = uas
Content-Length: 0
T ---> A2016/12/07 15: 07: 10.271017 172.16.1.1:5060 -> 172.16.1.10:45416
SIP / 2.0 404 Not Found
Via: SIP / 2.0 / UDP 172.16.1.10:45416;branch=z9hG4bK-524287-1---4ccc9dc26a7e83a5;received=172.16.1.10;rport=45416
From: <sip: 101@172.16.1.1; transport = UDP>; tag = e5ef327c
To: <sip: 102@172.16.1.1; transport = UDP>; tag = as4d3749c4
Call ID: O5xqrboDIZBqx44TJNEmPA ...
CSeq: 2 INVITE
Server: Asterisk PBX 11.25.0
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
Supported: replaces, timer
Content-Length: 0
In the
[global] section, it is also possible to set
use_q850_reason = yes in the
sip.conf settings, so that the
following information will appear in the drop- in package:
Q.8502016/12/07 15: 26: 35.746861 46.4.173.59:5060 -> 213.231.39.180:45416
SIP / 2.0 503 Service Unavailable
Via: SIP / 2.0 / UDP 213.231.39.180:45416;branch=z9hG4bK-524287-1---a8e0763fbaad1b3f;received=213.231.39.180;rport=45416
From: <sip: 101@46.4.173.59; transport = UDP>; tag = e4923c2b
To: <sip: 102@46.4.173.59; transport = UDP>; tag = as6328694d
Call ID: YhLSgjZP-vgF2rrqlKstcA ...
CSeq: 2 INVITE
Server: Asterisk PBX 11.25.0
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
Supported: replaces, timer
Reason: Q.850; cause = 34
Content-Length: 0
Thus, you can control what you give to your customers, and also customize the logic according to the specifics of your PBX based on Asterisk.
Thanks for attention.