From f10ba10c6341f7fcb6ff54f45c63917c9cd2033e Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Sat, 24 Nov 2018 20:14:59 +0200 Subject: [PATCH] Modified lidgren to support IPv6 (code from https://github.com/lidgren/lidgren-network-gen3/pull/33). TODO: test --- .../Lidgren.Network/Lidgren.Network.csproj | 2 +- Libraries/Lidgren.Network/NetPeer.Internal.cs | 9 +++-- .../NetPeer.LatencySimulation.cs | 5 ++- Libraries/Lidgren.Network/NetPeer.cs | 8 ++-- .../Lidgren.Network/NetPeerConfiguration.cs | 13 +++---- Libraries/Lidgren.Network/NetUtility.cs | 38 ++++++++++++------- 6 files changed, 46 insertions(+), 29 deletions(-) diff --git a/Libraries/Lidgren.Network/Lidgren.Network.csproj b/Libraries/Lidgren.Network/Lidgren.Network.csproj index 5d9799eb1..89ccd5f54 100644 --- a/Libraries/Lidgren.Network/Lidgren.Network.csproj +++ b/Libraries/Lidgren.Network/Lidgren.Network.csproj @@ -10,7 +10,7 @@ Properties Lidgren.Network Lidgren.Network - v4.0 + v4.5 512 publish\ true diff --git a/Libraries/Lidgren.Network/NetPeer.Internal.cs b/Libraries/Lidgren.Network/NetPeer.Internal.cs index b728d99e9..8375dca6e 100644 --- a/Libraries/Lidgren.Network/NetPeer.Internal.cs +++ b/Libraries/Lidgren.Network/NetPeer.Internal.cs @@ -124,17 +124,18 @@ namespace Lidgren.Network m_lastSocketBind = now; if (m_socket == null) - m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); + m_socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp); - if (reBind) + if (reBind) m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, (int)1); m_socket.ReceiveBufferSize = m_configuration.ReceiveBufferSize; m_socket.SendBufferSize = m_configuration.SendBufferSize; m_socket.Blocking = false; + m_socket.DualMode = true; - var ep = (EndPoint)new NetEndPoint(m_configuration.LocalAddress, reBind ? m_listenPort : m_configuration.Port); - m_socket.Bind(ep); + var ep = (EndPoint)new NetEndPoint(m_configuration.LocalAddress.MapToIPv6(), reBind ? m_listenPort : m_configuration.Port); + m_socket.Bind(ep); try { diff --git a/Libraries/Lidgren.Network/NetPeer.LatencySimulation.cs b/Libraries/Lidgren.Network/NetPeer.LatencySimulation.cs index 21c612538..b49585afa 100644 --- a/Libraries/Lidgren.Network/NetPeer.LatencySimulation.cs +++ b/Libraries/Lidgren.Network/NetPeer.LatencySimulation.cs @@ -135,7 +135,10 @@ namespace Lidgren.Network internal bool ActuallySendPacket(byte[] data, int numBytes, NetEndPoint target, out bool connectionReset) { connectionReset = false; - IPAddress ba = default(IPAddress); + + target = NetUtility.MapToIPv6(target); + + IPAddress ba = default(IPAddress); try { ba = NetUtility.GetCachedBroadcastAddress(); diff --git a/Libraries/Lidgren.Network/NetPeer.cs b/Libraries/Lidgren.Network/NetPeer.cs index af3b11ab3..eafef5d7c 100644 --- a/Libraries/Lidgren.Network/NetPeer.cs +++ b/Libraries/Lidgren.Network/NetPeer.cs @@ -121,8 +121,8 @@ namespace Lidgren.Network m_connections = new List(); m_connectionLookup = new Dictionary(); m_handshakes = new Dictionary(); - m_senderRemote = (EndPoint)new NetEndPoint(IPAddress.Any, 0); - m_status = NetPeerStatus.NotRunning; + m_senderRemote = (EndPoint)new NetEndPoint(IPAddress.IPv6Any, 0); + m_status = NetPeerStatus.NotRunning; m_receivedFragmentGroups = new Dictionary>(); } @@ -293,7 +293,9 @@ namespace Lidgren.Network if (remoteEndPoint == null) throw new ArgumentNullException("remoteEndPoint"); - lock (m_connections) + remoteEndPoint = NetUtility.MapToIPv6(remoteEndPoint); + + lock (m_connections) { if (m_status == NetPeerStatus.NotRunning) throw new NetException("Must call Start() first"); diff --git a/Libraries/Lidgren.Network/NetPeerConfiguration.cs b/Libraries/Lidgren.Network/NetPeerConfiguration.cs index e041aaa18..7105eca0c 100644 --- a/Libraries/Lidgren.Network/NetPeerConfiguration.cs +++ b/Libraries/Lidgren.Network/NetPeerConfiguration.cs @@ -93,8 +93,8 @@ namespace Lidgren.Network // m_disabledTypes = NetIncomingMessageType.ConnectionApproval | NetIncomingMessageType.UnconnectedData | NetIncomingMessageType.VerboseDebugMessage | NetIncomingMessageType.ConnectionLatencyUpdated | NetIncomingMessageType.NatIntroductionSuccess; m_networkThreadName = "Lidgren network thread"; - m_localAddress = IPAddress.Any; - m_broadcastAddress = IPAddress.Broadcast; + m_localAddress = IPAddress.IPv6Any; + m_broadcastAddress = IPAddress.Broadcast; var ip = NetUtility.GetBroadcastAddress(); if (ip != null) { @@ -326,11 +326,10 @@ namespace Lidgren.Network m_suppressUnreliableUnorderedAcks = value; } } - - /// - /// Gets or sets the local ip address to bind to. Defaults to IPAddress.Any. Cannot be changed once NetPeer is initialized. - /// - public IPAddress LocalAddress + /// + /// Gets or sets the local ip address to bind to. Defaults to . Cannot be changed once NetPeer is initialized. + /// + public IPAddress LocalAddress { get { return m_localAddress; } set diff --git a/Libraries/Lidgren.Network/NetUtility.cs b/Libraries/Lidgren.Network/NetUtility.cs index e3fb093f4..349d1150b 100644 --- a/Libraries/Lidgren.Network/NetUtility.cs +++ b/Libraries/Lidgren.Network/NetUtility.cs @@ -98,8 +98,8 @@ namespace Lidgren.Network NetAddress ipAddress = null; if (NetAddress.TryParse(ipOrHost, out ipAddress)) { - if (ipAddress.AddressFamily == AddressFamily.InterNetwork) - { + if (ipAddress.AddressFamily == AddressFamily.InterNetwork || ipAddress.AddressFamily == AddressFamily.InterNetworkV6) + { callback(ipAddress); return; } @@ -139,7 +139,7 @@ namespace Lidgren.Network // check each entry for a valid IP address foreach (var ipCurrent in entry.AddressList) { - if (ipCurrent.AddressFamily == AddressFamily.InterNetwork) + if (ipCurrent.AddressFamily == AddressFamily.InterNetwork || ipCurrent.AddressFamily == AddressFamily.InterNetworkV6) { callback(ipCurrent); return; @@ -176,22 +176,22 @@ namespace Lidgren.Network NetAddress ipAddress = null; if (NetAddress.TryParse(ipOrHost, out ipAddress)) { - if (ipAddress.AddressFamily == AddressFamily.InterNetwork) - return ipAddress; - throw new ArgumentException("This method will not currently resolve other than ipv4 addresses"); - } + if (ipAddress.AddressFamily == AddressFamily.InterNetwork || ipAddress.AddressFamily == AddressFamily.InterNetworkV6) + return ipAddress; + throw new ArgumentException("This method will not currently resolve other than IPv4 or IPv6 addresses"); + } - // ok must be a host name - try + // ok must be a host name + try { var addresses = Dns.GetHostAddresses(ipOrHost); if (addresses == null) return null; foreach (var address in addresses) { - if (address.AddressFamily == AddressFamily.InterNetwork) - return address; - } + if (address.AddressFamily == AddressFamily.InterNetwork || address.AddressFamily == AddressFamily.InterNetworkV6) + return address; + } return null; } catch (SocketException ex) @@ -453,5 +453,17 @@ namespace Lidgren.Network // this is defined in the platform specific files return ComputeSHAHash(bytes, 0, bytes.Length); } - } + + /// + /// Maps the IPEndPoint object to an IPv6 address, if it is currently mapped to an IPv4 address. + /// + internal static IPEndPoint MapToIPv6(IPEndPoint endPoint) + { + if (endPoint.AddressFamily == AddressFamily.InterNetwork) + { + return new IPEndPoint(endPoint.Address.MapToIPv6(), endPoint.Port); + } + return endPoint; + } + } } \ No newline at end of file