From 07fcee15f67b262b613d9fa6d8be5d1f3a2091be Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 28 Jul 2024 17:16:34 +0000 Subject: [PATCH] improvement: Resolve hostnames when comparing ip addrs So we can to configure pubsub routes by hostname or ipv4 (ipv6 hasn't been tested yet...so we should assume it doesn't work). --- src/DataStreamSuite/Bts.Net/Bts.Net.csproj | 9 ++ src/DataStreamSuite/Bts.Net/HostComparer.cs | 25 +++++ .../Bts.Net/IpAddressComparer.cs | 28 ++++++ .../Bts.Net/IpHostEntryJsonConverter.cs | 33 +++++++ .../Bts.Net/IpHostEntryJsonSerializer.cs | 39 ++++++++ src/DataStreamSuite/Bts.Net/IpUtils.cs | 94 +++++++++++++++++++ src/DataStreamSuite/DataStreamSuite.sln | 6 ++ .../AsyncTCPConnector_AsyncWrapper.cs | 4 +- src/DataStreamSuite/PubSub/PubSub.csproj | 1 + 9 files changed, 238 insertions(+), 1 deletion(-) create mode 100644 src/DataStreamSuite/Bts.Net/Bts.Net.csproj create mode 100644 src/DataStreamSuite/Bts.Net/HostComparer.cs create mode 100644 src/DataStreamSuite/Bts.Net/IpAddressComparer.cs create mode 100644 src/DataStreamSuite/Bts.Net/IpHostEntryJsonConverter.cs create mode 100644 src/DataStreamSuite/Bts.Net/IpHostEntryJsonSerializer.cs create mode 100644 src/DataStreamSuite/Bts.Net/IpUtils.cs diff --git a/src/DataStreamSuite/Bts.Net/Bts.Net.csproj b/src/DataStreamSuite/Bts.Net/Bts.Net.csproj new file mode 100644 index 000000000..cfadb03dd --- /dev/null +++ b/src/DataStreamSuite/Bts.Net/Bts.Net.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/src/DataStreamSuite/Bts.Net/HostComparer.cs b/src/DataStreamSuite/Bts.Net/HostComparer.cs new file mode 100644 index 000000000..9738fc774 --- /dev/null +++ b/src/DataStreamSuite/Bts.Net/HostComparer.cs @@ -0,0 +1,25 @@ +namespace Bts.Net; + +using System.Linq; +using System.Net; + +/// +/// Provides facilities to compare hosts for equality. +/// +/// This class doesn't implement IEqualityComparer because it cannot guarentee consistent hash codes +/// for equivalent host strings. The equals impl relies on Dns.GetHostAddresses, which is non-determinstic +/// (IEqualityComparer implementations that depends on mutable properties face a similar problem). +/// +/// +public class HostComparer { + public static bool Equals(string? x, string? y) { + if (x is null) { + return y is null; + } + if (y is null) { + return false; + } + var comp = new IpAddressComparer(); + return Dns.GetHostAddresses(x).Intersect(Dns.GetHostAddresses(y), comp).Any(); + } +} diff --git a/src/DataStreamSuite/Bts.Net/IpAddressComparer.cs b/src/DataStreamSuite/Bts.Net/IpAddressComparer.cs new file mode 100644 index 000000000..db4cc30fb --- /dev/null +++ b/src/DataStreamSuite/Bts.Net/IpAddressComparer.cs @@ -0,0 +1,28 @@ +namespace Bts.Net; + +using System.Diagnostics.CodeAnalysis; +using System.Net; + +/// +/// Compares ip addresses. Unlike the default comparer, v4 and equivalent v6 addresses are +/// considered equal. +/// +public class IpAddressComparer : IEqualityComparer { + public bool Equals(IPAddress? x, IPAddress? y) { + if (x is null) { + return y is null; + } + if (y is null) { + return false; + } + return x.Equals(y) || + (x.IsIPv4MappedToIPv6 && x.MapToIPv4().Equals(y)) || + (y.IsIPv4MappedToIPv6 && y.MapToIPv4().Equals(x)); + } + + public int GetHashCode([DisallowNull] IPAddress address) => + address.IsIPv4MappedToIPv6 + // Return consistent hash code for v4 and equivalent v6 + ? address.MapToIPv4().GetHashCode() + : address.GetHashCode(); +} diff --git a/src/DataStreamSuite/Bts.Net/IpHostEntryJsonConverter.cs b/src/DataStreamSuite/Bts.Net/IpHostEntryJsonConverter.cs new file mode 100644 index 000000000..43cf1e90c --- /dev/null +++ b/src/DataStreamSuite/Bts.Net/IpHostEntryJsonConverter.cs @@ -0,0 +1,33 @@ +namespace Bts.Net; + +using System; +using System.Net; +using System.Text.Json; +using System.Text.Json.Serialization; + +public class IpHostEntryJsonConverter : JsonConverter { + public override IPHostEntry? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { + throw new NotImplementedException(); + } + + public override void Write(Utf8JsonWriter writer, IPHostEntry value, JsonSerializerOptions options) { + writer.WriteStartObject(); + // HostName + writer.WriteString(nameof(value.HostName), value.HostName); + // Aliases + writer.WritePropertyName("Aliases"); + writer.WriteStartArray(); + foreach (var alias in value.Aliases) { + writer.WriteStringValue(alias); + } + writer.WriteEndArray(); + // Addresses + writer.WritePropertyName("Addresses"); + writer.WriteStartArray(); + foreach (var address in value.AddressList) { + writer.WriteStringValue(address.ToString()); + } + writer.WriteEndArray(); + writer.WriteEndObject(); + } +} diff --git a/src/DataStreamSuite/Bts.Net/IpHostEntryJsonSerializer.cs b/src/DataStreamSuite/Bts.Net/IpHostEntryJsonSerializer.cs new file mode 100644 index 000000000..918a02200 --- /dev/null +++ b/src/DataStreamSuite/Bts.Net/IpHostEntryJsonSerializer.cs @@ -0,0 +1,39 @@ +namespace Bts.Net; + +using System; +using System.IO; +using System.Net; +using System.Text; +using System.Text.Json; + +/// +/// This class seriailzes IpHostEntry to json (string). A common buffer is used to reduce memory +/// allocations on repeated calls to . For that reason, this class is NOT +/// thread safe. +/// +public class IpHostEntryJsonSerializer { + readonly MemoryStream buffer = new(); + readonly IpHostEntryJsonConverter converter; + + public IpHostEntryJsonSerializer(IpHostEntryJsonConverter? converter = null) => + this.converter = converter ?? new() { }; + + public string? Serialize(IPHostEntry dp, JsonSerializerOptions? opts = null) { + opts ??= JsonSerializerOptions.Default; + buffer.Position = 0; + buffer.SetLength(0); + using (var jsonWriter = new Utf8JsonWriter(buffer)) { + converter.Write(jsonWriter, dp, opts); + } + var bytes = buffer.GetBuffer(); + var span = new ArraySegment(bytes, 0, (int)buffer.Length); + return Encoding.UTF8.GetString(span); + } + + public void Serialize(IPHostEntry dp, Utf8JsonWriter jsonWriter, JsonSerializerOptions? opts = null) { + opts ??= JsonSerializerOptions.Default; + buffer.Position = 0; + buffer.SetLength(0); + converter.Write(jsonWriter, dp, opts); + } +} diff --git a/src/DataStreamSuite/Bts.Net/IpUtils.cs b/src/DataStreamSuite/Bts.Net/IpUtils.cs new file mode 100644 index 000000000..5bc803642 --- /dev/null +++ b/src/DataStreamSuite/Bts.Net/IpUtils.cs @@ -0,0 +1,94 @@ +namespace Bts.Net; + +using System.Diagnostics.CodeAnalysis; +using System.Net; +using System.Text.RegularExpressions; + +public static partial class IpUtils { + static readonly Regex ipv4Regex = new(string.Join('.', @"(((0|1)\d)|(2\d)|\d)?\d")); + + // public static void ParseUri(string str, out string? scheme, out string? host, out string? port, out string? path, out string? queryParams) { + // if (Uri.TryCreate(str, UriKind.Absolute, out var uri)) { + // } + // } + + // public static bool AreEndpointsEqual(Uri? x, Uri? y) { + // if (x is null) { + // return y is null; + // } + // if (y is null) { + // return false; + // } + // if (x.IsAbsoluteUri != y.IsAbsoluteUri) { + // return false; + // } + // if (x.IsAbsoluteUri) { + // if (x.Scheme != y.Scheme) { + // return false; + // } + // } + // string? xHost = null; + // } + + // static string ResolveHostIpv4(Uri uri) { + // // switch (uri.HostNameType) { + // // case UriHostNameType.IPv4: + // // return uri.Host; + // // case UriHostNameType.Dns: + // // break; + // // case UriHostNameType.IPv6: + // // var addresses = Dns.GetHostAddresses(uri.Host); + // // foreach (var address in addresses) { + // // if (address.IsIPv4MappedToIPv6) { + // // return address.MapToIPv4().Address; + // // } + // // } + // // break; + // // } + // var hostAddresses = Dns.GetHostAddresses(uri.Host); + // foreach (IPAddress address in hostAddresses) { + // if (address.Equals(remoteEndPoint.Address) || + // (address.IsIPv4MappedToIPv6 && address.MapToIPv4().Equals(remoteEndPoint.Address)) || + // (remoteEndPoint.Address.IsIPv4MappedToIPv6 && remoteEndPoint.Address.MapToIPv4().Equals(address))) { + // isMatch = true; + // break; + // } + // } + // } + + // public static bool AreEndpointsEqual(string? x, string? y) { + // if (x == y) { + // return true; + // } + // if (x is null) { + // return y is null; + // } + // if (y is null) { + // return false; + // } + // var xUri = new Uri(x); + // var yUri = new Uri(y); + // // return xUri.Equals(yUri); + // } + + public static bool IsIpv4(string host) { + host = removeSchemaFromUrl(host); + host = removePortFromUrl(host); + return ipv4Regex.IsMatch(host); + } + + static string removeSchemaFromUrl(string url) { + var i = url.IndexOf("://"); + return i < 0 ? url : url.Substring(i + 3); + } + + static string removePortFromUrl(string url) { + var match = MyRegex().Match(url); + return match.Success + ? string.Concat(url.AsSpan(0, match.Index), url.AsSpan(match.Index + match.Length)) + : url; + } + + [GeneratedRegex(":\\d{1,4}")] + private static partial Regex MyRegex(); +} diff --git a/src/DataStreamSuite/DataStreamSuite.sln b/src/DataStreamSuite/DataStreamSuite.sln index 988f3a279..842f7660d 100644 --- a/src/DataStreamSuite/DataStreamSuite.sln +++ b/src/DataStreamSuite/DataStreamSuite.sln @@ -44,6 +44,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "spark_websocket", "..\clien EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bts.Extensions.Hosting", "Bts.Extensions.Hosting\Bts.Extensions.Hosting.csproj", "{5C528A65-E43C-48A2-8CA9-ED2D0E356A37}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bts.Net", "Bts.Net\Bts.Net.csproj", "{D92E1456-91C1-43FA-AA87-67BFE663944F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -137,5 +139,9 @@ Global {5C528A65-E43C-48A2-8CA9-ED2D0E356A37}.Debug|Any CPU.Build.0 = Debug|Any CPU {5C528A65-E43C-48A2-8CA9-ED2D0E356A37}.Release|Any CPU.ActiveCfg = Release|Any CPU {5C528A65-E43C-48A2-8CA9-ED2D0E356A37}.Release|Any CPU.Build.0 = Release|Any CPU + {D92E1456-91C1-43FA-AA87-67BFE663944F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D92E1456-91C1-43FA-AA87-67BFE663944F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D92E1456-91C1-43FA-AA87-67BFE663944F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D92E1456-91C1-43FA-AA87-67BFE663944F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnector_AsyncWrapper.cs b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnector_AsyncWrapper.cs index 6995ba8dd..1025e12e3 100644 --- a/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnector_AsyncWrapper.cs +++ b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnector_AsyncWrapper.cs @@ -1,3 +1,5 @@ +using Bts.Net; + using System.Net.Sockets; using System.Net; @@ -42,7 +44,7 @@ namespace bts { ITCPConnection conn = _create_connection(tcp_client); var conn_rci = conn.get_peer_address(); bts.Log.Gen.info($"AsyncTCPConnector_AsyncWrapper._connect_to new connection, server_rci: {server_rci} connection.peer_address: {conn_rci}"); - Log.assert(server_rci.Equals(conn_rci), $"{server_rci}!={conn_rci}"); + Log.assert(HostComparer.Equals(server_rci.ip, conn_rci.ip), $"{server_rci}!={conn_rci}"); bts.Log.Gen.info($"AsyncTCPConnector_AsyncWrapper._connect_to new connection, self: {conn.get_self_address()} peer: {conn.get_peer_address()}, calling onConnection now"); handler.onConnection(conn); return true; diff --git a/src/DataStreamSuite/PubSub/PubSub.csproj b/src/DataStreamSuite/PubSub/PubSub.csproj index 303275917..2c82abef2 100644 --- a/src/DataStreamSuite/PubSub/PubSub.csproj +++ b/src/DataStreamSuite/PubSub/PubSub.csproj @@ -6,6 +6,7 @@ +