diag: Assert that local ip address is successfully obtained
This commit is contained in:
1 parent
4ade38ef9a
commit
d628f38bb1
5 files changed
+68
-14
No files matched your search
@@ -6,12 +6,12 @@ using System.Net;
|
||||
/// <summary>
|
||||
/// Provides facilities to compare hosts for equality.
|
||||
/// <para>
|
||||
/// 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).
|
||||
/// </para>
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
@@ -8,6 +8,8 @@ using System.Net;
|
||||
/// considered equal.
|
||||
/// </summary>
|
||||
public class IpAddressComparer : IEqualityComparer<IPAddress> {
|
||||
public static IEqualityComparer<IPAddress> Instance { get; } = new IpAddressComparer();
|
||||
|
||||
public bool Equals(IPAddress? x, IPAddress? y) {
|
||||
if (x is null) {
|
||||
return y is null;
|
||||
|
||||
+8
-1
@@ -14,6 +14,7 @@ using System.Reflection.Metadata.Ecma335;
|
||||
using Bts.PubSub;
|
||||
using Microsoft.Win32;
|
||||
using System.ComponentModel;
|
||||
using Bts.Net;
|
||||
|
||||
namespace BCG {
|
||||
/// <summary>
|
||||
@@ -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));
|
||||
|
||||
@@ -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<RemoteConnectionInfo> {
|
||||
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<IPAddress> GetCommonAddresses(RemoteConnectionInfo other) {
|
||||
var addresses = Dns.GetHostAddresses(ip);
|
||||
var otherAddresses = Dns.GetHostAddresses(other.ip);
|
||||
return addresses.Intersect(otherAddresses, IpAddressComparer.Instance);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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).
|
||||
/// </summary>
|
||||
/// <param name="other">The RemoteConnectionInfo to compare (with this one)</param>
|
||||
/// <returns>
|
||||
/// True if the other RemoteConnectionInfo is equivalent (to this one), otherwise false.
|
||||
/// </returns>
|
||||
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}";
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user