Pages

Saturday 14 November 2020

Setting Max Concurrent Call Limit on Kamailio



In this post I'll introduce "Setting Max Call Limit for a Customer Number or IP Address" to you on a Kamailio SIP proxy server. Kamailio is the most popular and flexible sip server which can be used as a proxy, registrar and loadbalancer like Opensips. It works based on "kamailio.cfg" and it does not know anything than this file. This basic tasks seperate Kamailio from Asterisk Freeswitch because these act as "call process servers" instead of being loadbalancer or proxy.


On Kamailio (working as a proxy), if you require to set a check for "Simultaneous Concurrent Call Limit" for your SIP trunks, probably you should read this post to achieve that. I'll show my way but I'm pretty sure there are other ways to realize this case. 


In my way, we will use dialog and sqlops module. Dialog module is used for keep current call number of our customer and sqlops is used for getting max call number from DB that includes needed information.


Before begining adding a new config you need to make sure whether both modules are loaded or not. 


loadmodule "dialog.so"

loadmodule "sqlops.so"


For paramaters make sure you have these lines:


# ----- dialog -----

modparam("dialog", "enable_stats", 1)

modparam("dialog", "db_url", DBURL)

modparam("dialog", "db_mode", 1)

modparam("dialog", "dlg_flag", FLT_DLG)

modparam("dialog", "profiles_with_value", "caller")


# ------ sqlops -----

modparam("sqlops","sqlcon", "cb=>mysql://kamailio:$password@$domain/kamailio")


Let's work on kamailio.cfg


Thanks to this config, script will check source ip of the incoming INVITE then check if it has a limit or not for this source IP. If is there a limit that is set in database, it will apply this limit via next script block. This block will compare concurrent calls number and concurrent max calls. If concurrent calls number reaches to max calls, it will send "503 Simultaneous calls limit reached" error message to remote side. 


#!define FLT_DLG 9

#!define FLT_DLGINFO 10

#!define FLT_DIALOG 4


        if (is_method("INVITE")) {

                $var(customer_ip) = $si;

                $var(limit_active)='';

                

                #check if there is a limit for this source IP.

                sql_pvquery("cb", "select limit_active from call_limit where customer_ip = $var(customer_ip)", "$var(limit_active)");

                

                if ($var(limit_active) == "true") {

                        $var(max_call)='';

                        dlg_manage();

                        

                        #check concurrent call number

                        get_profile_size("caller", "$si", "$var(SIZE)");


#check determined max concurrent calls number for customer IP

                        sql_pvquery("cb", "select max_call from call_limit where customer_ip = $var(customer_ip)", "$var(max_call)");

                        xlog("Number of calls present now is $var(SIZE)\n");


                        if( $var(SIZE) == $var(max_call) ) {

                                sl_send_reply("503", "Simultaneous calls limit reached");

                                xlog("Rejected calls with 503 Simultaneous calls limit reached\n");

                                exit;

                        }

                        #Keep this call in caller profile via dialog module

                        set_dlg_profile("caller","$si");

                }

}


Here is the DB table that is located in kamailio database.



mysql> describe call_limit;

+-----------------+-------------+------+-----+---------+----------------+

| Field               | Type             | Null    | Key | Default | Extra          |

+-----------------+-------------+------+-----+---------+----------------+

| task_id            | int(11)          | NO     | PRI | NULL    | auto_increment |

| limit_active     | tinyint(1)     | YES   |         | 0             |                |

| max_call          | int(11)         | YES   |         | NULL    |                |

| customer_ip     | varchar(30) | YES   | UNI | NULL    |                |

+-----------------+-------------+------+-----+---------+----------------+


For more details regarding modules please visit these official links; dialog module and sqlops module.






No comments:

Post a Comment