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.
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
variable and set it to
And set the
variable to somewhere writable by Kamailio (
works for this example):
While editing the
file, now would be a good time to also set the variable
. 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
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
file with a preferred editor:
$ sudo vim /etc/kamailio/kamailio.cfg
Add the following lines to the top of the
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
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
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
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
$ sudo kamctl add testuser testpasswd
At this point,
command line interface can be used to poke around the database. Specifically, the
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 18.104.22.168 2014-10-29 13:59:56 Enter ".help" for usage hints. sqlite> select * from subscriber; 1|testuser|22.214.171.124|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,
command line interface can be used to also check for successful registration. Successfully registered users will show up in the
1 2 3 4 5 6 7
$ sqlite3 /tmp/kamailio_sqlite.db SQLite version 126.96.36.199 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.
Share on Social