hermanverschooten / ip / Subnet

This library contains a number of functions for working with subnets. It can validate subnet mask strings, see if an address is inside a given subnet.

Validation

validate : String -> Basics.Bool

Check to see if the given subnet is valid.

validate "255.255.255.0" == True

validate "255.0.255.128" == False

CIDR

fromCIDR : Basics.Int -> Maybe String

Get the String representation for a given CIDR.

fromCIDR 24 == Just "255.255.255.0"

toCIDR : String -> Basics.Int

Calculates the CIDR length in bits from a subnet mask.

toCIDR "255.255.255.0" == 24

toCIDR "255.255.254.0" == 23

Common functionality

base : ( String, String ) -> Maybe String

Returns the base IP of the given subnet.

base ( "192.168.1.25", "255.255.255.0" ) == Just "192.168.1.0"

base ( "192.168.1.25", "255.255.255.252" ) == Just "192.168.1.24"

base ( "invalid ip", "or invalid subnet" ) == Nothing

hosts : String -> Basics.Int

Give a subnet mask, calculates the number of host possible.

hosts "255.255.255.0" == 256

included : ( String, String ) -> String -> Basics.Bool

Is the IP address in the subnet.

included ( "192.168.1.0", "255.255.255.0" ) "192.168.1.5" == True

included ( "192.168.17.0", "255.255.254.0" ) "192.168.18.5" == False