Skip to content

Commit

Permalink
fix ip resolving for Connect method (IPv4 and IPv6 conflict)
Browse files Browse the repository at this point in the history
  • Loading branch information
RevenantX committed Jun 14, 2016
1 parent e6c088e commit aabc670
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
2 changes: 1 addition & 1 deletion LiteNetLib/NetClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected override void ProcessError(string errorMessage)
public void Connect(string address, int port)
{
//Create target endpoint
NetEndPoint ep = new NetEndPoint(address, port);
NetEndPoint ep = new NetEndPoint(address, port, AddressType);
Connect(ep);
}

Expand Down
41 changes: 33 additions & 8 deletions LiteNetLib/NetEndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,10 @@ public NetEndPoint(string hostStr, int port)
IPAddress ipAddress;
if (!IPAddress.TryParse(hostStr, out ipAddress))
{
IPHostEntry host = Dns.GetHostEntry(hostStr);
foreach (IPAddress ip in host.AddressList)
ipAddress = ResolveAddress(hostStr, AddressFamily.InterNetworkV6);
if (ipAddress == null)
{
if (ip.AddressFamily == AddressFamily.InterNetwork ||
ip.AddressFamily == AddressFamily.InterNetworkV6)
{
ipAddress = ip;
break;
}
ipAddress = ResolveAddress(hostStr, AddressFamily.InterNetwork);
}
}
if (ipAddress == null)
Expand All @@ -73,6 +68,36 @@ public NetEndPoint(string hostStr, int port)
EndPoint = new IPEndPoint(ipAddress, port);
}

private IPAddress ResolveAddress(string hostStr, AddressFamily addressFamily)
{
IPHostEntry host = Dns.GetHostEntry(hostStr);
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == addressFamily)
{
return ip;
}
}
return null;
}

public NetEndPoint(string hostStr, int port, ConnectionAddressType addressType)
{
IPAddress ipAddress;
if (!IPAddress.TryParse(hostStr, out ipAddress))
{
ipAddress = ResolveAddress(hostStr,
addressType == ConnectionAddressType.IPv4
? AddressFamily.InterNetwork
: AddressFamily.InterNetworkV6);
}
if (ipAddress == null)
{
throw new Exception("Invalid address: " + hostStr);
}
EndPoint = new IPEndPoint(ipAddress, port);
}

internal long GetId()
{
byte[] addr = EndPoint.Address.GetAddressBytes();
Expand Down
6 changes: 3 additions & 3 deletions LiteNetLib/NtpSyncModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public class NtpSyncModule

public NtpSyncModule(string ntpServer)
{
_socket = new NetSocket(ConnectionAddressType.IPv4);
NetEndPoint ourEndPoint = new NetEndPoint(ConnectionAddressType.IPv4, 0);
_ntpEndPoint = new NetEndPoint(ntpServer, 123);
_socket = new NetSocket(_ntpEndPoint.AddressType);
NetEndPoint ourEndPoint = new NetEndPoint(_ntpEndPoint.AddressType, 0);
_socket.Bind(ref ourEndPoint);
_socket.ReceiveTimeout = 3000;
_ntpEndPoint = new NetEndPoint(ntpServer, 123);
SyncedTime = null;
}

Expand Down
5 changes: 5 additions & 0 deletions LiteNetLibWin81/NetEndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ public NetEndPoint(string hostName, int port)
PortStr = port.ToString();
}

public NetEndPoint(string hostName, int port, ConnectionAddressType addressType) : this(hostName, port)
{

}

internal NetEndPoint(HostName hostName, string port)
{
HostName = hostName;
Expand Down

0 comments on commit aabc670

Please sign in to comment.