SIP Trunking • Last Updated 9/25/2023

Kamailio SIP Trunk Registration

Here we'll guide you through setting up Kamailio to be a SIP registrar. We'll also set up a softphone to register with Kamailio..

By Sara Weichhand

kamailio-logo-2015-150x150.png

kamailio-logo-2015-600x464

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:

$ 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:

$ sudo vim /etc/kamailio/kamctlrc

Find the DBENGINE variable and set it to SQLITE:

DBENGINE=SQLITE

And set the DB_PATH variable to somewhere writable by Kamailio (/tmp works for this example):

DB_PATH=”/tmp/kamailio.db”

While editing the kamctlrc file, now would be a good time to also set the variable 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 SIP_DOMAIN variable will need to be set to the Kamailio host’s public IP or domain name:

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.

$ sudo kamdbctl create $ sudo chown kamailio:kamailio /tmp/kamailio.db


3. Kamailio Configuration

Open up the kamailio.cfg file with a preferred editor:

$ sudo vim /etc/kamailio/kamailio.cfg

Add the following lines to the top of the kamailio.cfg file:

`#!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:

#!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:

#!ifdef WITH_SQLITE loadmodule "db_sqlite.so" ``#!endif

Lastly, the alias variable needs to be set to the Kamailio node’s public IP or domain name and port.

alias=<Kamailio host’s public IP or domain name>:5060

That about covers the kamailio.cfg file. The next thing to tackle is the Kamailio default file:

$ 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

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

$ sudo kamctl add testuser testpasswd

At this point, sqlite3 command line interface can be used to poke around the database. Specifically, the subscriber table will have a list of users that should be able to register, e.g:

$ 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, sqlite3 command line interface can be used to also check for successful registration. Successfully registered users will show up in the location table:

$ 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:[email protected]: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.



To learn more about SIP Trunking and some of it's uses, take a look at our SIP Trunking page our test it our for yourself in a couple of minutes in the Telnyx Mission Control Portal.

Share on Social
Jump to:

Sign up for emails of our latest articles and news

Related articles

Sign up and start building.