Manipulating SIP To: headers on registered endpoints to reflect DID numbers
Sometimes, when routing calls to endpoints that are registered with your system, you would want to utilize custom SIP To:
headers. For example, if you are routing DIDs to a PBX or switch, the device you are sending the call to might expect the phone number you wish to reach in the To:
SIP header. However, the customer or PBX may have only a single registration to your service that represents multiple DIDs that need to be sent to them.
By default, no flags exist for changing the To:
header to match the DID when calling a registered endpoint. Since the registration to your server is typically done via a generic username that is not related to the DID, you must program your dialplan to retrieve a user's registration information and parse out the username portion of the To:
header, replacing it with your own. Care must be taken to replace only the username portion and keep the remaining parameters (after @
) intact, especially if the NAT traversal is expected to continue operating.
Getting ready
Be sure that you have your DIDs and users configured. In this example, we will use testuser
as the username, with a phone number of 4158867999
, and our domain will be my.phoneco.test
.
How to do it...
Create a dialplan extension specifically for handling calls to the DID number, and use some regular expression syntax to parse out the information. Here is an example:
<extension name="call_4158867999"> <condition field="destination_number" expression="^\+?1?4158867999$"/> <condition field="${sofia_contact(testuser@local.pbx.com)}" expression="^[^\@]+(.*)"> <action application="bridge" data="sofia/external/4158867999$1"/> </condition> </extension>
How it works...
You would typically send calls to testuser
using the bridge
command with an argument of user/testuser
. In this scenario, however, you would wish to call the testuser's registered endpoint and replace testuser
with a phone number, which is 4158867999
in our example. To do this, you must retrieve the testuser's current dial string and remove the username, replacing it with the DID number.
In this example, we leverage the sofia_contact
API and some regular expression magic. The first condition
simply matches the user's DID phone number. We only want to act if the destination number is 4158867999
. The interesting stuff happens in the second condition
. The field is ${sofia_contact(testuser@local.pbx.com)}
. By wrapping an API call in ${}
, the dialplan literally executes the API and uses the result as the field value. If we go to fs_cli
and type sofia_contacttestuser@local.pbx.com
, we get the result, which is something like this:
sofia/external/johndoe@12.34.56.7;fs_nat=yes
The ^[^\@]+(.*)
regular expression pattern is applied against this value. The result is that everything after, and including, the @
sign is placed in the $1
variable. In our example, $1
contains @12.13.56.7;fs_nat=yes
. Finally, we execute bridge
with the sofia/external/4158867999$1
dial string. With $1
expanded, our destination is as follows:
sofia/external/4158867999@12.34.56.7;fs_nat=yes
We have successfully replaced testuser
with 4158867999
, while preserving the necessary IP address and parameters for contacting the server, and sent the call to the proper destination.