Skip to content

Commit aabc670

Browse files
committed
fix ip resolving for Connect method (IPv4 and IPv6 conflict)
1 parent e6c088e commit aabc670

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

LiteNetLib/NetClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ protected override void ProcessError(string errorMessage)
105105
public void Connect(string address, int port)
106106
{
107107
//Create target endpoint
108-
NetEndPoint ep = new NetEndPoint(address, port);
108+
NetEndPoint ep = new NetEndPoint(address, port, AddressType);
109109
Connect(ep);
110110
}
111111

LiteNetLib/NetEndPoint.cs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,10 @@ public NetEndPoint(string hostStr, int port)
5555
IPAddress ipAddress;
5656
if (!IPAddress.TryParse(hostStr, out ipAddress))
5757
{
58-
IPHostEntry host = Dns.GetHostEntry(hostStr);
59-
foreach (IPAddress ip in host.AddressList)
58+
ipAddress = ResolveAddress(hostStr, AddressFamily.InterNetworkV6);
59+
if (ipAddress == null)
6060
{
61-
if (ip.AddressFamily == AddressFamily.InterNetwork ||
62-
ip.AddressFamily == AddressFamily.InterNetworkV6)
63-
{
64-
ipAddress = ip;
65-
break;
66-
}
61+
ipAddress = ResolveAddress(hostStr, AddressFamily.InterNetwork);
6762
}
6863
}
6964
if (ipAddress == null)
@@ -73,6 +68,36 @@ public NetEndPoint(string hostStr, int port)
7368
EndPoint = new IPEndPoint(ipAddress, port);
7469
}
7570

71+
private IPAddress ResolveAddress(string hostStr, AddressFamily addressFamily)
72+
{
73+
IPHostEntry host = Dns.GetHostEntry(hostStr);
74+
foreach (IPAddress ip in host.AddressList)
75+
{
76+
if (ip.AddressFamily == addressFamily)
77+
{
78+
return ip;
79+
}
80+
}
81+
return null;
82+
}
83+
84+
public NetEndPoint(string hostStr, int port, ConnectionAddressType addressType)
85+
{
86+
IPAddress ipAddress;
87+
if (!IPAddress.TryParse(hostStr, out ipAddress))
88+
{
89+
ipAddress = ResolveAddress(hostStr,
90+
addressType == ConnectionAddressType.IPv4
91+
? AddressFamily.InterNetwork
92+
: AddressFamily.InterNetworkV6);
93+
}
94+
if (ipAddress == null)
95+
{
96+
throw new Exception("Invalid address: " + hostStr);
97+
}
98+
EndPoint = new IPEndPoint(ipAddress, port);
99+
}
100+
76101
internal long GetId()
77102
{
78103
byte[] addr = EndPoint.Address.GetAddressBytes();

LiteNetLib/NtpSyncModule.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ public class NtpSyncModule
1010

1111
public NtpSyncModule(string ntpServer)
1212
{
13-
_socket = new NetSocket(ConnectionAddressType.IPv4);
14-
NetEndPoint ourEndPoint = new NetEndPoint(ConnectionAddressType.IPv4, 0);
13+
_ntpEndPoint = new NetEndPoint(ntpServer, 123);
14+
_socket = new NetSocket(_ntpEndPoint.AddressType);
15+
NetEndPoint ourEndPoint = new NetEndPoint(_ntpEndPoint.AddressType, 0);
1516
_socket.Bind(ref ourEndPoint);
1617
_socket.ReceiveTimeout = 3000;
17-
_ntpEndPoint = new NetEndPoint(ntpServer, 123);
1818
SyncedTime = null;
1919
}
2020

LiteNetLibWin81/NetEndPoint.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ public NetEndPoint(string hostName, int port)
102102
PortStr = port.ToString();
103103
}
104104

105+
public NetEndPoint(string hostName, int port, ConnectionAddressType addressType) : this(hostName, port)
106+
{
107+
108+
}
109+
105110
internal NetEndPoint(HostName hostName, string port)
106111
{
107112
HostName = hostName;

0 commit comments

Comments
 (0)