Regular Expressions For SIP Trunk



The regular expression is a sequence of characters that form a search pattern. See the definition in Wikipedia. It is a huge topic and takes a lot of time to explain. If you didn’t use it, you will get stuck in a nested “If” statements situation. Using regular expressions means professionalism. I am assuming you know about it and i will just apply it to SIP.


Regular Expressions For a SIP Trunk

Usually when a SIP router receives a SIP request which is addressed to a PSTN gateway (i.e. Its Request-URI contains a telephone number), it checks which group the requested user (called party) belongs to. The router also must check if the caller has permission to make this call. So the check can be done on the caller and calle. Any check done by the router is translated into this simple thinking logic:

If (Routing Condition){

  Do Specific Work


This can be in the router’s routing script or internally integrated in the router’s application.


If (The Call Is Addressed To A Local Number){

Do Specific Work


In OpenSIPS SIP router, the previous condition can be treated in the routing scripts in two ways:

  • Using the Script Variables (e.g. the reference to the request’s uri ($ru)) with the regular expression directly:

If ($ru=~sip:[2-9][0-9]{6}@){


The previous regular expression is for any 7 numbers starting by one digit from the range [2-9].

  • Using a function like “pcre_match” in “Regex” module which matches the given string against a given regular expression. The return value is TRUE if it is matched, FALSE otherwise:

If (pcre_match(“$ru”,sip:[2-9][0-9]{6}@){


The variable “$ru” is read/write variable so be aware where in the script you are checking its value. You can use “$ou” which is a reference to the request’s original URI. The module “Regex” is based on the library “PCRE” which is an implementation of regular expression pattern matching where the regular expression parameter will be compiled in PCRE object. So the development libraries of “PCRE” must be installed (“libpcre-dev” or “pcre-devel”).

Note: Use “&&” if you want to concatenate multiple conditions in one “if” statement.

If ((Routing Condition-1) && (Routing Condition-2)){

  Do Specific Work


Examples of Regular Expressions For SIP Trunk

You should test your regular expression before using it to know if you have constructed it correctly. There are many online free services that you can use to do your tests. Use search engines to search for syntax symbols that you can use to construct the regular expression. The following are examples. Note that each one defines a group:

  • Any user on any domain/IP: sip:(.*)@(.*)
  • Any user on certain domain: sip:(.*)
  • Any user on certain IP (  sip:(.*)@198\.18\.250\.10 In regular expression, the “.” is interpreted as “any character” symbol whereas “\.” is just a period (dot).
  • Any user on IP Range ( – sip:(.*)@198\.18\.250.* The symbol “*” is quantifier which means the preceding character is found 0 or more times. Here i left the last part of IP to be anything but you can restrict it.
  • To group a set of SIP URIs that are within a certain domain or certain subdomain, use .*mydomain\.com.* For example these URIs will be matched:;transport=tcp, and SIP/2.0
  • To group a set of SIP URIs that have certain string in the username part of URI and the ports 5060 and 5061 are accepted, use the regular expression .*group1@198\.18\.250.\10:506<01>.* For example these two URIs will be matched sip:serv1group1@;transport=tcp and sips:serv2group1@;tranport=tcp
  • 8-digit number on any domain: sip:[0-9]{8}@(.*) The [0-9] is a range for one number between 0 and 9 and {8} means repeat the preceding number 8 times.
  • 8-digit number starting optionally by 8 on any domain: sip:8?[0-9]{7}@(.*)  The symbol “?” means the repetition is 0 or 1 to the preceding. For example,,, and so on.
  • 4-digit number (could be an extension number) starting by 6 on a certain domain: sip:6[0-9]{3}@(.*)
  • 4-digit number which is not starting by 55 on any domain: sip:(?!55)[0-9]{4}@(.*)

 More Information

SIP Trunking -Definition and Examples


What is SIP Trunking ?


The term “trunk” came from circuit-switched telecommunications. It refers to a dedicated physical line that carries many signals simultaneously. The trunk is used to interconnect routers and switches between two telecommunications systems. The term “trunk” has been used with Session Initiation Protocol (SIP) despite its reference to a physical circuit. SIP trunk is not dedicated. It allows any number of clients to connect to it due to the nature of the Internet Protocol (IP).  SIP trunk is a virtual connection between telecommunications systems. It is more like a service than a connection. This service is based on SIP and delivered by Internet Telephony Service Provider (ITSP) to a large number of clients that connect.

siptrunkWhen requests come to the server which is providing the SIP trunking service, the server applies its rules on these requests. If  a request is formatted based on the server’s pre-defined rules Then a  pre-defined decisions will be made by the server on that request. The decisions are pre-defined processing & routing of the requests. The rules and the decisions define the behaviour of the trunk and they are configured in the configuration file of the provider’s proxy. SIP trunk behaviour is described in a contract between the client who bought this service and the server who is selling this service. The client is another server and it uses the trunk on behalf of end users.

Examples of SIP trunk

First i want to clarify what is called Direct Inward Dialing (DID) or Direct Dial-in (DDI) number:

♣ In PSTN world: It is a globally unique number that can be called directly from the PSTN. Number is in E.164 format (CountryCode+CityCode+SubscriberNumber).

♣  In VOIP world: It is virtual DID. It is the number that your ITSP assigned to you to connect you (as VOIP user) to the PSTN around the world.

SIP Trunk Examples:

  • Interconnection to the PSTN using SIP trunk: This virtual trunk will be between SIP-enabled PBX like Asterisk as trunk client and VOIP service provider (ITSP) as trunk provider. When the PBX registers itself (Sending SIP REGISTER Request periodically) to the ITSP, the ITSP knows the IP address of the PBX so the ITSP can send the inbound calls to that IP address. If the registration is not required, a pre-defined IP address is used by the ITSP. The PBX  is configured to trust the requests coming from ITSP (static peer). The inbound calls are delivered to the PBX by the ITSP. The PBX will route the call depending on the DID number which must be kept in the SIP request. The ITSP’s proxy overwrites the Request-URI with the PBX’s  registered  SIP contact (user direct route learned during PBX registration and involved in the “Contact” header field of the INVITE requests and responses). The user-part of the Request-URI can be replaced by the original DID.


The outbound calls are delivered to ITSP by the PBX. The mapping between the dialed DID and its corresponding SIP User-ID could be:

♥ User-ID = DID number (one to one relationship).

♥ User-ID = Constant prefix + DID or part of DID (one to one relationship).

♥ Set of DIDs is a set of aliases to the same SIP User-ID. The SIP user can have multiple DIDs. These DIDs are stored in the database of the ITSP’s proxy (one to many relationship). OpenSIPS stores aliases in “dbaliases” table. This table requires uniqueness on the “alias_idx” index. So it is ok to have many lines for the same user with different aliases.

♥ Anyway to do the translation and make the routing possible.

Getting the corresponding VOIP user-ID from  the  dialed DID number and the registered device’s location (IP address) from the user-ID are done by the ITSP’s proxy. The needed information are stored in the database of the ITSP’s proxy/registrar. The ITSP’s proxy/registrar see the IP-PBX as a SIP user with multiple DIDs (multiple aliases).

  • Interconnection to other server-based systems (voicemail servers, call centers, application servers): Usually runs over TCP and secured by TLS. The request URI is formatted according to RFC 4458. The requests are terminated in a SIP-based server. I will back explain this later.
  • SIP-based Interconnection between IP-PBXs: I will explain in another article.