Trunk - Sample Dialplan

Trunk

This is my sample dialplan for handling incoming calls to and from my telephony Carrier (sometimes an Internet connection through my ISP, sometimes an ethernet cable from the phone carrier.)

We handle the context= from the SIP configuration to make some settings.

Table of Contents

Inbound

The dialplan context used for managing calls from our trunk provider is specified with the context= line in sip.conf.

File extract: /etc/asterisk/sip.conf

[trunk]
...
context=from-trunk
...

The most basic dialplan for managing incoming calls, is to recieve the call on the dialled extension, and send it to the local device. Such as:

[from-trunk]
exten => _0212345678,1,Dial(SIP/5001,60) ; Dial for 60 seconds
 same => n,Hangup()

exten => s,1,Dial(SIP/5501,60)
 same => n,Hangup()

The above is nice and even scales well to multiple numbers, and multiple range of numbers.

For us, we started having problems with our inbound numbers so it became useful to do some clean up work on the incoming numbers so we adopted a two step process for incoming calls:

  • receipt pattern with from-trunk
  • routing patterns with trunk-internal (trunk to internal)

The basic form would be:

[from-trunk]
exten => _X!,1,Goto(trunk-internal,${EXTEN},1)

To ensure we only deal with the Full National Number (and avoid problems with any extra information from our carrier) we use basic variable manipulation [2] to send to trunk-internal only the 1st 10 numbers. (Full National Numbers in Australia are 10 digits.)

[from-trunk]
exten => _X!,1,Goto(trunk-internal,${EXTEN:0:10},1)

CallerID - Inbound

Many carriers support passing the caller id with a telephone call and there is no additional step required in Asterisk. When we transfer a call (DIAL) to a local extension, it should display the caller id.

For some PBX scenarios, a prefix must be dialled before an outside line can be called. For these environments, we add explicit setting of the caller id.

For example: If your phone system requires dialling “9” before you can dial an external phone number, then we add the line:

We dress things up a bit and it looks like this now:

[from-trunk]
exten => _X!,1,Verbose(2,TO ${EXTEN} FROM ${CALLERID(num)})
 same => n,GotoIf($["${CALLERID(num)}" = "anonymous"]?anon)
 same => n(setcid),Set(CALLERID(num)=${CALLERID(num)})
 same => n(anon),Goto(trunk-internal,${EXTEN:0:10},1)

We use GotoIf so that we don’t put a prefix if the caller id says “anonymous.”

Call Routing

Using the above logic for the [from-trunk] context we create a routing schedule for inbound numbers.

Below is a simple segment, dealing with only 2 inbound numbers. Using This context we can develop more elaborate catches for inbound calls/routing.

In all cases we just jump to a subroutine that presents the internal calls.

[trunk-internal]
 
 exten => _0212345678,1,Dial(SIP/5500,60)
 same => n,Hangup()
 
 exten => _0287654321,1,Dial(SIP/5501,60)
 same => n,Hangup()

Of course, we use a Subroutine to actually DIAL the extension.

[trunk-internal]
 
 exten => _0212345678,1,Gosub(stdexten,5500,1(,${GLOBAL(CONSOLE)}))
 same => n,Hangup()
 
 exten => _0287654321,1,Gosub(stdexten,5005,1(,${GLOBAL(CONSOLE)}))
 same => n,Hangup()
 
 exten => s,1,Gosub(stdexten,5500,1(,${GLOBAL(CONSOLE)}))
 same => n,Hangup()

Outbound

Making an outgoing call is similar to other calls handled by Asterisk.

exten => _12345678.,1,DIAL(SIP/${EXTEN}@trunk,60) ; Ring for 60 seconds maximum

The above dialplan (if reached) will Dial the extension 12345678 through the trunk SIP configuration.

To expand to any number to any recieved by the dialplan.

exten => _X.,1,DIAL(SIP/${EXTEN}@trunk,60) ; Ring for 60 seconds maximum

We wrap the Dial command around some error checking.

[dial-trunk]
 exten => _X.,1,Dial(SIP/${EXTEN}@trunk,60)  ; Ring the interface 60 seconds maximum
  same => n,Gosub(trunk-${DIALSTATUS},1)
  same => n,Hangup()

Caller ID

[ Ref: Wiki, Asterisk Docs, Setting CallerID in Asterisk, Caller ID in SIP and Asterisk ]

Depending on your situation, you may or may not want to set CallerID on our Outbound calls.

If your Voice Carrier supports the P-Asserted-Identity and Privacy Headers (i.e. Calling Party and Network Identification Definition.) You can insert these values into Asterisk’s SIP headers using: SIPAddHeader.

You can set the Caller ID Values such as the below.

[dial-trunk]
exten => _X.,1,Set(CALLERID(num)=021234${EXTEN})
 same => n,SIPAddHeader(P-Asserted-Identity: <sip:+6121234${EXTEN}@example.com>) ; My E164 Number
  same => n,Dial(SIP/${EXTEN}@trunk,60)  ; Ring the interface 60 seconds maximum
  same => n,Gosub(trunk-${DIALSTATUS},1)
  same => n,Hangup()

You will need to ensure the Channel Configuration (i.e. SIP) is enabled for sending P-Asserted-Id information with the following configuration:

File extract: /etc/asterisk/sip.conf

[trunk]
...
sendrpid=pai
trustrpid=yes

As always, refer to your vendor documentation for the features and settings required for Caller ID.

Enable

[Ref: RFC 5379, RFC 3323]

For some providers, doing the above is sufficent to enable Caller ID. For some others, you need to specify the Privacy setting, such as below:

 same => n,SIPAddHeader(Privacy:none)

And our dial-trunk will look like this.

[dial-trunk]
exten => _X.,1,Set(CALLERID(num)=021234${EXTEN})
 same => n,SIPAddHeader(P-Asserted-Identity: <sip:+6121234${EXTEN}@example.com>) ; My E164 Number
 same => n,SIPAddHeader(Privacy:none)
 same => n,Dial(SIP/${EXTEN}@trunk,60)  ; Ring the interface 60 seconds maximum
 same => n,Gosub(trunk-${DIALSTATUS},1)
 same => n,Hangup()
Disable

To disable caller id, specify the desired Privacy setting.

  same => n,SIPAddHeader(Privacy:id)

And our dial-trunk will look like this.

[dial-trunk]
exten => _X.,1,Set(CALLERID(num)=021234${EXTEN})
 same => n,SIPAddHeader(P-Asserted-Identity: <sip:+6121234${EXTEN}@example.com>) ; My E164 Number
 same => n,SIPAddHeader(Privacy:id)
 same => n,Dial(SIP/${EXTEN}@trunk,60)  ; Ring the interface 60 seconds maximum
 same => n,Gosub(trunk-${DIALSTATUS},1)
 same => n,Hangup()