From 37cd6ac2c36a10eb92ada851a1c05af047413749 Mon Sep 17 00:00:00 2001 From: Badartefact Date: Fri, 14 Jun 2019 12:13:14 +0300 Subject: [PATCH 1/2] Fixed null ref exception. Fixed problem with wrong ip address. --- Lidgren.Network/NetPeer.LatencySimulation.cs | 9 +++++++-- Lidgren.Network/NetUtility.cs | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Lidgren.Network/NetPeer.LatencySimulation.cs b/Lidgren.Network/NetPeer.LatencySimulation.cs index 5b699c60..b0983e34 100644 --- a/Lidgren.Network/NetPeer.LatencySimulation.cs +++ b/Lidgren.Network/NetPeer.LatencySimulation.cs @@ -154,8 +154,13 @@ internal bool ActuallySendPacket(byte[] data, int numBytes, NetEndPoint target, m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true); } else if(m_configuration.DualStack && m_configuration.LocalAddress.AddressFamily == AddressFamily.InterNetworkV6) - NetUtility.CopyEndpoint(target, targetCopy); //Maps to IPv6 for Dual Mode - + NetUtility.CopyEndpoint(target, targetCopy); //Maps to IPv6 for Dual Mode + else + { + targetCopy.Port = target.Port; + targetCopy.Address = target.Address; + } + int bytesSent = m_socket.SendTo(data, 0, numBytes, SocketFlags.None, targetCopy); if (numBytes != bytesSent) LogWarning("Failed to send the full " + numBytes + "; only " + bytesSent + " bytes sent in packet!"); diff --git a/Lidgren.Network/NetUtility.cs b/Lidgren.Network/NetUtility.cs index a7798fbe..19ed7970 100644 --- a/Lidgren.Network/NetUtility.cs +++ b/Lidgren.Network/NetUtility.cs @@ -189,7 +189,7 @@ public static NetAddress Resolve(string ipOrHost) return null; foreach (var address in addresses) { - if (address.AddressFamily == AddressFamily.InterNetwork || ipAddress.AddressFamily == AddressFamily.InterNetworkV6) + if (address.AddressFamily == AddressFamily.InterNetwork || address.AddressFamily == AddressFamily.InterNetworkV6) return address; } return null; From b9bfbb0be5fd088a9e3e751e9bd440efde36e3b7 Mon Sep 17 00:00:00 2001 From: Badartefact Date: Fri, 14 Jun 2019 22:39:29 +0300 Subject: [PATCH 2/2] Fixed problem with setting correct local address when we change DualStack property. Do not use any IPv6 features by default. --- Lidgren.Network/NetPeer.Internal.cs | 6 +++++- Lidgren.Network/NetPeer.cs | 3 ++- Lidgren.Network/NetPeerConfiguration.cs | 10 +++++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Lidgren.Network/NetPeer.Internal.cs b/Lidgren.Network/NetPeer.Internal.cs index 6ed899e2..90749652 100644 --- a/Lidgren.Network/NetPeer.Internal.cs +++ b/Lidgren.Network/NetPeer.Internal.cs @@ -135,7 +135,11 @@ private void BindSocket(bool reBind) if(m_configuration.DualStack && m_configuration.LocalAddress.AddressFamily == AddressFamily.InterNetworkV6) m_socket.DualMode = true; - var ep = (EndPoint)new NetEndPoint(m_configuration.LocalAddress.MapToIPv6(), reBind ? m_listenPort : m_configuration.Port); + var localAddress = m_configuration.DualStack + ? m_configuration.LocalAddress.MapToIPv6() + : m_configuration.LocalAddress; + + var ep = (EndPoint)new NetEndPoint(localAddress, reBind ? m_listenPort : m_configuration.Port); m_socket.Bind(ep); try diff --git a/Lidgren.Network/NetPeer.cs b/Lidgren.Network/NetPeer.cs index c17b776f..e1a74a33 100644 --- a/Lidgren.Network/NetPeer.cs +++ b/Lidgren.Network/NetPeer.cs @@ -121,7 +121,8 @@ public NetPeer(NetPeerConfiguration config) m_connections = new List(); m_connectionLookup = new Dictionary(); m_handshakes = new Dictionary(); - m_senderRemote = (EndPoint)new NetEndPoint(IPAddress.IPv6Any, 0); + var address = config.DualStack ? IPAddress.IPv6Any : IPAddress.Any; + m_senderRemote = (EndPoint)new NetEndPoint(address, 0); m_status = NetPeerStatus.NotRunning; m_receivedFragmentGroups = new Dictionary>(); } diff --git a/Lidgren.Network/NetPeerConfiguration.cs b/Lidgren.Network/NetPeerConfiguration.cs index b337e6d0..d33d9383 100644 --- a/Lidgren.Network/NetPeerConfiguration.cs +++ b/Lidgren.Network/NetPeerConfiguration.cs @@ -95,7 +95,7 @@ public NetPeerConfiguration(string appIdentifier) // m_disabledTypes = NetIncomingMessageType.ConnectionApproval | NetIncomingMessageType.UnconnectedData | NetIncomingMessageType.VerboseDebugMessage | NetIncomingMessageType.ConnectionLatencyUpdated | NetIncomingMessageType.NatIntroductionSuccess; m_networkThreadName = "Lidgren network thread"; - m_localAddress = IPAddress.IPv6Any; + m_localAddress = IPAddress.Any; m_broadcastAddress = IPAddress.Broadcast; var ip = NetUtility.GetBroadcastAddress(); if (ip != null) @@ -330,7 +330,7 @@ public bool SuppressUnreliableUnorderedAcks } /// - /// Gets or sets the local ip address to bind to. Defaults to IPAddress.IPv6Any. Cannot be changed once NetPeer is initialized. + /// Gets or sets the local ip address to bind to. Defaults to IPAddress.Any. Cannot be changed once NetPeer is initialized. /// public IPAddress LocalAddress { @@ -353,7 +353,11 @@ public bool DualStack { if (m_isLocked) throw new NetException(c_isLockedMessage); - m_dualStack = value; + m_dualStack = value; + if (m_dualStack && m_localAddress.Equals(IPAddress.Any)) + m_localAddress = IPAddress.IPv6Any; + if (!m_dualStack && m_localAddress.Equals(IPAddress.IPv6Any)) + m_localAddress = IPAddress.Any; } }