A2billing with OpenSIPS and DID calls

This post follows on from these posts about using A2Billing and OpenSIPS together – http://sysadminman.net/blog/2013/integrating-opensips-with-asterisk-and-a2billing-4843

In those posts we looked at customers outgoing calls from a2billing. This time we’re going to look at one idea for setting up DID numbers. This is only one suggestion, there are others.

Below we are going to get OpenSIPS to look at the called number (which in this case will be incoming from our DID provider) and if it matches a DID number set up in A2Billing then we will send the call to the A2Billing/Asterisk server for processing. We will add a custom SIP header to the call before sending it to the A2Billing/Asterisk server so that Asterisk knows this is a DID call, rather than an outbound call for an A2Billing customer.

ON ASTERISK/A2BILLING

If you check in the previous posts you will see that we had this SIP PEER set up in Asterisk –

[opensips]
host=1.1.1.1
type=peer
context=a2billing-opensips-sip


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 –

1 thought on “A2billing with OpenSIPS and DID calls

  1. Ruban

    Hi Matt,

    First of all I would like to thank you for the great tutorial, I was struggling to setup Opensips with Asterisk but with aid of your tutorial I successfully set up my box. One issue I found with the OpenSIPS and DID calls is that when you have Sip dialler connected to the Opensips server, DID Calls are not processed by a2billing. I have looked at simultaneous call option but no luck. This means I have to route the calls direct to asterisk box and pass the call to a2billing. Any thoughts on this issue?

    Thanks
    Ruban

Comments are closed.