我一直在寻找一些C#代码来将CIDR表示法(72.20.10.0/24)中的网络转换为IP地址范围,没有太多运气。 stackoverlow上有一些关于CIDR的线程,但似乎没有任何C#代码可以覆盖我需要的内容。所以我决定自己做饭,我不希望代码依赖System.Net进行此版本的任何转换。
也许这可能对某人有所帮助。
参考文献:
What's the best way to convert from network bitcount to netmask?
来自http://www.laffeycomputer.com/whatmask.html 的“Whatmask”C代码
用法:
uint startIP, endIP;
Network2IpRange("72.20.10.0/24", out startIP, out endIP);
代码假设一切都是32位。
static void Network2IpRange(string sNetwork, out uint startIP, out uint endIP)
{
uint ip, /* ip address */
mask, /* subnet mask */
broadcast, /* Broadcast address */
network; /* Network address */
int bits;
string[] elements = sNetwork.Split(new Char[] { '/' });
ip = IP2Int(elements[0]);
bits = Convert.ToInt32(elements[1]);
mask = ~(0xffffffff >> bits);
network = ip & mask;
broadcast = network + ~mask;
usableIps = (bits >30)?0:(broadcast - network - 1);
if (usableIps <= 0)
{
startIP = endIP = 0;
}
else
{
startIP = network + 1;
endIP = broadcast - 1;
}
}
public static uint IP2Int(string IPNumber)
{
uint ip = 0;
string[] elements = IPNumber.Split(new Char[] { '.' });
if (elements.Length==4)
{
ip = Convert.ToUInt32(elements[0])<<24;
ip += Convert.ToUInt32(elements[1])<<16;
ip += Convert.ToUInt32(elements[2])<<8;
ip += Convert.ToUInt32(elements[3]);
}
return ip;
}
随时提交您的改进。
答案 0 :(得分:17)
我建议使用Github中的C#IPNetwork类。
string net = "192.168.168.100/24";
IPNetwork ipnetwork = IPNetwork.Parse(net);
Console.WriteLine("Network : {0}", ipnetwork.Network);
Console.WriteLine("Netmask : {0}", ipnetwork.Netmask);
Console.WriteLine("Broadcast : {0}", ipnetwork.Broadcast);
Console.WriteLine("FirstUsable : {0}", ipnetwork.FirstUsable);
Console.WriteLine("LastUsable : {0}", ipnetwork.LastUsable);
Console.WriteLine("Usable : {0}", ipnetwork.Usable);
Console.WriteLine("Cidr : {0}", ipnetwork.Cidr);
它将输出
Network : 192.168.168.0
Netmask : 255.255.255.0
Broadcast : 192.168.168.255
FirstUsable : 192.168.168.1
LastUsable : 192.168.168.254
Usable : 254
Cidr : 24
玩得开心。
答案 1 :(得分:7)
以下是您为示例72.20.10.0/24
,
让Network
成为72.20.10.0
Mask
是~((1 << (32-24)) - 1)
//或者
Mask
为~(0xFFFFFFFF >> 24)
0xFFFFFF00
StartIP 是 - (Network & Mask)
;
72.20.10.0 & 0xFFFFFF00
EndIP 是 - ((Network & Mask) | ~Mask)
;
(72.20.10.0 & 0xFFFFFF00) | 0x000000FF
这将是72.20.10.0 -- 72.20.10.255
。
答案 2 :(得分:3)
对于network/maskBits
,
您可以通过以下两种方式之一计算mask
,
mask = ~((1 << (32 - maskBits)) - 1) // or,
mask = ~(0xFFFFFFFF >> maskBits)
那么范围是,
StartIP = network
EndIP = network | ~mask
更准确地说,
StartIP = network & mask
EndIP = (network & mask) | ~mask
其中,
<<
是按位左移(没有翻转)&
是按位AND,|
是按位OR,~
是按位INVERT。答案 3 :(得分:2)
以下是如何将CIDR表示法转换为T-SQL中的范围from my blog post:
首先在SQL Server中预先创建此函数(来自 http://www.stardeveloper.com)。
CREATE FUNCTION [dbo].[ConvertIPToLong](@IP varchar(15))
RETURNS bigint
AS
BEGIN
DECLARE @Long bigint
SET @Long = CONVERT(bigint, PARSENAME(@IP, 4)) * 256 * 256 * 256 +
CONVERT(bigint, PARSENAME(@IP, 3)) * 256 * 256 +
CONVERT(bigint, PARSENAME(@IP, 2)) * 256 +
CONVERT(bigint, PARSENAME(@IP, 1))
RETURN (@Long)
END
这是我放在一起计算的T-SQL代码示例 来自CIDR地址的低和高IP范围。这很乱,我不得不这样做 解决T-SQL缺少位移运算符的问题。
Declare @CidrIP varchar(50)
Set @CidrIP = '10.100.60.55/28'
Select dbo.[ConvertIPToLong](left(@CidrIP, patindex('%/%' , @CidrIP) - 1)) & (cast(4294967295 as bigint) ^ (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1)) as LowRange,
dbo.[ConvertIPToLong](left(@CidrIP, patindex('%/%' , @CidrIP) - 1)) & (cast(4294967295 as bigint) ^ (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1)) + (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1)