An Introduction to the Asterisk PBX
by zeitgeist
Recently, I got the chance to work with the Asterisk software.
Asterisk is an open-source Private Branch Exchange (PBX) which is kinda like a Swiss Army knife if you want to offer VoIP or traditional telephony services or if you want to make a bridge between them.
In this article I want to give a quick overview on the capabilities of the software and help you set up your own Asterisk server purely for VoIP (SIP protocol). Connecting the PSTN to the Asterisk box is beyond the scope of this article but is also not too difficult once you understood the concept. The learning curve for this software is very steep. Consider this as a small "lift".
The software is available for Linux, *BSD and OS X, I have also seen some implementation for Win32 but I will stick with telling you how to get it to work under Linux (any recent distribution should be fine on any standard PC, I even got it to work on a 400 MHz thin client booting from USB memory).
After you have successfully installed the software either from compiling it from www.asterisk.org or from your favorite package management, go ahead and start the software issuing the command asterisk -vvvvvc as root. This should start Asterisk in a pretty verbose mode and - if everything went well - drops you into the Asterisk Command Line Interface (CLI).
From the CLI you can do some administrative stuff.
Typing help always helps.
Typing help sip as an example gives you all the available help topics for SIP.
If everything started fine, exit the CLI by typing stop now which also halts Asterisk.
Most of the magic happens because of the configuration files which can usually be found under /etc/asterisk/. The most important ones that we are going to look into for this article are sip.conf and extension.conf Asterisk, both of them cluttered up with a lot of examples which are worth reading, but unsuitable for beginners to understand.
Some Theory
Asterisk organizes its extensions in so-called contexts, assuming there is a context "foo" and another context "bar" which both have extensions assigned to them. Each extension is a softphone, hardphone, or maybe an announcement or any other application that provides.
Each of the extensions is able to call other extensions in it's context, however it is by default not allowed to call from one context into another. This can however be archived when including one context into another. Through this inclusion one can create a type of hierarchy ("foo" includes "bar", but "bar" doesn't include "foo", so only the extensions from "foo" are allowed to call extensions in "bar" but not the other way around).
Each extension in a context is always defined by a number or an expression that evaluates numbers. More on this later, bear this little theory in mind but no need to memorize it.
Setting Up SIP Accounts
Now we would like to setup some SIP accounts for our Asterisk installation. You should have at least one SIP softphone available to try out your configuration.
X-Lite is a softphone available for Win32, Linux and Mac OS X, so you can grab a copy of it.
In the sip.conf configuration file, create an entry for each of your SIP phones that look like the following (note that for each individual SIP hard and softphone, these settings need to be adjusted as the phones support different things):
[xlite] username=8081 type=friend secret=123 qualify=no nat=no host=dynamic dtmfmode=rfc2833 callerid="X-Lite" <8081>Now you have setup a SIP account with username: 8081 and password: 123
We will not worry about the rest of this file for now, although make sure that you have at least the [general] section of that file from the default configuration that comes with the Asterisk installation.
Add as many SIP accounts as you want (you should add at least two so that they can call each other).
Start Asterisk again and make sure that the command sip show peers shows all the accounts that you have setup in the config file.
Now setup your X-Lite softphone to connect to your Asterisk server:
System Settings -> SIP Proxy -> Default -> Enabled: Yes, Username: 8081, Authorization User: 8081, Password: 123, Domain/Realm: IP, SIP Proxy: IP, Out Bound Proxy: IP, Register: Default
Where IP is the IP of your Asterisk server (can also be 127.0.0.1).Make sure that X-Lite logs in to your server. Watch the Asterisk CLI and type again sip show peers which should show you the IP address of the X-Lite phone(s).
Make Your First Call
If you haven't touched the extensions.conf file yet, go ahead and call the number 1000 from your softphone.
This gives you a menu that the default configuration of Asterisk supplies for you. If everything worked so far, you will hear a friendly greeting and you can play around with the menu. You will see a lot of output on the CLI because we started Asterisk in such a verbose mode.
Creating Extensions
Most of Asterisk's magic happens in the extensions.conf configuration file.
Make a backup copy of it if you want to preserve the nice menu that you have just dialed in, otherwise delete everything out of there, except for the [general] and [globals] sections. Each of these blocks that start with [somename] are the contexts I mentioned earlier. The context [default] should also always be there, this is where Asterisk starts to look. Create an extension in the "default" context by inserting something like this:
[general] exten => 8081,1,Ringing() exten => 8081,2,Dial(SIP/xlite,45,m) exten => 8081,3,CongestionWhat we are doing here is creating the extension 8081.
If Asterisk detects that someone has dialed the extension 8081 in the context "default," this block will get executed. The first number is always the extension, then comes a number that identifies in which order the statements for this extensions should be evaluated and executed.
This is what Asterisk does.
- Generate some ringing for the caller.
- Execute the Dial() function with some parameters.
These parameters are always in the form of: PROTOCOL/NAME,TIMEOUT,OPTIONS
Here the protocol is SIP and the NAME is the value of the block that we have identified in the sip.conf file. Here this block is called xlite (compare with sip.conf).
The other options mean that Asterisk will try to connect the call for 45 seconds and play some music for the caller while doing so. When the SIP/X-Lite phone picks up, the call is routed from the caller to the SIP phone being called.
Save the file, and start Asterisk again. On the CLI execute the show dialplan command, this will show you the extensions that are available.
If you have a second softphone configured with Asterisk, dial the 8081 extension from that one and the first softphone should ring.
If you have not setup a seconds softphone you can dial from the CLI: dial 8081@default which should also let the softphone ring (type hangup to hangup).
Where To Go From Here?
Your next step should be to familiarize yourself with the available applications that come with Asterisk and that can be called from the dialplan. There is for example an MP3 player that plays MP3s to the caller, there is the very neat voicemail application which gives you your own personalized voicemail and delivers the voicemails to you via e-mail, you can create menus such as the example menu, you can create conference rooms.
Another step would be to connect your Asterisk server with a SIP or IAX service from the Internet so you can start calling other people and also be reachable via a regular phone number from the PSTN (just search for VoIP provider). You can also connect the PSTN directly to your Asterisk server using ISDN or analog phone lines. For this however, some special hardware is needed.
An important site to look for tutorials and configuration examples is www.voip-info.org
Check my site (geisterstunde.org) for some Asterisk hacks.
Greetings to dodoex, macglove, beatle, albeu, poeggi, everyone else on dotsec and to the CCC machackers...