aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-10-08 21:03:35 -0500
committerJP Dillingham <jp@dillingham.me>2026-10-08 21:03:35 -0500
commit6490ca194b7ca5d9b110d82ebbf5fe86e1af015d (patch)
treea3e4f612505b1216eb18dca11c94a46f8eef06b1 /src
parent65e30b75178524730636a1b00621e05c08913d63 (diff)
downloadslskd-6490ca194b7ca5d9b110d82ebbf5fe86e1af015d.tar.gz
add ConfigureSocketMtu
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Application.cs29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs
index 506b1980..9d172a76 100644
--- a/src/slskd/Application.cs
+++ b/src/slskd/Application.cs
@@ -568,6 +568,35 @@ namespace slskd
}
}
+ private void ConfigureSocketMtu(Socket socket, int mtu)
+ {
+ /*
+ limits the size of the packets sent and received on this connection to the given MTU. there's no per-socket MTU setting,
+ so this is done by setting TCP_MAXSEG to the MTU minus the IPv4 and TCP headers (20 bytes each)
+
+ Windows is skipped because it only supports reading TCP_MAXSEG, not setting it
+ see: https://learn.microsoft.com/en-us/windows/win32/winsock/ipproto-tcp-socket-options
+ and: https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getsockopt
+ */
+ if (!(System.OperatingSystem.IsLinux() || System.OperatingSystem.IsMacOS() || System.OperatingSystem.IsFreeBSD()))
+ {
+ return;
+ }
+
+ try
+ {
+ const int IPPROTO_TCP = 6;
+ const int TCP_MAXSEG = 2;
+ const int IPV4_AND_TCP_HEADER_SIZE = 40;
+
+ socket.SetRawSocketOption(IPPROTO_TCP, TCP_MAXSEG, BitConverter.GetBytes(mtu - IPV4_AND_TCP_HEADER_SIZE));
+ }
+ catch (SocketException ex)
+ {
+ Log.Warning("Failed to set the MTU of the server connection to {MTU}: {Message}", mtu, ex.Message);
+ }
+ }
+
private async Task EnqueueDownload(string username, IPEndPoint endpoint, string filename)
{
Metrics.Enqueue.RequestsReceived.Inc(1);