• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Validating a subnet mask

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22822
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22822
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic