Kamailio A2billing Asterisk
Kamailio A2billing Asterisk
A2Billing SIP Customer --> OpenSIPS --> A2Billing/Asterisk --> Call provider
--> A2Billing/Asterisk --> Call provider
--> A2Billing/Asterisk --> Call provider
This diagram above shows calls going to 3 different A2Billing/Asterisk servers. In the example config
there is just one set up, but it will be obvious how to add more.
Also, in OpenSIPS there are 2 different ‘load balancing’ modules. There is one called ‘dispatcher’
which in unintelligent and just send the calls to a group of A2Billing/Asterisk servers. And there is a
module called ‘load-balancer’ which knows the state of each A2Billing/Asterisk server and evenly
distributes the load across them. For simplicity in this example I will be using the ‘dispatcher’ module.
This guide assumes that you have –
• a working A2Billing/Asterisk server in place
• a working OpenSIPS v1.8 server in place
• created a database called ‘opensips’ (as per the OpenSIPS install instructions) that is on MySQL
running on the A2BIlling/Asterisk server
We are going to have both the A2Billing and OpenSIPS databases running on the A2Billing server so
that we can integrate the two
In part 2 I’ll discuss some of the prerequisites and the database setup.
}
Finally, we called route(RELAY) several times in the script above, and we define that here, and a
couple of other bits, we forward the packets with t_relay –
route[RELAY] {
if (!t_relay()) {
sl_reply_error();
};
exit;
}
failure_route[GW_FAILOVER] {
if (t_was_cancelled()) {
exit;
}
if (t_check_status("(408)|([56][0-9][0-9])")) {
xlog("Failed trunk $rd/$du detected \n");
if ( ds_next_dst() ) {
t_on_failure("GW_FAILOVER");
t_relay();
exit;
}
send_reply("500","All GW are down");
}
}
And that’s the end of the config file!
The code above is definitely not designed to be totally cut and paste. You are going to have to check
some documentation and have a fair understanding of what’s going on and how the call is being
handled. I would also suggest learning the xlog command and some of the variables available. This you
can use at various points in the script to log some output and see why you calls might be failing.
If anyone experienced with OpenSIPS (or Kamailio) can offer any suggestions for how to improve the
config I’d be interested to hear them. Also, of anyone with Kamailio experience could let me know
how different that config would look in that I’d be interested to hear that too. Thanks!
So any call coming from our OpenSIPS server is sent to the context called a2billing-opensips-sip in
Asterisk. We will add a few lines to that context now (which is in extensions.conf) so that it looks like
this –
[a2billing-opensips-sip]
exten => _X.,1,Gotoif($["${SIP_HEADER(P-didnumber)}" != "yes"]?notdid)
exten => _X.,n,DeadAgi(a2billing.php,1,did)
exten => _X.,n,Hangup
exten => _X.,n(notdid),Set(CDR(accountcode)=${SIP_HEADER(P-Accountcode)})
exten => _X.,n,DeadAGI(a2billing.php,2)
exten => _X.,n,Hangup
So if the incoming SIP INVITE has a SIP HEADER called P-didnumber set to yes then we send the
call to a2billing.php as a DID call. We will set the P-didnumber header in OpenSIPS before we pass the
call to Asterisk. If the call doesn’t have that header set then we assume it’s a customer call and pass it to
A2Billing for processing.
IN THE MYSQL OPENSIPS DATABASE
If you check out the previous posts you will see we have our OpenSIPS and A2Billing database on the
same server. We are going to create a view in the ‘opensips’ database that looks at the ‘did’ table in the
A2Billing database –
mysql -p opensips
CREATE VIEW opensips.did_number AS
SELECT did AS did
from mya2billing.cc_did
So now when you create a DID number in A2Billing it becomes a valid DID number in OpenSIPS too.
ON THE OPENSIPS SERVER
Finally we do the OpenSIPS config in /etc/opensips/opensips.cfg. This is a modification to the setup
described in the previous post. These lines should go just before we check if the call is coming from the
IP address of an A2Billing customer –
avp_db_query("select did from did_number where did='$rU'","$avp(didnumber)");
if ($rU = $avp(didnumber)) {
remove_hf("P-didnumber");
append_hf("P-didnumber: yes\r\n");
route(RELAY);
};
So we’re doing a SQL query on the did_number table we created in MysQL, and if we find a matching
DID number we create a P-didnumber SIP header and set it to yes. Then we route the call to Asterisk
for processing.
Our DID provider will send the call to our OpenSIPS server, that checks that the call is coming to a
valid DID number on the database, and then sends it to A2Billing for processing.
Bear in mind that we will do this query for every SIP INVITE that hits the OpenSIPS server. I’d
definitely recommend getting used to the xlog command. That way if the above code doesn’t work as
expected you can insert some logging in the script to figure out what’s going on as the call is being
processed.
THE DID PROVIDER AND TESTING
Now if we want to set up the DID number 17062221111 and our OpenSIPS server IP is 1.1.1.1 we
would get our DID provider to send the call to the SIP URI – [email protected]
and in A2Billing we would create the DID number as normal –