10 -- An Octet consists of eight bits. For our purposes, the most
11 -- significant bit will come "first." That is, b1 is in the 2^7
12 -- place while b8 is in the 2^0 place.
13 data Octet = None | Octet { b1 :: Bit,
24 instance Show Octet where
25 show Octet.None = "None"
26 show oct = show (octet_to_int oct)
29 instance Arbitrary Octet where
31 a1 <- arbitrary :: Gen Bit
32 a2 <- arbitrary :: Gen Bit
33 a3 <- arbitrary :: Gen Bit
34 a4 <- arbitrary :: Gen Bit
35 a5 <- arbitrary :: Gen Bit
36 a6 <- arbitrary :: Gen Bit
37 a7 <- arbitrary :: Gen Bit
38 a8 <- arbitrary :: Gen Bit
39 return (Octet a1 a2 a3 a4 a5 a6 a7 a8)
42 instance Maskable Octet where
43 apply_mask _ Maskbits.None _ = Octet.None
44 apply_mask Octet.None _ _ = Octet.None
45 apply_mask oct mask bit
47 | mask == Seven = oct { b8 = bit }
48 | mask == Six = oct { b8 = bit, b7 = bit }
49 | mask == Five = oct { b8 = bit, b7 = bit, b6 = bit }
50 | mask == Four = oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit }
51 | mask == Three = oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit, b4 = bit }
52 | mask == Two = oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit, b4 = bit, b3 = bit }
53 | mask == Maskbits.One = oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit, b4 = bit, b3 = bit, b2 = bit }
54 | mask == Maskbits.Zero = oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit, b4 = bit, b3 = bit, b2 = bit, b1 = bit }
55 | otherwise = Octet.None
58 -- Convert each bit to its integer value, and multiply by the
59 -- appropriate power of two. Sum them up, and we should get an integer
61 octet_to_int :: Octet -> Int
63 128 * (bit_to_int (b1 x)) +
64 64 * (bit_to_int (b2 x)) +
65 32 * (bit_to_int (b3 x)) +
66 16 * (bit_to_int (b4 x)) +
67 8 * (bit_to_int (b5 x)) +
68 4 * (bit_to_int (b6 x)) +
69 2 * (bit_to_int (b7 x)) +
70 1 * (bit_to_int (b8 x))
74 octet_from_int :: Int -> Octet
76 | (x < 0) || (x > 255) = Octet.None
77 | otherwise = (Octet a1 a2 a3 a4 a5 a6 a7 a8)
79 a1 = if (x >= 128) then B.One else B.Zero
80 a2 = if ((x `mod` 128) >= 64) then B.One else B.Zero
81 a3 = if ((x `mod` 64) >= 32) then B.One else B.Zero
82 a4 = if ((x `mod` 32) >= 16) then B.One else B.Zero
83 a5 = if ((x `mod` 16) >= 8) then B.One else B.Zero
84 a6 = if ((x `mod` 8) >= 4) then B.One else B.Zero
85 a7 = if ((x `mod` 4) >= 2) then B.One else B.Zero
86 a8 = if ((x `mod` 2) == 1) then B.One else B.Zero
89 octet_from_string :: String -> Octet
91 case (reads s :: [(Int, String)]) of
93 x:_ -> octet_from_int (fst x)
96 -- The octet with the least possible value.
98 min_octet = Octet B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero
101 -- The octet with the greatest possible value.
103 max_octet = Octet B.One B.One B.One B.One B.One B.One B.One B.One
108 test_octet_from_int1 :: Test
109 test_octet_from_int1 =
110 TestCase $ assertEqual "octet_from_int 128 should parse as 10000000" oct1 (octet_from_int 128)
112 oct1 = Octet B.One B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero
115 test_octet_mask1 :: Test
117 TestCase $ assertEqual "The network bits of 255/4 should equal 240" oct2 (apply_mask oct1 Four B.Zero)
119 oct1 = octet_from_int 255
120 oct2 = octet_from_int 240
123 test_octet_mask2 :: Test
125 TestCase $ assertEqual "The network bits of 255/1 should equal 128" oct2 (apply_mask oct1 Maskbits.One B.Zero)
127 oct1 = octet_from_int 255
128 oct2 = octet_from_int 128
131 octet_tests :: [Test]
132 octet_tests = [ test_octet_from_int1,