Jun 052010
 

In my previous article we configured Asterisk with some SIP-devices, and created a basic dialplan so that they could dial eachother. We also created two additional extensions for test purposes. This time I will show you how to configure a SIP trunk, and add extensions in the dialplan so that the telephones can dial out through the trunk. And if you also have a telephone number (DID) associated with the trunk, for others to be able to dial your phones, through your Asterisk PBX.

What is a SIP trunk?

A SIP trunk is often defined using many buzz- and marketing words throughout the web, but, what it basically is, is a two-way connection to a VOIP-provider, that routes the calls you send to it, out on the PSTN for you, and charges you for the calls you make. If you also have a DID (Direct Inward Dialing) number at the provider, calls made to you are forwarded to your Asterisk PBX, then you switch the calls as you see fit. Through a trunk, many calls can be sent, the limit is only your bandwidth and computer resources at the machine where your Asterisk runs, unless your VOIP-provider, or you for that matter, limit the number of calls in some way (by configuring the PBX at either end of the trunk), that are allowed to go through it.

There are many companies offering SIP trunks. Some doesn’t call it a SIP trunk though, they call it simply “Broadband Telephony”, or “VOIP Service”, and so on. What they really do though, is set up a SIP trunk between a device in your home, and their telephone switch, which may very well be Asterisk, in many cases it is. Some companies don’t want people to run their own PBXs and create their own services, for free, and with the freedom that comes with using Asterisk, they want you to pay for their services, like voicemail, “answering machines”, and such. Everyone wants to sell a service, nothing wrong with that, but watch out for VOIP-providers that explicitly filter connections from users own Asterisk-servers. Choose another one instead. Have a look here for some alternatives.

When you have bought a suitable SIP trunk, and have gotten your account information from the provider, we can continue, and set it up.

Sections:

Requirements

Before we start there are a couple of things that we need:

  • A working installation of Asterisk, preferably with one or more telephones configured and working, that can dial the Asterisk server, and the other phones connected to it successfully. (See An introduction to Asterisk, The Open Source Telephony Project if you do not already have this configured and working.)
  • A SIP trunk, with a provider of your choice. (Take a look here to find one.)

Configuration

Setting up a SIP trunk is not harder than adding a SIP telephone. For a basic configuration only two files needs to be edited, sip.conf and extensions.conf. I will continue where the previous article left off, and use the configuration files that was created there, and add a SIP trunk to this setup, step by step.

sip.conf

Edit sip.conf in your favourite editor and add the following example configuration:

; Register and get calls from Foo Provider, to our number 1-555-455-1337
register => 15554551337:password123@sip.provider.foo

[fooprovider]
type=friend
secret=password123
username=15554551337
host=sip.provider.foo
dtmfmode=rfc2833
canreinvite=no
disallow=all
allow=ulaw
allow=alaw
allow=gsm
insecure=port,invite
fromdomain=sip.provider.foo
context=incoming

I prefer to have the above sections at the top in my sip.conf, but that is up to you. Modify it to reflect your account details. Some notes about the above configuration:

register => 15554551337:password123@sip.provider.foo

The register directive registers our Asterisk with the trunk-providers SIP-server, with the username (15554551337 in our example case) and the password (password123), that we have specified. We have to register to be able to have calls to our telephone number be forwarded to us.

context=incoming

Notice that we send all incoming calls to a specific, and named part of our dialplan. This is very important, for many reasons. Control, security, and segmentation of the dialplan. Our phones have their own context, and people calling us, from the outside, have their own context, with more restrictions. But more about this in the following steps.

extensions.conf


Now that we have added the definition of our trunk, we can use it in our dialplan, and make it possible for us to dial out, and for others to dial in. Before that will happen, we need to add a new context to the dialplan, and the simplest form of call handling, to start with. We start with making it possible for people to call us, on our first telephone, on extension 1000, that we configured in the previous article. Edit extensions.conf, and add:

[incoming]
exten => s,1,Log(NOTICE, Incoming call from ${CALLERID(all)})
exten => s,n,Dial(SIP/1000)
exten => s,n,Hangup()
; End of the "incoming" context

The “s” in the above extension definition means that this is the starting, default extension in the context. The Log() application writes to Asterisk’s logfile (with the specified syslog level), and Asterisk’s console. This is nice when testing and debugging the dialplan. The Dial() application then dials extension 1000, our first telephone. The Hangup() application ends the call, if the caller hangs up, Asterisk then needs to hangup the call internally aswell, and that is what happens on the last line in this extension. It is VERY IMPORTANT to always have a Hangup() at the end of every extension! Make it a habit. Read more about this in Asterisk: The Future of Telephony, 2nd Edition.


To make it possible for our telephones to dial out through the trunk, we need to catch the dialed phone numbers, and strip off the dialout extension number that we will use, then pass the real phone number to our provider, and let them route the call to its destination in the PSTN (or maybe we dial a SIP address, it is all handled in the same way, if your provider has configured their end correctly). Add the following in the context that our telephones are placed in:

[myphones]

; Call POTS numbers through Foo Provider (any number longer than 5 digits starting with 9)
exten => _9XXXX.,1,Log(NOTICE, Dialing out from ${CALLERID(all)} to ${EXTEN:1} through Foo Provider)
exten => _9XXXX.,n,Dial(SIP/fooprovider/${EXTEN:1},60)
exten => _9XXXX.,n,Playtones(congestion)
exten => _9XXXX.,n,Hangup()

There are a couple of things that might need explanation in the above. We use the Dial() application again, to dial the number we entered in our phone, but “${EXTEN:1}” uses the entered number, after the first digit, that is the meaning of “:1″. “60” is the number of seconds to let it ring, until we give up and let Asterisk play congestion tones to us, increase the time value if you think it is too short. You also exchange “fooprovider” with the name of your real provider that you configured in sip.conf.

Test it

For our configuration to take effect we either have to reload it from Asterisk’s command-line interface, or restart Asterisk. To reload the SIP configuration and the dialplan, connect to the running Asterisk’s command-line:

asterisk -vcr

And run:

sip reload
dialplan reload

Verify that your Asterisk server registers with your provider correctly:

sip show registry

If necessary, troubleshoot the registration, use the following Asterisk CLI commands:

sip set debug on

Now at last, test the configuration. Dial your Asterisk server from your mobile phone, and hopefully your first SIP telephone will ring. Also watch the Asterisk console and see the Log() notice that we added appear and make you smile.

If that works, proceed with dialing out to your mobile phone from any of your configured and registered SIP phones, remember to dial 9 in front of the actual phone number.

While the call is going on, run the following command to see the two channels that are created, and switched together in your Asterisk: One channel to/from your SIP phone, and one through your trunk, to your mobile phone:

core show channels

Full example reference configuration files

Here are the full contents of sip.conf and extensions.conf, from the previous article, with the configuration from this article added, making up a fully working, basic, but yet complete Asterisk configuration.


sip.conf:

[general]
context=incoming

allow=ulaw
allow=alaw
allow=gsm

; Register and get calls from Foo Provider, to our number 1-555-455-1337
register => 15554551337:password123@sip.provider.foo

[fooprovider]
type=friend
secret=password123
username=15554551337
host=sip.provider.foo
dtmfmode=rfc2833
canreinvite=no
disallow=all
allow=ulaw
allow=alaw
allow=gsm
insecure=port,invite
fromdomain=sip.provider.foo
context=incoming

; ------------------------------------

[1000]
type=friend
secret=replacethis123
dtmfmode=rfc2833
callerid="First Phone" <1000>
host=dynamic        ; The device must always register
canreinvite=no
; Deny registration from anywhere first
deny=0.0.0.0/0.0.0.0
; Replace the IP address and mask below with the actual IP address and mask
; of the computer running the softphone, or the address of the hardware phone,
; either a host address and full mask, or a network address and correct mask,
; registering will be allowed from that host/network.
permit=192.168.1.0/255.255.255.0
context=myphones

[1001]
type=friend
secret=replacethis321
dtmfmode=rfc2833
callerid="Second Phone" <1001>
host=dynamic        ; The device must always register
canreinvite=no
; Deny registration from anywhere first
deny=0.0.0.0/0.0.0.0
; Replace the IP address and mask below with the actual IP address and mask
; of the computer running the softphone, or the address of the hardware phone,
; either a host address and full mask, or a network address and correct mask,
; registering will be allowed from that host/network.
permit=192.168.1.0/255.255.255.0
context=myphones


extensions.conf:

[general]
static=yes
writeprotect=no
clearglobalvars=no

[globals]
; Global variables goes here

[incoming]
exten => s,1,Log(NOTICE, Incoming call from ${CALLERID(all)})
exten => s,n,Dial(SIP/1000)
exten => s,n,Hangup()
; End of the "incoming" context

[myphones]
; When we dial something from the phones we just added in
; sip.conf, Asterisk will look for a matching extension here,
; in this context.

; First Phone, extension 1000. If 1000 is called, here is
; where we land, and the device registered with the
; name 1000, is dialed, after that Asterisk hangs up.
exten => 1000,1,Dial(SIP/1000)
exten => 1000,n,Hangup()

; The same goes for Second Phone, extension 1001
exten => 1001,1,Dial(SIP/1001)
exten => 1001,n,Hangup()

; Testing extension, prepare to be insulted like a
; Monthy Python knight
exten => 201,1,Answer()
exten => 201,n,Playback(tt-monty-knights)
exten => 201,n,Hangup()

; Echo-test, it is good to test if we have sound in both directions.
; The call is answered
exten => 202,1,Answer()
; Welcome message is played
exten => 202,n,Playback(welcome)
; Play information about the echo test
exten => 202,n,Playback(demo-echotest)
; Do the echo test, end with the # key
exten => 202,n,Echo()
; Plays information that the echo test is done
exten => 202,n,Playback(demo-echodone)
; Goodbye message is played
exten => 202,n,Playback(vm-goodbye)
; Hangup() ends the call, hangs up the line
exten => 202,n,Hangup()

; Call POTS numbers through Foo Provider (any number longer than 5 digits starting with 9)
exten => _9XXXX.,1,Log(NOTICE, Dialing out from ${CALLERID(all)} to ${EXTEN:1} through Foo Provider)
exten => _9XXXX.,n,Dial(SIP/fooprovider/${EXTEN:1},60)
exten => _9XXXX.,n,Playtones(congestion)
exten => _9XXXX.,n,Hangup()

Related articles

I hope this has been informative, and I would like to thank You for reading. Feel free to comment.

  24 Responses to “How to set up a SIP trunk in the Asterisk PBX”

Comments (18) Pingbacks (6)
  1. nice post. thanks.

  2. This is such a great resource that you are providing and you give it away for free. I enjoy seeing websites that understand the value of providing a prime resource for free. I truly loved reading your post. Thanks!

  3. Is it just me or is there a discrepancy between the article and the full config files at the bottom? They don’t seem to match up and the full config doesn’t seem to have any rules for incoming?

    -jp

  4. Thank you for your comment Jp.
    You are absolutely right. I must have made a copypaste error or just not checking properly when assembling the full config at the end. I have corrected it now, the full config now corresponds to the rest.
    Thanks for observing and pointing it out, I appreciate it. I must have been tired when finishing the article.

    Apologies to anyone following it not getting correct information. I hope you return and see the correction.

  5. Great article! I really hope you’ll write some more! Easy to understand and follow. I’d really like to see an article like this about all the config files and options. Bookmarked immediately! It’s hard to find information that’s to the point like this for us who don’t have much experience.

    Thank You!

  6. Great article!

    I did have a problem getting it to work with my VOSP and Asterisk 1.4 (Asterisk and SIP clients behind a NAT router), though: In sip.conf, I had to have two sections (Outgoing and Incoming), and the Outgoing section had to be located before Incoming or I would get a BUSY signal when calling the VOSP number from a cellphone:


    ;========== sip.conf
    ...
    register => myaccount:mypasswd@myvosp.com

    [vosp_outgoing]
    type=peer
    host=myvosp.com
    username=myaccount
    secret=mypasswd
    fromuser=myaccount
    fromdomain=myvosp.com
    nat=yes
    canreinvite=no

    [vosp_incoming]
    type=peer
    host=myvosp.com
    context=from_vosp
    nat=yes
    canreinvite=no

    [1234]
    type=friend
    context=my-phones
    secret=5678
    host=dynamic
    qualify=yes
    nat=no

    ;========== extensions.conf
    [from_vosp]
    exten => s,1,Dial(SIP/1234)
    exten => s,n,Hangup()

    [my-phones]
    exten => 1234,1,Dial(SIP/1234)
    exten => 1234,n,Hangup()

    exten => _0.,1,Dial(SIP/vosp_outgoing/${EXTEN})
    exten => _0.,n,Hangup()

    I don’t know the cause of this, but it took me a couple of hours to finally figure out why I could call out but not call in.

  7. Hi,

    Nice article!

    But i got a problem. When i put register => blablabla at the top of sip.conf. I get a error message that the sip.conf is invalid. If i remove the line, it works. I know iam writing my line correct. What to do?

  8. Hi,

    I followed the instructions above. I can only received incoming call.
    What is the configuration for outgoing calls?

  9. Magne,
    It sounds like you put your “register =>” first in the file? You can’t do that, it has to be within the “[general]” section, within a context preferebly, like in my example above.

    Leon,
    The configuration for outgoing calls is the section under the “; Register and get calls from Foo Provider” comment in the sip.conf example above. The definition of “fooprovider” (an example provider) is directly underneath, in the “[fooprovider]” section.
    These are then used in extensions.conf to make outgoing calls. In my example above, any number dialed starting with a “9” and having more than 5 digits will be routed out to the provider (see the bottom of the example extensions.conf above).

  10. Hello,

    I have been having some serious problem trying to get my asterisk system to register with my sip provider. Could you please help me figure out why I am not able to connect to my sip provider?

    [general]
    register => username:password@sip.fooprovider.com

    [flowroute] ;keep this lowercase, do not change format
    type=friend
    secret=passworkd
    username=username
    host=sip.fooprovider.com
    dtmfmode=rfc2833
    context=inbound ;change to ‘ext-did’ or ‘from-trunk’ for asterisk@home
    canreinvite=no
    allow=ulaw
    allow=g729
    insecure=port,invite
    fromdomain=sip.fooprovider.com

    Your input on this will be much appreciated.

    Thanks
    Tamaso

  11. Its really helpful for the beginners like me i love read yor articles bro. :)

  12. I can make outgoing calls, but when I try to call in I get this error;

    [Apr 27 16:22:44] NOTICE[6002]: chan_sip.c:20161 handle_request_invite: Call from ‘140363’ to extension ‘3608137403’ rejected because extension not found in context ‘incoming’.

  13. I can make and recive calls but how do i enter an extension during a call?

    Thanks

  14. Hi,
    I wanted to know can we create sip trunk between two Asterisk server(To one with E1 From one without E1) Within a Lan Network.

  15. Hey hi this post of yours was really great thank u. . . can u tell me any disadvantage by using SIP TRUNK configuration in IP Phones. . . I need to implement on it can u please guide me. . . ???!!!

  16. Great article ! thank you

  17. Quality posts is the crucial to invite the viewers to go to see the web page, that’s what this site is providing.

  18. Good information. Lucky me I found your site by chance (stumbleupon).

    I’ve book marked it for later!

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>