C# 给定IP地址和网络掩码时如何计算IP范围?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1470792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:52:01  来源:igfitidea点击:

How to calculate the IP range when the IP address and the netmask is given?

c#ipnetmask

提问by Anheledir

When a IP-Range is written as aaa.bbb.ccc.ddd/netmask (CIDR Notation) I need to calculate the first and the last included ip address in this range with C#.

当 IP 范围写为 aaa.bbb.ccc.ddd/netmask ( CIDR Notation) 时,我需要使用 C# 计算此范围内包含的第一个和最后一个 IP 地址。

Example:

例子:

Input:192.168.0.1/25

输入:192.168.0.1/25

Result:192.168.0.1 - 192.168.0.126

结果:192.168.0.1 - 192.168.0.126

采纳答案by balexandre

my good friend Alessandrohave a nice postregarding bit operators in C#, you should read about it so you know what to do.

我的好朋友Alessandro有一篇关于 C# 中位运算符的好文章,你应该阅读它,这样你就知道该怎么做。

It's pretty easy. If you break down the IP given to you to binary, the network address is the ip address where all of the host bits (the 0's in the subnet mask) are 0,and the last address, the broadcast address, is where all the host bits are 1.

这很容易。如果将给定的 IP 分解为二进制,则网络地址是所有主机位(子网掩码中的 0)都为 0 的 IP 地址,最后一个地址即广播地址是所有主机所在的地址位为 1。

For example:

例如:

ip 192.168.33.72 mask 255.255.255.192

11111111.11111111.11111111.11000000 (subnet mask)
11000000.10101000.00100001.01001000 (ip address)

The bolded parts is the HOST bits (the rest are network bits). If you turn all the host bits to 0 on the IP, you get the first possible IP:

粗体部分是 HOST 位(其余是网络位)。如果您将 IP 上的所有主机位都设置为 0,您将获得第一个可能的 IP:

11000000.10101000.00100001.01000000 (192.168.33.64)

If you turn all the host bits to 1's, then you get the last possible IP (aka the broadcast address):

如果您将所有主机位都设置为 1,那么您将获得最后一个可能的 IP(也就是广播地址):

11000000.10101000.00100001.01111111 (192.168.33.127)

So for my example:

所以对于我的例子:

the network is "192.168.33.64/26":
Network address: 192.168.33.64
First usable: 192.168.33.65 (you can use the network address, but generally this is considered bad practice)
Last useable: 192.168.33.126
Broadcast address: 192.168.33.127

回答by Vladislav Rastrusny

Invert mask (XOR with ones), AND it with IP. Add 1. This will be the starting range. OR IP with mask. This will be the ending range.

反转掩码(与 1 异或),并将其与 IP。添加 1。这将是起始范围。或带掩码的 IP。这将是结束范围。

回答by Joren

I'll just post the code:

我就贴一下代码:

IPAddress ip = new IPAddress(new byte[] { 192, 168, 0, 1 });
int bits = 25;

uint mask = ~(uint.MaxValue >> bits);

// Convert the IP address to bytes.
byte[] ipBytes = ip.GetAddressBytes();

// BitConverter gives bytes in opposite order to GetAddressBytes().
byte[] maskBytes = BitConverter.GetBytes(mask).Reverse().ToArray();

byte[] startIPBytes = new byte[ipBytes.Length];
byte[] endIPBytes = new byte[ipBytes.Length];

// Calculate the bytes of the start and end IP addresses.
for (int i = 0; i < ipBytes.Length; i++)
{
    startIPBytes[i] = (byte)(ipBytes[i] & maskBytes[i]);
    endIPBytes[i] = (byte)(ipBytes[i] | ~maskBytes[i]);
}

// Convert the bytes to IP addresses.
IPAddress startIP = new IPAddress(startIPBytes);
IPAddress endIP = new IPAddress(endIPBytes);

回答by Trevor Tippins

You might already know this, but to check that you're getting this stuff right have a look at http://www.subnet-calculator.com/- you can see there how the bits represent the network and host portions of the address.

您可能已经知道这一点,但要检查您是否正确了解这些内容,请查看http://www.subnet-calculator.com/- 您可以在那里看到这些位如何表示地址的网络和主机部分.

回答by Jamison Ye

I learned this shortcut from working at the network deployment position. It helped me so much, I figured I will share this secret with everyone. So far, I have not able to find an easier way online that I know of.

我在网络部署岗位工作时学会了这个捷径。它对我帮助很大,我想我会与大家分享这个秘密。到目前为止,我还没有在网上找到我所知道的更简单的方法。

For example a network 192.115.103.64 /27, what is the range?

例如网络192.115.103.64 /27,范围是多少?

just remember that subnet mask is 0, 128, 192, 224, 240, 248, 252, 254, 255

只要记住子网掩码是 0, 128, 192, 224, 240, 248, 252, 254, 255

255.255.255.255 11111111.11111111.11111111.11111111 /32

255.255.255.255 11111111.11111111.11111111.11111111 /32

255.255.255.254 11111111.11111111.11111111.11111110 /31

255.255.255.254 11111111.11111111.11111111.11111110 /31

255.255.255.252 11111111.11111111.11111111.11111100 /30

255.255.255.252 11111111.11111111.11111111.11111100 /30

255.255.255.248 11111111.11111111.11111111.11111000 /29

255.255.255.248 11111111.11111111.11111111.11111000 /29

255.255.255.240 11111111.11111111.11111111.11110000 /28

255.255.255.240 11111111.11111111.11111111.11110000 /28

255.255.255.224 11111111.11111111.11111111.11100000 /27

255.255.255.224 11111111.11111111.11111111.11100000 /27

255.255.255.192 11111111.11111111.11111111.11000000 /26

255.255.255.192 11111111.11111111.11111111.11000000 /26

255.255.255.128 11111111.11111111.11111111.10000000 /25

255.255.255.128 11111111.11111111.11111111.10000000 /25

255.255.255.0 11111111.11111111.11111111.00000000 /24

255.255.255.0 11111111.11111111.11111111.00000000 /24

from /27 we know that (11111111.11111111.11111111.11100000). Counting from the left, it is the third number from the last octet, which equal 255.255.255.224 subnet mask. (Don't count 0, 0 is /24) so 128, 192, 224..etc

从 /27 我们知道 (11111111.11111111.11111111.11100000)。从左边数起,它是最后一个八位字节的第三个数字,等于 255.255.255.224 子网掩码。(不要数 0,0 是 /24)所以 128、192、224...等

Here where the math comes in:

数学在这里发挥作用:

use the subnet mask - subnet mask of the previous listed subnet mask in this case 224-192=32

在这种情况下使用子网掩码 - 前面列出的子网掩码的子网掩码 224-192=32

We know 192.115.103.64 is the network: 64 + 32 = 96 (the next network for /27)

我们知道 192.115.103.64 是网络:64 + 32 = 96(/27 的下一个网络)

which means we have .0 .32. 64. 96. 128. 160. 192. 224. (Can't use 256 because it is .255)

这意味着我们有 .0 .32。64. 96. 128. 160. 192. 224.(不能使用256,因为它是0.255)

Here is the range 64 -- 96.

这是范围 64 - 96。

network is 64.

网络是64。

first host is 65.(first network +1)

第一个主机是 65.(第一个网络+1)

Last host is 94. (broadcast -1)

最后一个主机是 94。(广播 -1)

broadcast is 95. (last network -1)

广播是 95。(最后一个网络 -1)

回答by Matt Millican

I know this is an older question, but I found this nifty library on nuget that seems to do just the trick for me:

我知道这是一个较老的问题,但我在 nuget 上发现了这个漂亮的库,它似乎对我有用:

http://nuget.org/packages/TakeIo.NetworkAddress/

http://nuget.org/packages/TakeIo.NetworkAddress/

回答by LukeSkywalker

I would recommend the use of IPNetwork Library https://github.com/lduchosal/ipnetwork. As of version 2, it supports IPv4 and IPv6 as well.

我会推荐使用 IPNetwork 库https://github.com/lduchosal/ipnetwork。从版本 2 开始,它也支持 IPv4 和 IPv6。

IPv4

IPv4

  IPNetwork ipnetwork = IPNetwork.Parse("192.168.0.1/25");

  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);

Output

输出

  Network : 192.168.0.0
  Netmask : 255.255.255.128
  Broadcast : 192.168.0.127
  FirstUsable : 192.168.0.1
  LastUsable : 192.168.0.126
  Usable : 126
  Cidr : 25

Have fun !

玩得开心 !

回答by Nilay Jha

Input: 192.168.0.1/25

输入: 192.168.0.1/25

The mask is this part: /25

面具是这部分: /25

To find the network address do the following:

要查找网络地址,请执行以下操作:

  • Subtract the mask from the ip length (32 - mask) = 32 - 25 = 7 and take those bits from the right

  • In the given ip address I.e: 192.168.0.1in binary is: 11111111 11111111 00000000 00000001Now, taking 7 bits from right '0' 1111111 11111111 00000000 00000000Which in decimal is: 192.168.0.0(this is the network address)

  • 从 ip 长度中减去掩码 (32 - 掩码) = 32 - 25 = 7 并从右边取出这些位

  • 在给定的ip地址即:192.168.0.1二进制是: 11111111 11111111 00000000 00000001现在,从右边'0'取7位 1111111 11111111 00000000 00000000,十进制是:( 192.168.0.0这是网络地址)

To find first valid/usable ip address add +1 to network address I.e: 192.168.0.1

要找到第一个有效/可用的 IP 地址,请将 +1 添加到网络地址,即: 192.168.0.1

To find the last/broadcast address the procedure is same as that of finding network address but here you have to make (32-mask) bits from right to '1'

要查找最后一个/广播地址,过程与查找网络地址的过程相同,但在这里您必须将(32 位掩码)位从右变为“1”

I.e: 11111111 11111111 00000000 01111111Which in decimal is 192.168.0.127

即:11111111 11111111 00000000 01111111十进制中的哪个是192.168.0.127

To find the last valid/usable ip address subtract 1 from the broadcast address I.e: 192.168.0.126

要找到最后一个有效/可用的 IP 地址,从广播地址中减去 1,即: 192.168.0.126