Skip to content

Commit

Permalink
Give access to the c2s_data to feature stanzas
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Aug 11, 2023
1 parent 8143798 commit 0c0ab93
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 47 deletions.
36 changes: 10 additions & 26 deletions src/c2s/mongoose_c2s.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
-xep([{xep, 170}, {version, "1.0"}]).

-behaviour(gen_statem).
-include("mongoose_c2s.hrl").
-include("mongoose_logger.hrl").
-include("mongoose.hrl").
-include("jlib.hrl").
-include_lib("exml/include/exml_stream.hrl").
-define(XMPP_VERSION, <<"1.0">>).
Expand All @@ -16,29 +16,14 @@
%% utils
-export([start_link/2, start/2, stop/2, exit/2, async/3, async_with_state/3, call/3, cast/3]).
-export([create_data/1, get_host_type/1, get_lserver/1, get_sid/1, get_jid/1,
get_info/1, set_info/2,
new_stream_id/0, get_info/1, set_info/2,
get_mod_state/2, get_listener_opts/1, merge_mod_state/2, remove_mod_state/2,
get_ip/1, get_socket/1, get_lang/1, get_stream_id/1, hook_arg/5]).
-export([get_auth_mechs/1, c2s_stream_error/2, maybe_retry_state/1, merge_states/2]).
-export([route/2, reroute_buffer/2, reroute_buffer_to_pid/3, open_session/1]).

-ignore_xref([get_ip/1, get_socket/1]).

-record(c2s_data, {
host_type :: undefined | mongooseim:host_type(),
lserver = ?MYNAME :: jid:lserver(),
lang = ?MYLANG :: ejabberd:lang(),
sid = ejabberd_sm:make_new_sid() :: ejabberd_sm:sid(),
streamid = new_stream_id() :: binary(),
jid :: undefined | jid:jid(),
socket :: undefined | mongoose_c2s_socket:socket(),
parser :: undefined | exml_stream:parser(),
shaper :: undefined | shaper:shaper(),
listener_opts :: undefined | listener_opts(),
auth_module :: undefined | module(),
state_mod = #{} :: #{module() => term()},
info = #{} :: info()
}).
-type data() :: #c2s_data{}.
-type maybe_ok() :: ok | {error, atom()}.
-type fsm_res() :: gen_statem:event_handler_result(state(), data()).
Expand Down Expand Up @@ -66,7 +51,7 @@
proto := tcp,
term() => term()}.

-export_type([packet/0, data/0, state/0, state/1, listener_opts/0]).
-export_type([packet/0, info/0, data/0, state/0, state/1, listener_opts/0]).

%%%----------------------------------------------------------------------
%%% gen_statem
Expand Down Expand Up @@ -443,20 +428,19 @@ handle_sasl_success(State = #c2s_data{listener_opts = LOpts}, Creds) ->

-spec stream_start_features_before_auth(data()) -> fsm_res().
stream_start_features_before_auth(#c2s_data{host_type = HostType, lserver = LServer,
listener_opts = LOpts} = S) ->
send_header(S),
listener_opts = LOpts} = StateData) ->
send_header(StateData),
CredOpts = mongoose_credentials:make_opts(LOpts),
Creds = mongoose_credentials:new(LServer, HostType, CredOpts),
SASLState = cyrsasl:server_new(<<"jabber">>, LServer, HostType, <<>>, [], Creds),
StreamFeatures = mongoose_c2s_stanzas:stream_features_before_auth(HostType, LServer, LOpts, S),
send_element_from_server_jid(S, StreamFeatures),
{next_state, {wait_for_feature_before_auth, SASLState, ?AUTH_RETRIES}, S, state_timeout(LOpts)}.
StreamFeatures = mongoose_c2s_stanzas:stream_features_before_auth(StateData),
send_element_from_server_jid(StateData, StreamFeatures),
{next_state, {wait_for_feature_before_auth, SASLState, ?AUTH_RETRIES}, StateData, state_timeout(LOpts)}.

-spec stream_start_features_after_auth(data()) -> fsm_res().
stream_start_features_after_auth(#c2s_data{host_type = HostType, lserver = LServer,
listener_opts = LOpts} = StateData) ->
stream_start_features_after_auth(#c2s_data{listener_opts = LOpts} = StateData) ->
send_header(StateData),
StreamFeatures = mongoose_c2s_stanzas:stream_features_after_auth(HostType, LServer, LOpts),
StreamFeatures = mongoose_c2s_stanzas:stream_features_after_auth(StateData),
send_element_from_server_jid(StateData, StreamFeatures),
{next_state, {wait_for_feature_after_auth, ?BIND_RETRIES}, StateData, state_timeout(LOpts)}.

Expand Down
19 changes: 19 additions & 0 deletions src/c2s/mongoose_c2s.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-ifndef(MONGOOSE_C2S_STATE).
-define(MONGOOSE_C2S_STATE, true).
-include("mongoose.hrl").
-record(c2s_data, {
host_type :: undefined | mongooseim:host_type(),
lserver = ?MYNAME :: jid:lserver(),
lang = ?MYLANG :: ejabberd:lang(),
sid = ejabberd_sm:make_new_sid() :: ejabberd_sm:sid(),
streamid = mongoose_c2s:new_stream_id() :: binary(),
jid :: undefined | jid:jid(),
socket :: undefined | mongoose_c2s_socket:socket(),
parser :: undefined | exml_stream:parser(),
shaper :: undefined | shaper:shaper(),
listener_opts :: undefined | mongoose_c2s:listener_opts(),
auth_module :: undefined | module(),
state_mod = #{} :: #{module() => term()},
info = #{} :: mongoose_c2s:info()
}).
-endif.
38 changes: 21 additions & 17 deletions src/c2s/mongoose_c2s_stanzas.erl
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
-module(mongoose_c2s_stanzas).

-include_lib("exml/include/exml_stream.hrl").
-include("mongoose_c2s.hrl").
-include("jlib.hrl").

-export([
stream_header/4,
stream_features_before_auth/4,
stream_features_before_auth/1,
tls_proceed/0,
stream_features_after_auth/3,
stream_features_after_auth/1,
sasl_success_stanza/1,
sasl_failure_stanza/1,
sasl_challenge_stanza/1,
Expand Down Expand Up @@ -36,11 +37,12 @@ stream_header(Server, Version, Lang, StreamId) ->
stream_features(Features) ->
#xmlel{name = <<"stream:features">>, children = Features}.

-spec stream_features_before_auth(
mongooseim:host_type(), jid:lserver(), mongoose_listener:options(), mongoose_c2s:data()) ->
exml:element().
stream_features_before_auth(HostType, LServer, LOpts, StateData) ->
IsSSL = mongoose_c2s_socket:is_ssl(mongoose_c2s:get_socket(StateData)),
-spec stream_features_before_auth(mongoose_c2s:data()) -> exml:element().
stream_features_before_auth(StateData = #c2s_data{host_type = HostType,
lserver = LServer,
listener_opts = LOpts,
socket = Socket}) ->
IsSSL = mongoose_c2s_socket:is_ssl(Socket),
Features = determine_features(HostType, LServer, LOpts, IsSSL, StateData),
stream_features(Features).

Expand All @@ -58,10 +60,12 @@ determine_features(_, _, #{tls := #{mode := starttls_required}}, false, _StateDa
[starttls_stanza(required)];
determine_features(HostType, LServer, _, true, StateData) ->
InitialFeatures = maybe_sasl_mechanisms(StateData),
mongoose_hooks:c2s_stream_features(HostType, LServer, InitialFeatures);
StreamFeaturesParams = #{c2s_data => StateData, lserver => LServer},
mongoose_hooks:c2s_stream_features(HostType, StreamFeaturesParams, InitialFeatures);

Check warning on line 64 in src/c2s/mongoose_c2s_stanzas.erl

View check run for this annotation

Codecov / codecov/patch

src/c2s/mongoose_c2s_stanzas.erl#L63-L64

Added lines #L63 - L64 were not covered by tests
determine_features(HostType, LServer, _, _, StateData) ->
InitialFeatures = [starttls_stanza(optional) | maybe_sasl_mechanisms(StateData)],
mongoose_hooks:c2s_stream_features(HostType, LServer, InitialFeatures).
StreamFeaturesParams = #{c2s_data => StateData, lserver => LServer},
mongoose_hooks:c2s_stream_features(HostType, StreamFeaturesParams, InitialFeatures).

-spec maybe_sasl_mechanisms(mongoose_c2s:data()) -> [exml:element()].
maybe_sasl_mechanisms(StateData) ->
Expand All @@ -88,24 +92,24 @@ tls_proceed() ->
#xmlel{name = <<"proceed">>,
attrs = [{<<"xmlns">>, ?NS_TLS}]}.

-spec stream_features_after_auth(mongooseim:host_type(), jid:lserver(), mongoose_listener:options()) ->
exml:element().
stream_features_after_auth(HostType, LServer, #{backwards_compatible_session := false}) ->
-spec stream_features_after_auth(mongoose_c2s:data()) -> exml:element().
stream_features_after_auth(#c2s_data{listener_opts = #{backwards_compatible_session := false}} = StateData) ->
Features = [#xmlel{name = <<"bind">>,
attrs = [{<<"xmlns">>, ?NS_BIND}]}
| hook_enabled_features(HostType, LServer)],
| hook_enabled_features(StateData)],
stream_features(Features);
stream_features_after_auth(HostType, LServer, #{backwards_compatible_session := true}) ->
stream_features_after_auth(#c2s_data{listener_opts = #{backwards_compatible_session := true}} = StateData) ->
Features = [#xmlel{name = <<"session">>,
attrs = [{<<"xmlns">>, ?NS_SESSION}]},
#xmlel{name = <<"bind">>,
attrs = [{<<"xmlns">>, ?NS_BIND}]}
| hook_enabled_features(HostType, LServer)],
| hook_enabled_features(StateData)],
stream_features(Features).

hook_enabled_features(HostType, LServer) ->
hook_enabled_features(StateData = #c2s_data{host_type = HostType, lserver = LServer}) ->
InitialFeatures = mongoose_hooks:roster_get_versioning_feature(HostType),
mongoose_hooks:c2s_stream_features(HostType, LServer, InitialFeatures).
StreamFeaturesParams = #{c2s_data => StateData, lserver => LServer},
mongoose_hooks:c2s_stream_features(HostType, StreamFeaturesParams, InitialFeatures).

-spec sasl_success_stanza(binary()) -> exml:element().
sasl_success_stanza(ServerOut) ->
Expand Down
7 changes: 3 additions & 4 deletions src/hooks/mongoose_hooks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -475,13 +475,12 @@ filter_pep_recipient(C2SData, Feature, To) ->
HostType = mongoose_c2s:get_host_type(C2SData),
run_hook_for_host_type(filter_pep_recipient, HostType, true, Params).

-spec c2s_stream_features(HostType, LServer, InitialFeatures) -> Result when
-spec c2s_stream_features(HostType, Params, InitialFeatures) -> Result when
HostType :: mongooseim:host_type(),
LServer :: jid:lserver(),
Params :: #{c2s_data := mongoose_c2s:data(), lserver := jid:lserver()},
InitialFeatures :: [exml:element()],
Result :: [exml:element()].
c2s_stream_features(HostType, LServer, InitialFeatures) ->
Params = #{lserver => LServer},
c2s_stream_features(HostType, Params, InitialFeatures) ->
run_hook_for_host_type(c2s_stream_features, HostType, InitialFeatures, Params).

-spec check_bl_c2s(IP) -> Result when
Expand Down

0 comments on commit 0c0ab93

Please sign in to comment.