IP Address Manipulation

N

Nick Coleman

I'm trying to write some code to manipulate IP addresses, particularly
to apply a netmask and come up with valid network addresses, host
addresses, broadcast addresses etc.

I know binary, and I've read Roedy's binary page (thank you Roedy), but
I'm having problems with bit arithmetic.

For example, to set a netmask of, say, /8, I do something like
long x = x | 0xFF; // create a mask of 8 bits in the LSB
x = ~x; // then 'shift' it to the MSB of the 4 byte word.

One issue is the '/8' isn't known: it is a parameter to the method.

The problem comes when I try to manipulate the network part of the mask
and the host part of the mask. Because I'm using maths to do it, my
routines aren't working; they start to get confused because the most
significant bit is 1, meaning it is a negative number, and thereby the
maths is not working.

I'm finding it much harder than I thought it would be to manipulate IP
addresses, particularly in the conversion to and from dotted decimal
notation. Is there a API class that knows about things like IP,
netmasks, dotted decimal, etc?

I've looked at java.net and InetAddress, but it seems oriented to real
world IP addresses (i.e it uses DNS), whereas I want to manipulate
theoretical addresses in order to set up a network.

Thanks for any help,
 
S

Steve Horsley

Nick said:
I'm trying to write some code to manipulate IP addresses, particularly
to apply a netmask and come up with valid network addresses, host
addresses, broadcast addresses etc.

I know binary, and I've read Roedy's binary page (thank you Roedy), but
I'm having problems with bit arithmetic.

For example, to set a netmask of, say, /8, I do something like
long x = x | 0xFF; // create a mask of 8 bits in the LSB
x = ~x; // then 'shift' it to the MSB of the 4 byte word.

One issue is the '/8' isn't known: it is a parameter to the method.

The problem comes when I try to manipulate the network part of the mask
and the host part of the mask. Because I'm using maths to do it, my
routines aren't working; they start to get confused because the most
significant bit is 1, meaning it is a negative number, and thereby the
maths is not working.

I'm finding it much harder than I thought it would be to manipulate IP
addresses, particularly in the conversion to and from dotted decimal
notation. Is there a API class that knows about things like IP,
netmasks, dotted decimal, etc?

I've looked at java.net and InetAddress, but it seems oriented to real
world IP addresses (i.e it uses DNS), whereas I want to manipulate
theoretical addresses in order to set up a network.

Thanks for any help,

How about starting with 0xFFFFFFFFL and shifting it right by the number
of bits in the mask. e.g. 0xFFFFFFFFL >> 8 gives 0xFFFFFFL.
XOR to get the network portion:
0xFFFFFFFFL ^ 0xFFFFFFL gives 0xFF000000L

I could be talking rubbish - I haven't tried it.

Steve
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top