IP List
-
Could someone give me a tip or help me make some code wich list all the ip addresses betwen to givel values. I need this for a host scaner and I spent a whole nigth working on it but I wrote only useless code. Thanks
What is the problem you are having?
-
Could someone give me a tip or help me make some code wich list all the ip addresses betwen to givel values. I need this for a host scaner and I spent a whole nigth working on it but I wrote only useless code. Thanks
-
For an example if I have 2 Ip address 127.0.0.1 and 127.0.2.1 I want to generate 127.0.0.1,127.0.0.2,127.0.0.3.......127.0.0.255....127.0.2.1 Do you understand now. Or if there is a link about this subject that woulb be helpful
Since an IP address is a 32 bit unsigned integer, you can convert the IPs into "flat" numbers, then generate all the addresses you want, then convert them into a "classic" IP address. For example:
string[] startAddr = "127.0.0.1".Split('.');
string[] endAddr = "127.2.0.1".Split('.');uint start = uint.Parse(startAddr[0]) +
uint.Parse(startAddr[1]) * 256 +
uint.Parse(startAddr[2]) * 256 * 256 +
uint.Parse(startAddr[3]) * 256 * 256 * 256;
uint end = ... // The sameuint[] addr = new uint[end - start];
for(int i = 0; i < addr.Length; i++) {
addr[i] = start + i;
}string[] result = new string[addr.Lenght];
string tmp = "";
for(int i = 0; i < result.Lenght; i++) {
result[i] = "";
result[i] += ((uint)(addr[i] % (256 * 256 * 256 * 256))).ToString() + ".";
result[i] += ((uint)(addr[i] % (256 * 256 * 256))).ToString() + ".";
result[i] += ((uint)(addr[i] % (256 * 256))).ToString() + ".";
result[i] += ((uint)(addr[i] % (256))).ToString();
}I don't know if this works (I didn't try it), but this is my idea. ;) EDIT: IT DOESN'T WORK AT ALL! ___________________________________ Tozzi is right: Gaia is getting rid of us. My Blog [ITA] -- modified at 4:59 Wednesday 15th February, 2006
-
Could someone give me a tip or help me make some code wich list all the ip addresses betwen to givel values. I need this for a host scaner and I spent a whole nigth working on it but I wrote only useless code. Thanks
I had a go at this for you, and got it to work, but there were plenty of weirdnesses! IPAddress has a constructor that looks like: IPAddress(byte[] address) Which kept throwing an exception when passed the return from IPAddress.GetAddressBytes - this is weird as all the docs say it should work. There is a weird limitation that requires the byte array to have a length og 16 but II ensured this.... So anyway, IPAddress.Parse was working so a quick method such as the following gets round the issue.
private IPAddress IPFromBytes(byte[] bytes) { return IPAddress.Parse( String.Format("{0}.{1}.{2}.{3}",bytes[0],bytes[1],bytes[2],bytes[3]) ); }
Now, to solve this problem I started by thinking about how to incremement an IPAddress, and I came up with this: Loop from the last digit to the first if the current number is 255 set it to 0 otherwise incremement the number and break out the loop this should cause the sequence to go 127.0.0.1 127.0.0.2 ...... 127.0.0.254 127.0.0.255 127.0.1.0 127.0.1.1 Here is the incremement code:private byte[] Incremement(byte[] current) { for(int i=3;i>-1;i--) { if(current[i] == 255) { current[i] = 0; } else { current[i]++; break; } } return current; }
Finally, the get range method must do the following capture start and end IP's loop while not at end capture current IP into a list incremement IP The only hard part there is "loop while not at end", as weve already written the incremement and capturing to a list is childs play.private System.Net.IPAddress [] GetRange(IPAddress start, IPAddress end) { byte[] startBytes = start.GetAddressBytes(); byte[] endBytes = end.GetAddressBytes(); byte[] currentBytes = startBytes; ArrayList list = new ArrayList(); while( currentBytes[0] < endBytes[0] || currentBytes[1] < endBytes[1] || currentBytes[2] < endBytes[2] || currentBytes[3] < endBytes[3]) { list.Add( IPFromBytes(currentBytes) ); currentBytes = this.Incremement(currentBytes); } return (IPAddress[])list.ToArray(typeof(IPAddress)); }
Hope it helps, and please rememeber not to be doing anything malicious with port scanners! -
I had a go at this for you, and got it to work, but there were plenty of weirdnesses! IPAddress has a constructor that looks like: IPAddress(byte[] address) Which kept throwing an exception when passed the return from IPAddress.GetAddressBytes - this is weird as all the docs say it should work. There is a weird limitation that requires the byte array to have a length og 16 but II ensured this.... So anyway, IPAddress.Parse was working so a quick method such as the following gets round the issue.
private IPAddress IPFromBytes(byte[] bytes) { return IPAddress.Parse( String.Format("{0}.{1}.{2}.{3}",bytes[0],bytes[1],bytes[2],bytes[3]) ); }
Now, to solve this problem I started by thinking about how to incremement an IPAddress, and I came up with this: Loop from the last digit to the first if the current number is 255 set it to 0 otherwise incremement the number and break out the loop this should cause the sequence to go 127.0.0.1 127.0.0.2 ...... 127.0.0.254 127.0.0.255 127.0.1.0 127.0.1.1 Here is the incremement code:private byte[] Incremement(byte[] current) { for(int i=3;i>-1;i--) { if(current[i] == 255) { current[i] = 0; } else { current[i]++; break; } } return current; }
Finally, the get range method must do the following capture start and end IP's loop while not at end capture current IP into a list incremement IP The only hard part there is "loop while not at end", as weve already written the incremement and capturing to a list is childs play.private System.Net.IPAddress [] GetRange(IPAddress start, IPAddress end) { byte[] startBytes = start.GetAddressBytes(); byte[] endBytes = end.GetAddressBytes(); byte[] currentBytes = startBytes; ArrayList list = new ArrayList(); while( currentBytes[0] < endBytes[0] || currentBytes[1] < endBytes[1] || currentBytes[2] < endBytes[2] || currentBytes[3] < endBytes[3]) { list.Add( IPFromBytes(currentBytes) ); currentBytes = this.Incremement(currentBytes); } return (IPAddress[])list.ToArray(typeof(IPAddress)); }
Hope it helps, and please rememeber not to be doing anything malicious with port scanners!