Calculating valid IP's in a subnet

R

robert

I'm following Roedy's code here:

http://mindprod.com/jgloss/ip.html#DISPLAYING

IP bit manipulation tricks

// IP bit tricks
// compute an n bit subnet mask, all 1s except for last n bits.
int subnet = ~( ( 1 << n ) - 1 );

// generate a list of possible local IP addresses, e.g.
// 192.168.0.0 .. 192.168.255.255

int base = (192 << 24) | (168 << 16);
for ( int i=0; i<0xffff; i++ )
{
int sample = base | i;
System.out.println( DottedQuad.dottedQuad( sample ) );
}

What I need is to pass an IP - say 192.168.10.10 - and a subnet for
example 255.255.255.0 - and get a list of IP's . I do know binary a
bit, yet it looks like he's putting a hex value in the for loop, and
I'm not sure how to calculate it. Should I do:

int subnet = ~( ( 1 << n ) - 1 );

Where n == 255.255.255.0 and use the 'int subnet' as the value in the
for loop ? And how do I calculate the '192 << 24' and the '168 << 16'
in the 'base' equation , ie, just strip and always use 16 and 24 ?

Robert
 
T

Thomas Weidenfeller

robert said:
What I need is to pass an IP - say 192.168.10.10 - and a subnet for
example 255.255.255.0

The later is a subnet mask, not a subnet. The term mask is particular
important for your problem. A mask is there to be applied, so apply it.

An algorithm would look like it follows - I am to lazy now to code all
the details and to check for all boundary cases:

Convert both dd representations (IP address and mask) to 32 bit
integers. Then

start = ip & mask;
end = start | (~mask);

If you want to ignore the network number and broadcast address:

start++;
end--;

Then loop from start to end to get all addresses. Be careful with Java's
32 signed integers - that's why I use "!= end" instead of "<= end":

for(int addr = start; addr != end; addr++) {

}
- and get a list of IP's . I do know binary a
bit, yet it looks like he's putting a hex value in the for loop, and
I'm not sure how to calculate it.

The same as any other value. People often don't believe it, but hex,
decimal, octal, etc are just different *representations*. They anyhow
end up as the same binary bit patterns in the computer memory.

Should I do:

int subnet = ~( ( 1 << n ) - 1 );

Where n == 255.255.255.0

Why? May I suggest that you spend some time to learn how IP addresses
are build and IP subnets are defined (you might want to include CIDR
notation, too). And that you also have a look at binary operations?

/Thomas
 
C

Chris Smith

Thomas Weidenfeller said:
for(int addr = start; addr != end; addr++) {

}

This has a fencepost error. It won't process the last address in the
range. You'd either need to NOT decrement end, or else write a more
complex loop:

boolean done = false;
int addr = start;
while (!done)
{
process(addr);

if (addr == end) done = true;
addr++;
}

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

robert

Chris Smith escreveu:
This has a fencepost error. It won't process the last address in the
range. You'd either need to NOT decrement end, or else write a more
complex loop:

boolean done = false;
int addr = start;
while (!done)
{
process(addr);

if (addr == end) done = true;
addr++;
}

Thanks guys - well except for Thomas's typical abrasive remarks, this
time because I ommitted the word 'mask', and the diatribe about that
hex, octal and binary being the same...

Anyways, this is how the rough draft turned out, for those who google
on it - start++; and end--; not working but that should be easy to
fix:

package cafe;

import static java.lang.System.out;
import java.util.StringTokenizer;

public class GetIPs {

/** Run the client.
* @param args for main() */
public static void main(String[] args) {
try {
doIPs();
out.println("doIPs completed!!!");

} catch (Exception ex) {
ex.printStackTrace();
}
}

/** Executa a chamada. */
private static void doIPs() {

try {
Integer ip = getIPAsInteger("192.168.200.200");
Integer mask = getIPAsInteger("255.255.0.0");
int start = ip & mask;
int end = start | (~mask);

// Ignore the network number and broadcast address:
start++;
end--;
boolean done = false;
int addr = start;
while (!done) {
getQuadString(addr);
if (addr == end) done = true;
addr++;
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

private static Integer getIPAsInteger(String ip) throws Exception {
if (ip == null) {
return null;
}
int result= 0;
StringTokenizer st= new StringTokenizer(ip, ".");
for (int i= 3; i >= 0; i--) {
result |= Integer.parseInt(st.nextToken()) << (i * 8);
}
return new Integer(result);
}

private static String getQuadString(long address) {
StringBuffer sb = new StringBuffer();
for (int i = 0, shift = 24; i < 4; i++, shift -= 8) {
long value = (address >> shift) & 0xff;
sb.append(value);
if (i != 3)
{
sb.append('.');
}
}
out.println(sb.toString());
return sb.toString();
}
}

Robert
 
C

Chris Smith

robert said:
Thanks guys - well except for Thomas's typical abrasive remarks, this
time because I ommitted the word 'mask', and the diatribe about that
hex, octal and binary being the same...

Nothing in Thomas's post seemed abrasive to me. I regret helping you
now, though. I suspect Thomas does, too.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

robert

Chris Smith escreveu:
Nothing in Thomas's post seemed abrasive to me. I regret helping you
now, though. I suspect Thomas does, too.

Reading the post again, I'm in the wrong here. Thanks for pointing that
out, FWIW.

Robert
 
C

Chris Uppal

Chris said:
Nothing in Thomas's post seemed abrasive to me.

I'm unsure whether it's wise for me to enter this -- is it actually going to
benefit anyone ?

But, FWIW, Thomas's style frequently seems abrasive to me (including the case
in point). Quite often I would call it hostile.

Thomas, I can't offer immediate examples, and anyway I'm not trying to join
any sort of crusade against you, but -- since the question has arisen -- I may
as well say that that's how your responses often strike me. Maybe it's a
language thing....

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top