diff --git a/src/amqp_main_reader.erl b/src/amqp_main_reader.erl index 7953e4d2..b1de33c6 100644 --- a/src/amqp_main_reader.erl +++ b/src/amqp_main_reader.erl @@ -62,9 +62,17 @@ handle_call(Call, From, State) -> handle_cast(Cast, State) -> {stop, {unexpected_cast, Cast}, State}. -handle_info({inet_async, Sock, _, {ok, <>}}, +handle_info({inet_async, Sock, _, {ok, <<"AMQP", A, B, C>>}}, State = #state{sock = Sock, message = none}) -> + {ok, <>} = rabbit_net:sync_recv(Sock, 1), + handle_error({refused, {A, B, C, D}}, State); +handle_info({inet_async, Sock, _, {ok, <>}}, + State = #state{sock = Sock, message = none}) when + Type =:= 1; Type =:= 2; Type =:= 3; Type =:= 4 -> next(Length + 1, State#state{message = {Type, Channel, Length}}); +handle_info({inet_async, Sock, _, {ok, <> = All}}, + State = #state{sock = Sock, message = none}) -> + handle_error({malformed_header, All}, State); handle_info({inet_async, Sock, _, {ok, Data}}, State = #state{sock = Sock, message = {Type, Channel, L}}) -> <> = Data, @@ -108,6 +116,12 @@ next(Length, State = #state{sock = Sock}) -> handle_error(closed, State = #state{connection = Conn}) -> Conn ! socket_closed, {noreply, State}; +handle_error({refused, Version}, State = #state{connection = Conn}) -> + Conn ! {refused, Version}, + {noreply, State}; +handle_error({malformed_header, Version}, State = #state{connection = Conn}) -> + Conn ! {malformed_header, Version}, + {noreply, State}; handle_error(Reason, State = #state{connection = Conn}) -> Conn ! {socket_error, Reason}, {stop, {socket_error, Reason}, State}. diff --git a/src/amqp_network_connection.erl b/src/amqp_network_connection.erl index 69fd71c8..2ab561e1 100644 --- a/src/amqp_network_connection.erl +++ b/src/amqp_network_connection.erl @@ -309,6 +309,10 @@ handshake_recv(Expecting) -> end; {socket_error, _} = SocketError -> exit({SocketError, {expecting, Expecting}}); + {refused, Version} -> + exit({server_refused_connection, Version}); + {malformed_header, All} -> + exit({server_sent_malformed_header, All}); heartbeat_timeout -> exit(heartbeat_timeout); Other ->