module Cidr
( Cidr(..),
- cidr_from_string,
- is_valid_cidr
+ cidr_from_string
) where
-import Text.Regex.Posix
-
import IPv4Address
import ListUtils
+import Maskbits
import Octet
maskbits :: Maskbits }
deriving (Eq, Show)
--- Will return True if the passed String is in CIDR notation, False
--- otherwise.
-is_valid_cidr :: String -> Bool
-is_valid_cidr cidr = cidr =~ "([0-9]{1,3}\\.){3}[0-9]{1,3}/[0-9]{1,2}"
-
-- Returns the mask portion of a CIDR address. That is, everything
-- after the trailing slash.
-maskbits_from_string :: String -> Maskbits
-maskbits_from_string s = read ((splitWith (`elem` "/") s) !! 1)
+maskbits_from_cidr_string :: String -> Maskbits
+maskbits_from_cidr_string s =
+ maskbits_from_string ((splitWith (`elem` "/") s) !! 1)
-- Takes an IP address String in CIDR notation, and returns a list of
-- its octets (as Ints).
-octets_from_string :: String -> [Int]
-octets_from_string s = map read (take 4 (splitWith (`elem` "./") s))
+octets_from_cidr_string :: String -> [Octet]
+octets_from_cidr_string s =
+ map octet_from_string (take 4 (splitWith (`elem` "./") s))
cidr_from_string :: String -> Cidr
cidr_from_string s
- | addr == IPv4Address.None = Cidr.None
+ | addr == IPv4Address.None = Cidr.None
+ | mbits == Maskbits.None = Cidr.None
| otherwise = Cidr addr mbits
where
addr = ipv4address_from_octets (oct1) (oct2) (oct3) (oct4)
- oct1 = octet_from_int (octs !! 0)
- oct2 = octet_from_int (octs !! 1)
- oct3 = octet_from_int (octs !! 2)
- oct4 = octet_from_int (octs !! 3)
- octs = octets_from_string s
- mbits = maskbits_from_string s
+ oct1 = (octs !! 0)
+ oct2 = (octs !! 1)
+ oct3 = (octs !! 2)
+ oct4 = (octs !! 3)
+ octs = octets_from_cidr_string s
+ mbits = maskbits_from_cidr_string s