From 4548b3c225000864306e85702e272400595af01d Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Fri, 20 Sep 2024 01:28:54 +0200 Subject: [PATCH] aqhome: discard data on bad messages, make socket non-blocking. still doesn't seem to recover from bad messages. --- aqhome/msg/endpoint_tty.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/aqhome/msg/endpoint_tty.c b/aqhome/msg/endpoint_tty.c index 7cbd813..1dcbd2a 100644 --- a/aqhome/msg/endpoint_tty.c +++ b/aqhome/msg/endpoint_tty.c @@ -149,8 +149,16 @@ int AQH_TtyEndpoint_Connect(GWEN_MSG_ENDPOINT *ep) } else { GWEN_SOCKET *sk; + int rv; sk=GWEN_Socket_fromFile(fd); + rv=GWEN_Socket_SetBlocking(sk, 0); + if (rv<0) { + DBG_INFO(AQH_LOGDOMAIN, "Error setting socket nonblocking: %d", rv); + GWEN_Socket_free(sk); + return GWEN_ERROR_IO; + } + GWEN_MsgEndpoint_SetSocket(ep, sk); GWEN_MsgEndpoint_SetState(ep, GWEN_MSG_ENDPOINT_STATE_CONNECTED); GWEN_MsgEndpoint_DiscardInput(ep); @@ -301,8 +309,9 @@ int _getBytesNeededForMessage(GWEN_UNUSED GWEN_MSG_ENDPOINT *ep, GWEN_MSG *msg) ptr=GWEN_Msg_GetConstBuffer(msg); msgSize=ptr[AQH_MSG_OFFS_ALL_PAYLOAD_LEN]+AQH_MSG_OFFS_ALL_PAYLOAD_BEGIN+1; if (msgSize>GWEN_Msg_GetMaxSize(msg)) { - DBG_INFO(AQH_LOGDOMAIN, "Message too long for msg (%d > %d)", msgSize, GWEN_Msg_GetMaxSize(msg)); - return GWEN_ERROR_GENERIC; + DBG_ERROR(AQH_LOGDOMAIN, "Message too long for msg (%d > %d)", msgSize, GWEN_Msg_GetMaxSize(msg)); + GWEN_MsgEndpoint_DiscardInput(ep); + return GWEN_ERROR_BAD_DATA; } bytesNeeded=(int) (msgSize-bytesInMsg); @@ -312,8 +321,7 @@ int _getBytesNeededForMessage(GWEN_UNUSED GWEN_MSG_ENDPOINT *ep, GWEN_MSG *msg) return GWEN_ERROR_BAD_DATA; } - - return (int) (msgSize-bytesInMsg); + return bytesNeeded; } }