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 parsing error with IPv6 addresses in CnCNetTunnel #642

Open
wants to merge 3 commits into
base: develop
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
21 changes: 18 additions & 3 deletions DXMainClient/DXGUI/Multiplayer/CnCNet/CnCNetGameLoadingLobby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace DTAClient.DXGUI.Multiplayer.CnCNet
{
Expand Down Expand Up @@ -73,7 +74,7 @@

private CnCNetManager connectionManager;

private List<GameMode> gameModes;

Check warning on line 77 in DXMainClient/DXGUI/Multiplayer/CnCNet/CnCNetGameLoadingLobby.cs

View workflow job for this annotation

GitHub Actions / build-clients (Ares)

The field 'CnCNetGameLoadingLobby.gameModes' is never used

Check warning on line 77 in DXMainClient/DXGUI/Multiplayer/CnCNet/CnCNetGameLoadingLobby.cs

View workflow job for this annotation

GitHub Actions / build-clients (TS)

The field 'CnCNetGameLoadingLobby.gameModes' is never used

Check warning on line 77 in DXMainClient/DXGUI/Multiplayer/CnCNet/CnCNetGameLoadingLobby.cs

View workflow job for this annotation

GitHub Actions / build-clients (YR)

The field 'CnCNetGameLoadingLobby.gameModes' is never used

private TunnelHandler tunnelHandler;
private readonly MapLoader mapLoader;
Expand Down Expand Up @@ -567,9 +568,23 @@
if (sender != hostName)
return;

string[] split = tunnelAddressAndPort.Split(':');
string tunnelAddress = split[0];
int tunnelPort = int.Parse(split[1]);
string tunnelAddress;
int tunnelPort;

if (tunnelAddressAndPort.Count(c => c == ':') > 1)
{
// IPv6 address
int lastColonIndex = tunnelAddressAndPort.LastIndexOf(':');
tunnelAddress = tunnelAddressAndPort.Substring(0, lastColonIndex);
tunnelPort = int.Parse(tunnelAddressAndPort.Substring(lastColonIndex + 1));
}
else
{
// IPv4 address or hostname
string[] detailedAddress = tunnelAddressAndPort.Split(':');
tunnelAddress = detailedAddress[0];
tunnelPort = int.Parse(detailedAddress[1]);
}

CnCNetTunnel tunnel = tunnelHandler.Tunnels.Find(t => t.Address == tunnelAddress && t.Port == tunnelPort);
if (tunnel == null)
Expand Down
21 changes: 18 additions & 3 deletions DXMainClient/DXGUI/Multiplayer/CnCNet/CnCNetLobby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1521,9 +1521,24 @@ private void GameBroadcastChannel_CTCPReceived(object sender, ChannelCTCPEventAr
string mapName = splitMessage[7];
string gameMode = splitMessage[8];

string[] tunnelAddressAndPort = splitMessage[9].Split(':');
string tunnelAddress = tunnelAddressAndPort[0];
int tunnelPort = int.Parse(tunnelAddressAndPort[1]);
string tunnelAddressAndPort = splitMessage[9];
string tunnelAddress;
int tunnelPort;

if (tunnelAddressAndPort.Count(c => c == ':') > 1)
{
// IPv6 address
int lastColonIndex = tunnelAddressAndPort.LastIndexOf(':');
tunnelAddress = tunnelAddressAndPort.Substring(0, lastColonIndex);
tunnelPort = int.Parse(tunnelAddressAndPort.Substring(lastColonIndex + 1));
}
else
{
// IPv4 address or hostname
string[] detailedAddress = tunnelAddressAndPort.Split(':');
tunnelAddress = detailedAddress[0];
tunnelPort = int.Parse(detailedAddress[1]);
}

string loadedGameId = splitMessage[10];
int gameDifficulty = int.Parse(splitMessage[11]);
Expand Down
20 changes: 17 additions & 3 deletions DXMainClient/DXGUI/Multiplayer/GameLobby/CnCNetGameLobby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1566,9 +1566,23 @@ private void HandleTunnelServerChangeMessage(string sender, string tunnelAddress
if (sender != hostName)
return;

string[] split = tunnelAddressAndPort.Split(':');
string tunnelAddress = split[0];
int tunnelPort = int.Parse(split[1]);
string tunnelAddress;
int tunnelPort;

if (tunnelAddressAndPort.Count(c => c == ':') > 1)
{
// IPv6 address
int lastColonIndex = tunnelAddressAndPort.LastIndexOf(':');
tunnelAddress = tunnelAddressAndPort.Substring(0, lastColonIndex);
tunnelPort = int.Parse(tunnelAddressAndPort.Substring(lastColonIndex + 1));
}
else
{
// IPv4 address or hostname
string[] detailedAddress = tunnelAddressAndPort.Split(':');
tunnelAddress = detailedAddress[0];
tunnelPort = int.Parse(detailedAddress[1]);
}

CnCNetTunnel tunnel = tunnelHandler.Tunnels.Find(t => t.Address == tunnelAddress && t.Port == tunnelPort);
if (tunnel == null)
Expand Down
25 changes: 21 additions & 4 deletions DXMainClient/Domain/Multiplayer/CnCNet/CnCNetTunnel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;

Expand Down Expand Up @@ -33,10 +34,26 @@ public static CnCNetTunnel Parse(string str)
string[] parts = str.Split(';');

string address = parts[0];
string[] detailedAddress = address.Split(new char[] { ':' });

tunnel.Address = detailedAddress[0];
tunnel.Port = int.Parse(detailedAddress[1]);
string host;
int port;

if (address.Count(c => c == ':') > 1)
{
// IPv6 address
int lastColonIndex = address.LastIndexOf(':');
host = "[" + address.Substring(0, lastColonIndex) + "]";
port = int.Parse(address.Substring(lastColonIndex + 1));
}
else
{
// IPv4 address or hostname
string[] detailedAddress = address.Split(':');
host = detailedAddress[0];
port = int.Parse(detailedAddress[1]);
}

tunnel.Address = host;
tunnel.Port = port;
tunnel.Country = parts[1];
tunnel.CountryCode = parts[2];
tunnel.Name = parts[3];
Expand Down
Loading