The approach used in that document is to use Kamailio database and create database views for Asterisk, a good approach if you started with Kamailio and want to add Asterisk for media services, mainly being about voicemail.

Soon I will take the time to upgrade that document for Kamailio 3.0.x and Asterisk 1.6.2, the latest stable versions.

However, this document uses the opposite approach - use Asterisk database to get the SIP user profile. Kamailio will be used to scale Asterisk, by handling the user authentication and registration. The authentication module in Kamailio can be configured to connect to any database and fetch the password from custom table and column, therefore creation of a database view is not really required, unless you want for other purposes.

This tutorial does the installation from sources, uses MySQL as database server and unixodbc for Asterisk realtime. The steps are given for Ubuntu/Debian operating systems.

Architecture

reuse as much as possible the default Asterisk relatime configuration

handle authentication in Kamailio

handle user location in Kamailio

routing of calls between local phones is managed by Asterisk

media services are handled by Asterisk according to Asterisk dialplan

routing of other SIP messages not related to calls are handled by Kamailio directly

Registration

Kamailio does authentication for registration. If successful, it notifies Asterisk with a new REGISTER that the phone is available at its IP.

Call Initiation

Call authentication is handled by Kamailio. When a new calls arrives and it is authenticated, Kamailio forwards it to Asterisk. If destination number is online, Asterisk will send the call back to Kamailio since the contact of destination is Kamailio IP. Then Kamailio will do location lookup and send to destination phone IP.

Install UnixODBC

To install the UnixODBC devel libraries, run:

apt-get install unixodbc-dev

If your operating system does not provide a package for it, download the sources from http://www.unixodbc.org/, compile and install. Then tune the Asterisk compilation system if the unixodbc is not detected automatically.

If you save it to asterisk.sql, then you can load it to MySQL server with:

mysql -u root -p <asterisk.sql

Before executing the SQL script, be sure you change the password for MySQL asterisk user, in line:

GRANT ALL ON asterisk.* to asterisk@localhost IDENTIFIED BY 'asterisk_password';

sipusers is the standard table required by Asterisk to store SIP user profile, with one extra column sippasswd where will be stored the password for SIP authentication. By default, Asterisk uses the column secret for SIP user password, but if that is filled in, Asterisk will ask for authentication again, resulting in double-authentication which we want to avoid.

sipregs is used to store SIP registrations. Registrations can be stored in sipusers tables as well, in case you do not want a separate table. Just omit the appropriate entry in /etc/asterisk/extconfig.conf.

voiceboxes is used to store voicemail box profiles and has the standard structure required by Asterisk. Storing voice box profiles in database allows to run several instances of Asterisk that can be load balanced or used in fail-over mode to store or listen to voice messages.

voicemessages is used to store voice messages and has the standard structure required by Asterisk. Storing voice messages in database allows to run several instances of Asterisk that can be load balanced or used in fail-over mode to store or listen to voice messages.

version is a table used by Kamailio to check database structure version. You can just forget about it once you create it.

Asterisk Configuration

In case you need to cache the realtime users, then edit /etc/asterisk/sip.conf and set:

rtcachefriends=yes

Dialplan Configuration

It is up to you what dialplan you build in /etc/asterisk/extensions.conf. Practically is nothing special for this configuration, as phones will appear in Asterisk with contact address pointing to Kamailio IP and port.

Sample data

For testing purposes, here is a sample that can be plugged in /etc/asterisk/extensions.conf:

In case you use sipregs you have to create a record for each extension where to set the 'name' to value of 'name' from sipusers. The rest is populated by Asterisk from registrations.

Kamailio Configuration

This configuration file is an update of default Kamailio 3.0.x configuration file. It is easy to spot the changes with diff or following #!define WITH_ASTERISK (i.e., the parts within #!ifdef WITH_ASTERISK … #!endif.

Practically, if you want to disable the routing through Asterisk, remove the line:

#!define WITH_ASTERISK

The define directives are supported only starting with version 3.0.0. Also, registering to Asterisk in behalf of phones setting the contact address to Kamailio IP and port is a feature introduced in Kamailio 1.5.x, don't try this config with other forks of SER, working variants are Kamailio 3.0.x+ or SER v3.0.x+.

Config File

Entire config file is pasted below. Do not forget to change the listen IP, port for Kamailio and Asterisk. In this example, Kamailio listens on IP 192.168.178.23 port 5060 and Asterisk listens on IP 192.168.178.23 port 5080.

Other Benefits

transport layer gatewaying - Kamailio has mature implementations for UDP, TCP, TLS and SCTP, therefore you can use it in front of Asterisk to translate between these protocols

load balancing - you can use several instances of Asterisk, Kamailio can do load balancing among them

high availability - Kamailio can be configured to re-route the call if selected Asterisk box does not react in a given period of time, e.g., if one Asterisk is not responsive in 2 sec, sent the call to another Asterisk