From 5d1cad8815dd9207028947bb5ee7e0440362e9e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albin=20Cor=C3=A9n?= Date: Mon, 13 Jan 2020 19:30:53 +0100 Subject: [PATCH] feat: Added TTL and additional bind safety --- Ruffles/Configuration/Constants.cs | 1 + Ruffles/Core/RuffleSocket.cs | 26 +++++++++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Ruffles/Configuration/Constants.cs b/Ruffles/Configuration/Constants.cs index 6d078eb..04d6bca 100644 --- a/Ruffles/Configuration/Constants.cs +++ b/Ruffles/Configuration/Constants.cs @@ -6,6 +6,7 @@ internal static class Constants internal static readonly byte[] RUFFLES_PROTOCOL_IDENTIFICATION = new byte[32] { 00, 00, 00, 00, 00, 00, 00, 82, 117, 102, 102, 108, 101, 115, 32, 71, 114, 101, 101, 116, 115, 32, 89, 111, 117, 00, 00, 00, 00, 00, 00, 00 }; internal static readonly int RECEIVE_SOCKET_BUFFER_SIZE = 1024 * 1024; internal static readonly int SEND_SOCKET_BUFFER_SIZE = 1024 * 1024; + internal static readonly int SOCKET_PACKET_TTL = 64; internal static readonly int MAX_CHANNELS = byte.MaxValue; internal static readonly int MAX_FRAGMENTS = 32768; } diff --git a/Ruffles/Core/RuffleSocket.cs b/Ruffles/Core/RuffleSocket.cs index 1a404d5..ad0a782 100644 --- a/Ruffles/Core/RuffleSocket.cs +++ b/Ruffles/Core/RuffleSocket.cs @@ -337,9 +337,16 @@ private bool SetupAndBind(Socket socket, IPEndPoint endpoint) } } - // Set the .NET buffer sizes. Defaults to 1 megabyte each - socket.ReceiveBufferSize = Constants.RECEIVE_SOCKET_BUFFER_SIZE; - socket.SendBufferSize = Constants.SEND_SOCKET_BUFFER_SIZE; + try + { + // Set the .NET buffer sizes. Defaults to 1 megabyte each + socket.ReceiveBufferSize = Constants.RECEIVE_SOCKET_BUFFER_SIZE; + socket.SendBufferSize = Constants.SEND_SOCKET_BUFFER_SIZE; + } + catch + { + if (Logging.CurrentLogLevel <= LogLevel.Debug) Logging.LogInfo("Failed to set socket buffer size"); + } try { @@ -350,14 +357,23 @@ private bool SetupAndBind(Socket socket, IPEndPoint endpoint) unchecked { socket.IOControl((int)SIO_UDP_CONNRESET, new byte[] { 0 }, null); - } - + } } catch { + if (Logging.CurrentLogLevel <= LogLevel.Debug) Logging.LogInfo("Failed to set SIO_UDP_CONNRESET"); // Ignore error when SIO_UDP_CONNRESET is not supported } + try + { + socket.Ttl = (short)Constants.SOCKET_PACKET_TTL; + } + catch + { + if (Logging.CurrentLogLevel <= LogLevel.Debug) Logging.LogInfo("Failed to set TTL"); + } + try { // Bind the socket to the OS