From ec64c4e13ff82db530b0b042d1252d9f3dc29037 Mon Sep 17 00:00:00 2001 From: Ole Rixmann Date: Wed, 10 May 2023 15:23:54 +0200 Subject: [PATCH] add possibility to use a custom function for dns lookup new config: `dns_lookup_hook` --- README.md | 4 +++- src/eradius_client.erl | 5 +++-- test/eradius_client_SUITE.erl | 30 ++++++++++++++++++++++++++++++ test/eradius_test_handler.erl | 6 ++++-- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 22f3938..414b558 100644 --- a/README.md +++ b/README.md @@ -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} ]}]. ``` diff --git a/src/eradius_client.erl b/src/eradius_client.erl index dfccd13..db9b0b7 100644 --- a/src/eradius_client.erl +++ b/src/eradius_client.erl @@ -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, @@ -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}} -> diff --git a/test/eradius_client_SUITE.erl b/test/eradius_client_SUITE.erl index ded4ea0..374f588 100644 --- a/test/eradius_client_SUITE.erl +++ b/test/eradius_client_SUITE.erl @@ -47,6 +47,7 @@ all() -> [ send_request, + dns_lookup_hook, wanna_send, reconf_address, wanna_send, @@ -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(), @@ -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; @@ -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), @@ -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}]}}. diff --git a/test/eradius_test_handler.erl b/test/eradius_test_handler.erl index e370af8..3ac6132 100644 --- a/test/eradius_test_handler.erl +++ b/test/eradius_test_handler.erl @@ -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"). @@ -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.