Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add backwards compatibility for eradius version < 0.6 for remote calling #10

Merged
merged 1 commit into from
Apr 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ eradius

A generic RADIUS client and server.

Version 0.6.2 - 23 Apr 2015
---------------------------
* add backwards compatibility with old eradius versions

Version 0.6.1 - 05 Mar 2015
---------------------------
* switching to lager logging
Expand Down
10 changes: 8 additions & 2 deletions src/eradius_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,14 @@ send_remote_request(Node, NAS, Request) ->
send_remote_request(Node, {IP, Port, Secret}, Request, Options) when ?GOOD_CMD(Request) ->
try gen_server:call({?SERVER, Node}, {wanna_send, {IP, Port}}) of
{Socket, ReqId} ->
Request1 = fill_authenticator(Request#radius_request{reqid = ReqId, secret = Secret}),
SenderPid = spawn(Node, ?MODULE, send_remote_request_loop, [self(), Socket, ReqId, {IP, Port}, Request1, Options]),
Request1 = fill_authenticator(Request#radius_request{reqid = ReqId, secret = Secret}),
Request2 = case eradius_node_mon:get_remote_version(Node) of
{0, Minor} when Minor < 6 ->
eradius_lib:encode_request(Request1);
_ ->
Request1
end,
SenderPid = spawn(Node, ?MODULE, send_remote_request_loop, [self(), Socket, ReqId, {IP, Port}, Request2, Options]),
SenderMonitor = monitor(process, SenderPid),
receive
{SenderPid, Result} ->
Expand Down
40 changes: 38 additions & 2 deletions src/eradius_node_mon.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
%% The node_mon server monitors the application master and removes it from
%% request processing when it goes down.
-module(eradius_node_mon).
-export([start_link/0, modules_ready/2, set_nodes/1, get_module_nodes/1]).
-export([start_link/0, modules_ready/2, set_nodes/1, get_module_nodes/1, get_remote_version/1]).

-behaviour(gen_server).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

-define(NODE_TAB, eradius_node_mon).
-define(NODE_INFO_TAB, eradius_node_info).
-define(PING_INTERVAL, 3000). % 3 sec
-define(PING_TIMEOUT, 300). % 0.3 sec
-define(SERVER, ?MODULE).
Expand All @@ -37,6 +38,15 @@ get_module_nodes(Module) ->
[]
end.

-spec get_remote_version(node()) -> {integer(), integer()} | undefined.
get_remote_version(Node) ->
try
ets:lookup_element(?NODE_INFO_TAB, Node, 2)
catch
error:badarg ->
undefined
end.

%% ------------------------------------------------------------------------------------------
%% -- gen_server callbacks
-record(state, {
Expand All @@ -48,10 +58,12 @@ get_module_nodes(Module) ->

init([]) ->
ets:new(?NODE_TAB, [bag, named_table, protected, {read_concurrency, true}]),
ets:new(?NODE_INFO_TAB, [set, named_table, protected, {read_concurrency, true}]),
PingTimer = erlang:send_after(?PING_INTERVAL, self(), ping_dead_nodes),
{ok, #state{ping_timer = PingTimer}}.

handle_call(remote_get_regs_v1, _From, State) ->
handle_call(remote_get_regs_v1, From, State) ->
check_eradius_version(From),
Registrations = dict:to_list(State#state.app_masters),
{reply, {ok, Registrations}, State};
handle_call({set_nodes, Nodes}, _From, State) ->
Expand All @@ -66,6 +78,7 @@ handle_cast({remote_modules_ready_v1, ApplicationMaster, Modules}, State) ->
handle_cast({modules_ready, ApplicationMaster, Modules}, State) ->
NewState = State#state{app_masters = register_locally({ApplicationMaster, Modules}, State#state.app_masters)},
lists:foreach(fun (Node) ->
check_eradius_version(Node),
gen_server:cast({?SERVER, Node}, {remote_modules_ready_v1, ApplicationMaster, Modules})
end, nodes()),
{noreply, NewState}.
Expand Down Expand Up @@ -131,3 +144,26 @@ register_locally({ApplicationMaster, Modules}, AppMasters) ->
ServerNode = node(ApplicationMaster),
ets:insert(?NODE_TAB, [{Mod, ServerNode} || Mod <- Modules]),
dict_prepend(ApplicationMaster, Modules, AppMasters).

check_eradius_version({Pid, _}) when is_pid(Pid) ->
check_eradius_version(Pid);
check_eradius_version(Pid) when is_pid(Pid) ->
check_eradius_version(node(Pid));
check_eradius_version(Node) ->
case rpc:call(Node, application, get_key, [eradius, vsn]) of
{ok, Vsn} ->
try interpret_vsn(Vsn) of
Version ->
ets:insert(?NODE_INFO_TAB, {Node, Version})
catch
_:_ ->
lager:warning("unknown eradius version format ~p on node ~p", [Vsn, Node])
end;
_ ->
lager:warning("eradius version do not known on node ~p", [Node])
end.

interpret_vsn(Vsn) ->
BinVsn = list_to_binary(Vsn),
[MajorVsn, MinorVsn | _] = binary:split(BinVsn, <<".">>, [global]),
{binary_to_integer(MajorVsn), binary_to_integer(MinorVsn)}.