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,
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,