Monday, 9 February 2009

Asterisk: A Bare-Bones VoIP Example p:2


After a match is made on ${EXTEN}, the applications start to be executed in the order in which they are listed by their priority values. There are a huge number of applications that are available, and additional applications are fairly easy to integrate. There is even an application called AGI that links to external programs, and there are Perl and Python libraries that allow for easy development of tools external to the applications built into Asterisk.

The most-used application is called Dial, and that is the application that rings a remote channel and then connects the two different channels together if there is an answer. The Dial application has some special abilities due to its multiple responses. If a Dial application gets an answer on the remote channel, then the two callers are bridged together and the call proceeds. After an answer, the only options are for one or both parties to hang up. When a hangup happens, the Dial routine exits with a non-zero status, and the priority list stops executing because we have lost the call -- this is a normal call termination.

If the Dial application rings the remote phone for 20 seconds (specified by the ,20 in our Dial statement) but there is no answer, Dial will exit and the next priority will be executed -- in our case, that next priority is a command to run the Voicemail application, which sends the caller to the "unavailable" greeting for the called party. If the Dial application gets a "busy" answer back from the remote phone, or the remote phone is not on-line, then the Dial application does something special: it adds 101 to the existing priority, and jumps to that priority. In our case, this means priority 102, which sends the caller to the "busy" greeting for the called party. Dial is the only application that has this special priority incrementing ability, though there are priority control applications that can give the administrator direct control to the priority, context, and extension such as Goto, and more sophisticated versions such as GotoIf, which can be used to evaluate expressions.

To get a list of applications, just type "show applications" at the command prompt, and then "show application xxxx," where xxxx is the application for which you want more information.


Asterisk currently runs on Linux > 2.4.x (various flavors) and OpenBSD 3.3. The OpenBSD version has not been tested thoroughly and should be considered "alpha" at the time of this writing (June 2003).

You will also need an SIP-capable phone, such as a Cisco 7960 or 7940, SNOM, Pingtel, or Grandstream. Alternately, you could get a software client for Windows such as the free client from Xten or a Linux SIP phone like kphone. Configuring these various SIP clients is outside of the scope of this article, but I will describe what is required to make them work with Asterisk as far as SIP login data is required.

I would strongly suggest a "hardphone" to start with, especially if you are using this as a demonstration platform. Anecdotal evidence strongly suggests that nobody except the most hardcore road-warrior wants to talk on the phone through their computer -- they want a handset that looks and acts like a phone. Plus, the SIP hardphones are usually debugged quite well, as the vendors cannot simply expect to roll out a "patch" to upgrade their customers without extensive preparation, so I have found that the hardphones are generally more reliable than softphones.

Your two phones or phone clients should have at least 80 kilobits of capacity each back to the Asterisk server, as we will be using G.711 codecs that take up quite a bit of bandwidth. There are other voice encoding methods that get as low as about 4kbps, but that's getting a bit too fancy for this article.

You will need a complete kernel source tree symlinked to /usr/src/linux since Asterisk requires certain header files from the kernel to compile correctly. If you are running OpenBSD, this is not required. You will need to have the following packages installed to complete a full installation: bison, cvs, gcc, kernel-source, libtermcap-devel, ncurses-devel, newt-devel, openssl096b, and openssl-devel.

SIP Client Configuration

You will need to configure your SIP clients so that they have their SIP gateway set to be the IP address of your Asterisk server. The usernames and passwords are contained below in the sip.conf file -- the username is the extension, and the password is listed in the secret= line for each extension. The configuration of the clients is often half the battle of getting VoIP to work, but once your particular client is understood, it normally becomes plug-and-play to add more phones. Your client must support registering with the SIP server -- I would suggest telling your client to register every 15 seconds or so during your experiments, to keep things re-registering quickly after Asterisk restarts. Later, you can take this back up to 1000 seconds or higher.

Installing Asterisk:

Go to the Asterisk home page and you can follow the "Download" link to get to the correct page. I would suggest not downloading the tarball, but using CVS to create your version of the application. The project sees an enormous amount of work done to it almost daily, so getting the most recent version via CVS is strongly suggested.

To download the latest CVS repository:

foo# cd /usr/src
foo# mkdir asterisk
foo# export
foo# cvs login

[type "anoncvs" without the quotes as your password]

foo# cvs checkout asterisk

[Here you should see the package being downloaded
and distributed into the ./asterisk directory.]

foo# cd asterisk
foo# make
foo# make install
foo# make samples

Now, to test, type asterisk -vvvvgc to start up Asterisk. If you end up with a prompt that says *CLI>, then you've successfully installed the demonstration configuration. If you get errors on the installation, I would suggest joining the IRC channel #Asterisk on FreeNode or asking the asterisk-users mailing list for advice.

Here's a compile hint. If you get "illegal instruction" errors on launching Asterisk, you probably have run across an issue with Asterisk compiling itself for the wrong processor type. Edit /usr/src/asterisk/Makefile and uncomment your processor type and comment out all other lines starting with PROC= in the processor definition section.

Asterisk is normally launched with safe_asterisk, which will be installed in /usr/sbin. To connect to the currently running version of Asterisk, launch a client with asterisk -r and you should see a command prompt.

Command-line Use

There are many instructions that are available from within Asterisk's CLI interface, and described below are several that you may find useful during your configuration. A full list may be displayed by simply hitting the "?" key, and applications may be displayed (but not executed) with "show applications."

reload - soft-restarts Asterisk and updates internal configs
with changes you've made to /etc/asterisk/* - does not hang up calls

show dialplan - shows the full dialplan of how your calls will be handled

sip show peers - shows all registered SIP clients

sip show channels - shows current "live" channels that are in
use by SIP clients (off-hook)

stop gracefully - shuts down Asterisk after all calls have hung up

stop now - shuts down Asterisk, hanging up any current calls

No comments: