beardy's blog

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.

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>
; Our phones will register to Asterisk.
; Otherwise we would define the IP address or FQDN of the phone on the following line.
host=dynamic
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>
; Our phones will register to Asterisk.
; Otherwise we would define the IP address or FQDN of the phone on the following line.
host=dynamic
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


Get the book

Asterisk: The Definitive Guide: Open Source Telephony for the Enterprise 5th Edition
Asterisk: The Definitive Guide: Open Source Telephony for the Enterprise 5th Edition

Links

Creating SIP Accounts

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

Hi, I’m beardy

48 Comments

  1. 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!

  2. 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

  3. 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.

  4. 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!

  5. 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.

  6. 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?

  7. Hi,

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

  8. 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).

  9. 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

  10. Its really helpful for the beginners like me i love read yor articles bro. 🙂

  11. 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’.

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

    Thanks

  13. 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.

  14. 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. . . ???!!!

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

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

    I’ve book marked it for later!

  17. Thank-You! Dude, you da man!!! Can’t believe I’m making and receiving calls through my Asterisk PBX and SIP line 🙂

  18. Nice article!

    I want to share my experience. For me, I’m going to setup SIP trunk(s) between two Asterisk servers, one in city1, one in city2. Here’s my configuration:

    SIP.conf in city1

    [city2-asterisk]
    type=peer
    context=from-city2
    language=zh_CN
    secret=This_is_the_password_for_city2-asterisk
    host=A.B.C.D

    defaultuser=city1-asterisk
    fromuser=city1-asterisk
    remotesecret=This_is_the_password_to_connect_to_city2-asterisk
    fromdomain=A.B.C.D
    fullname=hey, this is my SIP trunk to/from city2-asterisk

    ; behind NAT
    ; [Dec 18 17:02:39] WARNING[17317]: sip/config_parser.c:812 sip_parse_nat_option: nat=yes is deprecated, use nat=force_rport,comedia instead
    nat=force_rport,comedia
    ; send keep-alive packets
    qualify=yes

    extensions.conf in city1

    [globals]
    ExtensionTrunk = DAHDI/g1 ; This is the trunk connected to our traditional PBX where our phones are connected to

    ;===============================================================================
    ; This is the context for call from city2(remote) to city1(here) via SIP trunk
    ; city2 -> city1: 89XXX, and yes, 89XXX is our internal number in city1
    [from-city2]
    exten => _89XXX,1,NoOp(Call from city2 to city1 via SIP trunk)
    same => n,Answer
    same => n,Dial(${ExtensionTrunk}/${EXTEN},60)
    same => n,Congestion
    same => n,Hangup

    ;===============================================================================
    ; This is the context for call from city1(here) to city2(remote) via E1 trunk
    ; city1 -> city2: 88XXX and 83XXX, and yes, 88XXX and 83XXX are our internal numbers in city2
    [from-pstn]
    exten => _8[83]XXX,1,NoOp(Call from city1 to city2 via E1 trunk)
    same => n,Dial(SIP/guangzhou-asterisk/${EXTEN},60)
    same => n,Congestion
    same => n,Hangup

    The configurations in city2 is similar to city1.

  19. Good Morning: My Asterisk server is all set with my SIP provider (Reallinx) However, I can only receive one incoming call at a time. If attempted the caller receives a busy signal. Asterisk -rvvv show the call coming in. Any help would be greatly appreciated.
    Thank you.
    John W

  20. This is what I get after I click on Get an Ekiga PC-to-Phone account in Ekiga. Nonetheless, I have some criedt by another SIP provider, so I tested this from Ekiga. I was able to make a call and it was not too bad but the quality was not very good (some noise introduced when somebody speaks, a bit choppy as well), although I tested several codecs. Then I tested Twinkle for the same thing and it worked great completely clean voice from both sides! I am afraid that it might be caused by ALSA if I use ALSA in Twinkle instead of OSS I have similar problems as in Ekiga which uses ALSA only. I also tried to make a video call but without success so far both sides can see the webcam works fine in Ekiga when previewing before call but inside the call, there is no picture from remote side, on both sides. Well, it is in the beta version and after these problems are solved it might be a nice piece of software indeed!

  21. Any issues with doing a single register statement and doing 2 contexts for a single sip provider? Inbound on 1 and outbound on the other to trying to keep it a little cleaner?

  22. I realize this is not intended to be an all-inclusive example, but for those using it as a reference — you will need to make some modifications if you might need to calll out to Emergency Services like 911 in the US.

  23. it’s great to see the changes in the file (/etc/asterisk) via telnet while updating it in file editor (Asterisk/1.8.13.1), though it is beta so far.
    the file editor leaves out the ; comments, which makes it quite readable.
    thanks for posting it.
    now I have to set the variables for 12connect voip provider.

  24. Hi, I’m registering ims sip with asterisk incoming and outgoing are working fine but trunk registration is not stable it gets deregister automatically provider said asterisk is not sending 200ok for my request

  25. Does anybody know info on how I can have a SIP trunk (6 channels), and any incoming call on it automatically connects to another SIP trunk that also has 6 channels?
    I am trying to conenct an intercom system and Vocera. Both of these systems connect on SIP trunks, but I need a SIP trunk to connect to a SIP trunk.
    I have both trunks connected to TrixBox just fine, and I can test outbound calls to each using a softphone. But I cannot see how to setup incoming call routing to get to another trunk?? It only allows me to send incoming calls to an extension??

  26. Excellent weblog here! Additionally your website a lot up fast!

    What web host are you the usage of? Can I am getting your associate
    link for your host? I wish my site loaded up as quickly
    as yours lol

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.