»Lynx Posted February 20, 2011 Report Posted February 20, 2011 OKay, so I know a few good Java coders/programmers regularly read the forums here, so I have a quick question. This is more of a "What's the best way of doing this" question... I have a network address (192.168.192.0/24 for example) that I need to use to derive a subnet mask from, and also derive all possible valid IP addresses from (as they need to be allocated/relinquished properly). I can work out the netmasks easy enough, however the allocation of valid IP addresses is confusing me a little. The application I'm writing is Groovy/Grails (I have no choice, it's what my client uses on his servers). I've started by creating a domain class which asks the user for each octet, and then a controller which simply scaffolds the domain. Here's an example: package ipna3 class Network { int octet1 int octet2 int octet3 int octet4 int cidrNotation static constraints = { octet1(blank: false, range: 0..255) octet2(blank: false, range: 0..255) octet3(blank: false, range: 0..255) octet4(blank: false, range: 0..255) cidrNotation(blank: false, range: 0..32) } static mapping = { table 'networks' octet1 column: 'octal_1' octet2 column: 'octal_2' octet3 column: 'octal_3' octet4 column: 'octal_4' cidrNotation column: 'cidr_notation' } } The subnet masks I can calculate easy enough (I'm storing them in another table) however I am having trouble figuring out the best way to ensure that I am allocating valid IP addresses to service users. The client has over 10mln addresses once all blocks are expanded so I can't just pool them all into a table and just check that the address specified doesn't belong to user 'unallocated' as queries take forever... So the method I am thinking of doing is to firstly check if the address is valid, then disallow duplicate values. There's one thing that's miffing me a little, though - what is the best way (algorithmically) to check if an address is valid? I've checked the Java.net packages and I can't seem to find a way in there - and nobody else seems to have done it (unless I'm failing at Google) so how would any of you guys do it? Thanks in advance Quote
JoWie Posted February 20, 2011 Report Posted February 20, 2011 (edited) http://download.oracle.com/javase/1.4.2/docs/api/java/net/InetAddress.htmlhttp://www.2000trainers.com/cisco-ccna-05/ccna-defining-ip-address-ranges-subnets/ The only address you can not use is when the host part is all 0 or all 1 (binary).For example with 1.0.0.0/8 the only reserved addresses are 1.0.0.0 and 1.255.255.255, an address like 1.1.0.0 would be valid to assign. Oh, you probably know this, but you can easily store IP addresses as an integer in databases. Edited February 20, 2011 by JoWie Quote
»Lynx Posted February 20, 2011 Author Report Posted February 20, 2011 (edited) Cheers, that second article is brilliant. I was previously using the Inet data type in my schemas however I was just having a hard time actually storing it as the scaffold didn't inherently know how to display it properly - but I think I have it down now. Thanks again. =) Edited February 20, 2011 by Lynx Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.