From f99fd3c15aa2a9fddcf73957b6a15ce95bf46b14 Mon Sep 17 00:00:00 2001 From: Katie Fisher Date: Thu, 25 May 2023 13:02:41 +0100 Subject: [PATCH] Making the isLocal function refer to reserved internal IP address values. This is because we require websocket functionality on mobile hotspots, and can't rely on DNS entries. --- websocket-sharp/Ext.cs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs index c25e89dca..58cbedf30 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -1499,6 +1499,9 @@ public static bool IsLocal (this System.Net.IPAddress address) return true; } + if (address.IsInternal()) + return true; + var name = System.Net.Dns.GetHostName (); var addrs = System.Net.Dns.GetHostAddresses (name); @@ -1510,6 +1513,30 @@ public static bool IsLocal (this System.Net.IPAddress address) return false; } + /// + /// An extension method to determine if an IP address is internal + /// Class A Private IP Range: 10.0.0.0 ? 10.255.255.255 + /// Class B Private IP Range: 172.16.0.0 ? 172.31.255.255 + /// Class C Private IP Range: 192.168.0.0 ? 192.168.255.25 + /// + /// The IP address that will be tested + /// Returns true if the IP is internal, false if it is external + public static bool IsInternal(this System.Net.IPAddress address) + { + byte[] bytes = address.GetAddressBytes(); + switch( bytes[ 0 ] ) + { + case 10: + return true; + case 172: + return bytes[ 1 ] < 32 && bytes[ 1 ] >= 16; + case 192: + return bytes[ 1 ] == 168; + default: + return false; + } + } + /// /// Determines whether the specified string is or /// an empty string.