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.
If you check in the previous posts you will see that we had this SIP PEER set up in Asterisk –
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 –
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 –
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 –
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 22.214.171.124 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 –