From 04a1309c184073437d7ba1d724e9d4b255037be7 Mon Sep 17 00:00:00 2001
From: deuce <>
Date: Thu, 15 Mar 2018 22:21:09 +0000
Subject: [PATCH] Fix handling of timeouts vs. errors... they're not the same,
 so don't treat them that way.  Also, ensure there's a timeout on the second
 byte of the packet header since we toss out the first byte if the second byte
 times out (they should pretty much always be in the same packet, but why take
 the risk?)

---
 exec/load/binkp.js | 46 +++++++++++++++++++++++++++++++++++++---------
 1 file changed, 37 insertions(+), 9 deletions(-)

diff --git a/exec/load/binkp.js b/exec/load/binkp.js
index 5ac26b1e1a..06a2757ee8 100644
--- a/exec/load/binkp.js
+++ b/exec/load/binkp.js
@@ -591,7 +591,7 @@ BinkP.prototype.accept = function(sock, auth_cb)
 		log(LOG_DEBUG, "Initializing crypt keys.");
 		this.out_keys = [0, 0, 0];
 		this.in_keys = [0, 0, 0];
-    	this.crypt.init_keys(this.in_keys, pwd);
+		this.crypt.init_keys(this.in_keys, pwd);
 		this.crypt.init_keys(this.out_keys,  "-");
 		for (i=0; i<pwd.length; i++)
 			this.crypt.update_keys(this.out_keys, pwd[i]);
@@ -947,20 +947,40 @@ BinkP.prototype.recvFrame = function(timeout)
 
 	if (this.partialFrame === undefined) {
 		ret = new this.Frame();
-		buf = this.sock.recv(1, timeout);
-		if (buf === null || buf.length !== 1) {
-			log(LOG_INFO, "Error in recv() timeout or disconnect");
+		i = this.sock.recv(1, timeout);
+		if (i === null) {
+			log(LOG_INFO, "Error in recv() of first byte of packet header");
 			this.sock.close();
 			this.sock = undefined;
 			return undefined;
 		}
-		buf += this.sock.recv(1, timeout);
-		if (buf.length !== 2) {
-			log(LOG_ERROR, "Remote disconnected or timed out before sending second byte of packet header!");
+		if (i.length != 1) {
+			if (timeout) {
+				log(LOG_WARNING, "Timed out receiving first byte of packet header!");
+				this.sock.close();
+				this.sock = undefined;
+				return undefined;
+			}
+			return null;
+		}
+		buf = i;
+		i = this.sock.recv(1, this.timeout);
+		if (i === null) {
+			log(LOG_INFO, "Error in recv() of second byte of packet header");
 			this.sock.close();
 			this.sock = undefined;
 			return undefined;
 		}
+		if (i.length != 1) {
+			if (timeout) {
+				log(LOG_WARNING, "Timed out receiving second byte of packet header!");
+				this.sock.close();
+				this.sock = undefined;
+				return undefined;
+			}
+			return null;
+		}
+		buf += i;
 		buf = this.recv_buf(buf);
 		ret.length = (ascii(buf[0]) << 8) | ascii(buf[1]);
 		ret.is_cmd = (ret.length & 0x8000) ? true : false;
@@ -971,12 +991,20 @@ BinkP.prototype.recvFrame = function(timeout)
 		ret = this.partialFrame;
 
 	i = this.recv_buf(this.sock.recv(ret.length - ret.data.length));
-	if (i == null || i.length == 0) {
-		log(LOG_ERROR, "Remote disconnected or timed out while receiving packet data!");
+	if (i == null) {
+		log(LOG_INFO, "Error in recv() of packet data");
 		this.sock.close();
 		this.sock = undefined;
 		return undefined;
 	}
+	if (i.length == 0) {
+		if (timeout) {
+			log(LOG_ERROR, "Timed out receiving packet data!");
+			this.sock.close();
+			this.sock = undefined;
+			return undefined;
+		}
+	}
 	ret.data += i;
 
 	if (ret.data.length < ret.length)
-- 
GitLab