Harald Welte | b8bfc4e | 2011-10-11 18:49:59 +0200 | [diff] [blame] | 1 | % Internal SS7 route database keeping |
| 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/>. |
Harald Welte | f8bf032 | 2012-04-16 13:10:47 +0200 | [diff] [blame] | 19 | % |
| 20 | % Additional Permission under GNU AGPL version 3 section 7: |
| 21 | % |
| 22 | % If you modify this Program, or any covered work, by linking or |
| 23 | % combining it with runtime libraries of Erlang/OTP as released by |
| 24 | % Ericsson on http://www.erlang.org (or a modified version of these |
| 25 | % libraries), containing parts covered by the terms of the Erlang Public |
| 26 | % License (http://www.erlang.org/EPLICENSE), the licensors of this |
| 27 | % Program grant you additional permission to convey the resulting work |
| 28 | % without the need to license the runtime libraries of Erlang/OTP under |
| 29 | % the GNU Affero General Public License. Corresponding Source for a |
| 30 | % non-source form of such a combination shall include the source code |
| 31 | % for the parts of the runtime libraries of Erlang/OTP used as well as |
| 32 | % that of the covered work. |
Harald Welte | b8bfc4e | 2011-10-11 18:49:59 +0200 | [diff] [blame] | 33 | |
Harald Welte | 9bfab7c | 2011-10-12 00:02:42 +0200 | [diff] [blame] | 34 | |
| 35 | % this module is keeping the point code routing table for the MTP3 layer |
| 36 | % of the Omsocom SS7 protocol stack. Routes are created and deleted |
| 37 | % with create_route() and delete_route(), the arguments are |
| 38 | % * destination point code |
| 39 | % * point code mask |
| 40 | % * name of the linkset |
| 41 | % |
| 42 | % there is one function to actually make a routing decision: route_dpc/1 |
| 43 | % with a single argument: the destination point code. |
| 44 | |
Harald Welte | b8bfc4e | 2011-10-11 18:49:59 +0200 | [diff] [blame] | 45 | -module(ss7_routes). |
| 46 | -behaviour(gen_server). |
| 47 | |
| 48 | -include_lib("osmo_ss7/include/mtp3.hrl"). |
| 49 | |
| 50 | % gen_fsm callbacks |
| 51 | -export([init/1, handle_call/3, handle_info/2, terminate/2, code_change/3]). |
| 52 | |
| 53 | % our published API |
| 54 | -export([start_link/0]). |
| 55 | |
| 56 | % client functions, may internally talk to our sccp_user server |
| 57 | -export([create_route/3, delete_route/3]). |
| 58 | -export([dump/0]). |
| 59 | -export([route_dpc/1]). |
| 60 | |
| 61 | -record(ss7route, { |
| 62 | remote_pc_mask, % {remote_pc, remote_pc_mask} |
| 63 | linkset_name |
| 64 | }). |
| 65 | |
| 66 | -record(sr_state, { |
| 67 | route_tbl |
| 68 | }). |
| 69 | |
| 70 | % initialization code |
| 71 | |
| 72 | start_link() -> |
| 73 | gen_server:start_link({local, ?MODULE}, ?MODULE, [], [{debug, [trace]}]). |
| 74 | |
| 75 | init(_Arg) -> |
| 76 | RouteTbl = ets:new(ss7_routes, [ordered_set, named_table, |
| 77 | {keypos, #ss7route.remote_pc_mask}]), |
| 78 | process_flag(trap_exit, true), |
| 79 | {ok, #sr_state{route_tbl = RouteTbl}}. |
| 80 | |
| 81 | % client side API |
| 82 | |
| 83 | % all write operations go through gen_server:call(), as only the ?MODULE |
| 84 | % process has permission to modify the table content |
| 85 | |
Harald Welte | 4608ba1 | 2011-12-08 12:10:34 +0100 | [diff] [blame] | 86 | create_route(RemotePcIn, RemoteMask, LinksetName) -> |
| 87 | RemotePc = osmo_util:pointcode2int(RemotePcIn), |
Harald Welte | b8bfc4e | 2011-10-11 18:49:59 +0200 | [diff] [blame] | 88 | gen_server:call(?MODULE, {create_route, {RemotePc, RemoteMask, LinksetName}}). |
| 89 | |
Harald Welte | 4608ba1 | 2011-12-08 12:10:34 +0100 | [diff] [blame] | 90 | delete_route(RemotePcIn, RemoteMask, LinksetName) -> |
| 91 | RemotePc = osmo_util:pointcode2int(RemotePcIn), |
Harald Welte | b8bfc4e | 2011-10-11 18:49:59 +0200 | [diff] [blame] | 92 | gen_server:call(?MODULE, {delete_route, {RemotePc, RemoteMask, LinksetName}}). |
| 93 | |
| 94 | % the lookup functions can directly use the ets named_table from within |
| 95 | % the client process, no need to go through a synchronous IPC |
| 96 | |
Harald Welte | 19350ad | 2011-12-08 12:05:34 +0100 | [diff] [blame] | 97 | route_dpc(DpcIn) -> |
| 98 | Dpc = osmo_util:pointcode2int(DpcIn), |
Harald Welte | b8bfc4e | 2011-10-11 18:49:59 +0200 | [diff] [blame] | 99 | % this was generated by ets:fun2ms() on the shell |
| 100 | Match = [{#ss7route{remote_pc_mask={'$1','$2'},linkset_name='$3'}, |
| 101 | [{'==',{'band',Dpc,'$2'},'$1'}], |
| 102 | ['$3']}], |
| 103 | case ets:select(ss7_routes, Match) of |
| 104 | [Name|_] -> |
| 105 | {ok, Name}; |
| 106 | _ -> |
| 107 | {error, no_route} |
| 108 | end. |
| 109 | |
| 110 | dump() -> |
| 111 | List = ets:tab2list(ss7_routes), |
| 112 | dump_routes(List). |
| 113 | |
| 114 | dump_routes([]) -> |
| 115 | ok; |
| 116 | dump_routes([Head|Tail]) when is_record(Head, ss7route) -> |
| 117 | dump_single_route(Head), |
| 118 | dump_routes(Tail). |
| 119 | |
| 120 | dump_single_route(#ss7route{remote_pc_mask = {Pc, Mask}, |
| 121 | linkset_name = Name}) -> |
Harald Welte | 9bfab7c | 2011-10-12 00:02:42 +0200 | [diff] [blame] | 122 | PcTuple = osmo_util:pointcode_fmt(itu, Pc), |
| 123 | MaskTuple = osmo_util:pointcode_fmt(itu, Mask), |
Harald Welte | b8bfc4e | 2011-10-11 18:49:59 +0200 | [diff] [blame] | 124 | io:format("Dest PC ~p/~p -> Linkset ~p~n", |
Harald Welte | 9bfab7c | 2011-10-12 00:02:42 +0200 | [diff] [blame] | 125 | [PcTuple, MaskTuple, Name]). |
Harald Welte | b8bfc4e | 2011-10-11 18:49:59 +0200 | [diff] [blame] | 126 | |
| 127 | % server side code |
| 128 | |
| 129 | handle_call({create_route, {RemotePc, RemoteMask, Name}}, |
| 130 | {_FromPid, _FromRef}, S) -> |
| 131 | #sr_state{route_tbl = Tbl} = S, |
| 132 | R = #ss7route{remote_pc_mask = {RemotePc, RemoteMask}, |
| 133 | linkset_name = Name}, |
| 134 | case ets:insert_new(Tbl, R) of |
| 135 | false -> |
| 136 | {reply, {error, ets_insert}, S}; |
| 137 | _ -> |
| 138 | {reply, ok, S} |
| 139 | end; |
| 140 | |
| 141 | handle_call({delete_route, {RemotePc, RemoteMask, _Name}}, |
| 142 | {_FromPid, _FromRef}, S) -> |
| 143 | #sr_state{route_tbl = Tbl} = S, |
| 144 | ets:delete(Tbl, {RemotePc, RemoteMask}), |
| 145 | {reply, ok, S}. |
| 146 | |
| 147 | handle_info(Info, S) -> |
| 148 | error_logger:error_report(["unknown handle_info", |
| 149 | {module, ?MODULE}, |
| 150 | {info, Info}, {state, S}]), |
| 151 | {noreply, S}. |
| 152 | |
| 153 | terminate(Reason, _S) -> |
| 154 | io:format("terminating ~p with reason ~p", [?MODULE, Reason]), |
| 155 | ok. |
| 156 | |
| 157 | code_change(_OldVsn, State, _Extra) -> |
| 158 | {ok, State}. |