Anda di halaman 1dari 8

Sip trunk configuration

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, and 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 doesnt 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 dont 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 Asterisks logfile (with the specified syslog level), and Asterisks 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 Asterisks command-line interface, or restart Asterisk. To reload the SIP configuration and the dialplan, connect to the running Asterisks 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 is 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 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 ; The device must always register

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