FreeSWITCH with SIP Users in MySQL [Mod XML_CURL]

FreeSWITCH is one of the biggest attraction for me since my introduction to VoIP. Since I like it too much and work with it most of the time but couldn't post anything good related to Freeswitch so far. Luckily I got a link from one of my friend's blog Touchkanalogy about Freeswitch module mod_xml_curl sort-of saying "FreeSWITCH SIP Realtime". Like Asterisk's SIP real-time where SIP users are inserted into database and asterisk authenticates incoming SIP user REGISTRATIONs from database settings.

FreeSWITCH don't exactly work like asterisk. FreeSWITCH needs XML configurations for users' settings. Following my friend's blog and the source document for the freeswitch XML_CURL  module. I was able to successfully insert users in Database and incoming SIP user's authentication requests were processed according to the data in DB.



The Logic is simple. Incoming SIP requests are received by Freeswitch which asks a web-service about the details of the user. The web-server just queries data and if any relevant details are found returns them in XML format. Freeswitch accepts or reject the SIP request depending upon the XML response.


Ref URL:  http://wiki.freeswitch.org/wiki/Mod_xml_curl 
Another way to represent the above flowchart.
With SIP Proxy REGISTER Load-Balancer

Following are the commands and steps I performed to make this happen. Remember I had a CentOS server hosting Freeswitch so Ubuntu or other OS users need to find corresponding packages and directories accordingly.

root@FS_HA1#yum install mysql mysql-server php-pdo curl php-xml

Next Edit the php.ini file to include the extension for mysql and pdo.

root@FS_HA1#vim /etc/php.ini

Just below  this Huge tag


;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
Add these two lines.

extension=pdo.so
extension=pdo_mysql.so


Restart the web server to make changes effective.

root@FS_HA1#/etc/init.d/httpd restart
Then goto the freeswitch source directory. I already had FS installed so I installed the module explicitly.

root@FS_HA1#cd /usr/src/freeswitch
root@FS_HA1#make mod_xml_curl && make mod_xml_curl-install
Then enable the module in modules.conf

root@FS_HA1#cd /usr/local/freeswitch/conf/
root@FS_HA1#vim autoload_configs/modules.conf.xml

Uncomment the following tag by removing the  "<!--"and "-->"
<!-- <load module="mod_xml_curl"/> -->

Edit the settingfor the XML_CURL module.

root@FS_HA1#vim autoload_configs/xml_curl.conf.xml

The most importthing is to set the web-service URL where FS will send all the queries.

 <param name="gateway-url" value="http://localhost/fs_curl/" bindings="directory"/>

In my case the web-server is on localhost and I plan on using the sub directory fs_curl for the php pages.

Now its time to download the real thing.
root@FS_HA1#cd /usr/src/
root@FS_HA1#git clone git://git.freeswitch.org/freeswitch-contrib.git

root@FS_HA1#cd freeswitch-contrib/intralanman/PHP/fs_curl/
root@FS_HA1#mkdir /var/www/html/fs_curl
root@FS_HA1#cp -rf * /var/www/html/fs_curl
root@FS_HA1#cd /var/www/html/fs_curl/

Edit the DB connector values and debugging stuff from the global configurations file for FS_CURL
root@FS_HA1#vim global_defines.php

Edit the following lines.

define('DEFAULT_DSN', 'mysql:dbname=FS_DB;host=127.0.0.1');define('DEFAULT_DSN_LOGIN', 'freeswitch');
define('DEFAULT_DSN_PASSWORD', 'passw0rd');

If you want to debug uncomment the following (Not recommended for production servers)


define('FS_CURL_DEBUG', 9);define('FS_DEBUG_TYPE', 2); 
define('FS_DEBUG_FILE', '/var/log/fs_curl.debug');

root@FS_HA1#touch /var/log/fs_curl.debug
root@FS_HA1#chown apache:apache /var/log/fs_curl.debug

We need to modify the fs_directory.php file a bit to use the correct table names.
root@FS_HA1#vim fs_directory.php


CHANGE
[ON LINE:88]

FROM:$where_array [] = sprintf ( "domain_id='%s'", $domain ['id'] );
TO:$where_array [] = sprintf ( "domain='%s'", $domain ['id'] );

[LINE:106]
FROM:$query = sprintf ( "SELECT * FROM directory d %s %s ORDER BY username", $join_clause, $where_clause );
TO:$query = sprintf ( "SELECT * FROM directory %s %s ORDER BY username", $join_clause, $where_clause );

Save and Exit.

Create DATABASE
root@FS_HA1#cd /var/www/html/fs_curl/sql/
root@FS_HA1#vim +93 mysql-5.0-with-samples.sql
Delete this line all-together else you may get this error:

ERROR 1231 (42000) at line 93: Variable 'character_set_client' can't be set to the value of 'NULL
root@FS_HA1#mysql -uroot -p
mysql> create database FS_DB;
mysql> quit;
root@FS_HA1#mysql FS_DB < mysql-5.0-with-samples.sql
Create New users and passwords for them, domain needs to be added as well.
root@FS_HA1#mysql -uroot -p

mysql>use FS_DB;
mysql> insert into directory_domains values (3,'192.168.137.6');
Query OK, 1 row affected (0.00 sec)

mysql>INSERT into directory (username,domain) VALUES ("40277",3);
mysql>INSERT into directory (username,domain) VALUES ("50354",3);


GET THE "id" from the directory table for these newly created users.
mysql> select * from directory;
+----+----------+-----------------+
| id | username | domain          |
+----+----------+-----------------+
|  1 | 1000     | example.com     |
|  2 | 1001     | example.org     |
|  3 | 1002     | example.net     |
|  5 | 1003     | example.info    |
|  6 | 1004     | example.com     |
|  7 | 1005     | example.org     |
|  8 | 1006     | example.net     |
|  9 | 1007     | example.info    |
| 10 | 2000     | default         |
| 11 | 1009     | $${local_ip_v4} |
| 12 | 40277    | 3               |
| 13 | 50354    | 3               |
+----+----------+-----------------+
12 rows in set (0.00 sec)

mysql>


mysql> insert into directory_params (directory_id,param_name,param_value) VALUES (12,'password','S3cr3t40277');
mysql> insert into directory_params (directory_id,param_name,param_value) VALUES (13,'password','S3cr3t50354');
Thats All, now try registering your SOFT Phone using new username/password and see if your requests reach to WEB-Server.
You can troubleshoot the XML_CURL module by issuing the following command on FS Console.
freeswitch@internal> xml_curl debug_on
OK
For each XML response we'll now get an output on FS_CLI
mod_xml_curl.c:318 XML response is in /tmp/d8fc4b98-cb1a-11e1-b16b-2f327a1d3f98.tmp.xml

Comments