]> gitweb.michael.orlitzky.com - hath.git/blob - src/Cidr.hs
Removed the Text.Regex.Posix import.
[hath.git] / src / Cidr.hs
1 module Cidr
2 ( Cidr(..),
3 cidr_from_string
4 ) where
5
6 import IPv4Address
7 import ListUtils
8 import Maskbits
9 import Octet
10
11
12 data Cidr = None | Cidr { ipv4address :: IPv4Address,
13 maskbits :: Maskbits }
14 deriving (Eq, Show)
15
16
17 -- Returns the mask portion of a CIDR address. That is, everything
18 -- after the trailing slash.
19 maskbits_from_cidr_string :: String -> Maskbits
20 maskbits_from_cidr_string s =
21 maskbits_from_string ((splitWith (`elem` "/") s) !! 1)
22
23
24 -- Takes an IP address String in CIDR notation, and returns a list of
25 -- its octets (as Ints).
26 octets_from_cidr_string :: String -> [Octet]
27 octets_from_cidr_string s =
28 map octet_from_string (take 4 (splitWith (`elem` "./") s))
29
30
31 cidr_from_string :: String -> Cidr
32 cidr_from_string s
33 | addr == IPv4Address.None = Cidr.None
34 | mbits == Maskbits.None = Cidr.None
35 | otherwise = Cidr addr mbits
36 where
37 addr = ipv4address_from_octets (oct1) (oct2) (oct3) (oct4)
38 oct1 = (octs !! 0)
39 oct2 = (octs !! 1)
40 oct3 = (octs !! 2)
41 oct4 = (octs !! 3)
42 octs = octets_from_cidr_string s
43 mbits = maskbits_from_cidr_string s