;

How to Check if the Given IPv6 IP Address is Local or Not in C#


Tutorialsrack 31/07/2020 C#

In this article, you will learn how to check if a given IPv6 IP address is local or not in C#. 

So first you need to import the namespaces System.Net and System.Net.Sockets. Then after we check if it is a valid IPv6 address or not. If it is a valid IPv6 IP address, then check for it is a Local IP address or not.

Here is the source code of the program to check if a given IPv6 IP address is local or not.

How to Check if the Given IPv6 IP Address is Local or Not in C#
using System;
using System.Net;
using System.Net.Sockets;

namespace Tutorialsrack
{
    class Program
    {
        /* How to Check if the Given IPv6 IP Address is Local or Not in C# */
        static void Main(string[] args)
        {
            IsLocalIPv6(IPAddress.Parse("fe80::1c09:4f6f:5215:c864%13"));
            IsLocalIPv6(IPAddress.Parse("127.0.0.1"));
            IsLocalIPv6(IPAddress.Parse("5"));
            IsLocalIPv6(IPAddress.Parse("2001:0db8:85a3:0000:0000:8a2e:0370:7334"));
            IsLocalIPv6(IPAddress.Parse("::1"));

            //Hit ENTER to exit the program
            Console.ReadKey();
        }

        public static void IsLocalIPv6(IPAddress address)
        {
            bool flag = false;
            // Make sure we are dealing with an IPv6 address
            if (address.AddressFamily != AddressFamily.InterNetworkV6)
            {
                Console.WriteLine("The Given IP Address is Not a Valid IPv6 IP Address");
            }
            else { 

            var addressAsString = address.ToString();
            var firstWord = addressAsString.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries)[0];

                // equivalent of 127.0.0.1 in IPv6
                if (addressAsString == "::1")
                {
                    flag= true;
                }
                // The original IPv6 Site Local addresses (fec0::/10) are deprecated. Unfortunately IsIPv6SiteLocal only checks for the original deprecated version:
                else if (address.IsIPv6SiteLocal)
                {
                    flag= true;
                }
                // These days Unique Local Addresses (ULA) are used in place of Site Local. 
                // ULA has two variants: 
                //      fc00::/8 is not defined yet, but might be used in the future for internal-use addresses that are registered in a central place (ULA Central). 
                //      fd00::/8 is in use and does not have to registered anywhere.
                else if (firstWord.Length >= 4 && firstWord.Substring(0, 2) == "fc")
                {
                    flag= true;
                }
                else if (firstWord.Length >= 4 && firstWord.Substring(0, 2) == "fd")
                {
                    flag= true;
                }
                // Link local addresses (prefixed with fe80) are not routable
                else if (firstWord == "fe80")
                {
                    flag= true;
                }
                // Discard Prefix
                else if (firstWord == "100")
                {
                    flag= true;
                }
                else
                {
                    // Any other IP address is not Unique Local Address (ULA)
                    flag= false;
                }

                if (flag)
                {
                    Console.WriteLine("{0} is a Local IPv6 IP Address", address);
                }
                else
                {
                    Console.WriteLine("{0} is not a Local IPv6 IP Address", address);
                }
            }
            
        }

    }
}
Output

fe80::1c09:4f6f:5215:c864%13 is a Local IPv6 IP Address

The Given IP Address is Not a Valid IPv6 IP Address

The Given IP Address is Not a Valid IPv6 IP Address

2001:db8:85a3::8a2e:370:7334 is not a Local IPv6 IP Address

::1 is a Local IPv6 IP Address

I hope this article will help you to understand how to check if a given IPv6 IP address is local or not in C#.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags