Open Source - How to configure a Digium SIP Trunking account with Asterisk using chan_pjsip
Depending on the version of Asterisk that you are using, You may have two channels drivers that you could use in order to create a peer that you could use to place and receive calls, if you are looking for how to configure asterisk with chan_sip we have another KB article that talks about the configuration.
Asterisk with chan_pjsip
To configure Asterisk's PJSIP-based SIP channel driver, included with Asterisk versions 12, 13 and newer, to work with Digium's SIP Trunking service, you should configure 6 objects:
transport
auth
aor
endpoint
registration
identify
These objects will be configured in the chan_pjsip configuration file, pjsip.conf, typically located on your filesystem in /etc/asterisk. For more information on each of the chan_pjsip object types, please visit the PJSIP Configuration Sections and Relationships wiki page.
Transport
It's likely that you've already configured a transport object within chan_pjsip for use with SIP phones. If you have already configured a suitable transport you can use it rather than adding another. The transport object defines protocol options and the local binding address. A sample transport for use with Digium SIP Trunking would resemble:
[transport-udp]
type=transport
protocol=udp
bind=0.0.0.0
Here, we've declared a transport using the UDP protocol and bound to all local interfaces.
Auth
The auth object holds options and credentials related to outbound authentication, in this case, your Digium SIP Trunking username and password. A sample auth for use with Digium SIP Trunking would resemble:
[digium-siptrunk-auth]
type=auth
auth_type=userpass
username=your_digium_username
password=your_digium_password
Here, in the digium-siptrunk-auth auth object, we've declared that we're using userpass authentication type and set a username and password for it. You should fill in your Digium SIP Trunking username and password in lieu of these examples.
Address of Record (AoR)
The AoR object tells Asterisk where to contact Digium's SIP Trunking service. A sample aor for use with Digium's SIP Trunking would resemble:
[digium-siptrunk-aor]
type=aor
contact=sip:sip.digiumcloud.net:5060
Here, in the digium-siptrunk-aor object, we've declared that the Contact address for Digium's SIP Trunking will be sip.digiumcloud.net on port 5060.
Endpoint
The endpoint object is a profile for the configuration of a remote server (or a SIP endpoint) that ties together the other sections we've created. Declaring an endpoint object is necessary so that Asterisk can maintain a proper identifier for the SIP entity with which it'll be communicating. A sample endpoint for use with Digium's SIP Trunking, in consideration of the other sample objects we've already defined, would resemble:
[digium-siptrunk]
type=endpoint
transport=transport-udp
context=from-digium-siptrunk
disallow=all
allow=g722
allow=ulaw
allow=g729
outbound_auth=digium-siptrunk-auth
aors=digium-siptrunk-aor
Here, in the digium-siptrunk endpoint object, we've declared an endpoint that uses our transport-udp transport object, that operates in Asterisk Dialplan context from-digium-siptrunk (discussed later in this article), that allows (by virtue of first disallowing) the G.722, G.711 u-law and G.729a audio codecs (if you have purchase the G.729 codec license and have installed on your Asterisk system) , that uses the digium-siptrunk-auth authentication object and that is associated with the digium-siptrunk-aor address of record.
Registration
The registration object type contains information used when registering your system with another system, such as Digium's SIP Trunking service. A sample registration for use with Digium's SIP Trunking would resemble:
[digium-siptrunk-registration]
type=registration
transport=transport-udp
outbound_auth=digium-siptrunk-auth
server_uri=sip:sip.digiumcloud.net
client_uri=sip:your_digium_username@sip.digiumcloud.net
contact_user=your_digium_username
retry_interval=60
Here, in the digium-siptrunk-registration object, we've declared a registration that uses our transport-udp transport object, that uses the digium-siptrunk-auth authentication information for outbound authentication, that sets the outbound server URI as sip.digiumcloud.net, that specifies our client URI as your_digium_username at sip.digiumcloud.net, that sets our Contact user to your_digium_username, and that will retry failed registrations every 60 seconds.
Identify
The identify object type is used to help route incoming packets inside of Asterisk, so that Asterisk knows to what endpoint an incoming call should be associated. A sample identify for use with Digium SIP Trunking would resemble:
[digium-siptrunk-identify]
type=identify
match=sip.digiumcloud.net
endpoint=digium-siptrunk
Here, in the digium-siptrunk-identify object, we've declared that we'd like to match inbound calls from sip.digiumcloud.net (the Digium SIP Trunking server) to the digium-siptrunk endpoint (the one we've created here).
Asterisk Dialplan Configuration
Asterisk uses a configuration of numbers and patterns matched with specific actions, called dialplan, and configured in extensions.conf, typically located in /etc/asterisk in order to determine what to do with incoming calls. In order to complete your setup with Digium's SIP Trunking service, you will need to properly configure your Asterisk dialplan. To learn more about Asterisk's dialplan, please see the Dialplan wiki page, as well as its children, available on the Asterisk wiki.
Inbound Dialplan
Domestic calls that are inbound from Digium's SIP Trunking servers are delivered with full 10-digit DID. A sample incoming context for use with Digium's SIP Trunking, would resemble:
[from-digium-siptrunk]
exten => 8005551234,1,NoOp()
same => n,Answer()
same => n,Background(demo-congrats)
same => n,Dial(PJSIP/sample_endpoint,,25)
same => n,Hangup()
Here, because we earlier configured our Digium SIP Trunking PJSIP endpoint or SIP peer to attach to context from-digium-siptrunk, calls to our hypothetical inbound DID 8005551234 will first perform No Operation, then will be Answered, then will have the sound file demo-congrats played to them, will be connected via the Dial application (which will try for 25 seconds) to a hypothetical PJSIP endpoint local to your system called sample_endpoint, and will finally be hung up upon. Users of chan_sip, in lieu of chan_pjsip, may dial using the "SIP" technology instead of "PJSIP"
Outbound Dialplan
Outbound dialing should be handled by a separate context and should include pattern matches for local and long-distance calling, and this context should be included into whatever dialing context your SIP endpoints are otherwise configured. A sample outgoing context for use with Digium's SIP Trunking would resemble:
[sip-phones]
; The context that your SIP phones are configured to use
include => outbound-digium-siptrunk
[outbound-digium-siptrunk]
; Pattern matching 1 + 10-digit North American dialing
exten => _1NXXNXXXXXX,1,NoOp()
same => n,Set(CALLERID(num)=your_digium_caller_id_number)
same => n,Dial(PJSIP/${EXTEN}@digium-siptrunk,,25)
same => n,Hangup()
; Pattern matching 10-digit North American dialing that prefixes 1 to the dialed number
exten => _NXXNXXXXXX,1,NoOp()
same => n,Set(CALLERID(num)=your_digium_caller_id_number)
same => n,Dial(PJSIP/1${EXTEN}@digium-siptrunk,,25)
same => n,Hangup()
; Pattern matching International dialing using 011 as the access code
exten => _011.,1,NoOp()
same => n,Set(CALLERID(num)=your_digium_caller_id_number)
same => n,Dial(PJSIP/${EXTEN}@digium-siptrunk,,25)
same => n,Hangup()
Here, we have configured pattern matches for 1+ 10-digit North American dialing, 10-digit North American dialing, and International Dialing. Users of chan_sip, in lieu of chan_pjsip, may dial using the SIP technology instead of PJSIP. The ,,25 in each of the Dial statements means that Asterisk will attempt the dial for no more than 25 seconds before jumping to the next step - a Hangup() as we have configured here. You will need to replace your_digium_caller_id_number with your DID number as received from Digium, e.g. 8005551234.