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

fix: Only use IPv4 address without dual mode #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 22 additions & 1 deletion kcp2k/Assets/kcp2k/highlevel/KcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,28 @@ public void Connect(string address, ushort port, KcpConfig config)
OnDisconnected();
return;
}
IPAddress resolvedAddress = null;

// resolved address might return v6, even when we're configured to not use v6
// so we need to do some filtering
foreach (IPAddress ipAddress in addresses)
{
if (ipAddress.AddressFamily == AddressFamily.InterNetwork ||
config.DualMode && ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
{
resolvedAddress = ipAddress;
break;
}
}

if (resolvedAddress == null)
{
// pass error to user callback. no need to log it manually.
OnError(ErrorCode.DnsResolve, $"Failed to resolve host '{address}' to a IPv4{(config.DualMode ? "/IPv6" : "")} address");
OnDisconnected();
return;
}

// create fresh peer for each new session
peer = new KcpPeer(RawSend, OnAuthenticatedWrap, OnData, OnDisconnectedWrap, OnError, config);

Expand All @@ -94,7 +115,7 @@ void OnDisconnectedWrap()
Log.Info($"KcpClient: connect to {address}:{port}");

// create socket
remoteEndPoint = new IPEndPoint(addresses[0], port);
remoteEndPoint = new IPEndPoint(resolvedAddress, port);
socket = new Socket(remoteEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);

// configure buffer sizes:
Expand Down