fb

Asterisk PJSIP

Setting up Asterisk PJSIP with Zadarma

Data given in example:

  • 111111 Your sip-number from personal account.
  • Password You sip-number password from "SIP-settings" in personal account.
  • 101 Asterisk's internal number to which the softphone/IP phone is connected in order to receive incoming calls and to make outgoing calls.

Standard variant

Outgoing calls from internal number 101 are routed to trunk 111111. Incoming calls are received by registration and are routed to internal number 101.

Edit pjsip.conf

[udp-transport]
type=transport
protocol=udp
bind=0.0.0.0

[111111]
type=registration
transport=udp-transport
outbound_auth=111111_auth
server_uri=sip:sip.zadarma.com
client_uri=sip:111111@sip.zadarma.com
retry_interval=60
expiration=120
contact_user=111111

[111111_auth]
type=auth
auth_type=userpass
password=Password
username=111111

[111111]
type=aor
contact=sip:sip.zadarma.com:5060

[111111]
type=endpoint
transport=udp-transport
context=zadarma-in
disallow=all
allow=alaw
allow=ulaw
outbound_auth=111111_auth
aors=111111
from_user=111111
direct_media=no

[111111]
type=identify
endpoint=111111
match=sip.zadarma.com

Internal number 101 to which softphone/IP phone will be connected to receive incoming calls and to make outgoing calls.

[101]
type=endpoint
transport=udp-transport
context=zadarma-out
disallow=all
allow=alaw
allow=ulaw
auth=101
aors=101

[101]
type=auth
auth_type=userpass
password=101
username=101

[101]
type=aor
max_contacts=10
Incoming and outgoing routing is set in file extensions.conf


[zadarma-in]

exten => 111111,1, Dial(PJSIP/101)                     ; all incoming calls from trunk 111111
                                                       ; are routed on internal number 101

[zadarma-out]

exten => _XXX,1,Dial(PJSIP/${EXTEN})                   ; calls on 3-digit internal numbers
                                                       ; of Asterisk

exten => _XXX.,1,Dial(PJSIP/${EXTEN}@111111)           ; calls on numbers with 4 digits
                                                       ; or more via trunk 111111

Standard setup is complete.

If you have several active DID numbers, you can set "name" for each number, for example Moscow and London, and set incoming routing based on this parameter. "Name" of DID number is sent in parameter CALLERID(name).

In the following example, calls from the number Moscow are routed to internal number 101, calls from the number London are routed to internal number 102 and all other calls will be declined by Asterisk with a "Busy" tone.


[zadarma-in]

exten => _X.,1,GotoIf($["${CALLERID(name)}" = "moscow"]?2:3)
exten => _X.,2,Dial(PJSIP/101)
exten => _X.,3,GotoIf($["${CALLERID(name)}" = "london"]?4:5)
exten => _X.,4,Dial(PJSIP/102)
exten => _X.,5,Busy

The DID to which the call was received, is passed to the header CALLED_DID. Based on this parameter, you can configure inbound routing.

In the following example, calls from the number 74957776675 are routed to internal number 101, calls from the number 442037691880 are routed to internal number 102. Asterisk will decline all other calls with a "Busy" tone.


[zadarma-in]

exten => _X.,1,GotoIf($["${PJSIP_HEADER(read,CALLED_DID)}" = "74957776675"]?2:3)
exten => _X.,2,Dial(PJSIP/101)
exten => _X.,3,GotoIf($["${PJSIP_HEADER(read,CALLED_DID)}" = "442037691880"]?4:5)
exten => _X.,4,Dial(PJSIP/102)
exten => _X.,5,Busy


Setup is finished.

Example №2 (SIP URI)

If Asterisk is located on a "white" IP address (not behind router, for example in a data centre), incoming calls can be received without registration by SIP URI scheme. Data given in example:

  • 15555555555 - Your DID number connected to Zadarma.
  • 2.20.190.41 - IP of your Asterisk server.
In your personal account, under "Settings - Direct phone number" route calls from DID number to an external server (SIP URI) using the format 15555555555@2.20.190.41

Edit pjsip.conf

[15555555555]
type=aor
contact=sip:sip.zadarma.com:5060

[15555555555]
type=endpoint
transport=udp-transport
context=zadarma-in
disallow=all
allow=alaw
allow=ulaw
aors=15555555555
direct_media=no

[15555555555]
type=identify
endpoint=15555555555
match=sip.zadarma.com

Incoming route in file extensions.conf

[zadarma-in]

exten => 15555555555,1, Dial(PJSIP/101)

Setup is finished.

Example configuration with authorization by IP address.