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