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

Implementation of Gamespy's NAT Negotiation feature #1

Open
wants to merge 5 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
6 changes: 6 additions & 0 deletions .nuget/NuGet.Config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
</configuration>
Binary file added .nuget/NuGet.exe
Binary file not shown.
144 changes: 144 additions & 0 deletions .nuget/NuGet.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">$(MSBuildProjectDirectory)\..\</SolutionDir>

<!-- Enable the restore command to run before builds -->
<RestorePackages Condition=" '$(RestorePackages)' == '' ">false</RestorePackages>

<!-- Property that enables building a package from a project -->
<BuildPackage Condition=" '$(BuildPackage)' == '' ">false</BuildPackage>

<!-- Determines if package restore consent is required to restore packages -->
<RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">true</RequireRestoreConsent>

<!-- Download NuGet.exe if it does not already exist -->
<DownloadNuGetExe Condition=" '$(DownloadNuGetExe)' == '' ">false</DownloadNuGetExe>
</PropertyGroup>

<ItemGroup Condition=" '$(PackageSources)' == '' ">
<!-- Package sources used to restore packages. By default, registered sources under %APPDATA%\NuGet\NuGet.Config will be used -->
<!-- The official NuGet package source (https://www.nuget.org/api/v2/) will be excluded if package sources are specified and it does not appear in the list -->
<!--
<PackageSource Include="https://www.nuget.org/api/v2/" />
<PackageSource Include="https://my-nuget-source/nuget/" />
-->
</ItemGroup>

<PropertyGroup Condition=" '$(OS)' == 'Windows_NT'">
<!-- Windows specific commands -->
<NuGetToolsPath>$([System.IO.Path]::Combine($(SolutionDir), ".nuget"))</NuGetToolsPath>
</PropertyGroup>

<PropertyGroup Condition=" '$(OS)' != 'Windows_NT'">
<!-- We need to launch nuget.exe with the mono command if we're not on windows -->
<NuGetToolsPath>$(SolutionDir).nuget</NuGetToolsPath>
</PropertyGroup>

<PropertyGroup>
<PackagesProjectConfig Condition=" '$(OS)' == 'Windows_NT'">$(MSBuildProjectDirectory)\packages.$(MSBuildProjectName.Replace(' ', '_')).config</PackagesProjectConfig>
<PackagesProjectConfig Condition=" '$(OS)' != 'Windows_NT'">$(MSBuildProjectDirectory)\packages.$(MSBuildProjectName).config</PackagesProjectConfig>
</PropertyGroup>

<PropertyGroup>
<PackagesConfig Condition="Exists('$(MSBuildProjectDirectory)\packages.config')">$(MSBuildProjectDirectory)\packages.config</PackagesConfig>
<PackagesConfig Condition="Exists('$(PackagesProjectConfig)')">$(PackagesProjectConfig)</PackagesConfig>
</PropertyGroup>

<PropertyGroup>
<!-- NuGet command -->
<NuGetExePath Condition=" '$(NuGetExePath)' == '' ">$(NuGetToolsPath)\NuGet.exe</NuGetExePath>
<PackageSources Condition=" $(PackageSources) == '' ">@(PackageSource)</PackageSources>

<NuGetCommand Condition=" '$(OS)' == 'Windows_NT'">"$(NuGetExePath)"</NuGetCommand>
<NuGetCommand Condition=" '$(OS)' != 'Windows_NT' ">mono --runtime=v4.0.30319 "$(NuGetExePath)"</NuGetCommand>

<PackageOutputDir Condition="$(PackageOutputDir) == ''">$(TargetDir.Trim('\\'))</PackageOutputDir>

<RequireConsentSwitch Condition=" $(RequireRestoreConsent) == 'true' ">-RequireConsent</RequireConsentSwitch>
<NonInteractiveSwitch Condition=" '$(VisualStudioVersion)' != '' AND '$(OS)' == 'Windows_NT' ">-NonInteractive</NonInteractiveSwitch>

<PaddedSolutionDir Condition=" '$(OS)' == 'Windows_NT'">"$(SolutionDir) "</PaddedSolutionDir>
<PaddedSolutionDir Condition=" '$(OS)' != 'Windows_NT' ">"$(SolutionDir)"</PaddedSolutionDir>

<!-- Commands -->
<RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir)</RestoreCommand>
<BuildCommand>$(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols</BuildCommand>

<!-- We need to ensure packages are restored prior to assembly resolve -->
<BuildDependsOn Condition="$(RestorePackages) == 'true'">
RestorePackages;
$(BuildDependsOn);
</BuildDependsOn>

<!-- Make the build depend on restore packages -->
<BuildDependsOn Condition="$(BuildPackage) == 'true'">
$(BuildDependsOn);
BuildPackage;
</BuildDependsOn>
</PropertyGroup>

<Target Name="CheckPrerequisites">
<!-- Raise an error if we're unable to locate nuget.exe -->
<Error Condition="'$(DownloadNuGetExe)' != 'true' AND !Exists('$(NuGetExePath)')" Text="Unable to locate '$(NuGetExePath)'" />
<!--
Take advantage of MsBuild's build dependency tracking to make sure that we only ever download nuget.exe once.
This effectively acts as a lock that makes sure that the download operation will only happen once and all
parallel builds will have to wait for it to complete.
-->
<MsBuild Targets="_DownloadNuGet" Projects="$(MSBuildThisFileFullPath)" Properties="Configuration=NOT_IMPORTANT;DownloadNuGetExe=$(DownloadNuGetExe)" />
</Target>

<Target Name="_DownloadNuGet">
<DownloadNuGet OutputFilename="$(NuGetExePath)" Condition=" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')" />
</Target>

<Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites">
<Exec Command="$(RestoreCommand)"
Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" />

<Exec Command="$(RestoreCommand)"
LogStandardErrorAsError="true"
Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
</Target>

<Target Name="BuildPackage" DependsOnTargets="CheckPrerequisites">
<Exec Command="$(BuildCommand)"
Condition=" '$(OS)' != 'Windows_NT' " />

<Exec Command="$(BuildCommand)"
LogStandardErrorAsError="true"
Condition=" '$(OS)' == 'Windows_NT' " />
</Target>

<UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<OutputFilename ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Core" />
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Net" />
<Using Namespace="Microsoft.Build.Framework" />
<Using Namespace="Microsoft.Build.Utilities" />
<Code Type="Fragment" Language="cs">
<![CDATA[
try {
OutputFilename = Path.GetFullPath(OutputFilename);

Log.LogMessage("Downloading latest version of NuGet.exe...");
WebClient webClient = new WebClient();
webClient.DownloadFile("https://www.nuget.org/nuget.exe", OutputFilename);

return true;
}
catch (Exception ex) {
Log.LogErrorFromException(ex);
return false;
}
]]>
</Code>
</Task>
</UsingTask>
</Project>
11 changes: 8 additions & 3 deletions PRMasterServer.sln
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.30110.0
MinimumVisualStudioVersion = 10.0.40219.1
# Visual Studio Express 2012 for Windows Desktop
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PRMasterServer", "PRMasterServer\PRMasterServer.csproj", "{64BC01A8-FCD2-4AB7-8811-3200F83AE124}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{13A3CA56-09D8-474B-9944-677FF9AEE98B}"
ProjectSection(SolutionItems) = preProject
.nuget\NuGet.Config = .nuget\NuGet.Config
.nuget\NuGet.exe = .nuget\NuGet.exe
.nuget\NuGet.targets = .nuget\NuGet.targets
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
204 changes: 204 additions & 0 deletions PRMasterServer/Data/NatNegMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace PRMasterServer.Data
{
public class NatNegMessage
{
public int Constant; // always 1e 66 6a b2
public byte ProtocolVersion;
public byte RecordType;
public byte[] RecordSpecificData;

public int ClientId;
public byte SequenceId; // (0x00 to 0x03)
public byte Hoststate; // (0x00 for guest, 0x01 for host)
public byte UseGamePort;
public string PrivateIPAddress;
public ushort LocalPort;
public string GameName;

public string ClientPublicIPAddress;
public ushort ClientPublicPort;
public byte Error;
public byte GotData;

public byte PortType; // (0x00, 0x80 or 0x90)
public byte ReplyFlag;
public ushort ConnectAckUnknown2;
public byte ConnectAckUnknown3;
public int ConnectAckUnknown4;

public byte NatNegResult;
public int NatType;
public int NatMappingScheme;

public byte ReportAckUnknown1;
public byte ReportAckUnknown2;
public ushort ReportAckUnknown3;

public override string ToString()
{
if (RecordType == 0) return "INIT CLIENT " + ClientId + " SEQUENCE " + SequenceId + " HOSTSTATE " + Hoststate + " USEGAMEPORT " + UseGamePort + " PRIVATEIP " + PrivateIPAddress + " LOCALPORT " + LocalPort + " GAMENAME " + GameName;
if (RecordType == 1) return "INIT_ACK CLIENT " + ClientId + " SEQUENCE " + SequenceId + " HOSTSTATE " + Hoststate;
if (RecordType == 5) return "CONNECT CLIENT " + ClientId + " CLIENTPUBLICIP " + ClientPublicIPAddress + " CLIENTPUBLICPORT " + ClientPublicPort + " GOTDATA " + GotData + " ERROR " + Error;
if (RecordType == 6) return "CONNECT_ACK " + ClientId + " PORTTYPE " + PortType + " REPLYFLAG " + ReplyFlag + " UNKNOWN2 " + ConnectAckUnknown2 + " UNKNOWN3 " + ConnectAckUnknown3 + " UNKNOWN4 " + ConnectAckUnknown4;
if (RecordType == 13) return "REPORT " + ClientId + " PORTTYPE " + PortType + " HOSTSTATE " + Hoststate + " NATNEGRESULT " + NatNegResult + " NATTYPE " + NatType + " NATMAPPINGSCHEME " + NatMappingScheme + " GAMENAME " + GameName;
if (RecordType == 14) return "REPORT_ACK " + ClientId + " PORTTYPE " + PortType + " UNKNOWN1 " + ReportAckUnknown1 + " UNKNOWN2 " + ReportAckUnknown2 + " NATTYPE " + NatType + " UNKNOWN3 " + ReportAckUnknown3;
return "RECORDTYPE: " + RecordType;
}

public static NatNegMessage ParseData(byte[] bytes)
{
if (bytes.Length < 8) return null;
if (bytes[0] != 0xFD || bytes[1] != 0xFC) return null;
NatNegMessage msg = new NatNegMessage();
msg.Constant = _toInt(_getBytes(bytes, 2, 4));
msg.ProtocolVersion = bytes[6];
msg.RecordType = bytes[7];
if (bytes.Length > 8) msg.RecordSpecificData = _getBytes(bytes, 8, bytes.Length - 8);
if (msg.RecordType == 0)
{
// INIT
msg.ClientId = _toInt(_getBytes(msg.RecordSpecificData, 0, 4));
msg.SequenceId = msg.RecordSpecificData[4];
msg.Hoststate = msg.RecordSpecificData[5];
msg.UseGamePort = msg.RecordSpecificData[6];
msg.PrivateIPAddress = _toIpAddress(_getBytes(msg.RecordSpecificData, 7, 4));
msg.LocalPort = _toShort(_getBytes(msg.RecordSpecificData, 11, 2));
msg.GameName = _toString(_getBytes(msg.RecordSpecificData, 13, msg.RecordSpecificData.Length-13));
}
else if (msg.RecordType == 6)
{
// CONNECT_ACK
msg.ClientId = _toInt(_getBytes(msg.RecordSpecificData, 0, 4));
msg.PortType = msg.RecordSpecificData[4];
msg.ReplyFlag = msg.RecordSpecificData[5];
msg.ConnectAckUnknown2 = _toShort(_getBytes(msg.RecordSpecificData, 6, 2));
msg.ConnectAckUnknown3 = msg.RecordSpecificData[8];
msg.ConnectAckUnknown4 = _toInt(_getBytes(msg.RecordSpecificData, 9, 4));
}
else if (msg.RecordType == 13)
{
// CONNECT_ACK
msg.ClientId = _toInt(_getBytes(msg.RecordSpecificData, 0, 4));
msg.PortType = msg.RecordSpecificData[4];
msg.Hoststate = msg.RecordSpecificData[5];
msg.NatNegResult = msg.RecordSpecificData[6];
msg.NatType = _toIntBigEndian(_getBytes(msg.RecordSpecificData, 7, 4));
msg.NatMappingScheme = _toIntBigEndian(_getBytes(msg.RecordSpecificData, 11, 4));
msg.GameName = _toString(_getBytes(msg.RecordSpecificData, 15, msg.RecordSpecificData.Length - 15));
}
return msg;
}

public byte[] ToBytes()
{
List<byte> bytes = new List<byte>();
bytes.Add(0xFD);
bytes.Add(0xFC);
bytes.Add(0x1E);
bytes.Add(0x66);
bytes.Add(0x6a);
bytes.Add(0xb2);
bytes.Add(ProtocolVersion);
bytes.Add(RecordType);
_addInt(bytes, ClientId);
if (RecordType == 1)
{
// INIT_ACK (0x01)
bytes.Add(SequenceId);
bytes.Add(Hoststate);
_addBytes(bytes, 0xFF, 0xFF); //ff ff unknown_5. Always 0xffff
_addBytes(bytes, 0x6d, 0x16, 0xb5, 0x7d, 0xea); // unknown_6. Any useless constant (also for other games).
}
else if (RecordType == 5)
{
// CONNECT (0x05)
_addIPAddress(bytes, ClientPublicIPAddress);
_addShort(bytes, ClientPublicPort);
bytes.Add(GotData);
bytes.Add(Error);
}
else if (RecordType == 14)
{
// REPORT_ACK
bytes.Add(PortType);
bytes.Add(ReportAckUnknown1);
bytes.Add(ReportAckUnknown2);
_addInt(bytes, NatType);
_addShort(bytes, ReportAckUnknown3);
}
return bytes.ToArray();
}

private static string _toString(byte[] bytes) {
List<byte> bs = new List<byte>();
for (int i = 0; i < bytes.Length && bytes[i] > 0; i++)
bs.Add(bytes[i]);
return Encoding.ASCII.GetString(bs.ToArray());
}

private static void _addBytes(List<byte> bytes, params byte[] adds)
{
bytes.AddRange(adds);
}

private static void _addInt(List<byte> bytes, int value)
{
List<byte> b = new List<byte>(BitConverter.GetBytes((int)value));
if (BitConverter.IsLittleEndian)
{
b.Reverse();
}
while (b.Count < 4) b.Insert(0, 0);
bytes.AddRange(b);
}

private static void _addShort(List<byte> bytes, ushort value)
{
List<byte> b = new List<byte>(BitConverter.GetBytes(value));
if (BitConverter.IsLittleEndian)
{
b.Reverse();
}
while (b.Count < 2) b.Insert(0, 0);
bytes.AddRange(b);
}

private static void _addIPAddress(List<byte> bytes, string address)
{
bytes.AddRange(address.Split('.').Select((b) => { return (byte)Convert.ToInt32(b); }));
}

private static int _toInt(byte[] bytes)
{
return (int)bytes[0] * 256 * 256 * 256 + (int)bytes[1] * 256 * 256 + (int)bytes[2] * 256 + bytes[3];
}

private static int _toIntBigEndian(byte[] bytes)
{
return (int)bytes[3] * 256 * 256 * 256 + (int)bytes[2] * 256 * 256 + (int)bytes[1] * 256 + bytes[0];
}

private static ushort _toShort(byte[] bytes)
{
return (ushort)(bytes[0] * 256 + bytes[1]);
}

public static string _toIpAddress(byte[] bytes) {
return string.Join(".", bytes.Select((b) => { return b.ToString(); }));
}

private static byte[] _getBytes(byte[] bytes, int index, int nofBytes) {
byte[] result = new byte[nofBytes];
for(int i = 0; i < nofBytes; i++) {
if(index+i >= bytes.Length) result[i] = 0; else result[i] = bytes[index+i];
}
return result;
}
}
}
22 changes: 22 additions & 0 deletions PRMasterServer/Data/NatNegPeer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace PRMasterServer.Data
{
public class NatNegPeer
{
public IPEndPoint PublicAddress;
public IPEndPoint CommunicationAddress;
public bool IsHost;
}

public class NatNegClient
{
public int ClientId;
public NatNegPeer Host;
public NatNegPeer Guest;
}
}
Loading