Inbound Routing of Trunks

When a new call comes into the system, the system must determine whether the call is an internal extension or an outside call that is configured to a trunk. To do this, it first looks at the From field to see if it matches an extension. If it's not an extension, then from a registration trunk, it looks for a line parameter in the SIP INVITE. The line parameter is set by the system when the trunk is registered. Although it is not a predefined SIP parameter, the line parameter is used by many SIP implementations to identify where the call is routed to.

For gateways and proxies, this method is not possible, so if the line parameter isn't there to give clues on how to route the call, the system must continue searching the trunk. This is done by a recursive DNS-resolution of the outbound proxy of the trunk.

Note: The domain name localhost matches any domain name presented in the Request-URI. (The Request-URI is a Uniform Resource Identifier that lists the user or service to which the request is being addressed.) Also, if the From header identifies an extension on the system, the trunk identification will be cancelled and the system will assume that the call is from that extension, regardless of whether the extension is registered on the perceived IP address or not, and the system will try to authenticate the extension. The system determines which IP addresses and ports a trunk may send requests to.

How the System Routes a Call to the Proper Extension

Basic Routing

Once the trunk has been identified and the inbound call is trusted, the system must determine where in the system to send the call. Two possibilities are available for this:

  • The system first looks at the trunk's Send call to extension setting. If the setting includes an extension, agent group, hunt group or Auto Attendant, the call will be sent to that account. If the extension does not exist, the call will fail.

If the setting includes regular expressions, the system will route the call accordingly. When using regular expressions in the Send call to extension field, it is important to provide a default extension in the event the replacement pattern does not produce an existing extension (in the example shown below, 777 is the fallback extension).

  • If nothing was set in the trunk's Send call to extension setting, the system must go to the individual extensions to determine whether the called number is associated with any account. If the number dialed was 8124351234, the system will see that extension 508 is associated with the incoming number and will route the call accordingly. If no extension is associated with the incoming number, the call will fail.

Note: By default, the PBX uses the number in the Request-URI field for matching. But !([0-9]*)!\1!t!777 will force the PBX to use the To field instead (the 3rd field contains a "t"). If you use an "f" in the 3rd field, the PBX will use the From field, so that you can do routing based on who is calling.

The number shown to the right of the extension is known as a DID. This method of attaching a DID to an extension is a viable routing method only when the number of DIDs is small (e.g., four to ten DIDs). When a large number of DIDs are involved, using regular expressions in the trunk's Send call to extension field is a better approach.

Corresponding Log File and SIP Trace

This section provides a few SIP traces that correspond to the settings shown in the previous section. When a call has been routed to an extension through the Send call to extension setting, the system writes a log with the message Trunk <name> sends call to ... into the log file (log level 5).

If the trunk’s Send call to extension field is empty and the system finds an extension that is associated with the number called, the called number will be reflected in the user portion of the Request-URI is 8124351234.

When Regular Expressions are Involved

Regular expressions can be used for numerous call routing purposes, but they are especially helpful when you have a large number of DIDs. When you have a few DIDs, you can simply enter the DID number into the Account Number(s) setting in the extension that the DID is assigned to. Assigning a large number of DIDs to individual extension accounts, however, is cumbersome and could be expedited using regular expressions.

A regular expression is a special character string that describes a search pattern that the trunk will use when looking for a match for an incoming number. The expression consists of the following key elements:

  • Pattern: The pattern portion of the string defines the numbers that the system will use when searching for a match and how many. In the example shown below, the system will consider all numbers (0 through 9) when identifying a match and will include only the first seven digits. In expression 2, the system will still consider all numbers but will include only the last three digits.
  • Replacement: The replacement field tells the system which expression to use when dialing. In our example, expression 2 has been defined (as is indicated by \2), so if 9781234567 had been the incoming number, 567 would be dialed.
  • Flag: This flag tells the system where to look for the pattern input. By default, the system will look at the Request-URI ("u"). However, if you want the system to look at the "To" header, use a "t" as shown below (some Internet service providers provide the destination information in the "To" header). You may also use "f" to use the From-Header. In summary, the flags "u", "f" and "t" tell the PBX which header to use. You may specify where to look inside the header: "n" means to use the display name in the "To" or "From" header, "d" means to use the domain name. The default is the username in the URI. For the "f", "n" and "d" flags you need to use version 5 or higher. When using flag "n" you can also use flag "i", which means case-insensitive, and the pattern will be URL-decoded, so that space characters become possible (starting with version 58).
  • Delimiters (field separators): Each part must be separated by a delimiter (a unique character which is not used elsewhere in the setting string). In the example shown below, an exclamation mark has been used.

Note: Expressions are written without any spaces: ![0-9]{7}([0-9]{3}$)!\1!

Important: The example shown below simply shows that numerous expressions can be used in one string; however, a single expression in many cases would be sufficient. For example, to specify that only the last three digits of a number should be used for dialing, you could use !([0-9]{3}$)!\1! as shown below:

This expression allows the trunk to find the correct extension when lots of DIDs are used on the system. As a safety net, you can also direct the trunk to send the call to a default extension in the event the replacement pattern does not produce an existing extension. The string shown below includes a default extension number (777), so if the trunk is unable to find an extension using the replacement field, extension 777 will be called.

Regular Expression Syntax

The syntax used in expressions is explained in the table below:

Syntax Function
! Delimiter (separates each part)
[ ] Lists the digits that are accepted
{ } Defines the number of digits
( ) Stores the numbers that match
\2 Indicates which expression to call
* Wild card (any length and character)
$ Tells the system to read from end of string

The following table shows some common patterns.

Pattern Description
![0-9]{7}([0-9]*)!\1!t!100 This example is common in Europe. It is used to strip the main number of the system so that the remaining numbers can be used to identify the extension. If the extension is not found, the call will be sent to the auto attendant. This example assumes that the number starts with 7 digits (e.g., 0228123) and that the auto attendant is located on extension 100.
!([0-9]{4}$)!\1!t!100 This example always uses the last 4 digits of the number, regardless of how long the number is. This example assumes that the number of digits is always the same.
100 This example is common in U.S. offices, where it is typical to send all calls to an auto attendant. This example assumes that the auto attendant is located on account 100.
!1([0-9]*)!\1!u!100 This example can be used to strip the first digit from a DID number (default destination would be 100).

Regular Expressions and Direct Inward Dialing (DID)

Direct Inward Dialing (DID) allows users on your system to have their own (or even multiple) local numbers assigned to them. With DIDs, anyone outside the company can call the user’s unique phone number and reach the user on the same phone as those who dial the user’s extension number internally. Before you can use DID numbers, you will need to purchase them from a DID service provider, then link the DID numbers to the extension accounts. The easiest way of doing this is by entering the DID number into the Account Number(s) setting of the extension that will be using the DID.

While this method is easy to use, it is too cumbersome to employ for a large number of DIDs. You will need to use an alternate method, depending upon the numbering scheme of the DID numbers themselves. When the ending digits of the DIDs form a definite pattern that can lend itself to correlate with extension numbers, you simply need to enter a single string into the trunk’s Send call to extension field. The only requirement here is that the ending digits must begin with a 4 or higher (extension numbers should stay within the range from 4xx through 7xx to keep from overlapping with Direct Destinations numbers).

If the last 3 or 4 digits of a block of DIDs begin with a 1, 2, or 3 (e.g., 100, 101, etc.), the ending digits cannot be used as the extension numbers. You will need to use the workaround shown in Option 2.

Option 1: Clear pattern with a start number of 4 or higher

If you have a batch of DID numbers with a usable pattern on the ending digits, you can easily use regular expressions in the Send call to extension setting. The system will replace the 10-digit DID with the extension number. A list of DIDs that can be easily converted to 3- and 4-digit extensions using regular expressions.

DIDs and 3-Digit Extensions DIDs and 4-Digit Extensions
978 521-1400 978 521-4100
978 521-1401 978 521-4101
978 521-1402 978 521-4102
978 521-1403 978 521-4103
978 521-1404 978 521-4104
978 521-1405 978 521-4105

Follow the steps below:

  • Create the regular expression.
  • Place the string into the trunk’s Send call to extension setting.

Using the example shown below, the system will use either the last 3 or 4 digits of the 10-digit number, depending upon which string is used. (If the system cannot find a matching DID, the call will be sent to extension 777.)

Option 2: Defined pattern with a start number lower than 4

When DID ending patterns are in the 100, 200, or 300 range, you should not create extensions based on these numbers (extensions should begin with 4 or higher). As a workaround, you will need to create aliases for the extensions, and to save time, this should be done when you create the extensions. Once this is complete, a regular expression must be entered into the Send call to extension field so that the system will replace the 10-digit DID with the alias number:


  • Decide which alias numbers to use.
DID Alias
978 521-1100 100
978 521-1101 101
978 521-1102 102
978 521-1103 103
978 521-1104 104
978 521-1105 105
  • Enter the aliases into the corresponding extension accounts:

  • Enter a regular expression into the Send call to extension setting, telling the system to replace the 10-digit DID with the alias number that is associated with the extension account.


More Extended Regular Expressions

Pattern Meaning
!(\+[0-9]*)!\1!u!70! If you want to match the incoming Request URI (RURI) with the + sign. Useful when the domain country code is set.
!(\+[0-9]*)!\1!t!70! Same as above. Use "To" header on the incoming INVITE instead of RURI.
!([0-9]{3}$)!\1!t! !([0-9]{4}$)!\1!t! 70 Multiple ERE. First try to match the last 3 digits of "To". If it fails, try the last 4 digits of the "To" field. If that fails, then go to account 70 (generally the AA). This is useful, if your system has 3 & 4 digit accounts.
!^(978[0-9]*)!300!f! !^(617[0-9]*)!400!f! 800 "From" based routing. If the call comes "From" 978 area code then send call to account 300. If it comes from 617 area code then send call to account 400. Everything else to 800