Skip to content

Commit

Permalink
feat: Added TTL and additional bind safety
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoTenPvP committed Jan 13, 2020
1 parent a91a071 commit 5d1cad8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions Ruffles/Configuration/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
26 changes: 21 additions & 5 deletions Ruffles/Core/RuffleSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down

0 comments on commit 5d1cad8

Please sign in to comment.