blob: 98954c4f42759a78f36a7c9e0749a5529ec0f3f4 [file] [log] [blame]
Harald Welteb8bfc4e2011-10-11 18:49:59 +02001% 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/>.
19
Harald Welte9bfab7c2011-10-12 00:02:42 +020020
21% this module is keeping the point code routing table for the MTP3 layer
22% of the Omsocom SS7 protocol stack. Routes are created and deleted
23% with create_route() and delete_route(), the arguments are
24% * destination point code
25% * point code mask
26% * name of the linkset
27%
28% there is one function to actually make a routing decision: route_dpc/1
29% with a single argument: the destination point code.
30
Harald Welteb8bfc4e2011-10-11 18:49:59 +020031-module(ss7_routes).
32-behaviour(gen_server).
33
34-include_lib("osmo_ss7/include/mtp3.hrl").
35
36% gen_fsm callbacks
37-export([init/1, handle_call/3, handle_info/2, terminate/2, code_change/3]).
38
39% our published API
40-export([start_link/0]).
41
42% client functions, may internally talk to our sccp_user server
43-export([create_route/3, delete_route/3]).
44-export([dump/0]).
45-export([route_dpc/1]).
46
47-record(ss7route, {
48 remote_pc_mask, % {remote_pc, remote_pc_mask}
49 linkset_name
50}).
51
52-record(sr_state, {
53 route_tbl
54}).
55
56% initialization code
57
58start_link() ->
59 gen_server:start_link({local, ?MODULE}, ?MODULE, [], [{debug, [trace]}]).
60
61init(_Arg) ->
62 RouteTbl = ets:new(ss7_routes, [ordered_set, named_table,
63 {keypos, #ss7route.remote_pc_mask}]),
64 process_flag(trap_exit, true),
65 {ok, #sr_state{route_tbl = RouteTbl}}.
66
67% client side API
68
69% all write operations go through gen_server:call(), as only the ?MODULE
70% process has permission to modify the table content
71
72create_route(RemotePc, RemoteMask, LinksetName) ->
73 gen_server:call(?MODULE, {create_route, {RemotePc, RemoteMask, LinksetName}}).
74
75delete_route(RemotePc, RemoteMask, LinksetName) ->
76 gen_server:call(?MODULE, {delete_route, {RemotePc, RemoteMask, LinksetName}}).
77
78% the lookup functions can directly use the ets named_table from within
79% the client process, no need to go through a synchronous IPC
80
81route_dpc(Dpc) ->
82 % this was generated by ets:fun2ms() on the shell
83 Match = [{#ss7route{remote_pc_mask={'$1','$2'},linkset_name='$3'},
84 [{'==',{'band',Dpc,'$2'},'$1'}],
85 ['$3']}],
86 case ets:select(ss7_routes, Match) of
87 [Name|_] ->
88 {ok, Name};
89 _ ->
90 {error, no_route}
91 end.
92
93dump() ->
94 List = ets:tab2list(ss7_routes),
95 dump_routes(List).
96
97dump_routes([]) ->
98 ok;
99dump_routes([Head|Tail]) when is_record(Head, ss7route) ->
100 dump_single_route(Head),
101 dump_routes(Tail).
102
103dump_single_route(#ss7route{remote_pc_mask = {Pc, Mask},
104 linkset_name = Name}) ->
Harald Welte9bfab7c2011-10-12 00:02:42 +0200105 PcTuple = osmo_util:pointcode_fmt(itu, Pc),
106 MaskTuple = osmo_util:pointcode_fmt(itu, Mask),
Harald Welteb8bfc4e2011-10-11 18:49:59 +0200107 io:format("Dest PC ~p/~p -> Linkset ~p~n",
Harald Welte9bfab7c2011-10-12 00:02:42 +0200108 [PcTuple, MaskTuple, Name]).
Harald Welteb8bfc4e2011-10-11 18:49:59 +0200109
110% server side code
111
112handle_call({create_route, {RemotePc, RemoteMask, Name}},
113 {_FromPid, _FromRef}, S) ->
114 #sr_state{route_tbl = Tbl} = S,
115 R = #ss7route{remote_pc_mask = {RemotePc, RemoteMask},
116 linkset_name = Name},
117 case ets:insert_new(Tbl, R) of
118 false ->
119 {reply, {error, ets_insert}, S};
120 _ ->
121 {reply, ok, S}
122 end;
123
124handle_call({delete_route, {RemotePc, RemoteMask, _Name}},
125 {_FromPid, _FromRef}, S) ->
126 #sr_state{route_tbl = Tbl} = S,
127 ets:delete(Tbl, {RemotePc, RemoteMask}),
128 {reply, ok, S}.
129
130handle_info(Info, S) ->
131 error_logger:error_report(["unknown handle_info",
132 {module, ?MODULE},
133 {info, Info}, {state, S}]),
134 {noreply, S}.
135
136terminate(Reason, _S) ->
137 io:format("terminating ~p with reason ~p", [?MODULE, Reason]),
138 ok.
139
140code_change(_OldVsn, State, _Extra) ->
141 {ok, State}.