// Copyright (C) 2015-2026 The Neo Project.
//
// Helper.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using System.Net;

namespace Neo.Plugins.OracleService;

static class Helper
{
    public static bool IsInternal(this IPHostEntry entry)
    {
        return entry.AddressList.Any(p => IsInternal(p));
    }

    /// <summary>
    ///       ::1          -   IPv6  loopback
    ///       10.0.0.0     -   10.255.255.255  (10/8 prefix)
    ///       127.0.0.0    -   127.255.255.255  (127/8 prefix)
    ///       172.16.0.0   -   172.31.255.255  (172.16/12 prefix)
    ///       192.168.0.0  -   192.168.255.255 (192.168/16 prefix)
    /// </summary>
    /// <param name="ipAddress">Address</param>
    /// <returns>True if it was an internal address</returns>
    public static bool IsInternal(this IPAddress ipAddress)
    {
        if (IPAddress.IsLoopback(ipAddress)) return true;
        if (IPAddress.Broadcast.Equals(ipAddress)) return true;
        if (IPAddress.Any.Equals(ipAddress)) return true;
        if (IPAddress.IPv6Any.Equals(ipAddress)) return true;
        if (IPAddress.IPv6Loopback.Equals(ipAddress)) return true;

        var ip = ipAddress.GetAddressBytes();
        return ip[0] switch
        {
            10 or 127 => true,
            172 => ip[1] >= 16 && ip[1] < 32,
            192 => ip[1] == 168,
            _ => false,
        };
    }
}
