diff options
author | Evgeniy Khramtsov <ekhramtsov@process-one.net> | 2017-04-30 19:01:47 +0300 |
---|---|---|
committer | Evgeniy Khramtsov <ekhramtsov@process-one.net> | 2017-04-30 19:01:47 +0300 |
commit | fddd6110e00df12c99a20a2cc9d074f5f4f1f965 (patch) | |
tree | 366575b855f1b2013db7eeb02ecb213f81c98c1f /src/ejabberd_stun.erl | |
parent | Merge branch 'new-option-validation' (diff) |
Don't validate an option in gen_mod:get*opt() functions
The changes are very similar to those from previous commit:
* Now there is no need to pass validating function in
gen_mod:get_opt() and gen_mod:get_module_opt() functions,
because the modules' configuration keeps already validated values.
* New functions gen_mod:get_opt/2 and gen_mod:get_module_opt/3 are
introduced.
* Functions gen_mod:get_opt/4 and get_module_opt/5 are deprecated.
If the functions are still called, the "function" argument is
simply ignored.
* Validating callback Mod:listen_opt_type/1 is introduced to validate
listening options at startup.
Diffstat (limited to 'src/ejabberd_stun.erl')
-rw-r--r-- | src/ejabberd_stun.erl | 53 |
1 files changed, 44 insertions, 9 deletions
diff --git a/src/ejabberd_stun.erl b/src/ejabberd_stun.erl index ebe98f476..c45caf686 100644 --- a/src/ejabberd_stun.erl +++ b/src/ejabberd_stun.erl @@ -28,7 +28,7 @@ -protocol({xep, 176, '1.0'}). -export([tcp_init/2, udp_init/2, udp_recv/5, start/2, - socket_type/0]). + socket_type/0, listen_opt_type/1]). -include("ejabberd.hrl"). -include("logger.hrl"). @@ -73,14 +73,9 @@ prepare_turn_opts(Opts, _UseTurn = true) -> ok end, AuthFun = fun ejabberd_auth:get_password_s/2, - Shaper = gen_mod:get_opt(shaper, Opts, - fun(S) when is_atom(S) -> S end, - none), - AuthType = gen_mod:get_opt(auth_type, Opts, - fun(anonymous) -> anonymous; - (user) -> user - end, user), - Realm = case gen_mod:get_opt(auth_realm, Opts, fun iolist_to_binary/1) of + Shaper = gen_mod:get_opt(shaper, Opts, none), + AuthType = gen_mod:get_opt(auth_type, Opts, user), + Realm = case gen_mod:get_opt(auth_realm, Opts) of undefined when AuthType == user -> if NumberOfMyHosts > 1 -> ?WARNING_MSG("you have several virtual " @@ -100,3 +95,43 @@ prepare_turn_opts(Opts, _UseTurn = true) -> MaxRate = shaper:get_max_rate(Shaper), Realm ++ [{auth_fun, AuthFun},{shaper, MaxRate} | lists:keydelete(shaper, 1, Opts)]. + +listen_opt_type(use_turn) -> + fun(B) when is_boolean(B) -> B end; +listen_opt_type(turn_ip) -> + fun(S) -> + {ok, Addr} = inet_parse:ipv4_address(binary_to_list(S)), + Addr + end; +listen_opt_type(shaper) -> + fun acl:shaper_rules_validator/1; +listen_opt_type(auth_type) -> + fun(anonymous) -> anonymous; + (user) -> user + end; +listen_opt_type(auth_realm) -> + fun iolist_to_binary/1; +listen_opt_type(tls) -> + fun(B) when is_boolean(B) -> B end; +listen_opt_type(certfile) -> + fun iolist_to_binary/1; +listen_opt_type(turn_min_port) -> + fun(P) when is_integer(P), P > 0, P =< 65535 -> P end; +listen_opt_type(turn_max_port) -> + fun(P) when is_integer(P), P > 0, P =< 65535 -> P end; +listen_opt_type(turn_max_allocations) -> + fun(I) when is_integer(I), I>0 -> I; + (unlimited) -> infinity; + (infinity) -> infinity + end; +listen_opt_type(turn_max_permissions) -> + fun(I) when is_integer(I), I>0 -> I; + (unlimited) -> infinity; + (infinity) -> infinity + end; +listen_opt_type(server_name) -> + fun iolist_to_binary/1; +listen_opt_type(_) -> + [shaper, auth_type, auth_realm, tls, certfile, turn_min_port, + turn_max_port, turn_max_allocations, turn_max_permissions, + server_name]. |