Fix unexpected packet before connected

This commit is contained in:
zhouzb 2020-05-21 16:37:46 +08:00 committed by tigercl
parent 361730558f
commit 83b1f3e6c8
2 changed files with 13 additions and 2 deletions

View File

@ -250,8 +250,10 @@ handle_in(Packet = ?AUTH_PACKET(?RC_CONTINUE_AUTHENTICATION, _Properties), Chann
case ConnState of case ConnState of
connecting -> connecting ->
process_connect(NProperties, ensure_connected(NChannel)); process_connect(NProperties, ensure_connected(NChannel));
connected ->
handle_out(auth, {?RC_SUCCESS, NProperties}, NChannel);
_ -> _ ->
handle_out(auth, {?RC_SUCCESS, NProperties}, NChannel) handle_out(disconnect, ?RC_PROTOCOL_ERROR, Channel)
end; end;
{continue, NProperties, NChannel} -> {continue, NProperties, NChannel} ->
handle_out(auth, {?RC_CONTINUE_AUTHENTICATION, NProperties}, NChannel); handle_out(auth, {?RC_CONTINUE_AUTHENTICATION, NProperties}, NChannel);
@ -259,7 +261,7 @@ handle_in(Packet = ?AUTH_PACKET(?RC_CONTINUE_AUTHENTICATION, _Properties), Chann
handle_out(connack, NReasonCode, NChannel) handle_out(connack, NReasonCode, NChannel)
end; end;
handle_in(Packet = ?AUTH_PACKET(?RC_RE_AUTHENTICATE, _Properties), Channel) -> handle_in(Packet = ?AUTH_PACKET(?RC_RE_AUTHENTICATE, _Properties), Channel = #channel{conn_state = connected}) ->
case enhanced_auth(Packet, Channel) of case enhanced_auth(Packet, Channel) of
{ok, NProperties, NChannel} -> {ok, NProperties, NChannel} ->
handle_out(auth, {?RC_SUCCESS, NProperties}, NChannel); handle_out(auth, {?RC_SUCCESS, NProperties}, NChannel);
@ -269,6 +271,9 @@ handle_in(Packet = ?AUTH_PACKET(?RC_RE_AUTHENTICATE, _Properties), Channel) ->
handle_out(disconnect, NReasonCode, NChannel) handle_out(disconnect, NReasonCode, NChannel)
end; end;
handle_in(?PACKET(_), Channel = #channel{conn_state = ConnState}) when ConnState =/= connected ->
handle_out(disconnect, ?RC_PROTOCOL_ERROR, Channel);
handle_in(Packet = ?PUBLISH_PACKET(_QoS), Channel) -> handle_in(Packet = ?PUBLISH_PACKET(_QoS), Channel) ->
case emqx_packet:check(Packet) of case emqx_packet:check(Packet) of
ok -> process_publish(Packet, Channel); ok -> process_publish(Packet, Channel);

View File

@ -112,6 +112,12 @@ t_handle_in_unexpected_connect_packet(_) ->
{ok, [{outgoing, Packet}, {close, protocol_error}], Channel} = {ok, [{outgoing, Packet}, {close, protocol_error}], Channel} =
emqx_channel:handle_in(?CONNECT_PACKET(connpkt()), Channel). emqx_channel:handle_in(?CONNECT_PACKET(connpkt()), Channel).
t_handle_in_unexpected_packet(_) ->
Channel = emqx_channel:set_field(conn_state, idle, channel()),
Packet = ?DISCONNECT_PACKET(?RC_PROTOCOL_ERROR),
{ok, [{outgoing, Packet}, {close, protocol_error}], Channel} =
emqx_channel:handle_in(?PUBLISH_PACKET(?QOS_0), Channel).
t_handle_in_connect_auth_failed(_) -> t_handle_in_connect_auth_failed(_) ->
ConnPkt = #mqtt_packet_connect{ ConnPkt = #mqtt_packet_connect{
proto_name = <<"MQTT">>, proto_name = <<"MQTT">>,