谁说子网掩码的1必须连续?关于像255.255.0.255这样的非连续子网掩码

本文通过RFC950和RFC1219等标准详细解释了255.255.0.255这样的非连续子网掩码是否合法,并给出了实际应用案例。

      百度上搜索的都是说255.255.0.255的掩码不合法,其实这些人没有读过RFC,也就大惊小怪了;RFC950的内容:

    A Class C Network Case (illustrating non-contiguous subnet bits)

For this case, assume that the requesting host is on class C
network 192.1.127.0, has address 192.1.127.19, that there is a
gateway at 192.1.127.50, and that on network an 3-bit subnet field
is in use (01011000), that is, the address mask is 255.255.255.88.

The host sends the ICMP request to 255.255.255.255:

Source address: 192.1.127.19
Destination address: 255.255.255.255
Protocol: ICMP = 1
Type: Address Mask Request = AM1
Code: 0
Mask: 0
//19的二进制是00010011;88的二进制是01011000;AND的结果是00010000

The gateway can then respond directly to the requesting host.

Source address: 192.1.127.50
Destination address: 192.1.127.19
Protocol: ICMP = 1
Type: Address Mask Reply = AM2
Code: 0
Mask: 255.255.255.88.
//50的二进制是00110010;88的二进制是01011000;AND的结果是00010000,所以这两个机器在一个子网内,destination address直接就是192.1.127.19

In the diskless workstation case the host sends:

Source address: 0.0.0.0
Destination address: 255.255.255.255
Protocol: ICMP = 1
Type: Address Mask Request = AM1
Code: 0
Mask: 0

192.1.127.50 will hear the datagram, and should respond with this
datagram:

Source address: 192.1.127.50
Destination address: 255.255.255.255
Protocol: ICMP = 1
Type: Address Mask Reply = AM2
Code: 0
Mask: 255.255.255.88.
Read more: http://www.faqs.org/rfcs/rfc950.html#ixzz0VE0qJANw
另外一篇:
相信读过TCP-IP详解的都知道这个问题,位于中文版vol 1 004里面
10. (3.5) Is the subnet mask 255.255.0.255 valid for a class A address? See
RFC 1219 [Tsuchiya 1991] for more info. This is available from the
Networking I web page, as well as from the usual sources.
It's valid and it's called a noncontiguous subnet mask since the 16 bits for
the subnet mask are not contiguous. The RFC's, however, recommend against
using noncontiguous subnet masks.
//看到没,只是反对使用,没说不连续的子网掩码是非法的
在RFC 950 - Internet Standard Subnetting Procedure中:
Since the bits that identify the subnet are specified by a bitmask, they need not be adjacent in the address. However, we recommend that the subnet bits be contiguous and located as the most significant bits of the local address
RFC-950 [2] specifies a procedure for subnetting Internet addresses using a bit-mask.  While RFC-950 allows the "ones" in the subnet mask to be non-contiguous, RFC-950 recommends that 1) they be contiguous, and 2) that they occupy the most significant bits of the "host" part of the internet address.
### 子网掩码 255.255.255.0 的含义 子网掩码 `255.255.255.0` 是一种常见的子网划分方式,用于将 IP 地址划分为网络地址和主机地址两部分。该子网掩码的二进制表示为 `11111111.11111111.11111111.00000000`,表示前三个字节(24 位)为网络地址,最后一个字节(8 位)为主机地址[^1]。 --- ### 网络地址范围与主机地址范围计算 以一个典型的 IP 地址 `192.168.1.0/24`(即子网掩码为 `255.255.255.0`)为例: - **网络地址**:IP 地址与子网掩码进行按位“与”运算后得到的结果。例如: ```text IP地址: 192.168.1.10011000000.10101000.00000001.01100100 子网掩码: 255.255.255.011111111.11111111.11111111.00000000 按位与: → 11000000.10101000.00000001.00000000192.168.1.0 ``` 因此,该子网的网络地址为 `192.168.1.0`[^2]。 - **广播地址**:将网络地址中的主机位全部置为 1 后得到的地址。在 `255.255.255.0` 掩码下,主机位有 8 位,因此广播地址为 `192.168.1.255`。 - **可用主机地址范围**:从网络地址的下一个地址开始,到广播地址的前一个地址结束。因此,可用主机地址范围为 `192.168.1.1` 到 `192.168.1.254`,共 254 个地址[^3]。 --- ### 子网掩码 255.255.255.0 在网络划分中的作用 - **划分局域网段**:适用于小型局域网,如家庭或办公室网络,最多支持 254 台主机。 - **简化路由管理**:通过固定 24 位网络位,便于统一管理和配置路由器、DHCP 服务器等设备。 - **隔离同子网**:同子网之间的通信需要通过路由器转发,增强了网络的安全性和可管理性。 --- ### 示例明 假设 IP 地址为 `192.168.2.50`,子网掩码为 `255.255.255.0`: ```python # Python 示例:计算网络地址和广播地址 import ipaddress ip = ipaddress.IPv4Interface("192.168.2.50/24") network_address = ip.network.network_address broadcast_address = ip.network.broadcast_address host_range_start = network_address + 1 host_range_end = broadcast_address - 1 print(f"网络地址: {network_address}") print(f"广播地址: {broadcast_address}") print(f"可用主机地址范围: {host_range_start} - {host_range_end}") ``` 输出结果为: ```text 网络地址: 192.168.2.0 广播地址: 192.168.2.255 可用主机地址范围: 192.168.2.1 - 192.168.2.254 ``` ---
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值