How to manipulate IP addresses

N

Nick Coleman

I'm trying to write some code that will manipulate IP addresses,
particularly with a netmask, to generate valid network addresses and
valid host addresses.

For example, I would like to provide an IP address, apply a netmask, and
come up with valid network addresses, valid host addresses, broadcast
address, etc, in dotted decimal form.

I've tried things like:
// netmask = /8
long x = 0xff; // netmask in LSB
x = ~x // 'shift' netmask to MSB

netmask is not known, it is an input parameter to the method.

I know binary, and I've read Roedy's page on binary (thanks Roedy).
However, because I'm using maths to manipulate the mask and the
underlying IP address (to generate dotted decimal, for one thing), it
is failing as the most significant bit is '1', which means the long is
negative, therefore the maths is failing. This particularly applies to
converting dotted decimal and vice versa.

I've read java.net and InetAddress, but it seems oriented to real world
IP address space, whereas I am trying to manipulate IP addresses in a
theoretical space, suitable for doing things like setting up a network.

Is there an API class that knows about IP, netmask, dotted decimal, etc?

Many thanks,
 
R

Roedy Green

I know binary, and I've read Roedy's page on binary (thanks Roedy).
However, because I'm using maths to manipulate the mask and the
underlying IP address (to generate dotted decimal, for one thing), it
is failing as the most significant bit is '1', which means the long is
negative, therefore the maths is failing. This particularly applies to
converting dotted decimal and vice versa.

see http://mindprod.com/jgloss/masking.html

It demonstrates some IP bit fiddling in the examples.

If you want me to add some more IP pet tricks, just ask with a
specific request.
 
N

Nick Coleman

Roedy said:
see http://mindprod.com/jgloss/masking.html

It demonstrates some IP bit fiddling in the examples.

If you want me to add some more IP pet tricks, just ask with a
specific request.

Thanks Roedy, that gave me some ideas and, woo-hoo, I've got it working
now.

Again, thank you for your website.

PS. sorry about the double post. My newsreader crashed half-way through
posting.
 
R

Roedy Green

Thanks Roedy, that gave me some ideas and, woo-hoo, I've got it working
now.

good going. Is there anything you came up with you think might be of
general interest? I might add it to the masking or IP entries.
 
N

Nick Coleman

Roedy said:
good going. Is there anything you came up with you think might be of
general interest? I might add it to the masking or IP entries.

Nothing specific really; your masking glossary entry was very helpful in
getting me to think of the int as a sequence of bits rather than a
number and in terms of manipulating bits instead of doing arithmetic
(which would generate overflows, or use the sign bit incorrectly).
Your dottedQuad routine re-inforced that type of thinking with a
practical example.

So, the greatest benefit was in getting me to think in a different way
and in showing me how to do that.

thanks again,
 

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
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top