如何使用ip地址和IPv4Mask获取IP地址范围是多少? [英] How to use IPAddress and IPv4Mask to obtain IP address range?

查看:1445
本文介绍了如何使用ip地址和IPv4Mask获取IP地址范围是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现以下用C#/。NET 2.0:

I'm trying to accomplish the following in C#/.NET 2.0:

给定一个ip地址的对象(比如192.168.127.100),并包含另一个ip地址对象IPv4Mask /子网掩码(例如,255.255.248.0),我应该能够计算出IP地址范围的开始和结束。

Given an IPAddress object (say, 192.168.127.100) and another IPAddress object containing the IPv4Mask / subnet mask (say, 255.255.248.0), I should be able calculate the start and end of the IP Address range.

(是的,我想做一个for循环THRU一系列子网上的地址。)

(Yes, I'm trying to do a for-loop thru a range of addresses on a subnet.)

从理论上讲,我应该能够按位与上ip地址和子网掩码,以获得IPStart。然后,我应该能够在IPStart和反转(NOT'd)子网掩码执行按位异或,得到IPEND。

Theoretically, I should be able to bitwise AND on the IPAddress and the SubnetMask to get the IPStart. Then I should be able to perform a bitwise XOR on the IPStart and the inverted (NOT'd) SubnetMask, to get the IPEnd.

该ip地址对象提供的方法来输出该地址是一个长或字节[](字节数组)。

The IPAddress object provides methods to output the address as a "long" or as "byte[]" (array of bytes).

执行上长按位操作(因为它是签名吗?)产生无效结果。而我似乎无法为字节数组的IPAddresses执行按位运算

Performing the bitwise operation on the long (because it is signed?) produces invalid results. And I can't seem to perform bitwise operation on the IPAddresses as an array of bytes.

编辑-1:好吧,循环直通的阵列和执行位宽的每个字节AND,NOT和XOR(在各自的情况下),得到正确的结果。

EDIT-1: Okay, looping thru each byte in the array and performing bitwise AND, NOT, and XOR (in each respective situation) gets the right results.

我遇到的下一个问题是,我不能在后循环轻松地执行转换的byte []数组到UInt32的或长。所以,第一个值正常工作,但递增UINT /长期受人让从192.168.127.0到193.168.127.0的名称IP地址的增加 - 这看来以后byte []数组转换为uint /长的顺序字节被逆转。因此,有没有简单的方法从IPStart递增到IPEND。

The next problem I am running into is that I cannot perform the for-loop easily after converting the byte[] arrays into UInt32 or long. So, the first value works properly, but incrementing the uint/long by one makes the IPaddress increase from 192.168.127.0 to 193.168.127.0 -- It seems that after the byte[] array is transformed to uint/long, the order of the bytes gets reversed. So there is no easy way to increment from IPStart to IPEnd.

任何有什么建议?

推荐答案

通过两个字节[]数组爬起来,每一位对执行按位比较,并将结果放到第三个byte []数组的范围开始。您可以通过网络地址byte []数组抓取和反转每一位来产生通配符掩码(如你叫它,倒子网掩码),然后根据使用场所的子网掩码通配符掩码来计算前重复上述过程范围终点。还记得在范围中的第一值是子网的网络地址和上一个值是广播地址,所以这两个地址是不实际的主机地址范围。这里是一步一步的总结你的例子:

Crawl through the two byte[] arrays together, for each bit pair perform the bitwise comparison and place the result into a third byte[] array for the range start. You can crawl through the network address byte[] array and invert each bit to generate the wildcard mask (as you called it, inverted subnetmask), and then repeat the process as before using the wildcard mask in place of the subnet mask to calculate the range end. Remember also that the first value in the range is the subnet network address and the last value is the broadcast address, so those two addresses are not in the actual host address range. Here is your example in a step-by-step summary:

Address:   192.168.127.100       11000000.10101000.01111 111.01100100
Netmask:   255.255.248.0 = 21    11111111.11111111.11111 000.00000000
Wildcard:  0.0.7.255             00000000.00000000.00000 111.11111111
=>
Network:   192.168.120.0/21      11000000.10101000.01111 000.00000000
Broadcast: 192.168.127.255       11000000.10101000.01111 111.11111111
HostMin:   192.168.120.1         11000000.10101000.01111 000.00000001
HostMax:   192.168.127.254       11000000.10101000.01111 111.11111110

这篇关于如何使用ip地址和IPv4Mask获取IP地址范围是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆