aspose file tools
The moose likes Beginning Java and the fly likes Validating a subnet mask Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Validating a subnet mask" Watch "Validating a subnet mask" New topic
Author

Validating a subnet mask

Wouter Hermans
Greenhorn

Joined: Oct 16, 2010
Posts: 13
I'm working on some code that handles IPs entered as strings and stored as arraylists of integers. That part of the code is up and running.

Now I want to be able to enter subnet masks. This re-uses most of the code, but requires some extra validation (since only a few addresses are valid as subnet masks - see here). However, I'm not sure how to handle this since subnet masks follow some very specific rules.

Plan B is to just check against all valid subnet masks but that seems clumsy. Has anyone else had this problem before? Can anyone point me in the right direction to solve this?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19

Wouter Hermans wrote: However, I'm not sure how to handle this since subnet masks follow some very specific rules.


Take a look at all the valid submasks -- but this time examine it in binary. You will see that it follows a very specific pattern. And you can easily write a routine to validate it.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Instead of reusing the code, can't you extend / encapsulate the existing validation class(es)?


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Wouter Hermans
Greenhorn

Joined: Oct 16, 2010
Posts: 13
Rob Spoor wrote:Instead of reusing the code, can't you extend / encapsulate the existing validation class(es)?


(Sorry for the late reply)

The subnet mask code extends my IP class and reuses the bulk of its code, but the rules for subnets are slightly different.

As for the binary suggestion, that definitely seems like a good idea. Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Wouter Hermans wrote:
Rob Spoor wrote:Instead of reusing the code, can't you extend / encapsulate the existing validation class(es)?

The subnet mask code extends my IP class and reuses the bulk of its code, but the rules for subnets are slightly different.

I know. Basically you already followed my advice before I even gave it. I just thought that when you said "re-uses most of the code" you copy-pasted the code, but you actually meant inherited the methods and their behaviour.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Validating a subnet mask
 
Similar Threads
how to a do broadcast udp with java nio
Broadcast vs Subnet
Java endian and bit order and signed and unsigned and python and more confusion than I can deal with
RedHat Ethernet adapter problem
To swap the byte in long using bit