Skip to content
Open
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
36 changes: 27 additions & 9 deletions src/eradius_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
Req#radius_request.cmd == 'coareq' orelse
Req#radius_request.cmd == 'discreq')).

-type nas_address() :: {string() | binary() | inet:ip_address(),
eradius_server:port_number(),
-type nas_address() :: {string() | binary() | inet:ip_address(),
eradius_server:port_number(),
eradius_lib:secret()}.
-type options() :: [{retries, pos_integer()} |
{timeout, timeout()} |
Expand All @@ -63,10 +63,10 @@ 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)
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) ->
IP = get_ip(Host),
send_request({IP, Port, Secret}, Request, Options);
Expand Down Expand Up @@ -335,10 +335,19 @@ init([]) ->
Else -> Else
end.

%% @private
inet_family_based_on_peer(_PeerSocket = {{_, _, _, _}, _port}) ->
[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 @@ -512,11 +521,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 @@ -538,7 +547,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}.

init_server_status_metrics() ->
Expand Down Expand Up @@ -689,5 +698,14 @@ get_ip(Host) ->
{ok, #hostent{h_addrtype = inet, h_addr_list = [_ | _] = IPs}} ->
Index = rand:uniform(length(IPs)),
lists:nth(Index, IPs);
_ -> error(badarg)
_ ->
case inet:gethostbyname(Host, inet6) of
{ok, #hostent{h_addrtype = inet6, h_addr_list = [IP]}} ->
IP;
{ok, #hostent{h_addrtype = inet6, h_addr_list = [_ | _] = IPs}} ->
Index = rand:uniform(length(IPs)),
lists:nth(Index, IPs);
_Err ->
error(badarg)
end
end.
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),
SndBuf = application:get_env(eradius, sndbuf, 131072),
{ok, Socket} = gen_udp:open(0, [{active, once}, binary , {recbuf, RecBuf}, {sndbuf, SndBuf} | ExtraOptions]),
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 @@ -148,7 +148,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 @@ -165,7 +165,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