导出子网定义的逆
IP 子网由两部分定义:网络和前缀长度或掩码。
例如192.168.0.0/16
(或192.168.0.0/255.255.0.0
)。
像 192.168.1.1
这样的 IP 地址据说与此子网匹配,因为
(192.168.1.1 & 255.255.0.0) == 192.168.0.0
我对所谓的子网的逆子网感兴趣
是这样描述的,
对于给定的子网A(例如,NetworkA / MaskA),
SubnetA 的逆是 k 个子网的列表,例如,如果 IP 地址 A 与 SubnetA 匹配,
A 不会匹配任何 k 个子网,并且
每个与 SubnetA 不匹配的 IP 地址 B,
将完全匹配这些 k 个子网中的 1 个。
代码不是必需的,我对正确且最佳的方法感兴趣。
我在下面注明了供参考的优化答案,因此它不会分散人们尝试将此问题视为问题的注意力。 仍然接受拉法尔的答案,因为他也首先得到了正确的答案。
An IP Subnet is defined with two parts, a network and a prefix-length or mask.
For example 192.168.0.0/16
(or, 192.168.0.0/255.255.0.0
).
An IP address like 192.168.1.1
is said to match this subnet because,
(192.168.1.1 & 255.255.0.0) == 192.168.0.0
I am interested in what might be called the inverse of a subnet
which is described like this,
For a given SubnetA (say, NetworkA / MaskA),
The inverse of SubnetA is the list of k subnets, such that,If an IP address A, matches SubnetA,
A will not match any of these k subnets, and
Every IP address B that does not match SubnetA,
will match exactly 1 of these k subnets.
Code is not necessary, I am interested in a correct and optimal method.
I have the optimized answer noted for reference below so it does not distract people trying this as a problem. Have retained acceptance of Rafał's answer since he also got it right first.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
A
中每个未屏蔽位b
对应一个子网,与A
中所有之前的位匹配,与b
不同,屏蔽所有以下位。 这样,不在A
中的每个地址i
将仅匹配上述网络中的一个,即负责i
的第一位的网络与A
不匹配。One subnet for every unmasked bit
b
inA
, matching all the previous bits inA
, differing onb
, masking all the following bits. This way each addressi
not inA
will match only one of the above networks, namely the one responsible for the first bit ofi
that does not matchA
.嗯。 我想说它基本上是除 A 之外具有相同掩码的任何子网......
Hm. I'd say that it is basically any subnet other than A with the same mask...
如果您想象所有子网的树从
0.0.0.0/32
开始,每一位都有分支,那么您需要所有不通向您的子网的分支。 您向上一步(位),将该位清空,并将该节点的同级(在适当位置具有不同的位)添加到您的集合中。 (与 Rafał 所说的相同,只是表达方式不同。)您可以这样做(工作 C# 代码):最重要的是
writeComplementSubnets
方法。 IP 地址以自然(对我来说)表示形式表示,因此192.168.0.0
变为0xC0A80000
。编辑:我意识到递归在这里绝对是不必要的。 看来函数式编程有时会导致错误的思维。
If you imagine tree of all subnets starting at
0.0.0.0/32
, branching at every bit, you want all the branches that don't lead to your subnet. You go up one step (bit), null this bit and add sibling (has different bit on the appropriate place) of this node to your set. (It's the same as Rafał says, only expressed differently.) You can do it like this (working C# code):Most important is the
writeComplementSubnets
method. IP adress is represented in natural (for me) representation, so that192.168.0.0
becomes0xC0A80000
.EDIT: I realised that recursion is absolutelly unnecessary here. It seems functional programming causes wrong thinking sometimes.
我在此代码片段中注明了可供参考的优化答案。
接受拉法尔的回答,因为他也首先答对了。
这是
192.168.0.0/16
的逆,以检查正确性。I have the optimized answer noted for reference in this code snippet.
Accepted Rafał's answer since he also got it right first.
Here is the inverse for
192.168.0.0/16
, to check correctness.