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

Support optional socket options #215

Merged
merged 7 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
34 changes: 25 additions & 9 deletions src/eradius_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,21 @@ validate_new_config_start(Servers, Nodes) ->
map_helper(fun(Server) -> validate_new_server_config(Server, Nodes) end, Servers, flatten).

validate_new_server_config({Name, {IP, ListOfPorts}}, Nodes) ->
validate_new_server_config(Name, get_app_env(Name), validate_ip(IP), validate_ports(ListOfPorts), Nodes).
validate_new_server_config(Name, get_app_env(Name), validate_ip(IP), validate_ports(ListOfPorts), [], Nodes);

validate_new_server_config(_Server, {invalid, _} = Invalid, _IP, _ListOfPorts, _Nodes) -> Invalid;
validate_new_server_config(_Server, _NasList, {invalid, _} = Invalid, _ListOfPorts, _Nodes) -> Invalid;
validate_new_server_config(_Server, _NasList, _IP, {invalid, _} = Invalid, _Nodes) -> Invalid;
validate_new_server_config(Server, NasList, IP, ListOfPorts, Nodes) ->
validate_new_server_config({Name, {IP, ListOfPorts, Opts}}, Nodes) ->
validate_new_server_config(Name, get_app_env(Name), validate_ip(IP), validate_ports(ListOfPorts), validate_options(Opts), Nodes).

validate_new_server_config(_Server, {invalid, _} = Invalid, _IP, _ListOfPorts, _Opts, _Nodes) -> Invalid;
validate_new_server_config(_Server, _NasList, {invalid, _} = Invalid, _ListOfPorts, _Opts, _Nodes) -> Invalid;
validate_new_server_config(_Server, _NasList, _IP, {invalid, _} = Invalid, _Opts, _Nodes) -> Invalid;
validate_new_server_config(_Server, _NasList, _IP, _ListOfPorts, {invalid, _} = Invalid, _Nodes) -> Invalid;
validate_new_server_config(Server, NasList, IP, ListOfPorts, Opts, Nodes) ->
case validate_new_nas_list(NasList, {IP, ListOfPorts, Nodes}) of
{invalid, _} = Invalid ->
Invalid;
Values ->
lists:map(fun(Port) -> {Server, {IP, Port}, Values} end, ListOfPorts)
lists:map(fun(Port) -> {Server, {IP, Port, Opts}, Values} end, ListOfPorts)
end.

validate_new_nas_list(NasLists, ServerConfig) ->
Expand All @@ -65,11 +69,11 @@ validate_behavior({Module, Nas, _Args} = Value) when is_atom(Module) andalso ?is
false -> Value
end;
validate_behavior({Module, _, _}) when is_atom(Module) ->
?invalid("bad NAS Id in Behavior specifification: ~p", [Module]);
?invalid("bad NAS Id in Behavior specification: ~p", [Module]);
validate_behavior({Module, _, _}) ->
?invalid("bad module in Behavior specifification: ~p", [Module]);
?invalid("bad module in Behavior specification: ~p", [Module]);
validate_behavior(Term) ->
?invalid("bad Term in Behavior specifification: ~p", [Term]).
?invalid("bad Term in Behavior specification: ~p", [Term]).

validate_arguments({Module, Nas, Args} = Value) ->
case Module:validate_arguments(Args) of
Expand Down Expand Up @@ -125,6 +129,11 @@ validate_port(Port) when ?pos_int(Port) -> Port;
validate_port(Port) when is_integer(Port) -> ?invalid("port number out of range: ~p", [Port]);
validate_port(Port) -> ?invalid("bad port number: ~p", [Port]).

validate_options(Opts) when is_list(Opts) ->
Opts;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could use a check to restrict the options to the once we want to allow. For example there is no point to allow the binary (or list), the active, ip and recbuf options.

Don't do a negative filtering, explicitly check only for allowed options.

A full validation of the content of the options is not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: I have introduced validate_socket_options that checks if a specific options is in a banned list

validate_options(Opts) ->
?invalid("expect a list of options: ~p", Opts).

check_root([First | _] = AllNodes) when is_tuple(First) ->
map_helper(fun({Name, List}) ->
case validate_handler_nodes(List) of
Expand Down Expand Up @@ -228,6 +237,13 @@ validate_server(String) when is_list(String) ->
_ ->
{invalid, io_lib:format("bad address/port combination: ~p", [String])}
end;
validate_server({IP, Port, Opts}) when is_list(Opts) ->
case validate_server({IP, Port}) of
0xAX marked this conversation as resolved.
Show resolved Hide resolved
{invalid, _Reason} = E ->
E;
{ValidIP, ValidPort} ->
{ValidIP, ValidPort, Opts}
end;
validate_server(X) ->
{invalid, io_lib:format("bad address/port combination: ~p", [X])}.

Expand Down
22 changes: 12 additions & 10 deletions src/eradius_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
%% }).
%% '''
-module(eradius_server).
-export([start_link/3]).
-export([start_link/3, start_link/4]).
-export_type([port_number/0, req_id/0]).

%% internal
Expand All @@ -60,7 +60,6 @@
-behaviour(gen_server).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

-include_lib("stdlib/include/ms_transform.hrl").
-include_lib("kernel/include/logger.hrl").
-include("eradius_lib.hrl").
-include("dictionary.hrl").
Expand Down Expand Up @@ -92,27 +91,30 @@
-callback radius_request(#radius_request{}, #nas_prop{}, HandlerData :: term()) ->
{reply, #radius_request{}} | noreply | {error, timeout}.

%% @private
-spec start_link(atom(), inet:ip4_address(), port_number()) -> {ok, pid()} | {error, term()}.
start_link(ServerName, IP = {A,B,C,D}, Port) ->
start_link(ServerName, IP, Port) ->
start_link(ServerName, IP, Port, []).

-spec start_link(atom(), inet:ip4_address(), port_number(), [inet:socket_setopt()]) -> {ok, pid()} | {error, term()}.
start_link(ServerName, IP = {A,B,C,D}, Port, Opts) ->
Name = list_to_atom(lists:flatten(io_lib:format("eradius_server_~b.~b.~b.~b:~b", [A,B,C,D,Port]))),
gen_server:start_link({local, Name}, ?MODULE, {ServerName, IP, Port}, []).
gen_server:start_link({local, Name}, ?MODULE, {ServerName, IP, Port, Opts}, []).

stats(Server, Function) ->
gen_server:call(Server, {stats, Function}).

%% ------------------------------------------------------------------------------------------
%% -- gen_server Callbacks
%% @private
init({ServerName, IP, Port}) ->
init({ServerName, IP, Port, Opts}) ->
process_flag(trap_exit, true),
RecBuf = application:get_env(eradius, recbuf, 8192),
case gen_udp:open(Port, [{active, once}, {ip, IP}, binary, {recbuf, RecBuf}]) of
case gen_udp:open(Port, [{active, once}, {ip, IP}, binary, {recbuf, RecBuf}] ++ Opts) of
meox marked this conversation as resolved.
Show resolved Hide resolved
{ok, Socket} ->
{ok, #state{socket = Socket,
ip = IP, port = Port, name = ServerName,
transacts = ets:new(transacts, []),
counter = eradius_counter:init_counter({IP, Port, ServerName})}};
ip = IP, port = Port, name = ServerName,
vkatsuba marked this conversation as resolved.
Show resolved Hide resolved
transacts = ets:new(transacts, []),
counter = eradius_counter:init_counter({IP, Port, ServerName})}};
{error, Reason} ->
{stop, Reason}
end.
Expand Down
28 changes: 18 additions & 10 deletions src/eradius_server_mon.erl
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ configure(#state{running = Running}) ->
{ok, #state{running = NewRunning}}
end.

server_naslist({ServerName, {IP, Port, _Opts}, HandlerList}) ->
server_naslist({ServerName, {IP, Port}, HandlerList});
server_naslist({ServerName, {IP, Port}, HandlerList}) ->
lists:map(fun({NasId, NasIP, Secret, HandlerNodes, HandlerMod, HandlerArgs}) ->
ServerInfo = eradius_lib:make_addr_info({ServerName, {IP, Port}}),
Expand All @@ -144,18 +146,24 @@ update_server(Running, ToStop, ToStart) ->
eradius_server_sup:stop_instance(ServerAddr, Pid),
StoppedServer
end, ToStop),
NewStarted = lists:map(fun(ServerAddr = {ServerName, Addr = {IP, Port}}) ->
case eradius_server_sup:start_instance(ServerAddr) of
{ok, Pid} ->
{ServerName, Addr, Pid};
{error, Error} ->
?LOG(error, "Could not start listener on host: ~s, occuring error: ~p",
[printable_peer(IP, Port), Error])
end
end, ToStart),
StartFn = fun({ServerName, Addr = {IP, Port, _Opts}}=ServerAddr) ->
case eradius_server_sup:start_instance(ServerAddr) of
{ok, Pid} ->
{ServerName, Addr, Pid};
{error, Error} ->
?LOG(error, "Could not start listener on host: ~s, occurring error: ~p",
[printable_peer(IP, Port), Error])
end
end,
NewStarted = lists:map(fun
({ServerName, {IP, Port}}) ->
StartFn({ServerName, {IP, Port, []}});
(ServerAddr) ->
StartFn(ServerAddr)
end,
ToStart),
(Running -- Stopped) ++ NewStarted.

update_nases(ToDelete, ToInsert) ->
lists:foreach(fun(Nas) -> ets:delete_object(?NAS_TAB, Nas) end, ToDelete),
lists:foreach(fun(Nas) -> ets:insert(?NAS_TAB, Nas) end, ToInsert).

6 changes: 5 additions & 1 deletion src/eradius_server_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ start_link() ->

start_instance(_ServerAddr = {ServerName, {IP, Port}}) ->
?LOG(info, "Starting RADIUS Listener at ~s", [printable_peer(IP, Port)]),
supervisor:start_child(?SERVER, [ServerName, IP, Port]).
supervisor:start_child(?SERVER, [ServerName, IP, Port]);
0xAX marked this conversation as resolved.
Show resolved Hide resolved

start_instance(_ServerAddr = {ServerName, {IP, Port, Opts}}) ->
?LOG(info, "Starting RADIUS Listener at ~s", [printable_peer(IP, Port)]),
supervisor:start_child(?SERVER, [ServerName, IP, Port, Opts]).

stop_instance(_ServerAddr = {_ServerName, {IP, Port}}, Pid) ->
?LOG(info, "Stopping RADIUS Listener at ~s", [printable_peer(IP, Port)]),
Expand Down