From e15ed963d83d11c8263e863e02eba94c9816cb3b Mon Sep 17 00:00:00 2001 From: krrios Date: Wed, 25 Apr 2007 00:42:47 +0000 Subject: [PATCH] updated a few places to use NetState.Address rather than Socket.RemoteEndPoint.Address added Utility.Intern(IPAddress) and implemented where appropriate --- Scripts/Accounting/Account.cs | 7 ++--- Scripts/Accounting/AccountHandler.cs | 2 +- Scripts/Commands/Generic/Commands/Commands.cs | 2 +- Scripts/Commands/Handlers.cs | 30 ------------------- Scripts/Engines/Factions/Core/Election.cs | 2 +- Scripts/Misc/ServerList.cs | 4 +-- Scripts/Misc/ShardPoller.cs | 2 +- Server/Network/NetState.cs | 2 +- Server/Utility.cs | 21 +++++++++++++ 9 files changed, 29 insertions(+), 43 deletions(-) diff --git a/Scripts/Accounting/Account.cs b/Scripts/Accounting/Account.cs index aa4094488..b88733823 100644 --- a/Scripts/Accounting/Account.cs +++ b/Scripts/Accounting/Account.cs @@ -740,7 +740,7 @@ namespace Server.Accounting if( IPAddress.TryParse( Utility.GetText( ip, null ), out address ) ) { - list[count] = address; + list[count] = Utility.Intern( address ); count++; } } @@ -881,10 +881,7 @@ namespace Server.Accounting return false; } - IPAddress ipAddress; - - try { ipAddress = ( (IPEndPoint) ns.Socket.RemoteEndPoint ).Address; } - catch { return false; } + IPAddress ipAddress = ns.Address; bool accessAllowed = ( m_IPRestrictions.Length == 0 || IPLimiter.IsExempt( ipAddress ) ); diff --git a/Scripts/Accounting/AccountHandler.cs b/Scripts/Accounting/AccountHandler.cs index 85464c6b8..bd267f5d9 100644 --- a/Scripts/Accounting/AccountHandler.cs +++ b/Scripts/Accounting/AccountHandler.cs @@ -120,7 +120,7 @@ namespace Server.Misc try { - IPAddress ipAddress = ((IPEndPoint)ns.Socket.RemoteEndPoint).Address; + IPAddress ipAddress = ns.Address; if ( Utility.IPMatchClassC( accessList[0], ipAddress ) ) { diff --git a/Scripts/Commands/Generic/Commands/Commands.cs b/Scripts/Commands/Generic/Commands/Commands.cs index 2e2bf9358..0e9368354 100644 --- a/Scripts/Commands/Generic/Commands/Commands.cs +++ b/Scripts/Commands/Generic/Commands/Commands.cs @@ -956,7 +956,7 @@ namespace Server.Commands.Generic try { - Firewall.Add( ((IPEndPoint)state.Socket.RemoteEndPoint).Address ); + Firewall.Add( state.Address ); AddResponse( "They have been firewalled." ); } catch ( Exception ex ) diff --git a/Scripts/Commands/Handlers.cs b/Scripts/Commands/Handlers.cs index 8ebb0bff4..cd7caff07 100644 --- a/Scripts/Commands/Handlers.cs +++ b/Scripts/Commands/Handlers.cs @@ -993,36 +993,6 @@ namespace Server.Commands e.Mobile.Target = new PickMoveTarget(); } - private class FirewallTarget : Target - { - public FirewallTarget() : base( -1, false, TargetFlags.None ) - { - } - - protected override void OnTarget( Mobile from, object targeted ) - { - if ( targeted is Mobile ) - { - Mobile targ = (Mobile)targeted; - - NetState state = targ.NetState; - - if ( state != null ) - { - CommandLogging.WriteLine( from, "{0} {1} firewalling {2}", from.AccessLevel, CommandLogging.Format( from ), CommandLogging.Format( targeted ) ); - - try - { - Firewall.Add( ((IPEndPoint)state.Socket.RemoteEndPoint).Address ); - } - catch - { - } - } - } - } - } - [Usage( "Save" )] [Description( "Saves the world." )] private static void Save_OnCommand( CommandEventArgs e ) diff --git a/Scripts/Engines/Factions/Core/Election.cs b/Scripts/Engines/Factions/Core/Election.cs index e66305061..64fab6e88 100644 --- a/Scripts/Engines/Factions/Core/Election.cs +++ b/Scripts/Engines/Factions/Core/Election.cs @@ -459,7 +459,7 @@ namespace Server.Factions case 0: { m_From = reader.ReadMobile(); - m_Address = reader.ReadIPAddress(); + m_Address = Utility.Intern( reader.ReadIPAddress() ); m_Time = reader.ReadDateTime(); break; diff --git a/Scripts/Misc/ServerList.cs b/Scripts/Misc/ServerList.cs index 297442fad..b616e6fa6 100644 --- a/Scripts/Misc/ServerList.cs +++ b/Scripts/Misc/ServerList.cs @@ -74,9 +74,7 @@ namespace Server.Misc private static bool IsLocalMachine( NetState state ) { - Socket sock = state.Socket; - - IPAddress theirAddress = ((IPEndPoint)sock.RemoteEndPoint).Address; + IPAddress theirAddress = state.Address; if ( IPAddress.IsLoopback( theirAddress ) ) return true; diff --git a/Scripts/Misc/ShardPoller.cs b/Scripts/Misc/ShardPoller.cs index 6cf845ad7..582daf9fc 100644 --- a/Scripts/Misc/ShardPoller.cs +++ b/Scripts/Misc/ShardPoller.cs @@ -377,7 +377,7 @@ namespace Server.Misc m_Voters = new IPAddress[reader.ReadInt()]; for ( int i = 0; i < m_Voters.Length; ++i ) - m_Voters[i] = reader.ReadIPAddress(); + m_Voters[i] = Utility.Intern( reader.ReadIPAddress() ); break; } diff --git a/Server/Network/NetState.cs b/Server/Network/NetState.cs index c2e3dbc8b..1fe7dcbc2 100644 --- a/Server/Network/NetState.cs +++ b/Server/Network/NetState.cs @@ -515,7 +515,7 @@ namespace Server.Network { m_Instances.Add( this ); try { - m_Address = ( ( IPEndPoint ) m_Socket.RemoteEndPoint ).Address; + m_Address = Utility.Intern( ( ( IPEndPoint ) m_Socket.RemoteEndPoint ).Address ); m_ToString = m_Address.ToString(); } catch { m_Address = IPAddress.None; diff --git a/Server/Utility.cs b/Server/Utility.cs index 3b6741f18..55e692fc4 100644 --- a/Server/Utility.cs +++ b/Server/Utility.cs @@ -82,6 +82,27 @@ namespace Server str = Intern( str ); } + private static Dictionary _ipAddressTable; + + public static IPAddress Intern( IPAddress ipAddress ) { + if ( _ipAddressTable == null ) { + _ipAddressTable = new Dictionary(); + } + + IPAddress interned; + + if ( !_ipAddressTable.TryGetValue( ipAddress, out interned ) ) { + interned = ipAddress; + _ipAddressTable[ipAddress] = interned; + } + + return interned; + } + + public static void Intern( ref IPAddress ipAddress ) { + ipAddress = Intern( ipAddress ); + } + public static bool IsValidIP( string text ) { bool valid = true;