How to incremet IndetAddress / IP numbers

M

Markus Kern

Hey,

i am coding a simple port scanner.
now i want to make it scan a whole range of ip numbers. how can i
increment the datatype InetAddress? i checked all it's methodes, but
i can't see a nice solution for that. you could cast it to string and split
it into int octets, but thats not really nice i think.

then i thought about the getAddress methode, but look at this code:

public class test {

public static void main(String[] args) throws UnknownHostException {
InetAddress test = null;
test = InetAddress.getByName("200.120.254.255");
byte[] ip = test.getAddress();
for (int i = 0; i<ip.length; i++)
System.out.println (ip);
}
}

output:
-56
120
-2
-1

so this dosn't help me really - i thought i get the real octets then and could write
a methode to inc them, but like this i don't really know what to do.

i think you understood my problem.
maybe someone has a nice solution for this.
(btw i googeled alot for ip/port scanner java source code - but couldn't find help
do you got a hint where to find code snipplets etc)

thanx in advance.
markus
 
P

Paul Lutus

Markus said:
Hey,

i am coding a simple port scanner.
now i want to make it scan a whole range of ip numbers. how can i
increment the datatype InetAddress?

Your inquiry has been asked and answered, in a thread you started in
comp.lang.java.help on 9/14/2004. Don't ask the same question again.

If you post to Usenet, you are expected to read the replies. If you do not
read the replies, you can expect us not to try again. It is just that
simple.
 
M

Markus Kern

i am sorry, but your answer in java.help didn't help
me out really. and no one replyed to further posts.

so i thought of posting the question here again, maybe
i get some more ideas here.

i am sorry that i don't know how to do crosspostings -
post one thread into several newsgroups.

Markus
 
P

Paul Lutus

Markus said:
i am sorry, but your answer in java.help didn't help
me out really.

What? I showed you how to increment an InetAddress instance, with a working
code sample than increments an InetAddress instance. How exactly does that
not reply to your request to be shown how to increment an InetAddress
instance?
and no one replyed to further posts.

That is because your inquiry had been answered in full.
so i thought of posting the question here again, maybe
i get some more ideas here.

Ideas like what? Yo asked the exact same question again, after receiving a
working code sample that does what you want.

If you cannot figure out how to integrate my code into a full port scanner,
then it is too soon for you to be asking for help, because what you need is
a programmer, not an advisor.
i am sorry that i don't know how to do crosspostings -
post one thread into several newsgroups.

That is not a good idea in any case. You are doing fine by posting only to
one group, all else aside.
 
J

John B. Matthews

Paul Lutus said:
Your inquiry has been asked and answered, in a thread you started in
comp.lang.java.help on 9/14/2004.
[...]

I had missed this thread on c.l.j.h. Before seeing your very
illuminating code, I had tried the following:

public static InetAddress inc(InetAddress ip)
throws UnknownHostException {
byte[] b = ip.getAddress();
b[3]++;
if (b[3] == 0) b[2]++;
if (b[2] == 0) b[1]++;
if (b[1] == 0) b[0]++;
return InetAddress.getByAddress(b);
}

It seems to work, but I'm vaguely uneasy about relying on
wrap-around with signed integer arithmetic. In particular, the
(post) increment operator is subject to binary numeric promotion
(and subsequent narrowing), while the shift operator is not. Is
this a problem?

Thanks for any insight.

John
 
P

Paul Lutus

John said:
Paul Lutus said:
Your inquiry has been asked and answered, in a thread you started in
comp.lang.java.help on 9/14/2004.
[...]

I had missed this thread on c.l.j.h. Before seeing your very
illuminating code, I had tried the following:

public static InetAddress inc(InetAddress ip)
throws UnknownHostException {
byte[] b = ip.getAddress();
b[3]++;
if (b[3] == 0) b[2]++;
if (b[2] == 0) b[1]++;
if (b[1] == 0) b[0]++;
return InetAddress.getByAddress(b);
}

It seems to work, but I'm vaguely uneasy about relying on
wrap-around with signed integer arithmetic.

There is no need to use this approach. Just increment an integer or a long,
dismantle a copy into byte components, and use the bytes to construct the
IP address, as in my prior code example.
 
J

John B. Matthews

Paul Lutus said:
John said:
Paul Lutus said:
Markus Kern wrote:

Hey,

i am coding a simple port scanner.
now i want to make it scan a whole range of ip numbers. how can i
increment the datatype InetAddress?

Your inquiry has been asked and answered, in a thread you started in
comp.lang.java.help on 9/14/2004.
[...]

I had missed this thread on c.l.j.h. Before seeing your very
illuminating code, I had tried the following:
[my erroneous code deleted]

It seems to work, but I'm vaguely uneasy about relying on
wrap-around with signed integer arithmetic.

There is no need to use this approach. Just increment an integer or a long,
dismantle a copy into byte components, and use the bytes to construct the
IP address, as in my prior code example.

Yes, I see. You proposed something like this:

public static InetAddress toInetAddress(int addr)
throws UnknownHostException {
byte[] b = new byte[4];
b[3] = (byte) addr;
addr >>= 8;
b[2] = (byte) addr;
addr >>= 8;
b[1] = (byte) addr;
addr >>= 8;
b[0] = (byte) addr;
return InetAddress.getByAddress(b);
}

The inverse would just combine the bytes:

public static int toInt(InetAddress ip)
throws UnknownHostException {
byte[] b = ip.getAddress();
int addr = 0;
for (int i = 0; i < 4; i++) {
addr |= (((int) b) & 0xff) << (8 * (3 - i));
}
return addr;
}

For reference, my original code was erroneous. The correction is
as follows:

public static InetAddress inc(InetAddress ip) throws
UnknownHostException {
byte[] b = ip.getAddress();
b[3]++;
if (b[3] == 0) {
b[2]++;
if (b[2] == 0) {
b[1]++;
if (b[1] == 0) {
b[0]++;
}
}
}
return InetAddress.getByAddress(b);
}

Sorry, I just can't resist bit-twidling in every language I
learn:)

John
 
P

Paul Lutus

John B. Matthews wrote:

/ ...
I had missed this thread on c.l.j.h. Before seeing your very
illuminating code, I had tried the following:
[my erroneous code deleted]

It seems to work, but I'm vaguely uneasy about relying on
wrap-around with signed integer arithmetic.

There is no need to use this approach. Just increment an integer or a
long, dismantle a copy into byte components, and use the bytes to
construct the IP address, as in my prior code example.

Yes, I see. You proposed something like this:

public static InetAddress toInetAddress(int addr)
throws UnknownHostException {
byte[] b = new byte[4];
b[3] = (byte) addr;
addr >>= 8;
b[2] = (byte) addr;
addr >>= 8;
b[1] = (byte) addr;
addr >>= 8;
b[0] = (byte) addr;
return InetAddress.getByAddress(b);
}

Yes. Kind of ugly, isn't it? How about this instead:

byte[] b = new byte[4];
int addr = 0x12345678;
for(int i = 3;i >= 0;i--) {
b = (byte) addr;
addr >>= 8;
}

This is one of those code examples where creating a loop only marginally
improves its looks or its length.
The inverse would just combine the bytes:

public static int toInt(InetAddress ip)
throws UnknownHostException {
byte[] b = ip.getAddress();
int addr = 0;
for (int i = 0; i < 4; i++) {
addr |= (((int) b) & 0xff) << (8 * (3 - i));
}
return addr;
}

For reference, my original code was erroneous. The correction is
as follows:

public static InetAddress inc(InetAddress ip) throws
UnknownHostException {
byte[] b = ip.getAddress();
b[3]++;
if (b[3] == 0) {
b[2]++;
if (b[2] == 0) {
b[1]++;
if (b[1] == 0) {
b[0]++;
}
}
}
return InetAddress.getByAddress(b);
}

Sorry, I just can't resist bit-twidling in every language I
learn:)


Cute, but IMHO the form that increments an integer is much cleaner. All but
the part where you must convert to four bytes. :)

On that topic, I can't believe there is no InetAddress creation method that
accepts an integer.
 
M

Markus Kern

i now got another problem,
what's a good way to compare two instances of InetAddress.
I want to check if one InetAddress is smaller that another.

Greetings
Markus
 
M

Markus Kern

btw, i solved the incrementing problem like this :

----
public static InetAddress incIP(InetAddress address) {
byte[] ip = address.getAddress();
for (int i = ip.length - 1; i >= 0; i--) {
if (ip < Byte.MAX_VALUE) {
ip++;
if (ip != 0) {
break;
}
} else { // ip == Byte.MAX_VALUE
ip = Byte.MIN_VALUE;
break;
}
}
InetAddress inc = null;
try {
inc = InetAddress.getByAddress(ip);
} catch (UnknownHostException e) {
//sollte nicht passieren
e.printStackTrace();
}
return inc;
}
----

anyway, i am no working on a method that can compare
two InetAddress instances. I want to check if the the first
arument is smaller then the other (for syntax check of the
port scanner).

i implemented it like this, but really don't like the code at
all, i am sure you can solve it wy better...

----
public class smallerThan1 {

public static boolean smallerThan (InetAddress x, InetAddress y){
int count1 = 1;
byte[] a = x.getAddress();
for (int i = 0; i < a.length-1; i++){
if (a != 0) {
count1 = count1 * a; // * 255 wird zu groß
}
else if (i > 0){
count1 = count1 * 1; // * 255 wird zu groß
}
}
count1 += a[3];

int count2 = 1;
byte[] b = y.getAddress();
for (int i = 0; i < b.length-1; i++){
if (b != 0) {
count2 = count2 * b; // * 255 wird zu groß
}
else if (i > 0){
count2 = count2 * 1; // * 255 wird zu groß
}
}
count2 += b[3];

if (count1 - count2 <= 0) {
return true;
}
else {
return false;
}
}
----

any hints appreciated ! =)

the next thing would be, check how many host there are in an
ip-range. i want to use this to implement a progress meter of the
scan.
if the user would try the range 0.0.0.0-255.255.255.255 you
will get a really big number of hosts.
Anyone got a question how to code such a progress meter sohow
else?

Thanx in advace for all you answers!
greetings
Markus

p.s.may i perhaps open a new thread for these new questions ?
 
M

Markus Kern

shit! i am sorry the code of smallerThan doesn't seem to
work correctly =/.
i am lacking ideas now - i hope someone helps me out.

Markus
 
P

Paul Lutus

Markus said:
i now got another problem,
what's a good way to compare two instances of InetAddress.
I want to check if one InetAddress is smaller that another.

Compare the integers I had you create previously. You cannot apply a
criterion such as "smaller" to an InetAddress, but you can to an integer.
 
P

Paul Lutus

Markus said:
btw, i solved the incrementing problem like this :

This is not a solution. Create integers, convert the integers to bytes as I
showed you.

/ ...
anyway, i am no working on a method that can compare
two InetAddress instances. I want to check if the the first
arument is smaller then the other (for syntax check of the
port scanner).

Use the integers I showed you. You cannot compare two InetAddresses in the
sense of "smaller".
i implemented it like this, but really don't like the code at
all, i am sure you can solve it wy better...

I already have. Go back to the code I already posted.
 
P

Paul Lutus

Markus said:
shit! i am sorry the code of smallerThan doesn't seem to
work correctly =/.

I already solved this problem. Use the code that has already been posted.
i am lacking ideas now - i hope someone helps me out.

What? Use the code that has been posted already.

Perhaps you can say what is wrong with the solution that has already been
offered.
 
J

John B. Matthews

Paul Lutus said:
This is not a solution.

Indeed, you (and I) are (were) attempting to propagate a carry
in software; let the hardware do it.
Create integers, convert the integers to bytes as I
showed you.

Or, if you really must start with an InetAddress, use the
toInt() code above.
Use the integers I showed you. You cannot compare two InetAddresses in the
sense of "smaller".

I already have. Go back to the code I already posted.

John
 
B

Babu Kalakrishnan

Paul said:
Compare the integers I had you create previously. You cannot apply a
criterion such as "smaller" to an InetAddress, but you can to an integer.

A simple integer comparison will not work if the desired order is
0.0.0.0 to 255.255.255.255. You really need unsigned integer comparison.

BK
 
P

Paul Lutus

Babu said:
A simple integer comparison will not work if the desired order is
0.0.0.0 to 255.255.255.255. You really need unsigned integer comparison.

Yes, quite so, but this approach and its variants is far simpler than what
has been proposed until now. I suggest the use of a long for the purposes
of simplifying the comparison, in which case the previously posted code
will obligingly break it into the required bytes.
 
M

matthews

Markus Kern said:
btw, i solved the incrementing problem like this :

----
public static InetAddress incIP(InetAddress address) {
byte[] ip = address.getAddress();
for (int i = ip.length - 1; i >= 0; i--) {
if (ip < Byte.MAX_VALUE) {
ip++;
if (ip != 0) {
break;
}
} else { // ip == Byte.MAX_VALUE
ip = Byte.MIN_VALUE;
break;
}
}
InetAddress inc = null;
try {
inc = InetAddress.getByAddress(ip);
} catch (UnknownHostException e) {
//sollte nicht passieren
e.printStackTrace();
}
return inc;
}


This seems awkward to me. The platform uses signed, twos-complement
arithmetic; there is no overflow. Using an integral intermediate is
really the easiest route; but if you are determined to go down this
road, consider the recursive version:

public static byte[] incByteArray(byte[] b, int i) {
if (i >= 0) {
b++;
if (b == 0) b = incByteArray(b, i - 1);
}
return b;
}

public static InetAddress incIP(InetAddress ip)
throws UnknownHostException {
byte[] b = ip.getAddress();
incByteArray(b, b.length - 1);
return InetAddress.getByAddress(b);
}
 
J

John B. Matthews

matthews said:
Markus Kern said:
btw, i solved the incrementing problem like this :

----
public static InetAddress incIP(InetAddress address) {
byte[] ip = address.getAddress();
for (int i = ip.length - 1; i >= 0; i--) {
if (ip < Byte.MAX_VALUE) {
ip++;
if (ip != 0) {
break;
}
} else { // ip == Byte.MAX_VALUE
ip = Byte.MIN_VALUE;
break;
}
}
InetAddress inc = null;
try {
inc = InetAddress.getByAddress(ip);
} catch (UnknownHostException e) {
//sollte nicht passieren
e.printStackTrace();
}
return inc;
}


This seems awkward to me. The platform uses signed, twos-complement
arithmetic; there is no overflow. Using an integral intermediate is
really the easiest route; but if you are determined to go down this
road, consider the recursive version:

public static byte[] incByteArray(byte[] b, int i) {
if (i >= 0) {
b++;
if (b == 0) b = incByteArray(b, i - 1);
}
return b;
}

public static InetAddress incIP(InetAddress ip)
throws UnknownHostException {
byte[] b = ip.getAddress();
incByteArray(b, b.length - 1);
return InetAddress.getByAddress(b);
}


Or this plain, old iterative version:

public static InetAddress inc(InetAddress ip)
throws UnknownHostException {
byte[] b = ip.getAddress();
int i = b.length - 1;
b++;
int carry = b == 0 ? 1 : 0;
while (i-- > 0) {
b += carry;
carry = carry > 0 && b == 0 ? 1 : 0;
}
return InetAddress.getByAddress(b);
}
 

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,262
Messages
2,571,052
Members
48,769
Latest member
Clifft

Latest Threads

Top