using System; using System.Collections.Generic; using System.Text; using System.Net; using bts.utils; using System.Net.NetworkInformation; using bts; using Bts.Net; using System.Linq; namespace BCG { public class RemoteConnectionInfo : IEquatable { private string _ip; private int _port; //private IPEndPoint _ip_endpoint; public static string ipv4_compat_prefix = "::ffff:"; public static RemoteConnectionInfo null_instance = new RemoteConnectionInfo(String.Empty, 0); public static string prefix = "bts-cloud://"; //IP addresses that start with ::ffff: are IPv6-mapped IPv4 addresses. This format is used to represent an IPv4 address within an IPv6 address space. public static string clean_ip(string ip) { string rv = ip; if (!String.IsNullOrEmpty(ip)) { if (ip.StartsWith(ipv4_compat_prefix)) { rv = ip.Substring(ipv4_compat_prefix.Length); } } return rv; } public RemoteConnectionInfo(string ip, int port) { _ip = clean_ip(ip); _port = port; Log.Gen.info($"RemoteConnectionInfo constructed ip_arg: {ip} _ip: {_ip} port: {_port}"); } public static RemoteConnectionInfo parse_url(string url) { string[] toks = url.Split("//", StringSplitOptions.RemoveEmptyEntries); var prefix = toks[0]; var ip_port = toks[1]; var ip_port_toks = toks[1].Split(':'); string ip = ip_port_toks[0]; int port = Int32.Parse(ip_port_toks[1]); return new RemoteConnectionInfo(ip, port); } // public RemoteConnectionInfo(string ip, int port, IPEndPoint ip_endpoint) // : this(ip, port) { // _ip_endpoint = ip_endpoint; // } public string ip { get { return _ip; } } public int port { get { return _port; } } // public IPEndPoint end_point { // // new IPEndPoint(IPAddress.Parse(server_address.ip), server_address.port); // 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}"; } public override int GetHashCode() { return ip.GetHashCode() ^ port.GetHashCode(); } public override bool Equals(object? obj) { if (obj == null) { return false; } RemoteConnectionInfo other = obj as RemoteConnectionInfo; return other != null && other.ip == ip && other.port == port; } } }