Avatar of smile
smileFlag for Germany

asked on 

Need JavaScript's 'isInNet()' in Java

I'm looking for an equivalent of the isInNet() Method from JavaScript (used to build proxy.pac Files). I can't believe that there seems to be no utility Method and I have to construct it 'by hand'.

I'm NOT looking for an example implementation but for any existing classes, that I simply can use. I did not find any network or adress-range methods in the Java API.


// intended simple use of the searched method as follows
// assuming, the name would be IpUtils.isInNet()
 
String address = "192.168.27.5";
String netspec = "192.168.27.0/24";
 
if ( IpUtils.isInNet(address, netspec) ){
   System.out.println( address +" is w/in net "+ netspec);
} else {
   System.out.println( address +" is w/out net "+ netspec);
}

Open in new window

Java

Avatar of undefined
Last Comment
CEHJ
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Haven't looked at this closely but relevant stuff here:

http://js.ibex.org/src/org/ibex/js/ProxyAutoConfig.java
Avatar of smile
smile
Flag of Germany image

ASKER

thanks CEHJ, BUT:

1. the InetAdress API descriptions shows no occurences of mask or range an does not mention any subnet operations.

2. ProxyAutoConfig simply shows an implementation. I did not want to implement it by myself.
By the way: this implementation fails for IPV6.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

1. Yes, that's why i said:

>>and handle the netmask yourself

2. I realise that. I posted the link as there's relevant code in there
Avatar of Mick Barry
Mick Barry
Flag of Australia image

theres nothing that i am aware of, you would probably need to implement it yourself.
I'll ask around and see if anyone knows of anything.

Avatar of Mick Barry
Mick Barry
Flag of Australia image

nothing exists ready to use sorry, the best someone could offer was some internal code from sun (which I haven't even looked at). Posting it here in case it helps

    public boolean isInNet(String host, String pattern, String mask) {
        // int count= number of 255's in mask
        int count = 0;
        int startPos = 0;
        while ((startPos = mask.indexOf("255", startPos + 1)) > -1)
            count++;

        // String tokenize host and pattern with "." as delimeter
        StringTokenizer hostTok = new StringTokenizer(host, ".");
        StringTokenizer patternTok = new StringTokenizer(pattern, ".");

        for (int i = 0; i <= count; i++) {
            if ((!hostTok.hasMoreTokens()) || (!patternTok.hasMoreTokens()))
                return false;
            if (!(hostTok.nextToken()).equals(patternTok.nextToken())) {
                return false;
            }
        }
        return true;

        // compare count times the tokens of host and pattern

        // default impl as per browser's Java Plugin
        // return false;

    }

(Though sun's plugin implementation was been broken prior to java6)

Avatar of smile
smile
Flag of Germany image

ASKER

@objects: again: I don't need implementation suggestions - I can do that myself. But then I have to take care myself about changes in network addressing (IPV6 as most prominent example). I really can't believe, that in twelve years of Java nobody addressed that lack of functionality in java.net.


@CEHJ: thanks. That comes very near to what I'm looking for. But lenya seems to be quite big for that specific use case. Is there any other significally smaller commonly accepted library which supplies an equivalent functionallity?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

realised that, but as there isn't anything available thought I'd mention it.
If you're needing it in a browser environment then you could use the js version.
Sun does have it own implementation but just uses it internally in the plugin.

that lenya code is also busted.

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of smile
smile
Flag of Germany image

ASKER

unbelievable: even the apache commons-net seems to get that functionality late with the upcoming version 2.0. And that fresh feature ist not able to handle IPV6.

Overall, Apache's commons-net-2.0 is an acceptable solution, regarding to my initial question. Thanks to all.

PS: maybe I'll write some code myself and publish it.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

:-)
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo