CGrates – FreeSWITCH Interaction

In our last post we talked about setting rates in CGrates and testing them out, but what’s the point in learning a charging system without services to charge?

This post focuses on intergrating FreeSWITCH and CGrates, other posts cover integrating Asterisk and CGrates, Kamailio and CGrates and Diameter and CGrates.

Future posts in this series will focus on the CGrates side, but this post will be a bit of a sidebar to get our FreeSWITCH environment connected to CGrates so we can put all our rating and charging logic into FreeSWITCH.

CGrates interacts with FreeSWITCH via the Event-Socket-Language in FreeSWITCH, which I’ve written about before, in essence when enabled, CGrates is able to make decisions regarding if a call should proceed or not, monitor currently up calls, and terminate calls when a subscriber has used their allocated balance.

Adding ESL Binding Support in FreeSWITCH

The configuration for CGrates is defined through the cgrates.json file in /etc/cgrates on your rating server.

By default, FreeSWITCH’s event socket only listens on localhost, as it is a pretty huge security flaw to open it to the world, but in order for our CGrates server to be able to access we’ll need to bind it to an IP Address assigned to the FreeSWITCH server so we can reach it from elsewhere on the network.

<configuration name="event_socket.conf" description="Socket Client">
  <settings>
    <param name="nat-map" value="false"/>
    <param name="listen-ip" value="0.0.0.0"/>
    <param name="listen-port" value="8021"/>
    <param name="password" value="ClueCon"/>
    <param name="apply-inbound-acl" value="any_v4.auto"/>
  </settings>
</configuration>

Please setup the ACLs & password securely!

You may want to have CGrates installed on a different machine to your FreeSWITCH instance, or you may want to have multiple FreeSWITCH instances all getting credit control from CGrates.

Well, inside the cgrates.json config file, is where we populate the ESL connection details so CGrates can connect to FreeSWITCH.

"freeswitch_agent": {
        "enabled": true,
        "event_socket_conns":[
                {"address": "10.0.1.56:8021", "password": "ClueCon", "reconnects": -1,"alias":"Remote_FS_1"}
        ],
        "sessions_conns": ["*birpc_internal"],
        "empty_balance_ann_file": "/usr/share/freeswitch/sounds/en/us/callie/misc/8000/misc-your_call_has_been_terminated.wav",
        "empty_balance_ann_file": "/usr/share/freeswitch/sounds/en/us/callie/misc/8000/phone_not_auth.wav",
        "create_cdr": true
},

Dialplan Support

We’ll need to add the following config to our dialplan in order to tag in CGRates for the call.

 <extension name="unloop">
      <condition field="${unroll_loops}" expression="^true$" />
      <condition field="${sip_looped_call}" expression="^true$">
        <action application="deflect" data="${destination_number}" />
      </condition>
    </extension>
    <extension name="call_debug" continue="true">
      <condition field="${call_debug}" expression="^true$" break="never">
        <action application="info" />
      </condition>
    </extension>
   <extension name="CGRateS_Auth">
    <condition field="${cgr_notify}" expression="^$">
        <aciton application="log" data="In the CGRateS_Auth block" />
        <action application="info"/>
        <action application="park" />
      </condition>
    </extension>
    <extension name="CGRateS_AuthForbidden">
      <condition field="${cgr_notify}" expression="^-INSUFFICIENT_FUNDS$">
        <action application="log" data="Insufficent Funds" />
        <action application="set" data="proto_specific_hangup_cause=sip:403" />
        <action application="hangup" />
      </condition>
    </extension>
    <extension name="CGRateS_AuthForbidden">
      <condition field="${cgr_notify}" expression="^-UNAUTHORIZED_DESTINATION$">
        <action application="log" expression"CGrates Auth Forbidden" />
        <action application="set" data="proto_specific_hangup_cause=sip:403" />
        <action application="hangup" />
      </condition>
    </extension>
    <extension name="CGRateS_Error">
      <condition field="${cgr_notify}" expression="^-SYSTEM_ERROR$">
        <action application="set" data="proto_specific_hangup_cause=sip:503" />
        <action application="hangup" />
      </condition>
    </extension>
     <extension name="CGR Routes">
     <condition field="cgr_routes" expression=".+">
        <action application="log" data="In the CGR Routes block..." />
        <action application="set" data="cgr_route=${cgr_routes[1]}" />
      </condition>
    </extension>

Extension Support

Next we’ll need to tag the extensions we want to charge,

In order to do this we’ll need to set the type of the account (Ie. Prepaid, Postpaid, etc), and the flags to apply, which dictate which of the modules we’re going to use inside CGrateS.

FreeSWITCH won’t actually parse this info, it’s just passed to CGrateS.

<include>
  <user id="1001">
    <params>
      <param name="password" value="$${default_password}"/>
    </params>
    <variables>
      <variable name="accountcode" value="1001"/>
      <variable name="user_context" value="default"/>
      <variable name="effective_caller_id_number" value="1001"/>
      <variable name="outbound_caller_id_name" value="$${outbound_caller_name}"/>
      <variable name="outbound_caller_id_number" value="$${outbound_caller_id}"/>
      <variable name="cgr_reqtype" value="*prepaid"/>
      <variable name="cgr_flags" value="*resources;*attributes;*sessions;*routes;*thresholds;*stats;*accounts"/>
      <variable name="cgr_acd" value="30"/>
    </variables>
  </user>
</include>

If this is not set, the user won’t be charged.

And that’s pretty much it, when you restart FreeSWITCH and CGrates you should see in the CGrates log that it is connected to your FreeSWITCH instance, and when you make a call, FreeSWITCH will authorize it through CGrates.

We’ll get back into the nitty gritty about setting up CGrates in a future post, and cover setting up integration like this with other Platforms (Kamailio / Asterisk) and Protocols (Diameter & Radius) in future posts.

One thought on “CGrates – FreeSWITCH Interaction

  1. HI nick. you are doing a great work.
    I have follow step by step your series on cgrates . I am trying to calculate billing in realtime by integrating freeswitch and cgrates.
    Although , offline billing a CDR is working fine. But when I make a call freeswitch and try calculate the billing It gives me error.

    =========================
    “CGRateS [7787]: could not process answer for event de02cbfc-e677-4ac9-b8c1-3ba494addf64, error: RESOURCES_ERROR:NOT_FOUND
    Jun 23 06:01:49 debian cgr-engine[7787]: CGRateS [7787]: Could not terminate session with event de02cbfc-e677-4ac9-b8c1-3ba494addf64, error: RALS_ERROR:CHARGERS_ERROR:NOT_FOUND
    Jun 23 06:01:49 debian cgr-engine[7787]: CGRateS [7787]: error: processing event {“Tenant”:”cgrates.org”,”ID”:”3651efb”,”Time”:”2024-06-23T06:01:44-07:00″,”Event”:{“ACD”:30000000000,”Account”:”3001″,”AnswerTime”:”2024-06-23T06:01:58-07:00″,”CGRID”:”24330084454890b045c5f20b73f6f864ae723f44″,”Category”:”call”,”Cost”:-1,”Destination”:”1002″,”DisconnectCause”:”MANAGER_REQUEST”,”OriginHost”:”127.0.0.1:8021″,”OriginID”:”de02cbfc-e677-4ac9-b8c1-3ba494addf64″,”PDD”:10220000000,”RequestType”:”*prepaid”,”Route”:””,”SetupTime”:”2024-06-23T06:01:44-07:00″,”Source”:”FS_CHANNEL_HANGUP_COMPLETE”,”Subject”:”3001″,”Tenant”:”cgrates.org”,”ToR”:”*voice”,”Usage”:0},”APIOpts”:{}} with ChargerS
    Jun 23 06:01:49 debian cgr-engine[7787]: CGRateS [7787]: Failed processing CGREvent: {“Tenant”:”cgrates.org”,”ID”:”3651efb”,”Time”:”2024-06-23T06:01:44-07:00″,”Event”:{“ACD”:30000000000,”Account”:”3001″,”AnswerTime”:”2024-06-23T06:01:58-07:00″,”CGRID”:”24330084454890b045c5f20b73f6f864ae723f44″,”Category”:”call”,”Cost”:-1,”Destination”:”1002″,”DisconnectCause”:”MANAGER_REQUEST”,”OriginHost”:”127.0.0.1:8021″,”OriginID”:”de02cbfc-e677-4ac9-b8c1-3ba494addf64″,”PDD”:10220000000,”RequestType”:”*prepaid”,”Route”:””,”SetupTime”:”2024-06-23T06:01:44-07:00″,”Source”:”FS_CHANNEL_HANGUP_COMPLETE”,”Subject”:”3001″,”Tenant”:”cgrates.org”,”ToR”:”*voice”,”Usage”:0},”APIOpts”:{}}, error: ”

    ====================
    One more thing , I have changes the Tenant from cgrate.or g to “Telecard”. But it still show cgrates.org . Although I have reload the storeDB to cache.
    Can you pleasae give me some detailed guideline, what is the module need to configure in Cgrates for realtime billing .

Leave a Reply

Your email address will not be published. Required fields are marked *