计算一个子网内的所有地址...对于IPv6 [英] Calculating all addresses within a subnet...for IPv6

查看:524
本文介绍了计算一个子网内的所有地址...对于IPv6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过很多伟大的 C#示例其中演示如何在CIDR表示法提供的IPv4地址转换(如192.168.0.1/25)到其相关的范围(192.168.0.1 - 192.168.0.126)。我的程序需要能够做到这一点(来计算我的本地子网中的所有地址),但我想也支持IPv6。



如果我的C#程序具有所有我的典型IPCONFIG信息(IPv4地址,子网掩码,IPv6地址,链路本地V6地址,默认网关) - 我怎么会去我的本地子网中生成所有IPv6地址的列表,并将其输出到控制台<? / p>

解决方案

您可以使用eExNetworkLibrary.IP.IPAddressAnalysis类从的 eExNetworkLibrary



下面的代码适用于IPv4和IPv6(只是测试)。

 字符串〜应变=2001:DB8 :: / 120; 

//拆分为地址和前缀
串strAddress = strIn.Substring部分字符串(0,strIn.IndexOf('/'));
串strPrefix = strIn.Substring(strIn.IndexOf('/')+ 1);

INT iPrefix = Int32.Parse(strPrefix);
ip地址ip地址= IPAddress.Parse(strAddress);

//将前缀长度为一个有效的子网掩码

INT iMaskLength = 32;

如果(ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
{
iMaskLength = 128;
}

BitArray btArray =新BitArray(iMaskLength);
为(INT IC1 = 0; IC1< iMaskLength; IC1 ++)
{
//指数的计算是有点奇怪,因为你必须让你的主意字节顺序。
INT iIndex =(int)的((iMaskLength - IC1 - 1)/ 8)* 8 +(IC1%8);

如果(IC1≤(iMaskLength - iPrefix))
{
btArray.Set(iIndex,FALSE);
}
,否则
{
btArray.Set(iIndex,真);
}
}

字节[] = bMaskData新字节[iMaskLength / 8]。

btArray.CopyTo(bMaskData,0);

//创建子网掩码
子网掩码smMask =新子网掩码(bMaskData);

//获取IP范围
ip地址ipaStart = IPAddressAnalysis.GetClasslessNetworkAddress(ip地址,smMask);
ip地址ipaEnd = IPAddressAnalysis.GetClasslessBroadcastAddress(ip地址,smMask);

//忽略以下行,如果你的网络范围大
ip地址[] = ipaRange IPAddressAnalysis.GetIPRange(ipaStart,ipaEnd);

//调试输出
的foreach(在ipaRange ip地址IPA)
{
Console.WriteLine(ipa.ToString());
}

到Console.ReadLine();



<击>我不能完全肯定,如果我也做了转换,从前缀长度为包含字节数组子网掩码正确的,但是这个代码应该给你一个很好的起点。



修改:更新的代码位弯曲部分。可能是丑陋的,但是适用于这个例子。我想你一定能够找到一个更好的解决方案,如果你需要的。这些BitArrays是颈部疼痛。



请注意,产生IPv6网络的范围可以是一个非常内存/ CPU辛苦的任务,如果网络规模较大。


I have seen plenty of great C# examples which demonstrate how to convert IPv4 addresses provided in CIDR notation (e.g. 192.168.0.1/25) into their relevant ranges (192.168.0.1 - 192.168.0.126). My program needs to be able to do this (to compute all the addresses within my local subnet) but I want to also support IPv6.

If my C# program has all of my typical ipconfig information (IPv4 address, subnet mask, IPv6 address, link-local v6 address, default gateway) - how would I go about generating a list of all of the IPv6 addresses in my local subnet and outputting them to the console?

解决方案

You can use the eExNetworkLibrary.IP.IPAddressAnalysis class from the eExNetworkLibrary.

The following code works with IPv4 and IPv6 (just tested).

        string strIn = "2001:DB8::/120";

        //Split the string in parts for address and prefix
        string strAddress = strIn.Substring(0, strIn.IndexOf('/'));
        string strPrefix = strIn.Substring(strIn.IndexOf('/') + 1);

        int iPrefix = Int32.Parse(strPrefix);
        IPAddress ipAddress = IPAddress.Parse(strAddress);

        //Convert the prefix length to a valid SubnetMask

        int iMaskLength = 32;

        if(ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
        {
            iMaskLength = 128;
        }

        BitArray btArray = new BitArray(iMaskLength);
        for (int iC1 = 0; iC1 < iMaskLength; iC1++)
        {
            //Index calculation is a bit strange, since you have to make your mind about byte order.
            int iIndex = (int)((iMaskLength - iC1 - 1) / 8) * 8 + (iC1 % 8);

            if (iC1 < (iMaskLength - iPrefix))
            {
                btArray.Set(iIndex, false);
            }
            else
            {
                btArray.Set(iIndex, true);
            }
        }

        byte[] bMaskData = new byte[iMaskLength / 8];

        btArray.CopyTo(bMaskData, 0);

        //Create subnetmask
        Subnetmask smMask = new Subnetmask(bMaskData);

        //Get the IP range
        IPAddress ipaStart = IPAddressAnalysis.GetClasslessNetworkAddress(ipAddress, smMask);
        IPAddress ipaEnd = IPAddressAnalysis.GetClasslessBroadcastAddress(ipAddress, smMask);

        //Omit the following lines if your network range is large
        IPAddress[] ipaRange = IPAddressAnalysis.GetIPRange(ipaStart, ipaEnd);

        //Debug output
        foreach (IPAddress ipa in ipaRange)
        {
            Console.WriteLine(ipa.ToString());
        }

        Console.ReadLine();

I'm not completely sure if I have done the conversion from the prefix length to a byte array containing the subnet mask right, but this code should give you a good starting point.

Edit: Updated the bit-bending part of the code. May be ugly, but works for this example. I think you will be capable of finding a better solution, if you need to. Those BitArrays are a pain in the neck.

Be aware that generating an IPv6 network range can be a very memory/cpu exhausting task if the network is large.

这篇关于计算一个子网内的所有地址...对于IPv6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆