From 835770772f2a97565119cf6620241cc16f25253b Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Wed, 21 Mar 2018 13:11:25 +0000 Subject: [PATCH] IPAddress Cleanup --- .../Client/ServerlistTest.cs | 18 ++++++++++++++++ Facepunch.Steamworks/Interfaces/Inventory.cs | 1 - Facepunch.Steamworks/Server.cs | 6 ++++-- Facepunch.Steamworks/Server/ServerInit.cs | 3 ++- Facepunch.Steamworks/Utility.cs | 21 +++++++++---------- 5 files changed, 34 insertions(+), 15 deletions(-) diff --git a/Facepunch.Steamworks.Test/Client/ServerlistTest.cs b/Facepunch.Steamworks.Test/Client/ServerlistTest.cs index bba5e01..6fa37a0 100644 --- a/Facepunch.Steamworks.Test/Client/ServerlistTest.cs +++ b/Facepunch.Steamworks.Test/Client/ServerlistTest.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Text; using System.Threading; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -12,6 +13,23 @@ namespace Facepunch.Steamworks.Test [DeploymentItem( "steam_api64.dll" )] public partial class ServerList { + [TestMethod] + public void IpAddressConversions() + { + var ipstr = "185.38.150.40"; + var ip = IPAddress.Parse( ipstr ); + + var ip_int = Facepunch.Steamworks.Utility.IpToInt32( ip ); + + var ip_back = Facepunch.Steamworks.Utility.Int32ToIp( ip_int ); + + Console.WriteLine( "ipstr: " + ipstr ); + Console.WriteLine( "ip: " + ip ); + Console.WriteLine( "ip int: " + ip_int ); + Console.WriteLine( "ip_back: " + ip_back ); + } + + [TestMethod] public void InternetList() { diff --git a/Facepunch.Steamworks/Interfaces/Inventory.cs b/Facepunch.Steamworks/Interfaces/Inventory.cs index bc00919..eff2da8 100644 --- a/Facepunch.Steamworks/Interfaces/Inventory.cs +++ b/Facepunch.Steamworks/Interfaces/Inventory.cs @@ -70,7 +70,6 @@ namespace Facepunch.Steamworks /// private void onDefinitionsUpdated( SteamInventoryDefinitionUpdate_t obj ) { - Console.WriteLine( "onDefinitionsUpdated" ); LoadDefinitions(); UpdatePrices(); diff --git a/Facepunch.Steamworks/Server.cs b/Facepunch.Steamworks/Server.cs index 1aca880..e2091c0 100644 --- a/Facepunch.Steamworks/Server.cs +++ b/Facepunch.Steamworks/Server.cs @@ -33,13 +33,15 @@ namespace Facepunch.Steamworks Instance = this; native = new Interop.NativeInterface(); + uint ipaddress = 0; // Any Port if ( init.SteamPort == 0 ) init.RandomSteamPort(); + if ( init.IpAddress != null ) ipaddress = Utility.IpToInt32( init.IpAddress ); // // Get other interfaces // - if ( !native.InitServer( this, init.IpAddress, init.SteamPort, init.GamePort, init.QueryPort, init.Secure ? 3 : 2, init.VersionString ) ) + if ( !native.InitServer( this, ipaddress, init.SteamPort, init.GamePort, init.QueryPort, init.Secure ? 3 : 2, init.VersionString ) ) { native.Dispose(); native = null; @@ -300,7 +302,7 @@ namespace Facepunch.Steamworks var ip = native.gameServer.GetPublicIP(); if ( ip == 0 ) return null; - return new System.Net.IPAddress( Utility.SwapBytes( ip ) ); + return Utility.Int32ToIp( ip ); } } diff --git a/Facepunch.Steamworks/Server/ServerInit.cs b/Facepunch.Steamworks/Server/ServerInit.cs index b4787a2..d4117ec 100644 --- a/Facepunch.Steamworks/Server/ServerInit.cs +++ b/Facepunch.Steamworks/Server/ServerInit.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Runtime.InteropServices; using System.Text; @@ -12,7 +13,7 @@ namespace Facepunch.Steamworks /// public class ServerInit { - public uint IpAddress = 0; + public IPAddress IpAddress; public ushort SteamPort; public ushort GamePort = 27015; public ushort QueryPort = 27016; diff --git a/Facepunch.Steamworks/Utility.cs b/Facepunch.Steamworks/Utility.cs index 7b048cc..bbd5764 100644 --- a/Facepunch.Steamworks/Utility.cs +++ b/Facepunch.Steamworks/Utility.cs @@ -6,25 +6,24 @@ using System.Text; namespace Facepunch.Steamworks { - internal static class Utility + public static class Utility { - static internal uint SwapBytes( uint x ) + static internal uint Swap( uint x ) { - return ( ( x & 0x000000ff ) << 24 ) + - ( ( x & 0x0000ff00 ) << 8 ) + - ( ( x & 0x00ff0000 ) >> 8 ) + - ( ( x & 0xff000000 ) >> 24 ); + return ((x & 0x000000ff) << 24) + + ((x & 0x0000ff00) << 8) + + ((x & 0x00ff0000) >> 8) + + ((x & 0xff000000) >> 24); } - - static internal uint IpToInt32( this IPAddress ipAddress ) + static public uint IpToInt32( this IPAddress ipAddress ) { - return BitConverter.ToUInt32( ipAddress.GetAddressBytes().Reverse().ToArray(), 0 ); + return Swap( (uint) ipAddress.Address ); } - static internal IPAddress Int32ToIp( uint ipAddress ) + static public IPAddress Int32ToIp( uint ipAddress ) { - return new IPAddress( BitConverter.GetBytes( ipAddress ).Reverse().ToArray() ); + return new IPAddress( Swap( ipAddress ) ); } static internal class Epoch