23. Nov 2014 09:11
No comments

Asterisk as a virtual switchboard

Making a call with Asterisk

I worked with Asterisk for some years now and would like to share my experience with you in this article. Today I run all my phone calls and many other operations through asterisk. Especially when it comes to long distance calling and routing calls. My first contact with Asterisk was in 2002 when I bought an imported Bigium FXO card from Belgium and crashed the main switchboard appliance with it. Until today I do not know what caused this failure. This time I wanted to install Asterisk on the Raspberry Pi to provide my computer, my tablet and the Android phones a few additional phone services.

Objective of my Asterisk system

Since the beginning I learned that Asterisk is a very powerful and mighty system with which you can provide any solution when it comes to telecommunication. This makes it more complex to install and administer Asterisk, but once you learned how to use it is stunning und unbelieveable. The most important starting point is to create yourself a plan on what you want to achieve with your Asterisk installation. The following shows the devices I am using with my installation.

  • Motorola Moto G LTE Android Smartphone with CSipSimple
  • Samsung Galaxy Tab 3.0 tablet with CSipSimple
  • Lenovo X230 with openSUSE 13.1 and Linphone
  • Raspberry Pi Model B with Raspbian and Asterisk 1.8

This just gives you a rough overview of what devices you can use with Asterisk and of course you can use a number of other devices and the system is extendable. It is good however to have a list of devices in your plan. Next up you should define what services you want to provide to the devices. 

  • Calls between the different devices
  • Calls to landlines and mobile networks worldwide

For calls between the devices all they need is a SIP softphone or SIP software with which they can connect to the Asterisk server. When it comes to the worldwide phone calls we will use Localphone.com as a SIP provider, but you can use any that you want.

Asterisk installation on the Raspberry Pi

These installation instructions are ment to be used on the Raspberry Pi and its Raspbian operating system, but it can also be applied on every Ubuntu and Debian as they work on the same base system. It starts with installing Asterisk on the Raspbian first.

Afterwards the Raspberry Pi is loading hundreds of megabytes and install the Asterisk system on the Raspbian operating system. I recommend using at least a 32GB SD Card with Class 10 for maximum speed. After Asterisk is installed you finally need to start it.

 The connection to the Asterisk server, to check status and functionality, can be done using the CLI or "Command Line Interface" which you can start by typing the following command into the terminal.

There are a number of commands the CLI supports, but for the installation part we do not need them at all. Most important is to ensure Asterisk is running and you can access the system through the terminal and command line. In the next steps we will configure the devices.

Important: after editing the sip.conf configuration file you need to log into the AsteriskCLI and execute the command "sip reload" to make Asterisk reload the sip.conf configuration file. With the command "dialplan reload" you can make Asterisk reload the extensions.conf that includes your dial plan. That is very important as you otherwise forget it and wonder why the changes you made do not go into effect.

Configuring the devices in the sip.conf file

As described earlier, we want to connect all devices to Android using a SIP software or softphone. The devices need to be put into the sip.conf file and can login to the Asterisk server afterwards. My sip.conf file looks like the following.

In the section "[general]" all general settings for the Asterisk SIP configuration are held. There you provide the port of the Asterisk SIP server for example, for security reasons the local network behind the NAT router that the Asterisk is in. If you would remove the general section it would still work.

£32.96
Order now »
Asterisk: The Definitive Guide
Versandfertig in 1 - 2 Werktagen

The section "[1000]" marks the phone number and sip connections. If for example the phone number of the device is not 1000, 2000 or 3000 but 4711 then you could change it to [4711]. The setting "permit" defines from which network the device is allowed to login from. My devices are allowed to login from the local network only. The setting "type" defines what each device is allowed to do. The value "friend" allows the device to receive and initiate calls while "peer" is allowed to receive calls, but not initiate calls itself. Context defines a type of group and is reused in the extensions.conf, but we'll discuss that later in this article. With the setup of "host=dynamic" we define that the device is allowed to login from any IP rather than a specific one. Last but not least the setting "secret" defines the password each device has to use to login.

Creating the dial plan in the extensions.conf

After we entered all the SIP devices in the sip.conf, the devices can now log in. However it is for no use as they cannot call each other. My first basic extensions.conf looks like the following.

In the configuration we see the known sections we saw in the sip.conf. The section "[globals]" for global variables and "[general]" for general settings. Interesting for us is now the "[internal]" section which defines the context for internal calls. The section at the bottom is the context for our phone and is called "[phones]". With "include => internal" we include "internal"-context in the "phones"-context. The actual dial plan lies within the "internal" context which in my case is a one-liner and says that all four digit numbers that are called will be called as a SIP extension. This means if someone dials 1000, he will reach the device with the SIP extension 1000.

Dial plans in Asterisk can become very complex and offer a wide range of possibilities. You need quite some time to get familiar with all the settings and commands. For this article I decided to only show the most important settings and commands to get started with a basic setup. There will be more Asterisk articles in the future that cover more detailed setups.

Configuring the SIP-devices for Asterisk

Now we are getting close to making our first call. For that we need to configure the devices to log onto the Asterisk system and call each other. That is different with every SIP client and I will describe the configuration for the most popular SIP clients. It may become complicated with hardware SIP phones.

Configuration of CSipSimple for Asterisk

The configuration of Android phones is relatively simple and just requires settings such as server name, user name, server address and the corresponding password. The user name in this case is the phone number with our example of 1000, 2000 or 3000. The password is the one we defined in the "secret" setting value in the section of each device within the sip.conf.

As SIP server we provide the IP address or the host name of our Asterisk system. Beforehand we made sure that the ports on the Asterisk system for UDP on 5060 as well as 10000 to 20000 are open for inbound and outbound traffic. As soon as you click "save" CSipSimple will start to connect to the Asterisk server and the saved settings will get a "green" icon.

Configuration of Linphone for Asterisk

Linphone is a GNOME-based SIP client that is relatively simple and ideal for phone tests or making phone calls in Linux in general. It exists since the first Asterisk versions back in the 2000s. 

Linphone configuration for Asterisk

Linphone is a Linux application and therefor has lots of configuration options as this is usual with Linux applications. It took me quite a while in the beginning to find the right settings dialogs although I am using Linphone for some years now. Linphone as well needs the SIP server, the username and password. The screenshot shows how you set the configuration in the settings "Manage SIP accounts" and "Proxy accounts".

Configuring the external SIP connection of the VoIP provider

Although the devices can now call each other, we haven't reached our objective. We wanted to have the possibility to call anyone worldwide. For this I picked the VoIP provider Localphone because the voice quality is good and the cost is the lowest I found on the net with a professional provider. Localphone provides cheap worldwide rates also with mobile networks. On his website Localphone also provides step-by-step configuration for Asterisk: Asterisk and Localphone

In my sip.conf I now added the following settings for Localphone so that the SIP connection to Localphone is set up and Asterisk can log into Localphone's SIP server. The example shows only the settings that need to  be added to the configuration and I stripped out the rest.

Afterwards you need to reload the sip.conf file by executing "sip reload" in the Asterisk CLI and Asterisk will reload the file and its configuration. Once done you can see how Asterisk tries to connect to Localphone. That may fail if you are behind a firewall or a NAT router.

Port forwarding for Asterisk in the firewall

Generally the UDP port 5060 and the UDP ports from 10000 to 20000 need to be forwarded to the Asterisk machine for inbound traffic so that the login at Localphone and other SIP providers works.  In my OpenWrt router the firewall configuration looks as follow.

As soon as you restart the firewall or the firewall rules become active, Asterisk can connect to the Localphone SIP server. If you execute "sip show registry" in the Asterisk CLI it will list all existing SIP connections.

Dial plan and its configuration for external calls

As the last task we need to amend the dial plan so thatr the devices with the number 1000, 2000 and 3000 can initiate calls into external phone networks. In the extensions.conf we need to put the following lines into the "[outgoing]" context.

With this section we provide Asterisk the information that outgoing calls with a leading 0 are dialing through Localphone. This setting lets all devices call through Localphone into external networks. If we now want to configure the incoming calls with the "incoming" context to make all phones ring when incoming calls arrive then it will look like the following.

This lets all registered SIP devices with the numbers ring when a call arrives. With this last configuration section we have finalized our switchboard with Asterisk and the devices can call each other as well as any valid phone number in the world.

Help for Asterisk and the free Asterisk book

The book "Asterisk - The Future Of Telephony" from the well known publisher O'Reilly is available free of charge. It is really a very good book, but requires you to already have good Linux, Unix skills and strong knowledge about networking. Beginners should pick easier and simpler books tailor made for them. If you need help you will always find it in the #asterisk channel on irc.freenode.net which is perfect for experienced users that are not getting any further with the advanced books. There is also an official Asterisk Forum for support. Apart from that I recommed concentration, a slient environment and caution when working with Asterisk.

Asterisk is an amazing system and specifically together with the Raspberry Pi it becomes unbeatable. In your home environment when you do not want a server rack in your living room to boost your electricity bill the Raspberry Pi for Asterisk is ideal and delivers strong performance. The form factor is significantly smaller than existing switchboards.

Books about the topic „Asterisk“

The following books are all about the topic "Asterisk" and are highly recommended. I have not read all of these books, but a good number of them and some of them I used for my research as well.
£23.50
Order now »
Asterisk: The Definitive Guide
Russell Bryant, O'Reilly Media
£2.74
Order now »
Asterix the Gaul
René Goscinny, Orion Children's Books
£7.11
Order now »
Omnibus 1: Asterix the Gaul, Asterix and the Golden Sickle, Asterix and the Goths
René Goscinny, Orion Children's Books
£5.63
Order now »
The Asterisk War, Vol. 3 (Novel): The Phoenix War Dance
Yuu Miyazaki, Yen Press

Comments about the topic „Asterisk as a virtual switchboard“

If you like you can leave a comment about the topic and exchange with other reads. In order to comment you need to login and then you can start immediately.
Login now to comment