Checking if an IP is in a subnet range. SubnetUtils.SubnetInfo isInRange bug.
1 Comment Published March 25th, 2009 in Java Tags: .Today I used commons-net to verify when an IP address is in a subnet specified in CIDR notation. So, I got a bug.
This code:
SubnetUtils utils = new SubnetUtils("77.24.0.0/16");
System.out.println("lower address: " + utils.getInfo().getLowAddress());
System.out.println("higher address: " + utils.getInfo().getHighAddress());
System.out.println("77.23.255.254 is in range? :" + utils.getInfo().isInRange( "77.23.255.254" ));
gives this output:
lower address: 77.24.0.1 higher address: 77.24.255.254 77.23.255.254 is in range? :true
Obviously the last ip is not in range...
From the sources you see how the isInRange is implemented:
private boolean isInRange(int address) { return ((address-low()) <= (high()-low())); }
Wrong.
So I fixed in my caller code with a my implementation of isInRange:
private boolean isInRange( SubnetInfo info, String remoteAddr ) {
int address = info.asInteger( remoteAddr );
int low = info.asInteger( info.getLowAddress() );
int high = info.asInteger( info.getHighAddress() );
return low <= address && address <= high;
}
Unfortunately low and high are not accessible, so I had to convert to int from the string representation.
Search
Calendar
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « Feb | Apr » | |||||
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 | |||||
Archives
Categories
- Android (3)
- Apple (26)
- Books (7)
- Eclipse (14)
- Errors (3)
- Firefox (7)
- Git (2)
- Hardware (16)
- Horror Code (8)
- Internet (18)
- Java (98)
- JavaScript (9)
- Life, universe and everything (45)
- Lifehacks (25)
- Linux (50)
- Opinions (25)
- OSX (4)
- Python (1)
- Software (27)
- Speeches and Conferences (8)
- Unix (3)
- Web (21)
- Windows (19)
Tag Cloud
Android apple architecture Bash colors configuration CSS Development Düsseldorf Eclipse germany Git Google Hardware hdr How-To Java JAXB job junit Karmic Linux MacBook music night Open Source Opinion oracle OSX patterns Pitfalls Practices Resume Security Software Suspend TDD Testing tip tonemapped Tricks Ubuntu video Web XML
WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.
Blog License
Blogs I like
Books on the desk
Friends' Blogs
- Antonio Terreno & Valter Bernardini
- Bruno Bossola
- Daniele Galluccio
- Domenico Ventura
- Ed Schepis
- Fabrizio Gianneschi
- Luca Grulla
- Luigi Zanderighi
- Marcello Teodori
- Mida Boghetich
- Muralidharan Chandrasekaran
- Piero Ricca
- Renzo Borgatti
- Simone Bordet
- Simone Bruno
- Uberto Barbini
- Valvolog
- Webtide blogs (Greg Wilkins & Jan Bartel)
Links




















Hi Luigi,
I’ve the same problem with Apache Commons. I’ve downloaded the latest version, and still fails.
I’ve make some “changes” to your code. I’ve changed the “isInRange(String)” method, and not to add new method -like you-
// Doesn’t go OK
// public boolean isInRange(String address) { return isInRange(toInteger(address)); }
private boolean isInRange(int address) { return ((address-low())