• Post Reply Bookmark Topic Watch Topic
  • New Topic

Validating a subnet mask  RSS feed

 
Wouter Hermans
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of reusing the code, can't you extend / encapsulate the existing validation class(es)?
 
Wouter Hermans
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!