Harald Welte | 1f47f67 | 2011-04-02 14:33:31 +0200 | [diff] [blame] | 1 | % Osmocom Global Title Translation |
| 2 | |
| 3 | % (C) 2011 by Harald Welte <laforge@gnumonks.org> |
| 4 | % |
| 5 | % All Rights Reserved |
| 6 | % |
| 7 | % This program is free software; you can redistribute it and/or modify |
| 8 | % it under the terms of the GNU Affero General Public License as |
| 9 | % published by the Free Software Foundation; either version 3 of the |
| 10 | % License, or (at your option) any later version. |
| 11 | % |
| 12 | % This program is distributed in the hope that it will be useful, |
| 13 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | % GNU General Public License for more details. |
| 16 | % |
| 17 | % You should have received a copy of the GNU Affero General Public License |
| 18 | % along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | -module(osmo_ss7_gtt). |
| 21 | -author('Harald Welte <laforge@gnumonks.org>'). |
| 22 | |
| 23 | -include("sccp.hrl"). |
| 24 | -include("gtt.hrl"). |
| 25 | |
| 26 | -export([global_title_match/2, apply_gtt_actions/2, execute_gtt/2]). |
| 27 | |
Harald Welte | 8e7cdaf | 2011-04-02 17:24:47 +0200 | [diff] [blame] | 28 | -compile({parse_transform, exprecs}). |
| 29 | -export_records([gtt_match, gtt_act_repl_digits, gtt_act_repl_num_plan]). |
| 30 | |
Harald Welte | 1f47f67 | 2011-04-02 14:33:31 +0200 | [diff] [blame] | 31 | % Match a given GT against an ordered list of {match, action} tuples |
Harald Welte | 24a4924 | 2011-04-02 17:58:04 +0200 | [diff] [blame] | 32 | global_title_match([], _Gt) -> |
Harald Welte | 1f47f67 | 2011-04-02 14:33:31 +0200 | [diff] [blame] | 33 | false; |
| 34 | global_title_match([{Match, Action}|Tail], Gt) when is_record(Gt, global_title) -> |
| 35 | PhoneNumInt = osmo_util:digit_list2int(Gt#global_title.phone_number), |
| 36 | if Match#gtt_match.gt_range_from >= PhoneNumInt -> |
| 37 | % in an ordered list, we can assume that no trailing rules will match |
| 38 | false; |
| 39 | true -> |
| 40 | case single_gt_match(Match, Gt) of |
| 41 | true -> |
| 42 | Action; |
| 43 | _ -> |
| 44 | % iterate further over the list of GTT rules |
| 45 | global_title_match(Tail, Gt) |
| 46 | end |
Harald Welte | 24a4924 | 2011-04-02 17:58:04 +0200 | [diff] [blame] | 47 | end; |
| 48 | % Same as above, but for SCCP Address (i.e. GT + point code and SSN) |
| 49 | global_title_match([{Match, Action}|Tail], SccpAddr) when is_record(SccpAddr, sccp_addr) -> |
| 50 | Gt = SccpAddr#sccp_addr.global_title, |
| 51 | PhoneNumInt = osmo_util:digit_list2int(Gt#global_title.phone_number), |
| 52 | if Match#gtt_match.gt_range_from >= PhoneNumInt -> |
| 53 | % in an ordered list, we can assume that no trailing rules will match |
| 54 | false; |
| 55 | true -> |
| 56 | case single_gt_match(Match, SccpAddr) of |
| 57 | true -> |
| 58 | Action; |
| 59 | _ -> |
| 60 | % iterate further over the list of GTT rules |
| 61 | global_title_match(Tail, SccpAddr) |
| 62 | end |
Harald Welte | 1f47f67 | 2011-04-02 14:33:31 +0200 | [diff] [blame] | 63 | end. |
| 64 | |
Harald Welte | 24a4924 | 2011-04-02 17:58:04 +0200 | [diff] [blame] | 65 | |
Harald Welte | 1f47f67 | 2011-04-02 14:33:31 +0200 | [diff] [blame] | 66 | % perform matching of a given global title against a single match |
| 67 | single_gt_match(Match, Gt) when is_record(Match, gtt_match), is_record(Gt, global_title) -> |
| 68 | #gtt_match{gt_range_from = RangeFrom, gt_range_to = RangeTo, |
Harald Welte | 24a4924 | 2011-04-02 17:58:04 +0200 | [diff] [blame] | 69 | numbering_plan = NumPlan, nature_of_addr_ind = NatureInd} = Match, |
Harald Welte | 1f47f67 | 2011-04-02 14:33:31 +0200 | [diff] [blame] | 70 | % build a list of the individual criteria that all have to match |
| 71 | SubMatchList = [{digits, {RangeFrom, RangeTo}, Gt#global_title.phone_number}, |
| 72 | {numbering_plan, NumPlan, Gt#global_title.numbering_plan}, |
| 73 | {nature_of_addr_ind, NatureInd, Gt#global_title.nature_of_addr_ind}], |
Harald Welte | 24a4924 | 2011-04-02 17:58:04 +0200 | [diff] [blame] | 74 | gt_sub_match_list(SubMatchList); |
| 75 | % Same as above, but for SCCP Address (i.e. GT + point code and SSN) |
| 76 | single_gt_match(Match, SccpAddr) when is_record(Match, gtt_match), is_record(SccpAddr, sccp_addr) -> |
| 77 | #gtt_match{dpc = Dpc, ssn = Ssn} = Match, |
| 78 | Gt = SccpAddr#sccp_addr.global_title, |
| 79 | % First match the GT part |
| 80 | case single_gt_match(Match, Gt) of |
| 81 | false -> |
| 82 | false; |
| 83 | true -> |
| 84 | % build a list of the individual criteria that all have to match |
Harald Welte | 2da7a85 | 2011-04-06 17:18:13 +0200 | [diff] [blame] | 85 | SubMatchList = [{dpc, Dpc, SccpAddr#sccp_addr.point_code}, |
| 86 | {ssn, Ssn, SccpAddr#sccp_addr.ssn}], |
Harald Welte | 24a4924 | 2011-04-02 17:58:04 +0200 | [diff] [blame] | 87 | gt_sub_match_list(SubMatchList) |
| 88 | end. |
Harald Welte | 1f47f67 | 2011-04-02 14:33:31 +0200 | [diff] [blame] | 89 | |
| 90 | % iterate over the list of individual match criteria and call the match function |
| 91 | gt_sub_match_list([]) -> |
| 92 | true; |
| 93 | gt_sub_match_list([{What, MatchPart, GtPart}|SubMatchList]) -> |
| 94 | case gt_sub_match(What, MatchPart, GtPart) of |
| 95 | false -> |
| 96 | false; |
| 97 | true -> |
| 98 | gt_sub_match_list(SubMatchList) |
| 99 | end. |
| 100 | |
| 101 | % matching of the actual phone number digits |
| 102 | gt_sub_match(digits, {DigitsFrom, DigitsTo}, GtPart) -> |
| 103 | PhoneNumInt = osmo_util:digit_list2int(GtPart), |
| 104 | if |
| 105 | PhoneNumInt >= DigitsFrom, PhoneNumInt =< DigitsTo -> true; |
| 106 | true -> false |
| 107 | end; |
| 108 | % any match that is not qualified will always match |
| 109 | gt_sub_match(_What, undefined, _GtPart) -> |
| 110 | true; |
| 111 | % remaining default match for all other fields |
| 112 | gt_sub_match(_What, MatchPart, GtPart) -> |
| 113 | if |
| 114 | MatchPart == GtPart -> true; |
| 115 | true -> false |
| 116 | end. |
| 117 | |
| 118 | |
| 119 | % Execute a single action: Replac some digits in the GT |
| 120 | gtt_action(Gt, Action) when is_record(Gt, global_title), is_record(Action, gtt_act_repl_digits) -> |
| 121 | #gtt_act_repl_digits{replace_digit_start = ReplDigStart, |
| 122 | replace_digit_end = ReplDigEnd, |
| 123 | new_digits = NewDigits} = Action, |
| 124 | GtDigitList = Gt#global_title.phone_number, |
| 125 | Header = lists:sublist(GtDigitList, 1, ReplDigStart-1), |
| 126 | Trailer = lists:sublist(GtDigitList, ReplDigEnd+1, length(GtDigitList)), |
| 127 | Gt#global_title{phone_number = Header ++ NewDigits ++ Trailer}; |
| 128 | |
| 129 | % Execute a single action: Replac the numbering plan in the GT |
Harald Welte | 7a321db | 2011-04-02 18:15:04 +0200 | [diff] [blame] | 130 | gtt_action(Gt, #gtt_act_repl_num_plan{numbering_plan = NewNumPlan}) when is_record(Gt, global_title) -> |
| 131 | Gt#global_title{numbering_plan = NewNumPlan}; |
| 132 | |
| 133 | % Execute a single 'generic purpose' action that will call apply/2 |
| 134 | gtt_action(Gt, #gtt_act_apply{funct = Funct, args = Args}) when is_record(Gt, global_title) -> |
| 135 | apply(Funct, Args). |
| 136 | |
Harald Welte | 1f47f67 | 2011-04-02 14:33:31 +0200 | [diff] [blame] | 137 | |
| 138 | % appliy a list of GTT actions to a Global Title |
| 139 | apply_gtt_actions(Gt, []) when is_record(Gt, global_title) -> |
| 140 | Gt; |
| 141 | apply_gtt_actions(Gt, [Head|List]) when is_record(Gt, global_title) -> |
| 142 | NewGt = gtt_action(Gt, Head), |
| 143 | apply_gtt_actions(NewGt, List); |
| 144 | apply_gtt_actions(Gt, Action) when is_record(Gt, global_title) -> |
| 145 | gtt_action(Gt, Action). |
| 146 | |
| 147 | % Execute a complete GTT operation: matching + executing the action |
| 148 | execute_gtt(Gt, RulesList) when is_record(Gt, global_title), is_list(RulesList) -> |
| 149 | case global_title_match(RulesList, Gt) of |
| 150 | false -> |
| 151 | Gt; |
| 152 | Action -> |
| 153 | apply_gtt_actions(Gt, Action) |
Harald Welte | 24a4924 | 2011-04-02 17:58:04 +0200 | [diff] [blame] | 154 | end; |
| 155 | % Same as above, but for SCCP Address (i.e. GT + point code and SSN) |
| 156 | execute_gtt(SccpAddr, RulesList) when is_record(SccpAddr, sccp_addr), is_list(RulesList) -> |
| 157 | Gt = SccpAddr#sccp_addr.global_title, |
| 158 | NewGt = execute_gtt(Gt, RulesList), |
| 159 | SccpAddr#sccp_addr{global_title = NewGt}. |