From 2bae9a96e637c06d8f48c8c6a3e74835db430b4f Mon Sep 17 00:00:00 2001
From: "Rob Swindell (on Windows 11)" <rob@synchro.net>
Date: Tue, 23 Apr 2024 00:15:57 -0700
Subject: [PATCH] Track Telnet Binary TX mode set by server via Telnet (IAC)
 commands

As reported by Chris/akacastor (21:1/162), SyncTERM would always assume the
Telnet session was in ASCII mode until/unless conn_binary_mode_on() was called
which is only called at the beginning of a file transfer, which sets the
conn_api.binary_mode accordingly. Unfortunately, if the Telnet server
requested binary mode itself (e.g. during initial connection), SyncTERM would
(try to) set the connection back to ASCII mode after any file transfer. :-(

With this change, the conn_api.binary_mode will track the actual binary TX
mode whether initiated locally or remotely (by the server), but it'll only be
set to true when *both* directions are succesfully set to binary mode. And
this way, if the connection was already in binary mode before any file
transfer, it'll remain in binary mode as was apparently desired by the server.

Also with this change, we could probably remove the setting of
conn_api.binary_mode to true/false in conn_binary_mode_on()/off() functions,
but it shouldn't hurt anything if it's left there I suppose (in case some
*other* terminal transport protocols have the concept of binary mode?).
---
 src/syncterm/telnet_io.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/src/syncterm/telnet_io.c b/src/syncterm/telnet_io.c
index 2ba2358dc4..9117c413ee 100644
--- a/src/syncterm/telnet_io.c
+++ b/src/syncterm/telnet_io.c
@@ -92,6 +92,14 @@ request_telnet_opt(uchar cmd, uchar opt)
 	send_telnet_cmd(cmd, opt);
 }
 
+// Have conn_api.binary_mode auto-track (to true) when both sides are in binary TX mode
+static inline
+update_binary_mode()
+{
+	conn_api.binary_mode = (telnet_remote_option[TELNET_BINARY_TX] == TELNET_WILL)
+		&& (telnet_local_option[TELNET_BINARY_TX] == TELNET_DO);
+}
+
 BYTE *
 telnet_interpret(BYTE *inbuf, size_t inlen, BYTE *outbuf, size_t *outlen)
 {
@@ -193,6 +201,8 @@ telnet_interpret(BYTE *inbuf, size_t inlen, BYTE *outbuf, size_t *outlen)
 							case TELNET_NEGOTIATE_WINDOW_SIZE:
 								telnet_local_option[option] = command;
 								send_telnet_cmd(telnet_opt_ack(command), option);
+								if(option == TELNET_BINARY_TX)
+									update_binary_mode();
 								break;
 							default:                          /* unsupported local options
                                                                                            */
@@ -232,6 +242,8 @@ telnet_interpret(BYTE *inbuf, size_t inlen, BYTE *outbuf, size_t *outlen)
 							case TELNET_NEGOTIATE_WINDOW_SIZE:
 								telnet_remote_option[option] = command;
 								send_telnet_cmd(telnet_opt_ack(command), option);
+								if(option == TELNET_BINARY_TX)
+									update_binary_mode();
 								break;
 							default:                            /* unsupported remote
                                                                                              * options */
-- 
GitLab