Asterisk config extensions.conf sorting

NOTE:
All the explained in this page is not true (at least in Asterisk 1.4.X). For example:


[example]
exten => _918.,1,Dial(Zap/1/${EXTEN})
exten => _.,1,Dial(Zap/2/${EXTEN})
exten => h,1,Hangup


The real sort order is:
  1. _918.
  2. _.
  3. h

Major change from 1.2 to 1.4


In 1.2, the dialplan was sorted by pure ASCII collating sequence. So N before X, X before Z, [ before N, etc.

As of 1.4, the dialplan is sorted, digit by digit, by the number of things matched. So N before Z, Z before X, and [8-9] before [1-9], [12] before X, but X before [1-2a-z], because the latter has more possible matches.



Sort Order of Extension Patterns

Each context defined in the Dialplan tells Asterisk how to process telephone numbers dialed in that context.

Because you may use patterns to define extensions, more than one extension pattern could match a given telephone number. Asterisk does not match against the extension patterns in the order you define them; the extension patterns are sorted first. Hence Asterisk may process a telephone number differently than you intended.

This document describes how Asterisk sorts the extension patterns and how to control the sort order to cause Asterisk to select the extension you want when two or more extension patterns match a given dialed number.

Example Problem

Let's say that for context "example", you want numbers beginning with 918 to go out on an analog telephone line connected on Zap/1, and all other numbers are to go out on Zap/2. So you write something like this:


[example]
exten => _918.,1,Dial(Zap/1/${EXTEN})
exten => _.,1,Dial(Zap/2/${EXTEN})
exten => h,1,Hangup


But it doesn't work! You discover that regardless of which number you dial, all numbers are sent out via Zap/2. This is because Asterisk sorts the extensions and takes the first match it finds. To see the order in which Asterisk sorts the extensions, type the command show dialplan example in the Asterisk console. You will discover that the sort order is:
  1. _.
  2. _918.
  3. h
Note that this is a different order than what you defined in your extensions.conf file. The entry _. is now first, matching any dialed number, including those beginning with 918. Note also that the hangup extension h can not be reached, because it will also be matched by _. (that is why one usually doesn't want to use _.: it will match the special extensions s, i, t, h, etc, which is usually not what is wanted; use _X. instead.)

Controlling Sort Order

So how do you get Asterisk to match the extension patterns in the order you want? By using the include keyword to include another context's extension patterns within the current context. Here's an example:


[example]
include => example-sub
exten => h,1,Hangup
exten => _918.,1,Dial(Zap/1/${EXTEN})

[example-sub]
exten => _.,1,Dial(Zap/2/${EXTEN})


Asterisk (when in context "example") will now process dialed numbers in this sort order:
  1. _918.
  2. h
  3. _.

Note what Asterisk has done:
  1. the exten entries within the "example" context are sorted and tested first;
  2. the contents of each included context are sorted are tested in turn.
Included contexts are tested in the order of the include lines in extensions.conf.

The sort order

The sort order (in 1.2 and before) is lexicographic (dictionary) ASCII order, with the exception that patterns (starting with _) will always be after non-patterns. As of 1.4, the sort order is digit by digit by the number of things matched.

In many cases, you can change your patterns to other equivalent patterns to manipulate the sorting order. For example, you can use _1X. instead of _1. to implement a match for all numbers starting with 1, but that will be sorted after more specific patterns (assuming the more specific patterns have a digit after the 1, not Z or [). The assumption here is that no number will be shorter than three digits, and thus that pattern will be just as acceptable as _1. .

These instructions are misleading. It appears that the pattern matching occurs at every sequence number. Thus:

exten => _123X.,1,Playback(not-authorized)
exten => _1X.,1,Playback(connecting)
exten => _1X.,n,Dial(SIP/${EXTEN})

will still dial the extension after playing not=authorized

You need something similar to this to obtain the expected result:

exten => _123X.,1,Playback(not-authorized)
exten => _123X.,n,NoOp()
exten => _1X.,1,Playback(connecting)
exten => _1X.,n,Dial(SIP/${EXTEN})


Example 2

For national calls, you need to dial "0" in front of long-distance numbers, but nothing in front of local numbers. The example is for the Amsterdam (area code 20) in the Netherlands (country code 31); in a dialplan using country code + area code + subscriber number always.

This one will not work as expected:
[trunk-national]
exten => _3120.,1,Dial(Zap/1/${EXTEN:4})
exten => _31.,1,Dial(Zap/1/0${EXTEN:2})

This one will (because X sorts after 2):
[trunk-national]
exten => _3120.,1,Dial(Zap/1/${EXTEN:4})
exten => _31X.,1,Dial(Zap/1/0${EXTEN:2})

Or alternatively (using the include trick):
[trunk-national]
exten => _3124.,1,Dial(Zap/1/${EXTEN:4})
include => trunk-national-sub
[trunk-national-sub]
exten => _31.,1,Dial(Zap/1/0${EXTEN:2})


Asterisk | Configuration | The Dialplan - extensions.conf

Created by: JazEzork, Last modification: Wed 23 of May, 2012 (18:39 UTC) by admin


Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+

Page Changes | Comments

 

Featured -

Search: