> [!IMPORTANT] > **Breaking Changes** > - DecodePacket and EncodePacket delegates replaced with IClientEncryption interface > - NetState.Connection (Socket) replaced with internal RingSocket management > - NetState.RecvPipe and NetState.SendPipe removed (buffers managed internally) ## Summary Upgrades the networking stack from PollGroup-based I/O to io_uring, significantly improving I/O performance on Linux. This also adds native client encryption support for encrypted UO clients. ## Major Changes io_uring Networking Architecture - Replaced PollGroup with IORingGroup for async socket I/O operations - Removed Pipe.cs (mirrored ring buffer) and TcpServer.cs in favor of RingSocketManager - Added NetState.Network.cs - centralized network infrastructure handling accept, recv, send, and disconnect completions - Added SocketHelper.cs - platform-specific socket utilities for raw socket handle operations (getpeername, getsockname) - Buffer management now handled by RingSocketManager with configurable slab allocation ### Client Encryption Support - Added full encryption stack in Network/Encryption/: - EncryptionConfig.cs - configurable encryption modes (None, Unencrypted, Encrypted, Both) - EncryptionManager.cs - encryption detection and initialization for login/game packets - LoginEncryption.cs - handles login packet encryption with version-derived keys - GameEncryption.cs - handles game server encryption using Twofish - TwofishEngine.cs - optimized Twofish block cipher implementation - LoginKeys.cs - encryption key table for client versions - IClientEncryption.cs - interface for client encryption implementations ### NetState Improvements - Replaced Socket Connection with RingSocket _socket for managed socket lifecycle - Changed from GCHandle polling to event-based completion processing - Disconnect handling now properly waits for pending sends to flush - Simplified connecting socket management using lazy queue removal ### Configuration - New settings: network.encryptionMode and network.encryptionDebug - Encryption mode flags: Unencrypted, Encrypted, or Both ### Dependencies - Replaced PollGroup NuGet package with IORingGroup - Linux requires liburing-dev / liburing-devel package ### Test plan - Verify server starts and accepts connections on Linux with io_uring - Verify server starts and accepts connections on Windows (fallback to IOCP) - Test unencrypted client connections (ClassicUO with encryption disabled) - Test encrypted client connections if available - Verify graceful disconnect flushes pending data - Confirm CI builds pass on all target platforms
89 lines
3 KiB
C#
89 lines
3 KiB
C#
/*************************************************************************
|
|
* ModernUO *
|
|
* Copyright 2019-2025 - ModernUO Development Team *
|
|
* Email: hi@modernuo.com *
|
|
* File: LoginKeys.cs *
|
|
* *
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
* it under the terms of the GNU General Public License as published by *
|
|
* the Free Software Foundation, either version 3 of the License, or *
|
|
* (at your option) any later version. *
|
|
* *
|
|
* You should have received a copy of the GNU General Public License *
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
*************************************************************************/
|
|
|
|
using System.Collections.Generic;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
namespace Server.Network;
|
|
|
|
/// <summary>
|
|
/// Represents encryption keys derived from a client version.
|
|
/// Used for login packet encryption/decryption.
|
|
/// </summary>
|
|
public readonly struct LoginKeys
|
|
{
|
|
public static readonly LoginKeys Empty = new(0, 0);
|
|
|
|
private static readonly Dictionary<ClientVersion, LoginKeys> _cache = [];
|
|
|
|
public uint Key1 { get; }
|
|
public uint Key2 { get; }
|
|
|
|
private LoginKeys(uint key1, uint key2)
|
|
{
|
|
Key1 = key1;
|
|
Key2 = key2;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or computes encryption keys for the specified client version.
|
|
/// Results are cached for performance.
|
|
/// </summary>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static LoginKeys GetKeys(ClientVersion version)
|
|
{
|
|
if (version == null)
|
|
{
|
|
return Empty;
|
|
}
|
|
|
|
if (_cache.TryGetValue(version, out var keys))
|
|
{
|
|
return keys;
|
|
}
|
|
|
|
keys = ComputeKeys(version);
|
|
_cache[version] = keys;
|
|
return keys;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Computes encryption keys from client version using the UO key derivation algorithm.
|
|
/// </summary>
|
|
private static LoginKeys ComputeKeys(ClientVersion version)
|
|
{
|
|
uint major = (uint)version.Major;
|
|
uint minor = (uint)version.Minor;
|
|
uint revision = (uint)version.Revision;
|
|
|
|
// Key1 derivation
|
|
uint key1 = (major << 23) | (minor << 14) | (revision << 4);
|
|
key1 ^= (revision * revision) << 9;
|
|
key1 ^= minor * minor;
|
|
key1 ^= (minor * 11) << 24;
|
|
key1 ^= (revision * 7) << 19;
|
|
key1 ^= 0x2C13A5FD;
|
|
|
|
// Key2 derivation
|
|
uint key2 = (major << 22) | (revision << 13) | (minor << 3);
|
|
key2 ^= (revision * revision * 3) << 10;
|
|
key2 ^= minor * minor;
|
|
key2 ^= (minor * 13) << 23;
|
|
key2 ^= (revision * 7) << 18;
|
|
key2 ^= 0xA31D527F;
|
|
|
|
return new LoginKeys(key1, key2);
|
|
}
|
|
}
|