子网CIDR:IP是否需要是子网中的第一个IP?
当定义子网(例如1.2.3.0/24
)时,该子网中的255个主机从1.2.3.0到1.2.3.255。
但是,如果我错误地用1.2.3.64/24
定义了子网怎么办?这是否意味着该范围从1.2.3.64
到1.2.3.255
?
我找不到有关此的正式文件。
编辑:在Java iPaddress Lib(5.2.1)中,1.2.3.64/24将被视为1.2.3.64到1.2.3.255,而不是1.2.3.0到1.2.3.0到1.2.3.3.255!代码为:新的ipaddressstring(“ 1.2.3.64/24”)。getSequentialRange()
edit:my sopology:我的道歉:iPaddress lib的行为的上述描述是错误的。正是其他事情使我认为范围成为1.2.3.64至1.2.3.255。 确实,
new IPAddressString("1.2.3.64/24").getSequentialRange().getLower()
是1.2.3.64, 并且
new IPAddressString("1.2.3.64/24").getSequentialRange().getUpper()
也是1.2.3.64
, 因此,我认为这是一个合理的实施!我没有问题。
这
new IPAddressString("1.2.3.64/24").getAddress().toPrefixBlock().getLower()`
will be the expect one: 1.2.3.0.
And the `.getUpper()` is 1.2.3.255.
so this is the perfect way to get correct ip range.
When define a subnet, such as 1.2.3.0/24
, it means 255 hosts in the subnet start from 1.2.3.0 to 1.2.3.255.
But what if I wrongly define the subnet with 1.2.3.64/24
? Does it then mean the range is start from 1.2.3.64
to 1.2.3.255
?
I could not find a formal document about this.
EDIT: in Java ipaddress lib (5.2.1), the 1.2.3.64/24 will be treated as range from 1.2.3.64 to 1.2.3.255, instead of 1.2.3.0 to 1.2.3.255!. The code is: new IPAddressString("1.2.3.64/24").getSequentialRange()
EDIT: My apology: the above description of ipaddress lib's behavior is wrong. It was other things that caused me think the the range become 1.2.3.64 to 1.2.3.255.
Indeed, the
new IPAddressString("1.2.3.64/24").getSequentialRange().getLower()
is 1.2.3.64,
and
new IPAddressString("1.2.3.64/24").getSequentialRange().getUpper()
is also 1.2.3.64
,
so I think this is a reasonable implementation!. I have no problem of that.
The
new IPAddressString("1.2.3.64/24").getAddress().toPrefixBlock().getLower()`
will be the expect one: 1.2.3.0.
And the `.getUpper()` is 1.2.3.255.
so this is the perfect way to get correct ip range.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有正式的规范表明将未固定的位(主机位)设置为0是一个约定。但是,在描述子网而不是单个地址时,这是常见的做法,即您将主机位视为零。这是一个非正式的惯例。如果您要描述子网,如果要忽略的话,为什么将这些位设置为零以外的任何东西?因此,这就是所有网络工程师和大多数其他人都这样做的,当主机可以承担任何值时,他们在描述完整子网时将其作为零。
1.2.3.64/24之类的规范是地址的网络部分是前24位,地址的主机部分是其余的8位。因此,网络为1.2.3.0,主机为0.0.0.64。规格没有其他说明。
同样,1.2.3.0/24表示网络为1.2.3.0,主机为0.0.0.0。但是,就像我说的那样,当主机为0时,通常用于参考整个子网,这意味着主机可以采用任何值。当人们写下该子网的255个地址的字符串时,他们将写1.2.3.0/24。
实际上,看到1.2.3.0用作单个地址是不寻常的,因为许多路由器不接受0的主机
。您如何解析这些东西是任何库。一些库将整个子网解析为1.2.3.64/24,抛出0.0.0.64。一些库将1.2.3.64/24分析为1.2.3.64。一些库有单独的方法来执行一种或另一种方法。
使用iPaddress库,相同的解析器解析了地址和子网。当它解析1.2.3.64/24或1.2.3.0/24时,解析器需要决定每个人的含义。对于前者而言,它将1.2.3.64/24解释为前缀长度24的地址1.2.3.64,因为如果您不打算64表示什么意思?为什么要把它扔出去?
对于1.2.3.0/24,它将其解释为整个子网,因为就像我所说的那样,通常使用0的主机来指代整个子网,并且很少使用1.2.3.0作为地址。
但是,如果您希望在解析这些内容时选择其他含义,则库提供其他选项。
没有正式的规格。实际上,没有针对IP地址的许多不同方面的正式规范,其中很多是非正式的,并且在多年来才发展为使用的常见实践。
新的ipaddressstring(“ 1.2.3.64/24”)。getSequentialRange()
被解析为范围“ 1.2.3.64 - > 1.2.3.64”。图书馆的早期版本的行为有所不同,但这就是过去几年所有最新版本的解析方式。免责声明:我是该图书馆的项目经理。
There is no formal specification indicating that setting the un-fixed bits (the host bits) to 0 is a convention. However, it is common practice when describing a subnet as opposed to a single address, that you leave the host bits as zero. It is an informal convention. If you are describing a subnet, why would you set those bits to anything other than zero if they are intended to be ignored? So that is what all network engineers and most other people do, they leave the bits as zero when they are describing the full subnet, when the host can take on any value.
The specification for something like 1.2.3.64/24 is that the network part of the address is the first 24 bits, and the host part of the address is the remaining 8 bits. So, the network is 1.2.3.0 and the host is 0.0.0.64. The specification says nothing else.
Likewise, 1.2.3.0/24 indicates the network is 1.2.3.0 and the host is 0.0.0.0. However, like I said, when the host is 0, that is commonly used to refer to the entire subnet, meaning the host can take on any value. When people write down a string for that subnet of 255 addresses, they will write 1.2.3.0/24.
In fact, it is unusual to see 1.2.3.0 used as a single address because many routers do not accept a host of 0.
How you parse those things is specific to any library. Some libraries parse 1.2.3.64/24 as the whole subnet, throwing out the 0.0.0.64. Some libraries parse 1.2.3.64/24 as 1.2.3.64. Some libraries have separate methods to do one or the other.
With the IPAddress library, the same parser parses both addresses and subnets. When it parses 1.2.3.64/24 or 1.2.3.0/24, the parser needs to decide what you mean by each. For the former, it interprets 1.2.3.64/24 as the address 1.2.3.64 with prefix length 24, because why else is the 0.0.0.64 there at all if you do not intend that 64 to mean something? Why throw it out?
For 1.2.3.0/24, it interprets that as the whole subnet, because, like I said, a host of 0 is commonly used to refer to the entire subnet, and it is rare to use 1.2.3.0 as an address.
However, if you wish to choose some other meaning when parsing those things, the library provides other options.
There is no formal specification for this. In fact, there is no formal specification for a lot of the different aspects of IP addressing, a lot of it is informal and simply evolved over the years to the common practices in use.
new IPAddressString("1.2.3.64/24").getSequentialRange()
is parsed as the range "1.2.3.64 -> 1.2.3.64". Earlier versions of the library behaved differently, but that is how all the latest releases for the last few years parse it.Disclaimer: I am the project manager of that library.
这很有趣,因为我似乎也找不到有关为什么将未固定位设置为
0
的正式规范。查看 rfc 4632 说::即使将未固定位设置为
0
的惯例,您也可能具有在非零数字中具有IP结尾的CIDR符号。例如:192.168.1.254/31
表示IPS范围从192.168.1.254
到192.168.1.255
。CIDR符号仅指定已固定的位数,因此即使您放置
.64
,也看来/24
仍然仅表示IP地址的24位固定位。您可以看到 cidr计算器来自arin中的从arin中显示
1.2.2.3.64/24/24/24/24/24 < /code>仍然表示从
1.2.3.0
到1.2.3.255
的所有IP地址。屏幕截图。
现在,可能会实施不同的系统来以不同的方式处理这种情况,因此我个人会遵循《公约》,但是从CIDR符号的角度来看(以及在RFC中我似乎可以找到的),它仍然应该代表整个范围。
It's interesting because I too can't seem to find any formal specification on why setting the un-fixed bits to
0
is the convention. Looking at RFC 4632 says:Even with the convention of setting the un-fixed bits to
0
, you may have CIDR notation that has an IP ending in a non-zero number. For example:192.168.1.254/31
which represents the range of IPs from192.168.1.254
to192.168.1.255
.CIDR notation only specifies the number of bits that are fixed, so even if you put
.64
it would appear that the/24
still represents only 24 fixed bits of the IP address.You can see that the CIDR Calculator from ARIN shows that
1.2.3.64/24
still represents all IP addresses from1.2.3.0
to1.2.3.255
.Screenshot here.
Now it may be that different systems are implemented to handle this situation differently, so I would personally follow the convention, but from the perspective of CIDR notation (and what I can seem to find in the RFC) it should still represent the entire range.