Jump to content
SubSpace Forum Network

Recommended Posts

Posted

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

Posted (edited)

http://download.oracle.com/javase/1.4.2/docs/api/java/net/InetAddress.html

http://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 by JoWie
Posted (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. :o

 

Thanks again. =)

Edited by Lynx

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...