Skip to content

Commit

Permalink
add possibility to use a custom function for dns lookup
Browse files Browse the repository at this point in the history
new config: `dns_lookup_hook`
  • Loading branch information
Ole Rixmann committed May 10, 2023
1 parent 32e7c73 commit ec64c4e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ Example of full configuration with keys which can use in `eradius`:
%% Size of RADIUS receive buffer
{recbuf, 8192},
%% The minimum size of the send buffer to use for the RADIUS
{sndbuf, 131072}
{sndbuf, 131072},
%% Define a custom DNS-lookup function to replace `inet:gethostbyname/1`
{dns_lookup_hook, fun inet:gethostbyname/1}
]}].
```

Expand Down
5 changes: 3 additions & 2 deletions src/eradius_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
%% parameter. Changing it currently requires a restart. It can be given as a string or ip address tuple,
%% or the atom ``undefined'' (the default), which uses whatever address the OS selects.
-module(eradius_client).
-export([start_link/0, send_request/2, send_request/3, send_remote_request/3, send_remote_request/4]).
-export([start_link/0, send_request/2, send_request/3, send_remote_request/3, send_remote_request/4, get_ip/1]).
%% internal
-export([reconfigure/0, send_remote_request_loop/8, find_suitable_peer/1,
restore_upstream_server/1, store_radius_server_from_pool/3,
Expand Down Expand Up @@ -655,7 +655,8 @@ find_suitable_peer([{IP, Port, Secret, _Opts} | Pool]) ->
find_suitable_peer([{IP, Port, Secret} | Pool]).

get_ip(Host) ->
case inet:gethostbyname(Host) of
DnsLookup = application:get_env(eradius, dns_lookup_hook, fun inet:gethostbyname/1 ),
case DnsLookup(Host) of
{ok, #hostent{h_addrtype = inet, h_addr_list = [IP]}} ->
IP;
{ok, #hostent{h_addrtype = inet, h_addr_list = [_ | _] = IPs}} ->
Expand Down
30 changes: 30 additions & 0 deletions test/eradius_client_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

all() -> [
send_request,
dns_lookup_hook,
wanna_send,
reconf_address,
wanna_send,
Expand All @@ -72,6 +73,10 @@ init_per_testcase(send_request, Config) ->
application:stop(eradius),
eradius_test_handler:start(),
Config;
init_per_testcase(dns_lookup_hook, Config) ->
application:stop(eradius),
eradius_test_handler:start(),
Config;
init_per_testcase(send_request_failover, Config) ->
application:stop(eradius),
eradius_test_handler:start(),
Expand All @@ -86,6 +91,9 @@ init_per_testcase(_Test, Config) ->
end_per_testcase(send_request, Config) ->
eradius_test_handler:stop(),
Config;
end_per_testcase(dns_lookup_hook, Config) ->
eradius_test_handler:stop(),
Config;
end_per_testcase(send_request_failover, Config) ->
eradius_test_handler:stop(),
Config;
Expand Down Expand Up @@ -207,6 +215,22 @@ send_request(_Config) ->
?equal(accept, eradius_test_handler:send_request(eradius_test_handler:localhost(binary))),
ok.

dns_lookup_hook(_Config) ->
application:set_env(eradius, dns_lookup_hook, fun dns_lookup_callback/1),
?equal(accept, eradius_test_handler:send_request(eradius_test_handler:localhost(binary))),

%% Test against 127.0.0.2 => timeout
application:set_env(eradius, dns_lookup_hook, fun dns_lookup_fail_callback/1),
try
eradius_test_handler:send_request(eradius_test_handler:localhost(binary), [{timeout, 100}]),
error(badmatch)
catch
error:{badmatch, {error, timeout}} ->
ok
end,
application:unset_env(eradius, dns_lookup_hook).


send(FUN, Ports, Address) ->
meckStart(),
{ok, OldState} = gen_server:call(eradius_client, debug),
Expand Down Expand Up @@ -252,3 +276,9 @@ send_request_failover(_Config) ->
check_upstream_servers(_Config) ->
?equal(?RADIUS_SERVERS, ets:tab2list(eradius_client)),
ok.

dns_lookup_callback(_) ->
{ok,{hostent,"localhost",[],inet,4,[{127,0,0,1}]}}.

dns_lookup_fail_callback(_) ->
{ok,{hostent,"localhost",[],inet,4,[{127,0,0,2}]}}.
6 changes: 4 additions & 2 deletions test/eradius_test_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

-behaviour(eradius_server).

-export([start/0, stop/0, send_request/1, send_request_failover/1, radius_request/3]).
-export([start/0, stop/0, send_request/1, send_request/2, send_request_failover/1, radius_request/3]).
-export([localhost/1]).

-include("include/eradius_lib.hrl").
Expand Down Expand Up @@ -30,7 +30,9 @@ stop() ->
application:start(eradius).

send_request(IP) ->
{ok, R, A} = eradius_client:send_request({IP, 1812, "secret"}, #radius_request{cmd = request}, []),
send_request(IP, []).
send_request(IP, Options) ->
{ok, R, A} = eradius_client:send_request({IP, 1812, "secret"}, #radius_request{cmd = request}, Options),
#radius_request{cmd = Cmd} = eradius_lib:decode_request(R, <<"secret">>, A),
Cmd.

Expand Down

0 comments on commit ec64c4e

Please sign in to comment.