Skip to content
Closed
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
37 changes: 28 additions & 9 deletions src/eradius_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,29 @@ send_request(NAS, Request) ->
% If no answer is received within the specified timeout, the request will be sent again.
-spec send_request(nas_address(), #radius_request{}, options()) ->
{ok, binary(), eradius_lib:authenticator()} | {error, 'timeout' | 'socket_down'}.
send_request({Host, Port, Secret}, Request, Options)
send_request({Host, Port, Secret}, Request, Options)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please, don't mix pure formatting and white space changes with functional changes.

If you think the whitespace problems need fixing, create a separate pull request for that.

when ?GOOD_CMD(Request) andalso is_binary(Host) ->
send_request({erlang:binary_to_list(Host), Port, Secret}, Request, Options);
send_request({Host, Port, Secret}, Request, Options)
send_request({Host, Port, Secret}, Request, Options)
when ?GOOD_CMD(Request) andalso is_list(Host) ->
case inet:gethostbyname(Host) of
{ok, #hostent{h_addrtype = inet, h_addr_list = [IP]}} ->
{ok, #hostent{h_addrtype = inet, h_addr_list = [IP]}} ->
send_request({IP, Port, Secret}, Request, Options);
{ok, #hostent{h_addrtype = inet, h_addr_list = [_ | _] = IPs}} ->
{ok, #hostent{h_addrtype = inet, h_addr_list = [_ | _] = IPs}} ->
Index = rand:uniform(length(IPs)),
IP = lists:nth(Index, IPs),
send_request({IP, Port, Secret}, Request, Options);
_ -> error(badarg)
_Err ->

@0xAX 0xAX Oct 14, 2021

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This could be simplified a bit. The whole send_request function may look like this:

send_request({Host, Port, Secret}, Request, Options)
  when ?GOOD_CMD(Request) andalso is_list(Host) ->
    case gethostbyname(Host) of
        {ok, #hostent{h_addr_list = [IP]}} ->
            send_request({IP, Port, Secret}, Request, Options);
        {ok, #hostent{h_addr_list = [_ | _] = IPs}} ->
            Index = rand:uniform(length(IPs)),
            IP = lists:nth(Index, IPs),
            send_request({IP, Port, Secret}, Request, Options);
        _Err -> error(badarg)
     end
   end;

gethostbyname(Host) ->
  case inet:gеthostbyname(Host, inet6) do
    {error, _} ->
      inet:gethostbyname(Host, inet);
    Hostent ->
      Hostent
end.

case inet:gethostbyname(Host, inet6) of
{ok, #hostent{h_addrtype = inet6, h_addr_list = [IP]}} ->
send_request({IP, Port, Secret}, Request, Options);
{ok, #hostent{h_addrtype = inet6, h_addr_list = [_ | _] = IPs}} ->
Index = rand:uniform(length(IPs)),
IP = lists:nth(Index, IPs),
send_request({IP, Port, Secret}, Request, Options);
_Err ->
error(badarg)
end
end;
send_request({IP, Port, Secret}, Request, Options) when ?GOOD_CMD(Request) andalso is_tuple(IP) ->
TS1 = eradius_lib:timestamp(milli_seconds),
Expand Down Expand Up @@ -315,10 +325,19 @@ init([]) ->
Else -> Else
end.

%% @private
inet_family_based_on_peer(_PeerSocket = {{_, _, _, _}, _port}) ->

@0xAX 0xAX Oct 14, 2021

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Only inet_family_based_on_peer/1 is called everywhere. So the:

inet_family_based_on_peer(_PeerSocket) ->
    [].

should be removed at all and inet_family_based_on_peer/2 should be reworked to inet_family_based_on_peer/1 like:

inet_family_based_on_peer({_, _, _, _}) ->
    [inet];
inet_family_based_on_peer({_, _, _, _, _, _, _, _}) ->
    [inet6].

That should be safe to do as inet_family_based_on_peer/1 will be called with proper IP address all the time.

[inet];
inet_family_based_on_peer(_PeerSocket = {{_, _, _, _, _, _, _, _}, _port}) ->
[inet6];
inet_family_based_on_peer(_PeerSocket) ->
[].

%% @private
handle_call({wanna_send, Peer = {_PeerName, PeerSocket}, _MetricsInfo}, _From, State) ->
{PortIdx, ReqId, NewIdCounters} = next_port_and_req_id(PeerSocket, State#state.no_ports, State#state.idcounters),
{SocketProcess, NewSockets} = find_socket_process(PortIdx, State#state.sockets, State#state.socket_ip, State#state.sup),
InetFamily = inet_family_based_on_peer(PeerSocket),
{SocketProcess, NewSockets} = find_socket_process(PortIdx, State#state.sockets, State#state.socket_ip, InetFamily, State#state.sup),
IsCreated = lists:member(Peer, State#state.clients),
NewState = case IsCreated of
false ->
Expand Down Expand Up @@ -464,11 +483,11 @@ next_port_and_req_id(Peer, NumberOfPorts, Counters) ->
NewCounters = Counters#{Peer => {NextPortIdx, NextReqId}},
{NextPortIdx, NextReqId, NewCounters}.

find_socket_process(PortIdx, Sockets, SocketIP, Sup) ->
find_socket_process(PortIdx, Sockets, SocketIP, Options, Sup) ->
case array:get(PortIdx, Sockets) of
undefined ->
Res = supervisor:start_child(Sup, {PortIdx,
{eradius_client_socket, start, [SocketIP, self(), PortIdx]},
{eradius_client_socket, start, [SocketIP, self(), PortIdx, Options]},
transient, brutal_kill, worker, [eradius_client_socket]}),
Pid = case Res of
{ok, P} -> P;
Expand All @@ -490,7 +509,7 @@ parse_ip(Address) when is_list(Address) ->
inet_parse:address(Address);
parse_ip(T = {_, _, _, _}) ->
{ok, T};
parse_ip(T = {_, _, _, _, _, _}) ->
parse_ip(T = {_, _, _, _, _, _, _, _}) ->
{ok, T}.

make_metrics_info(Options, {ServerIP, ServerPort}) ->
Expand Down
21 changes: 11 additions & 10 deletions src/eradius_client_socket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@

-behaviour(gen_server).

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

-record(state, {client, socket, pending, mode, counter}).

start(SocketIP, Client, PortIdx) ->
gen_server:start_link(?MODULE, [SocketIP, Client, PortIdx], []).
start(SocketIP, Client, PortIdx, Options) ->
gen_server:start_link(?MODULE, [SocketIP, Client, PortIdx, Options], []).

init([SocketIP, Client, PortIdx]) ->
init([SocketIP, Client, PortIdx, Options]) ->
Client ! {PortIdx, self()},
case SocketIP of
undefined ->
ExtraOptions = [];
SocketIP when is_tuple(SocketIP) ->
ExtraOptions = [{ip, SocketIP}]
end,
ExtraOptions =
case SocketIP of
undefined ->
Options;
SocketIP when is_tuple(SocketIP) ->
[{ip, SocketIP} | Options]
end,
RecBuf = application:get_env(eradius, recbuf, 8192),
{ok, Socket} = gen_udp:open(0, [{active, once}, binary , {recbuf, RecBuf} | ExtraOptions]),
{ok, #state{client = Client, socket = Socket, pending = maps:new(), mode = active, counter = 0}}.
Expand Down
4 changes: 2 additions & 2 deletions test/eradius_client_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ split2(N, List1, [L|List2]) -> split2(N-1, [L|List1], List2).

meckStart() ->
ok = meck:new(eradius_client_socket),
ok = meck:expect(eradius_client_socket, start, fun(X, Y, Z) -> eradius_client_socket_test:start(X, Y, Z) end),
ok = meck:expect(eradius_client_socket, start, fun(X, Y, Z, O) -> eradius_client_socket_test:start(X, Y, Z, O) end),
ok = meck:expect(eradius_client_socket, init, fun(X) -> eradius_client_socket_test:init(X) end),
ok = meck:expect(eradius_client_socket, handle_call, fun(X, Y, Z) -> eradius_client_socket_test:handle_call(X, Y, Z) end),
ok = meck:expect(eradius_client_socket, handle_cast, fun(X, Y) -> eradius_client_socket_test:handle_cast(X, Y) end),
Expand All @@ -139,7 +139,7 @@ parse_ip(Address) when is_list(Address) ->
inet_parse:address(Address);
parse_ip(T = {_, _, _, _}) ->
{ok, T};
parse_ip(T = {_, _, _, _, _, _}) ->
parse_ip(T = {_, _, _, _, _, _, _, _}) ->
{ok, T}.

%% CHECK
Expand Down
8 changes: 4 additions & 4 deletions test/eradius_client_socket_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@

-behaviour(gen_server).

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

-record(state, {client, socket, pending, mode, counter}).

start(SocketIP, Client, PortIdx) ->
gen_server:start_link(?MODULE, [SocketIP, Client, PortIdx], []).
start(SocketIP, Client, PortIdx, Options) ->
gen_server:start_link(?MODULE, [SocketIP, Client, PortIdx, Options], []).

init([_SocketIP, Client, PortIdx]) ->
init([_SocketIP, Client, PortIdx, Options]) ->
Client ! {PortIdx, self()},
eradius_client_SUITE:addSocket(),
{ok, #state{pending = maps:new(), mode = active, counter = 0}}.
Expand Down