diff --git a/src/DataStreamSuite/Bts.Net/HostComparer.cs b/src/DataStreamSuite/Bts.Net/HostComparer.cs
index 9738fc774..0b4590247 100644
--- a/src/DataStreamSuite/Bts.Net/HostComparer.cs
+++ b/src/DataStreamSuite/Bts.Net/HostComparer.cs
@@ -6,12 +6,12 @@ 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).
+/// This class doesn't implement IEqualityComparer because a consistent hash code cannot be
+/// guarenteed for equivalent host strings. The equals impl relies on Dns.GetHostAddresses, which
+/// is non-determinstic (implementations that depends on mutable properties face a similar problem).
///
///
-public class HostComparer {
+public class HostComparer {
public static bool Equals(string? x, string? y) {
if (x is null) {
return y is null;
@@ -19,7 +19,10 @@ public class HostComparer {
if (y is null) {
return false;
}
- var comp = new IpAddressComparer();
- return Dns.GetHostAddresses(x).Intersect(Dns.GetHostAddresses(y), comp).Any();
+ return Dns.GetHostAddresses(x).Intersect(
+ Dns.GetHostAddresses(y), IpAddressComparer.Instance).Any();
}
+
+ public static bool Equals(IPHostEntry x, IPHostEntry y) =>
+ x.AddressList.Intersect(y.AddressList, IpAddressComparer.Instance).Any();
}
diff --git a/src/DataStreamSuite/Bts.Net/IpAddressComparer.cs b/src/DataStreamSuite/Bts.Net/IpAddressComparer.cs
index db4cc30fb..ba06bc759 100644
--- a/src/DataStreamSuite/Bts.Net/IpAddressComparer.cs
+++ b/src/DataStreamSuite/Bts.Net/IpAddressComparer.cs
@@ -8,6 +8,8 @@ using System.Net;
/// considered equal.
///
public class IpAddressComparer : IEqualityComparer {
+ public static IEqualityComparer Instance { get; } = new IpAddressComparer();
+
public bool Equals(IPAddress? x, IPAddress? y) {
if (x is null) {
return y is null;
diff --git a/src/DataStreamSuite/PubSub/Client/Communication/ManagedTCP/PubSubServerManagedTCPConnection.cs b/src/DataStreamSuite/PubSub/Client/Communication/ManagedTCP/PubSubServerManagedTCPConnection.cs
index e47783055..d0aba735b 100644
--- a/src/DataStreamSuite/PubSub/Client/Communication/ManagedTCP/PubSubServerManagedTCPConnection.cs
+++ b/src/DataStreamSuite/PubSub/Client/Communication/ManagedTCP/PubSubServerManagedTCPConnection.cs
@@ -14,6 +14,7 @@ using System.Reflection.Metadata.Ecma335;
using Bts.PubSub;
using Microsoft.Win32;
using System.ComponentModel;
+using Bts.Net;
namespace BCG {
///
@@ -193,7 +194,13 @@ namespace BCG {
public void onConnection(ITCPConnection connection) {
var server_rci = connection.get_peer_address();
bts.Log.Gen.info($"PubSubServerManagedTCPConnection.onConnection connection: {_connection_txt(connection)}");
- Log.assert(_server_rc_info.Equals(server_rci), $"Remote endpoint differs from expected. expected: {_server_rc_info} actual: {server_rci}");
+ Log.assert(_server_rc_info.port == server_rci.port);
+ if (!_server_rc_info.Equals(server_rci)) {
+ Log.assert(_server_rc_info.IsEquivalent(server_rci), $"Remote endpoint differs from expected. expected: {_server_rc_info} actual: {server_rci}");
+ var matchAddress = _server_rc_info.GetCommonAddresses(server_rci).FirstOrDefault();
+ var matchRci = new RemoteConnectionInfo(matchAddress!.ToString(), _server_rc_info.port);
+ Log.Gen.warn($"Remote host differs from expected. Expected: {_server_rc_info}, Actual: {server_rci}, Resolved: {matchRci}");
+ }
// _last_hb = DateTime.Now;
// _new_connection = connection;
_conn_cb_queue.enqueue(new ConnectionStatusChange(ConnectionStatusChange.Status.CONNECTED, connection));
diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/RemoteConnectionInfo.cs b/src/DataStreamSuite/PubSub/ManagedTCP/RemoteConnectionInfo.cs
index 4e3799100..5bfcd6dee 100644
--- a/src/DataStreamSuite/PubSub/ManagedTCP/RemoteConnectionInfo.cs
+++ b/src/DataStreamSuite/PubSub/ManagedTCP/RemoteConnectionInfo.cs
@@ -5,9 +5,11 @@ using System.Net;
using bts.utils;
using System.Net.NetworkInformation;
using bts;
+using Bts.Net;
+using System.Linq;
namespace BCG {
- public class RemoteConnectionInfo {
+ public class RemoteConnectionInfo : IEquatable {
private string _ip;
private int _port;
//private IPEndPoint _ip_endpoint;
@@ -60,6 +62,41 @@ namespace BCG {
// get { return _ip_endpoint; }
// }
+ public bool IsEquivalentHost(RemoteConnectionInfo other) =>
+ HostComparer.Equals(Dns.GetHostEntry(ip), Dns.GetHostEntry(other.ip));
+
+ public IEnumerable GetCommonAddresses(RemoteConnectionInfo other) {
+ var addresses = Dns.GetHostAddresses(ip);
+ var otherAddresses = Dns.GetHostAddresses(other.ip);
+ return addresses.Intersect(otherAddresses, IpAddressComparer.Instance);
+ }
+
+ ///
+ /// Determines whether or not another RemoteConnectionInfo refers to the same host and port
+ /// as this (one host may be associated with multiple ip addresses and/or host names).
+ ///
+ /// The RemoteConnectionInfo to compare (with this one)
+ ///
+ /// True if the other RemoteConnectionInfo is equivalent (to this one), otherwise false.
+ ///
+ public bool IsEquivalent(RemoteConnectionInfo other) =>
+ Equals(other) || (IsEquivalentHost(other) && port == other.port);
+
+ public RemoteConnectionInfo? ResolveHost(RemoteConnectionInfo other) {
+ if (other.ip == ip) {
+ return new(ip, port);
+ }
+ var otherHostEntry = Dns.GetHostEntry(other.ip);
+ var comp = new IpAddressComparer();
+ if (Dns.GetHostAddresses(ip).Intersect(otherHostEntry.AddressList, comp).Any()) {
+ return new(otherHostEntry.HostName, port);
+ }
+ return null;
+ }
+
+ public bool Equals(RemoteConnectionInfo? other) =>
+ other is not null && ip == other.ip && port == other.port;
+
public override string ToString() {
//return ip + ":" + port;
return $"{prefix}{ip}:{port}";
diff --git a/src/DataStreamSuite/PubSub/Misc/SysInfoManager.cs b/src/DataStreamSuite/PubSub/Misc/SysInfoManager.cs
index 40c89351a..96e805553 100644
--- a/src/DataStreamSuite/PubSub/Misc/SysInfoManager.cs
+++ b/src/DataStreamSuite/PubSub/Misc/SysInfoManager.cs
@@ -1,3 +1,5 @@
+using bts;
+
using System;
//using System.Windows.Forms;
using System.Collections;
@@ -44,12 +46,15 @@ namespace BCG.Utilities.General
{
// TODOds
- string hostName = Dns.GetHostName();
-
- // Get the IP addresses associated with the host name
- IPAddress[] ipAddresses = Dns.GetHostAddresses(hostName);
- return ipAddresses[0].ToString();
-
+ try {
+ string hostName = Dns.GetHostName();
+ // Get the IP addresses associated with the host name
+ IPAddress[] ipAddresses = Dns.GetHostAddresses(hostName);
+ return ipAddresses[0].ToString();
+ } catch (Exception ex) {
+ Log.assert(false, $"Dns.GetHostName threw: {ex}");
+ return string.Empty;
+ }
// string cName = SystemInformation.ComputerName;
// IPAddress[] ipas;