Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed errors with discovery when using IPv6 #150

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Lidgren.Network/NetPeer.Discovery.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

#if !__NOIPENDPOINT__
Expand All @@ -19,7 +20,11 @@ public void DiscoverLocalPeers(int serverPort)
um.m_messageType = NetMessageType.Discovery;
Interlocked.Increment(ref um.m_recyclingCount);

m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(new NetEndPoint(NetUtility.GetBroadcastAddress(), serverPort), um));
var broadcastAddress = NetUtility.GetBroadcastAddress();
if (m_configuration.DualStack)
broadcastAddress = NetUtility.MapToIPv6(broadcastAddress);

m_unsentUnconnectedMessages.Enqueue(new NetTuple<NetEndPoint, NetOutgoingMessage>(new NetEndPoint(broadcastAddress, serverPort), um));
}

/// <summary>
Expand All @@ -39,6 +44,11 @@ public bool DiscoverKnownPeer(string host, int serverPort)
/// </summary>
public void DiscoverKnownPeer(NetEndPoint endPoint)
{
if (endPoint == null)
throw new ArgumentNullException(nameof(endPoint));
if (m_configuration.DualStack)
endPoint = NetUtility.MapToIPv6(endPoint);

NetOutgoingMessage om = CreateMessage(0);
om.m_messageType = NetMessageType.Discovery;
om.m_recyclingCount = 1;
Expand Down
10 changes: 10 additions & 0 deletions Lidgren.Network/NetUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -489,5 +489,15 @@ internal static IPEndPoint MapToIPv6(IPEndPoint endPoint)
return new IPEndPoint(endPoint.Address.MapToIPv6(), endPoint.Port);
return endPoint;
}

/// <summary>
/// Maps the IPAddress object to an IPv6 address. Has allocation
/// </summary>
internal static IPAddress MapToIPv6(IPAddress address)
{
if (address.AddressFamily == AddressFamily.InterNetwork)
return address.MapToIPv6();
return address;
}
}
}