From e6a439473b957cab8c2cb6b95416276a462b690b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Deuc=D0=B5?= <shurd@sasktel.net>
Date: Thu, 29 Dec 2022 20:53:32 -0500
Subject: [PATCH] Use OutbufHighwaterMark as a default, not an override.

The MSS detection will set this to the best value on a per-connection
basis.  This will allow larger packets to be sent from the BBS.
---
 ctrl/sbbs.ini        |  6 ++---
 src/sbbs3/main.cpp   | 52 +++++++++++++++++++++-----------------------
 src/sbbs3/sbbs_ini.c |  8 +------
 src/sbbs3/websrvr.c  | 52 +++++++++++++++++++++-----------------------
 4 files changed, 53 insertions(+), 65 deletions(-)

diff --git a/ctrl/sbbs.ini b/ctrl/sbbs.ini
index 82988c785c..7dcd5c5147 100644
--- a/ctrl/sbbs.ini
+++ b/ctrl/sbbs.ini
@@ -112,10 +112,8 @@ DOSemuConfPath = dosemu.conf
 ; ie: Send output whenever there are at least this many bytes waiting.
 ; This should definately not be higher than the MTU.
 ;
-; On Linux and BSD (Any platform which supports the TCP_MAXSEG socket opt)
-; This can (and probably should) be set to zero to automagically set the
-; highwater mark to the MSS and ensure no single call to send() is larger
-; than this.
+; This is only a default, when possible, the MSS of a socket is detected
+; and used instead.
         OutbufHighwaterMark = 1024
 ; Timout to wait for output buffer to fill (Milliseconds)
 ; If there is ANY data in the output buffer after waiting this long, send
diff --git a/src/sbbs3/main.cpp b/src/sbbs3/main.cpp
index 3c15875326..634244b262 100644
--- a/src/sbbs3/main.cpp
+++ b/src/sbbs3/main.cpp
@@ -2297,43 +2297,41 @@ void output_thread(void* arg)
 	 * Auto-tune the highwater mark to be the negotiated MSS for the
 	 * socket (when possible)
 	 */
-	if(!sbbs->outbuf.highwater_mark) {
-		socklen_t	sl;
-		sl=sizeof(i);
-		if(!getsockopt(sbbs->client_socket, IPPROTO_TCP, TCP_MAXSEG,
+	socklen_t	sl;
+	sl=sizeof(i);
+	if(!getsockopt(sbbs->client_socket, IPPROTO_TCP, TCP_MAXSEG,
 #ifdef _WIN32
-			(char *)
+		(char *)
 #endif
-			&i, &sl)) {
-			/* Check for sanity... */
-			if(i>100) {
+		&i, &sl)) {
+		/* Check for sanity... */
+		if(i>100) {
 #ifdef _WIN32
 #ifdef TCP_TIMESTAMPS
-				DWORD ts;
-				sl = sizeof(ts);
-				if (!getsockopt(sbbs->client_socket, IPPROTO_TCP, TCP_TIMESTAMPS, (char *)&ts, &sl)) {
-					if (ts)
-						i -= 12;
-				}
+			DWORD ts;
+			sl = sizeof(ts);
+			if (!getsockopt(sbbs->client_socket, IPPROTO_TCP, TCP_TIMESTAMPS, (char *)&ts, &sl)) {
+				if (ts)
+					i -= 12;
+			}
 #endif
 #else
 #if (defined(TCP_INFO) && defined(TCPI_OPT_TIMESTAMPS))
-				struct tcp_info tcpi;
+			struct tcp_info tcpi;
 
-				sl = sizeof(tcpi);
-				if (!getsockopt(sbbs->client_socket, IPPROTO_TCP, TCP_INFO,&tcpi, &sl)) {
-					if (tcpi.tcpi_options & TCPI_OPT_TIMESTAMPS)
-						i -= 12;
-				}
+			sl = sizeof(tcpi);
+			if (!getsockopt(sbbs->client_socket, IPPROTO_TCP, TCP_INFO,&tcpi, &sl)) {
+				if (tcpi.tcpi_options & TCPI_OPT_TIMESTAMPS)
+					i -= 12;
+			}
 #endif
 #endif
-				sbbs->outbuf.highwater_mark=i;
-				lprintf(LOG_DEBUG,"Autotuning outbuf highwater mark to %d based on MSS",i);
-				mss=sbbs->outbuf.highwater_mark;
-				if(mss>IO_THREAD_BUF_SIZE) {
-					mss=IO_THREAD_BUF_SIZE;
-					lprintf(LOG_DEBUG,"MSS (%d) is higher than IO_THREAD_BUF_SIZE (%d)",i,IO_THREAD_BUF_SIZE);
-				}
+			sbbs->outbuf.highwater_mark=i;
+			lprintf(LOG_DEBUG,"Autotuning outbuf highwater mark to %d based on MSS",i);
+			mss=sbbs->outbuf.highwater_mark;
+			if(mss>IO_THREAD_BUF_SIZE) {
+				mss=IO_THREAD_BUF_SIZE;
+				lprintf(LOG_DEBUG,"MSS (%d) is higher than IO_THREAD_BUF_SIZE (%d)",i,IO_THREAD_BUF_SIZE);
 			}
 		}
 	}
diff --git a/src/sbbs3/sbbs_ini.c b/src/sbbs3/sbbs_ini.c
index 3495e03106..31ac1f5c8e 100644
--- a/src/sbbs3/sbbs_ini.c
+++ b/src/sbbs3/sbbs_ini.c
@@ -398,13 +398,7 @@ void sbbs_read_ini(
 			=iniGetShortInt(list,section,"LastNode",4);
 
 		bbs->outbuf_highwater_mark
-			=iniGetShortInt(list,section,"OutbufHighwaterMark"
-#ifdef TCP_MAXSEG	/* Auto-tune if possible.  Would this be defined here? */
-			,0
-#else
-			,1024
-#endif
-			);
+			=iniGetShortInt(list,section,"OutbufHighwaterMark" ,1024);
 		bbs->outbuf_drain_timeout
 			=iniGetShortInt(list,section,"OutbufDrainTimeout",10);
 
diff --git a/src/sbbs3/websrvr.c b/src/sbbs3/websrvr.c
index 0cb535f1de..c2e066e713 100644
--- a/src/sbbs3/websrvr.c
+++ b/src/sbbs3/websrvr.c
@@ -6359,41 +6359,39 @@ void http_output_thread(void *arg)
 	 * Auto-tune the highwater mark to be the negotiated MSS for the
 	 * socket (when possible)
 	 */
-	if(!obuf->highwater_mark) {
-		socklen_t   sl;
-		sl=sizeof(i);
-		if(!getsockopt(session->socket, IPPROTO_TCP, TCP_MAXSEG, (char*)&i, &sl)) {
-			/* Check for sanity... */
-			if(i>100) {
+	socklen_t   sl;
+	sl=sizeof(i);
+	if(!getsockopt(session->socket, IPPROTO_TCP, TCP_MAXSEG, (char*)&i, &sl)) {
+		/* Check for sanity... */
+		if(i>100) {
 #ifdef _WIN32
 #ifdef TCP_TIMESTAMPS
-				DWORD ts;
-				sl = sizeof(ts);
-				if (!getsockopt(session->socket, IPPROTO_TCP, TCP_TIMESTAMPS, (char *)&ts, &sl)) {
-					if (ts)
-						i -= 12;
-				}
+			DWORD ts;
+			sl = sizeof(ts);
+			if (!getsockopt(session->socket, IPPROTO_TCP, TCP_TIMESTAMPS, (char *)&ts, &sl)) {
+				if (ts)
+					i -= 12;
+			}
 #endif
 #else
 #if (defined(TCP_INFO) && defined(TCPI_OPT_TIMESTAMPS))
-				struct tcp_info tcpi;
+			struct tcp_info tcpi;
 
-				sl = sizeof(tcpi);
-				if (!getsockopt(session->socket, IPPROTO_TCP, TCP_INFO,&tcpi, &sl)) {
-					if (tcpi.tcpi_options & TCPI_OPT_TIMESTAMPS)
-						i -= 12;
-				}
+			sl = sizeof(tcpi);
+			if (!getsockopt(session->socket, IPPROTO_TCP, TCP_INFO,&tcpi, &sl)) {
+				if (tcpi.tcpi_options & TCPI_OPT_TIMESTAMPS)
+					i -= 12;
+			}
 #endif
 #endif
-				obuf->highwater_mark=i;
-				lprintf(LOG_DEBUG,"%04d Autotuning outbuf highwater mark to %d based on MSS"
-					,session->socket,i);
-				mss=obuf->highwater_mark;
-				if(mss>OUTBUF_LEN) {
-					mss=OUTBUF_LEN;
-					lprintf(LOG_DEBUG,"%04d MSS (%d) is higher than OUTBUF_LEN (%d)"
-						,session->socket,i,OUTBUF_LEN);
-				}
+			obuf->highwater_mark=i;
+			lprintf(LOG_DEBUG,"%04d Autotuning outbuf highwater mark to %d based on MSS"
+				,session->socket,i);
+			mss=obuf->highwater_mark;
+			if(mss>OUTBUF_LEN) {
+				mss=OUTBUF_LEN;
+				lprintf(LOG_DEBUG,"%04d MSS (%d) is higher than OUTBUF_LEN (%d)"
+					,session->socket,i,OUTBUF_LEN);
 			}
 		}
 	}
-- 
GitLab