From e2a62732f504cca5db8fb8d6d38ca64dfd4b92ac Mon Sep 17 00:00:00 2001 From: Dave Hibberd Date: Thu, 28 Nov 2024 22:33:53 +0000 Subject: New upstream version 6.0.24.51 --- AGWAPI.c | 125 +++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 70 insertions(+), 55 deletions(-) (limited to 'AGWAPI.c') diff --git a/AGWAPI.c b/AGWAPI.c index 0f2d86c..80c1ce4 100644 --- a/AGWAPI.c +++ b/AGWAPI.c @@ -1017,6 +1017,7 @@ int AGWDataSocket_Read(struct AGWSocketConnectionInfo * sockptr, SOCKET sock) { int i; int DataLength; + struct AGWHeader * AGW = &sockptr->AGWRXHeader; ioctlsocket(sock,FIONREAD,&DataLength); @@ -1028,84 +1029,98 @@ int AGWDataSocket_Read(struct AGWSocketConnectionInfo * sockptr, SOCKET sock) return 0; } - - if (sockptr->GotHeader) + if (DataLength < 36) // A header { - // Received a header, without sufficient data bytes - - if (DataLength < sockptr->MsgDataLength) - { - // Fiddle - seem to be problems somtimes with un-Neagled hosts - - Sleep(500); + // If we don't get a header within a few ms assume a rogue connection and close it + + int n = 50; + while (n--) + { + Sleep(10); ioctlsocket(sock,FIONREAD,&DataLength); + + if (DataLength >= 36) + break; } - - if (DataLength >= sockptr->MsgDataLength) - { - // Read Data and Process Command - sockptr->MsgData = malloc(sockptr->MsgDataLength); - - i = recv(sock, sockptr->MsgData, sockptr->MsgDataLength, 0); - - ProcessAGWCommand (sockptr); - free(sockptr->MsgData); - - sockptr->GotHeader = FALSE; + if (n < 1) + { + Debugprintf("Corrupt AGW Packet Received"); + AGWDataSocket_Disconnect(sockptr); + return 0; } + } - // Not Enough Data - wait + // Have a header - } - else // Not got header + i=recv(sock,(char *)&sockptr->AGWRXHeader, 36, 0); + + if (i == SOCKET_ERROR) { - if (DataLength > 35)// A header - { - struct AGWHeader * AGW = &sockptr->AGWRXHeader; + i=WSAGetLastError(); + AGWDataSocket_Disconnect(sockptr); + } + + sockptr->MsgDataLength = sockptr->AGWRXHeader.DataLength; - i=recv(sock,(char *)&sockptr->AGWRXHeader, 36, 0); - - if (i == SOCKET_ERROR) - { - i=WSAGetLastError(); + // Validate packet to protect against accidental (or malicious!) connects from a non-agw application - AGWDataSocket_Disconnect(sockptr); - } + if (AGW->Port > 64 || AGW->filler2 != 0 || AGW->filler3 != 0 || AGW->DataLength > 400) + { + Debugprintf("Corrupt AGW Packet Received"); + AGWDataSocket_Disconnect(sockptr); + return 0; + } + + if (sockptr->MsgDataLength == 0) + ProcessAGWCommand (sockptr); + else + sockptr->GotHeader = TRUE; // Wait for data + ioctlsocket(sock,FIONREAD,&DataLength); // See if more data - sockptr->MsgDataLength = sockptr->AGWRXHeader.DataLength; - - // Validate packet to protect against accidental (or malicious!) connects from a non-agw application + if (sockptr->GotHeader) + { + // Received a header, without sufficient data bytes + + if (DataLength < sockptr->MsgDataLength) + { + // Fiddle - seem to be problems somtimes with un-Neagled hosts so wait a few ms + // if we don't get a full packet assume a rogue connection and close it + int n = 50; - if (AGW->Port > 64 || AGW->filler2 != 0 || AGW->filler3 != 0 || AGW->DataLength > 400) + while (n--) { - Debugprintf("Corrupt AGW Packet Received"); - AGWDataSocket_Disconnect(sockptr); - return 0; + Sleep(10); + ioctlsocket(sock,FIONREAD,&DataLength); + + if (DataLength >= sockptr->MsgDataLength) + break; } - if (sockptr->MsgDataLength > 500) - OutputDebugString("Corrupt AGW message"); - - - if (sockptr->MsgDataLength == 0) - { - ProcessAGWCommand (sockptr); - } - else + if (n < 1) { - sockptr->GotHeader = TRUE; // Wait for data + Debugprintf("Corrupt AGW Packet Received"); + AGWDataSocket_Disconnect(sockptr); + return 0; } - - } + } - // not got 36 bytes + if (DataLength >= sockptr->MsgDataLength) + { + // Read Data and Process Command + sockptr->MsgData = malloc(sockptr->MsgDataLength); + + i = recv(sock, sockptr->MsgData, sockptr->MsgDataLength, 0); + + ProcessAGWCommand (sockptr); + free(sockptr->MsgData); + sockptr->GotHeader = FALSE; + } } - return 0; } -- cgit v1.2.3