--- /dev/null
+module IPv4Address (
+ IPv4Address,
+ ipv4address_from_string,
+ min_octet1,
+ min_octet2,
+ min_octet3,
+ min_octet4,
+ max_octet1,
+ max_octet2,
+ max_octet3,
+ max_octet4
+) where
+
+import Bit
+import Octet
+
+type Maskbits = Int
+
+data IPv4Address = IPv4Address { octet1 :: Octet,
+ octet2 :: Octet,
+ octet3 :: Octet,
+ octet4 :: Octet }
+ deriving (Eq, Show)
+
+
+default_ipv4address :: IPv4Address
+default_ipv4address = IPv4Address (min_octet) (min_octet) (min_octet) (min_octet)
+
+
+ipv4address_from_string :: String -> IPv4Address
+ipv4address_from_string s
+ | length s < 32 = default_ipv4address
+ | otherwise = IPv4Address (Octet a1 a2 a3 a4 a5 a6 a7 a8) (Octet a9 a10 a11 a12 a13 a14 a15 a16)(Octet a17 a18 a19 a20 a21 a22 a23 a24) (Octet a25 a26 a27 a28 a29 a30 a31 a32)
+ where
+ a1 = bit_from_char (s !! 0)
+ a2 = bit_from_char (s !! 1)
+ a3 = bit_from_char (s !! 2)
+ a4 = bit_from_char (s !! 3)
+ a5 = bit_from_char (s !! 4)
+ a6 = bit_from_char (s !! 5)
+ a7 = bit_from_char (s !! 6)
+ a8 = bit_from_char (s !! 7)
+ a9 = bit_from_char (s !! 8)
+ a10 = bit_from_char (s !! 9)
+ a11 = bit_from_char (s !! 10)
+ a12 = bit_from_char (s !! 11)
+ a13 = bit_from_char (s !! 12)
+ a14 = bit_from_char (s !! 13)
+ a15 = bit_from_char (s !! 14)
+ a16 = bit_from_char (s !! 15)
+ a17 = bit_from_char (s !! 16)
+ a18 = bit_from_char (s !! 17)
+ a19 = bit_from_char (s !! 18)
+ a20 = bit_from_char (s !! 19)
+ a21 = bit_from_char (s !! 20)
+ a22 = bit_from_char (s !! 21)
+ a23 = bit_from_char (s !! 22)
+ a24 = bit_from_char (s !! 23)
+ a25 = bit_from_char (s !! 24)
+ a26 = bit_from_char (s !! 25)
+ a27 = bit_from_char (s !! 26)
+ a28 = bit_from_char (s !! 27)
+ a29 = bit_from_char (s !! 28)
+ a30 = bit_from_char (s !! 29)
+ a31 = bit_from_char (s !! 30)
+ a32 = bit_from_char (s !! 31)
+
+
+min_address :: IPv4Address -> Maskbits -> IPv4Address
+min_address addr mask
+ | mask == 32 = IPv4Address oct1 oct2 oct3 oct4
+ | mask == 31 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 a29 a30 a31 Zero)
+ | mask == 30 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 a29 a30 Zero Zero)
+ | mask == 29 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 a29 Zero Zero Zero)
+ | mask == 28 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 Zero Zero Zero Zero)
+ | mask == 27 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 Zero Zero Zero Zero Zero)
+ | mask == 26 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 Zero Zero Zero Zero Zero Zero)
+ | mask == 25 = IPv4Address oct1 oct2 oct3 (Octet a25 Zero Zero Zero Zero Zero Zero Zero)
+ | mask == 24 = IPv4Address oct1 oct2 oct3 (min_octet)
+ | mask == 23 = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 a21 a22 a23 Zero) (min_octet)
+ | mask == 22 = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 a21 a22 Zero Zero) (min_octet)
+ | mask == 21 = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 a21 Zero Zero Zero) (min_octet)
+ | mask == 20 = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 Zero Zero Zero Zero) (min_octet)
+ | mask == 19 = IPv4Address oct1 oct2 (Octet a17 a18 a19 Zero Zero Zero Zero Zero) (min_octet)
+ | mask == 18 = IPv4Address oct1 oct2 (Octet a17 a18 Zero Zero Zero Zero Zero Zero) (min_octet)
+ | mask == 17 = IPv4Address oct1 oct2 (Octet a17 Zero Zero Zero Zero Zero Zero Zero) (min_octet)
+ | mask == 16 = IPv4Address oct1 oct2 (min_octet) (min_octet)
+ | mask == 15 = IPv4Address oct1 (Octet a9 a10 a11 a12 a13 a14 a15 Zero) (min_octet) (min_octet)
+ | mask == 14 = IPv4Address oct1 (Octet a9 a10 a11 a12 a13 a14 Zero Zero) (min_octet) (min_octet)
+ | mask == 13 = IPv4Address oct1 (Octet a9 a10 a11 a12 a13 Zero Zero Zero) (min_octet) (min_octet)
+ | mask == 12 = IPv4Address oct1 (Octet a9 a10 a11 a12 Zero Zero Zero Zero) (min_octet) (min_octet)
+ | mask == 11 = IPv4Address oct1 (Octet a9 a10 a11 Zero Zero Zero Zero Zero) (min_octet) (min_octet)
+ | mask == 10 = IPv4Address oct1 (Octet a9 a10 Zero Zero Zero Zero Zero Zero) (min_octet) (min_octet)
+ | mask == 9 = IPv4Address oct1 (Octet a9 Zero Zero Zero Zero Zero Zero Zero) (min_octet) (min_octet)
+ | mask == 8 = IPv4Address oct1 (min_octet) (min_octet) (min_octet)
+ | mask == 7 = IPv4Address (Octet a1 a2 a3 a4 a5 a6 a7 Zero) (min_octet) (min_octet) (min_octet)
+ | mask == 6 = IPv4Address (Octet a1 a2 a3 a4 a5 a6 Zero Zero) (min_octet) (min_octet) (min_octet)
+ | mask == 5 = IPv4Address (Octet a1 a2 a3 a4 a5 Zero Zero Zero) (min_octet) (min_octet) (min_octet)
+ | mask == 4 = IPv4Address (Octet a1 a2 a3 a4 Zero Zero Zero Zero) (min_octet) (min_octet) (min_octet)
+ | mask == 3 = IPv4Address (Octet a1 a2 a3 Zero Zero Zero Zero Zero) (min_octet) (min_octet) (min_octet)
+ | mask == 2 = IPv4Address (Octet a1 a2 Zero Zero Zero Zero Zero Zero) (min_octet) (min_octet) (min_octet)
+ | mask == 1 = IPv4Address (Octet a1 Zero Zero Zero Zero Zero Zero Zero) (min_octet) (min_octet) (min_octet)
+ | mask == 0 = IPv4Address (min_octet) (min_octet) (min_octet) (min_octet)
+ | otherwise = addr
+ where
+ a1 = (b1 oct1)
+ a2 = (b2 oct1)
+ a3 = (b3 oct1)
+ a4 = (b4 oct1)
+ a5 = (b5 oct1)
+ a6 = (b6 oct1)
+ a7 = (b7 oct1)
+ a9 = (b1 oct2)
+ a10 = (b2 oct2)
+ a11 = (b3 oct2)
+ a12 = (b4 oct2)
+ a13 = (b5 oct2)
+ a14 = (b6 oct2)
+ a15 = (b7 oct2)
+ a17 = (b1 oct3)
+ a18 = (b2 oct3)
+ a19 = (b3 oct3)
+ a20 = (b4 oct3)
+ a21 = (b5 oct3)
+ a22 = (b6 oct3)
+ a23 = (b7 oct3)
+ a25 = (b1 oct4)
+ a26 = (b2 oct4)
+ a27 = (b3 oct4)
+ a28 = (b4 oct4)
+ a29 = (b5 oct4)
+ a30 = (b6 oct4)
+ a31 = (b7 oct4)
+ oct1 = (octet1 addr)
+ oct2 = (octet2 addr)
+ oct3 = (octet3 addr)
+ oct4 = (octet4 addr)
+
+
+
+max_address :: IPv4Address -> Maskbits -> IPv4Address
+max_address addr mask
+ | mask == 32 = IPv4Address oct1 oct2 oct3 oct4
+ | mask == 31 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 a29 a30 a31 One)
+ | mask == 30 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 a29 a30 One One)
+ | mask == 29 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 a29 One One One)
+ | mask == 28 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 One One One One)
+ | mask == 27 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 One One One One One)
+ | mask == 26 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 One One One One One One)
+ | mask == 25 = IPv4Address oct1 oct2 oct3 (Octet a25 One One One One One One One)
+ | mask == 24 = IPv4Address oct1 oct2 oct3 (max_octet)
+ | mask == 23 = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 a21 a22 a23 One) (max_octet)
+ | mask == 22 = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 a21 a22 One One) (max_octet)
+ | mask == 21 = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 a21 One One One) (max_octet)
+ | mask == 20 = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 One One One One) (max_octet)
+ | mask == 19 = IPv4Address oct1 oct2 (Octet a17 a18 a19 One One One One One) (max_octet)
+ | mask == 18 = IPv4Address oct1 oct2 (Octet a17 a18 One One One One One One) (max_octet)
+ | mask == 17 = IPv4Address oct1 oct2 (Octet a17 One One One One One One One) (max_octet)
+ | mask == 16 = IPv4Address oct1 oct2 (max_octet) (max_octet)
+ | mask == 15 = IPv4Address oct1 (Octet a9 a10 a11 a12 a13 a14 a15 One) (max_octet) (max_octet)
+ | mask == 14 = IPv4Address oct1 (Octet a9 a10 a11 a12 a13 a14 One One) (max_octet) (max_octet)
+ | mask == 13 = IPv4Address oct1 (Octet a9 a10 a11 a12 a13 One One One) (max_octet) (max_octet)
+ | mask == 12 = IPv4Address oct1 (Octet a9 a10 a11 a12 One One One One) (max_octet) (max_octet)
+ | mask == 11 = IPv4Address oct1 (Octet a9 a10 a11 One One One One One) (max_octet) (max_octet)
+ | mask == 10 = IPv4Address oct1 (Octet a9 a10 One One One One One One) (max_octet) (max_octet)
+ | mask == 9 = IPv4Address oct1 (Octet a9 One One One One One One One) (max_octet) (max_octet)
+ | mask == 8 = IPv4Address oct1 (max_octet) (max_octet) (max_octet)
+ | mask == 7 = IPv4Address (Octet a1 a2 a3 a4 a5 a6 a7 One) (max_octet) (max_octet) (max_octet)
+ | mask == 6 = IPv4Address (Octet a1 a2 a3 a4 a5 a6 One One) (max_octet) (max_octet) (max_octet)
+ | mask == 5 = IPv4Address (Octet a1 a2 a3 a4 a5 One One One) (max_octet) (max_octet) (max_octet)
+ | mask == 4 = IPv4Address (Octet a1 a2 a3 a4 One One One One) (max_octet) (max_octet) (max_octet)
+ | mask == 3 = IPv4Address (Octet a1 a2 a3 One One One One One) (max_octet) (max_octet) (max_octet)
+ | mask == 2 = IPv4Address (Octet a1 a2 One One One One One One) (max_octet) (max_octet) (max_octet)
+ | mask == 1 = IPv4Address (Octet a1 One One One One One One One) (max_octet) (max_octet) (max_octet)
+ | mask == 0 = IPv4Address (max_octet) (max_octet) (max_octet) (max_octet)
+ | otherwise = addr
+ where
+ a1 = (b1 oct1)
+ a2 = (b2 oct1)
+ a3 = (b3 oct1)
+ a4 = (b4 oct1)
+ a5 = (b5 oct1)
+ a6 = (b6 oct1)
+ a7 = (b7 oct1)
+ a9 = (b1 oct2)
+ a10 = (b2 oct2)
+ a11 = (b3 oct2)
+ a12 = (b4 oct2)
+ a13 = (b5 oct2)
+ a14 = (b6 oct2)
+ a15 = (b7 oct2)
+ a17 = (b1 oct3)
+ a18 = (b2 oct3)
+ a19 = (b3 oct3)
+ a20 = (b4 oct3)
+ a21 = (b5 oct3)
+ a22 = (b6 oct3)
+ a23 = (b7 oct3)
+ a25 = (b1 oct4)
+ a26 = (b2 oct4)
+ a27 = (b3 oct4)
+ a28 = (b4 oct4)
+ a29 = (b5 oct4)
+ a30 = (b6 oct4)
+ a31 = (b7 oct4)
+ oct1 = (octet1 addr)
+ oct2 = (octet2 addr)
+ oct3 = (octet3 addr)
+ oct4 = (octet4 addr)
+
+
+
+min_octet1 :: IPv4Address -> Maskbits -> Octet
+min_octet1 addr mask = octet1 (min_address addr mask)
+
+min_octet2 :: IPv4Address -> Maskbits -> Octet
+min_octet2 addr mask = octet2 (min_address addr mask)
+
+min_octet3 :: IPv4Address -> Maskbits -> Octet
+min_octet3 addr mask = octet3 (min_address addr mask)
+
+min_octet4 :: IPv4Address -> Maskbits -> Octet
+min_octet4 addr mask = octet4 (min_address addr mask)
+
+max_octet1 :: IPv4Address -> Maskbits -> Octet
+max_octet1 addr mask = octet1 (max_address addr mask)
+
+max_octet2 :: IPv4Address -> Maskbits -> Octet
+max_octet2 addr mask = octet2 (max_address addr mask)
+
+max_octet3 :: IPv4Address -> Maskbits -> Octet
+max_octet3 addr mask = octet3 (max_address addr mask)
+
+max_octet4 :: IPv4Address -> Maskbits -> Octet
+max_octet4 addr mask = octet4 (max_address addr mask)