Hello!
I work a lot with VoIP networks. With commercial equipment, of course, too, but also a lot with OpenSource (the article is written in the context of using Asterisk PBX).
In telephony, there is often a simple task, to divide routes into specific directions. Well, for example, send calls to fixed numbers in the direction of operator 1, MG - in the direction of operator 2, MN - in the direction of operator 3.
The task, in general, is trivial, and it is easy to implement on Asterisk:
')
; : 7 ( 3- 7- ), .
exten => _0X,1,dial(SIP/itsp1/${EXTEN})
exten => _0XX,1,dial(SIP/itsp1/${EXTEN})
exten => _XXXXXXX,1,dial(SIP/itsp1/${EXTEN})
; : ( - "8") + 10 .
exten => _8[348]XXXXXXXXX,1,dial(SIP/itsp2/${EXTEN})
exten => _89XXXXXXXXX,1,dial(SIP/itsp2/${EXTEN})
; : ( - "810") + .
exten => _810X.,1,dial(SIP/itsp3/${EXTEN})
However, sometimes it becomes necessary to provide the subscriber access only to the mobile phones in his area, and here the simple "_89XXXXXXXXX" will not work.
Formulation of the problem
The fact is that the telephone codes of mobile operators are non-geographical (so-called DEF-codes), which means that there is no code linking to a specific region. Because of this, in the code "900", for example, there are ranges belonging to operators from the Vologda region, the Pskov, Chechen Republic, and so on.
A mobile operator can theoretically afford to select any free range in any region, and this leads to the fact that in different regions there are telephone numbers from different ranges with different numbers of numbers. For example, in the Vologda Oblast, 46 different ranges are allocated, in the Moscow Oblast - 398.
If all the ranges were “beautiful”, with 1000 or 10000 numbers, like this: 5100000 - 5109999 (10,000 numbers in the 914 code), then everything would be more or less good.
But often large ranges of 10,000 numbers are “cut” into subranges from numbers that are not multiple to their order (the order of the number of numbers in a range). Offhand:
Code | From | Before | Capacity | Region |
958 | 2997500 | 2998749 | 1250 | Republic of Adygea |
958 | 2998750 | 2999999 | 1250 | Udmurtia |
Or even one number (I will not give).
Such a range (2997500 - 2998749) cannot be described by one regular Asterisk expression, as a result you will get 3 routes:
exten => _89582997[5-9]XX,1,dial(SIP/itsp1/${EXTEN})
exten => _89582998[0-6]XX,1,dial(SIP/itsp1/${EXTEN})
exten => _895829987[0-4]X,1,dial(SIP/itsp1/${EXTEN})
And this is one particular range. If you take a whole region, the number of regular expressions increases dramatically. In addition, over time DEF codes tend to change. Some disappear, others appear. If you do not take this into account, this may lead to the fact that your subscribers, at best, will not be able to call certain ranges of your region.
So, the task is formulated:
It is necessary to create regular expressions from any number of any ranges that your telephone exchange recognizes.In all sources (MTT, the Ministry of Communications of the Russian Federation) precisely the ranges appear, and not regular expressions. Since the work on creating routes is boring and routine, and I have to work with different regions of the Russian Federation, I decided to automate it. Oddly enough, I did not find any ready-made tools for this.
Therefore, I made the tool myself, and at the same time I share it with everyone. A kind of regular expression service :).
Decision
Take an arbitrary range (2633722 - 2673388). A person can easily create regular expressions on its basis. As a result, they will turn out like this:
Subrange | Regular expression |
2633722-2633729 | _263372 [2-9] |
2633730-2633799 | _26337 [3-9] X |
2633800-2633999 | _2633 [8-9] XX |
2634000-2639999 | _263 [4-9] XXX |
2640000-2669999 | _26 [4-6] XXXX |
2670000-2672999 | _267 [0-2] XXX |
2673000-2673299 | _2673 [0-2] XX |
2673300-2673379 | _26733 [0-7] X |
2673380-2673388 | _267338 [0-8] |
Consider how to do this automatically.
Algorithm
- Get a list of ranges and regions.
I take the ranges in a convenient tabular form from the FAS website. The registry is updated 2 times a month, even if there were no changes in it. - Get a list of ranges of a particular region (the region is entered by the user).
The result of the work should be a list of regular expressions for a specific region. - Split the first range into subranges, ready to create a regular expression.
The most difficult part.
I made a doubly linked list that initially contains 2 elements (minimum and maximum), and with each step before the last element 2 numbers are added: the first number, which together with the minimum can become a new range (if you take the example above, then in the first step it is 2633729 , on the second - 2633799, etc.), and the second number is one more. In each step, the penultimate and last elements of the list are considered, and 2 new elements are inserted between them. It was quite possible to manage with a two-dimensional array, but I initially thought that it would be more difficult for me to fill it than a list.
As a result, the list is a set of pairs of numbers, each of which is capable of creating a regular expression of the form "26337 [3-9] X". - Make a regular expression out of each subrange.
This is a matter of technology. - Repeat steps 3 and 4 with each range of the initial list.
Implementation and Opportunities
I wrote in php. As a result, the script can:
- Download the registry of numbers from the FAS website and convert it to csv in UTF-8 encoding
- Give the user a list of regular expressions that are ready for work in terms of the Asterisk set for the selected region
- As a bonus, it accepts any range of natural numbers as input and returns a list of regular expressions.
Screenshot:

There were many subtleties in writing, and they are hardly worth listing here.
Source code is available
here . There are many things that can be improved (while in the plans there are summation of ranges before the 3rd paragraph of the algorithm and adding regular expressions of other formats, as well as full-fledged work from the CLI to automate regular updating of routes on existing Asterisk servers), so please write me all your wishes . I will also be happy to hear recommendations in terms of web interface and security. I'm not a programmer, so the quality of the code can suffer.
Links