Equivalent of isSiteLocalAddress in C#

212 views Asked by At

In java there is a method inetaddress.isSiteLocalAddrress to determine if the IP address is in the same range as that of site, do we have any equivalent method in C#

1

There are 1 answers

2
Rami Sakr On BEST ANSWER
private bool isIPLocal(IPAddress ipaddress)
{
    String[] straryIPAddress = ipaddress.ToString().Split(new String[] { "." }, StringSplitOptions.RemoveEmptyEntries);
    int[] iaryIPAddress = new int[] { int.Parse(straryIPAddress[0]), int.Parse(straryIPAddress[1]), int.Parse(straryIPAddress[2]), int.Parse(straryIPAddress[3]) };
    if (iaryIPAddress[0] == 10 || (iaryIPAddress[0] == 192 && iaryIPAddress[1] == 168) || (iaryIPAddress[0] == 172 && (iaryIPAddress[1] >= 16 && iaryIPAddress[1] <= 31)))
    {
        return true;
    }
    else
    {
        // IP Address is "probably" public. This doesn't catch some VPN ranges like OpenVPN and Hamachi.
        return false;
    }
}