Kamailio SIP Trunk Registration
Articles

Kamailio SIP Trunk Registration

SIP Trunk Registration is a method for Softphones to register with a VoIP system even though they may have dynamic IP addresses or may be behind NAT.
This blog entry will go through setting up Kamailio to be a SIP registrar. It will also briefly set up a softphone (namely Zoiper on Android) to register with Kamailio.
Once configured, the softphone will register periodically (typically every 60 seconds) with the Kamailio host on port 5060. Since registration takes place so frequently, Kamailio will be able to detect if the softphone IP changes and continue to route calls to/from it. Additionally, since it’s the softphone that’s reaching out to Kamailio when sending calls to the softphone, Kamailio can just point to the softphone IP and ephemeral port allowing it to reach the softphone even if it’s behind NAT.
Assuming a Debian-like server, Kamailio can be easily installed via apt-get. The registration feature of Kamailio will require the use of a database - for simplicity, SQLite will be used in this example.

1. Installation

Start by installing Kamailio and the associated SQLite module, optionally the command line interface for SQLite which can be useful for poking around the Kamailio database:
1
$ sudo apt-get install kamailio kamailio-sqlite-modules sqlite3

2. Database Creation

Next, set up the SQLite database. Open up the kamctlrc file with a preferred editor:
1
$ sudo vim /etc/kamailio/kamctlrc
Find the
1
DBENGINE
variable and set it to
1
SQLITE
:
1
DBENGINE=SQLITE
And set the
1
DB_PATH
variable to somewhere writable by Kamailio (
1
/tmp
works for this example):
1
DB_PATH=”/tmp/kamailio.db”
While editing the
1
kamctlrc
file, now would be a good time to also set the variable 
1
SIP_DOMAIN
. This is not strictly needed to create the database but will be needed later to actually run Kamailio and have a SoftPhone register with it. The
1
SIP_DOMAIN
variable will need to be set to the Kamailio host’s public IP or domain name:
1
SIP_DOMAIN=<Kamailio host’s public IP or domain name>
At this point, the database can be created with the kamdbctl command. The ownership of the file may also need to be changed to allow the Kamailio user read/write access to the database.
1
2
$ sudo kamdbctl create
$ sudo chown kamailio:kamailio /tmp/kamailio.db

3. Kamailio Configuration

Open up the
1
kamailio.cfg
file with a preferred editor:
1
$ sudo vim /etc/kamailio/kamailio.cfg
Add the following lines to the top of the
1
kamailio.cfg
file:
1
2
3
#!define WITH_SQLITE
#!define WITH_AUTH
#!define WITH_USRLOCDB
In the Defined Values section, add the following block to set the database location for SQLite:
1
2
3
4
5
#!ifdef WITH_SQLITE
#!ifndef DBURL
#!define DBURL "sqlite:///tmp/kamailio.db"
#!endif
#!endif
In the Modules Section, add the following block to have Kamailio to load the SQLite module:
1
2
3
#!ifdef WITH_SQLITE
loadmodule "db_sqlite.so"
``#!endif
Lastly, the
1
alias
variable needs to be set to the Kamailio node’s public IP or domain name and port.
1
alias=<Kamailio host’s public IP or domain name>:5060
That about covers the
1
kamailio.cfg
file. The next thing to tackle is the Kamailio default file:
1
$ sudo vim /etc/default/kamailio
Set the following variables to have the Kamailio service run as under the Kamailio user and group and to enable Kamailio to run
1
2
3
RUN_KAMAILIO=yes
USER=kamailio
GROUP=kamailio

4. Adding a User to Kamailio

Next, add a user to Kamailio - this is the account the softphone will use to register with Kamailio
1
$ sudo kamctl add testuser testpasswd
At this point,
1
sqlite3
command line interface can be used to poke around the database. Specifically, the
1
subscriber
table will have a list of users that should be able to register, e.g:
1
2
3
4
5
6
7
$ sqlite3 /tmp/kamailio.db
SQLite version 3.8.7.1 2014-10-29 13:59:56
Enter ".help" for usage hints.
sqlite> select * from subscriber;
1|testuser|104.197.207.1|testpasswd||2215e221e0006cdf04ed82aa
a0b9c366|d0a735afa4fc0bb97805e5f82da52736|
sqlite>

5. Setup Softphone (Zoiper on Android)

Zoiper is a softphone that can be installed from the Google Play store. Once installed, start up Zoiper and navigate to Config and then Accounts.
Next select Add account, and on the dialog that asks whether or not there are a username and password, select Yes , and then Manual configuration.
Choose SIP Account and set the Account name , Host , Username and Password.
The Username and Password should be the same as used in step 4 above, and Host should point to the Kamailio instance’s IP or domain. Once the account is saved, registration should be successful.
On the Kamailio side,
1
sqlite3
command line interface can be used to also check for successful registration. Successfully registered users will show up in the
1
location
table:
1
2
3
4
5
6
7
$ sqlite3 /tmp/kamailio_sqlite.db
SQLite version 3.8.7.1 2014-10-29 13:59:56
Enter ".help" for usage hints.
sqlite> select * from location;
1|uloc-56561efc-27ec-b|testuser||sip:testuser@208.54.80.137:44449;transport=UDP;rinstance=acddf60257622f19|
|2457352.40403356|-1.0|YJboWl_-kBRQd1Zt03ojJA..|4|2457352.40333912|0|0|Zoiper r34646|udp:10.240.0.2:5060|5087||0
sqlite>
That should be enough to get one started using Kamailio as a registrar. A future entry will cover integrating the aforementioned set up with FreeSWITCH and what the call flow looks like moving between the pieces.
Share on Social

Worth checking out

By using the site, you agree to our use of cookies. Accept and close Find out more here.