namespace Minecraft.Server.FourKit.Net;
///
/// Represents an Internet Protocol (IP) address.
///
public class InetAddress
{
private readonly string _hostAddress;
internal InetAddress(string hostAddress)
{
_hostAddress = hostAddress ?? string.Empty;
}
///
/// Returns the IP address string in textual presentation.
///
/// The IP address as a string.
public string getHostAddress() => _hostAddress;
///
/// Gets the host name for this IP address.
/// For this implementation, returns the IP address string.
///
/// The host name.
public string getHostName() => _hostAddress;
///
/// Returns the raw IP address of this InetAddress object as bytes.
///
/// The raw IP address bytes, or an empty array if parsing fails.
public byte[] getAddress()
{
if (System.Net.IPAddress.TryParse(_hostAddress, out var ip))
return ip.GetAddressBytes();
return [];
}
///
/// Checks whether this is a loopback address (127.x.x.x or ::1).
///
/// true if this is a loopback address.
public bool isLoopbackAddress()
{
if (System.Net.IPAddress.TryParse(_hostAddress, out var ip))
return System.Net.IPAddress.IsLoopback(ip);
return false;
}
///
/// Checks whether this is a site-local (private) address.
///
/// true if this is a site-local address.
public bool isSiteLocalAddress()
{
if (!System.Net.IPAddress.TryParse(_hostAddress, out var ip))
return false;
byte[] bytes = ip.GetAddressBytes();
if (bytes.Length != 4) return false;
// 10.x.x.x, 172.16-31.x.x, 192.168.x.x
if (bytes[0] == 10) return true;
if (bytes[0] == 172 && bytes[1] >= 16 && bytes[1] <= 31) return true;
if (bytes[0] == 192 && bytes[1] == 168) return true;
return false;
}
///
/// Checks whether this is a link-local address (169.254.x.x).
///
/// true if this is a link-local address.
public bool isLinkLocalAddress()
{
if (!System.Net.IPAddress.TryParse(_hostAddress, out var ip))
return false;
byte[] bytes = ip.GetAddressBytes();
if (bytes.Length != 4) return false;
return bytes[0] == 169 && bytes[1] == 254;
}
///
/// Checks whether this is a multicast address (224-239.x.x.x).
///
/// true if this is a multicast address.
public bool isMulticastAddress()
{
if (!System.Net.IPAddress.TryParse(_hostAddress, out var ip))
return false;
byte[] bytes = ip.GetAddressBytes();
if (bytes.Length != 4) return false;
return bytes[0] >= 224 && bytes[0] <= 239;
}
///
/// Checks whether this is the wildcard (any) address (0.0.0.0).
///
/// true if this is the wildcard address.
public bool isAnyLocalAddress()
{
if (System.Net.IPAddress.TryParse(_hostAddress, out var ip))
return ip.Equals(System.Net.IPAddress.Any) || ip.Equals(System.Net.IPAddress.IPv6Any);
return false;
}
///
public override string ToString() => _hostAddress;
}