Skip to content

Commit

Permalink
Merge pull request #4095 from esl/c2s/fixes
Browse files Browse the repository at this point in the history
C2s/fixes
Reduce number of args in. send_header function to 1
  • Loading branch information
arcusfelis authored Aug 12, 2023
2 parents 23aa906 + 8143798 commit b3b5460
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
5 changes: 3 additions & 2 deletions big_tests/tests/auth_methods_for_c2s_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ cannot_login_with_not_allowed_method(Config) ->

can_login_to_another_listener(Config) ->
Spec = proplists:get_value(spec, Config),
Spec2 = [{port, ct:get_config({hosts, mim, c2s_tls_port})},
{password, <<"wrong">>}|Spec],
TlsPort = ct:get_config({hosts, mim, c2s_tls_port}),
Spec2 = [{port, TlsPort}, {ssl, true}, {ssl_opts, [{verify, verify_none}]},
{password, <<"wrong">>} | Spec],
{ok, _, _} = escalus_connection:start(Spec2).

metrics_incremented_on_user_connect(ConfigIn) ->
Expand Down
4 changes: 3 additions & 1 deletion rel/mim1.vars-toml.config
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
port = {{ c2s_tls_port }}
access = \"c2s\"
shaper = \"c2s_shaper\"
max_stanza_size = 65536"}.
max_stanza_size = 65536
tls.certfile = \"priv/ssl/fake_server.pem\"
tls.mode = \"tls\""}.
{listen_service,
"[[listen.service]]
port = {{ service_port }}
Expand Down
32 changes: 15 additions & 17 deletions src/c2s/mongoose_c2s.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
-include("mongoose.hrl").
-include("jlib.hrl").
-include_lib("exml/include/exml_stream.hrl").
-define(XMPP_VERSION, <<"1.0">>).
-define(AUTH_RETRIES, 3).
-define(BIND_RETRIES, 5).

Expand Down Expand Up @@ -95,12 +96,12 @@ handle_event(internal, {connect, {SocketModule, SocketOpts}}, connect,
handle_event(internal, #xmlstreamstart{name = Name, attrs = Attrs}, {wait_for_stream, StreamState}, StateData) ->
StreamStart = #xmlel{name = Name, attrs = Attrs},
handle_stream_start(StateData, StreamStart, StreamState);
handle_event(internal, _Unexpected, {wait_for_stream, _}, StateData = #c2s_data{lserver = LServer}) ->
handle_event(internal, _Unexpected, {wait_for_stream, _}, StateData) ->
case mongoose_config:get_opt(hide_service_name, false) of
true ->
{stop, {shutdown, stream_error}};
false ->
send_header(StateData, LServer, <<"1.0">>, <<>>),
send_header(StateData),
c2s_stream_error(StateData, mongoose_xmpp_errors:xml_not_well_formed())
end;
handle_event(internal, #xmlstreamstart{}, _, StateData) ->
Expand Down Expand Up @@ -324,10 +325,10 @@ handle_stream_start(S0, StreamStart, StreamState) ->
exml_query:attr(StreamStart, <<"xmlns:stream">>, <<>>),
exml_query:attr(StreamStart, <<"version">>, <<>>),
mongoose_domain_api:get_domain_host_type(LServer)} of
{stream_start, ?NS_STREAM, <<"1.0">>, {ok, HostType}} ->
{stream_start, ?NS_STREAM, ?XMPP_VERSION, {ok, HostType}} ->
S = S0#c2s_data{host_type = HostType, lserver = LServer, lang = Lang},
stream_start_features_before_auth(S);
{authenticated, ?NS_STREAM, <<"1.0">>, {ok, HostType}} ->
{authenticated, ?NS_STREAM, ?XMPP_VERSION, {ok, HostType}} ->
S = S0#c2s_data{host_type = HostType, lserver = LServer, lang = Lang},
stream_start_features_after_auth(S);
{_, ?NS_STREAM, _Pre1_0, {ok, HostType}} ->
Expand Down Expand Up @@ -442,8 +443,8 @@ 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,
lang = Lang, listener_opts = LOpts} = S) ->
send_header(S, LServer, <<"1.0">>, Lang),
listener_opts = LOpts} = S) ->
send_header(S),
CredOpts = mongoose_credentials:make_opts(LOpts),
Creds = mongoose_credentials:new(LServer, HostType, CredOpts),
SASLState = cyrsasl:server_new(<<"jabber">>, LServer, HostType, <<>>, [], Creds),
Expand All @@ -453,11 +454,11 @@ stream_start_features_before_auth(#c2s_data{host_type = HostType, lserver = LSer

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

-spec handle_bind_resource(data(), state(), exml:element(), jlib:iq()) -> fsm_res().
handle_bind_resource(StateData, C2SState, El, #iq{sub_el = SubEl} = IQ) ->
Expand Down Expand Up @@ -831,15 +832,12 @@ element_to_origin_accum(StateData = #c2s_data{sid = SID, jid = Jid}, El) ->

-spec stream_start_error(data(), exml:element()) -> fsm_res().
stream_start_error(StateData, Error) ->
send_header(StateData, ?MYNAME, <<>>, StateData#c2s_data.lang),
send_header(StateData),
c2s_stream_error(StateData, Error).

-spec send_header(StateData :: data(),
Server :: jid:lserver(),
Version :: binary(),
Lang :: ejabberd:lang()) -> any().
send_header(StateData, Server, Version, Lang) ->
Header = mongoose_c2s_stanzas:stream_header(Server, Version, Lang, StateData#c2s_data.streamid),
-spec send_header(StateData :: data()) -> any().
send_header(StateData = #c2s_data{lserver = LServer, lang = Lang, streamid = StreamId}) ->
Header = mongoose_c2s_stanzas:stream_header(LServer, ?XMPP_VERSION, Lang, StreamId),
send_xml(StateData, Header).

send_trailer(StateData) ->
Expand Down

0 comments on commit b3b5460

Please sign in to comment.