1 -- | An IPv4 address pattern has four fields separated by ".". Each
2 -- field is either a decimal number, or a sequence inside "[]" that
3 -- contains one or more ";"-separated decimal numbers or
4 -- number..number ranges.
6 -- Thus, any pattern field can be a sequence inside "[]", but a "[]"
7 -- sequence cannot span multiple address fields, and a pattern field
8 -- cannot contain both a number and a "[]" sequence at the same
11 -- This means that the pattern 1.2.[3.4] is not valid (the sequence
12 -- [3.4] cannot span two address fields) and the pattern
13 -- 1.2.3.3[6..9] is also not valid (the last field cannot be both
14 -- number 3 and sequence [6..9] at the same time).
16 -- The syntax for IPv4 patterns is as follows:
18 -- v4pattern = v4field "." v4field "." v4field "." v4field
19 -- v4field = v4octet | "[" v4sequence "]"
20 -- v4octet = any decimal number in the range 0 through 255
21 -- v4sequence = v4seq_member | v4sequence ";" v4seq_member
22 -- v4seq_member = v4octet | v4octet ".." v4octet
28 import Test.Tasty ( TestTree, testGroup )
29 import Test.Tasty.HUnit ( (@?=), testCase )
39 import Text.Parsec.String ( Parser )
40 import Text.Read ( readMaybe )
42 import Pretty ( Pretty(..) )
47 -- | An ipv4 octet; that is, an integer between @0@ and @255@
48 -- inclusive. This is the data type corresponding to a \"v4octet\"
49 -- in the postscreen parser.
51 newtype IPv4Octet = IPv4Octet Int
55 instance Pretty IPv4Octet where
56 pretty_show (IPv4Octet x) = show x
59 -- | Parse an IPv4 octet, which should contain a string of digits.
60 -- Should fail if the parsed integer does not lie between @0@ and
65 -- >>> import Text.Parsec ( parseTest )
67 -- Standard octets are parsed correctly:
69 -- >>> parseTest v4octet "0"
72 -- >>> parseTest v4octet "127"
75 -- >>> parseTest v4octet "255"
78 -- Non-digit input throws an error:
80 -- >>> parseTest v4octet "Hello, World!"
81 -- parse error at (line 1, column 1):
85 -- If we're given an integer outside the range @0..255@ (i.e. not a
86 -- valid octet), we fail:
88 -- >>> parseTest v4octet "9000"
89 -- parse error at (line 1, column 5):
90 -- unexpected end of input
92 -- Octet "9000" must be between 0 and 255.
94 v4octet :: Parser IPv4Octet
97 case ( readMaybe s :: Maybe Int ) of
98 -- If "many1 digit" gives us a list of digits, we should be able
99 -- to convert that to an Int! It will overflow rather than fail
100 -- if the input is too big/small, so it should really always
102 Nothing -> unexpected "v4octet: readMaybe failed on a sequence of digits!"
104 -- If we got an Int, make sure it's actually a representation of
106 Just k -> if 0 <= k && k <= 255
107 then return (IPv4Octet k)
108 else fail ("Octet \"" ++ (show k)
109 ++ "\" must be between 0 and 255.")
114 -- * Sequence members
117 -- | An ipv4 \"sequence member\". A sequence member is either an
118 -- integer (an octet) or a range of integers (contained in an
119 -- octet). This data type corresponds to \"v4seq_member\" in the
120 -- postscreen parser.
122 data IPv4SequenceMember =
123 IPv4SequenceMemberOctet IPv4Octet
124 | IPv4SequenceMemberOctetRange IPv4Octet IPv4Octet
128 instance Pretty IPv4SequenceMember where
129 pretty_show (IPv4SequenceMemberOctet octet) = pretty_show octet
130 pretty_show (IPv4SequenceMemberOctetRange octet1 octet2) =
131 (pretty_show octet1) ++ ".." ++ (pretty_show octet2)
134 -- | Parse an IPv4 \"sequence member\". A sequence member is either an
135 -- octet, or a start..end sequence (like an enumeration, in Haskell).
139 -- >>> import Text.Parsec ( parseTest )
141 -- >>> parseTest v4seq_member "127"
142 -- IPv4SequenceMemberOctet (IPv4Octet 127)
144 -- >>> parseTest v4seq_member "1..5"
145 -- IPv4SequenceMemberOctetRange (IPv4Octet 1) (IPv4Octet 5)
147 v4seq_member :: Parser IPv4SequenceMember
148 v4seq_member = try both <|> just_one
154 return $ IPv4SequenceMemberOctetRange oct1 oct2
156 just_one = fmap IPv4SequenceMemberOctet v4octet
162 -- | An ipv4 \"sequence\". A sequence contains either a single
163 -- \"sequence member\" (see 'IPv4SequenceMember'), or a sequence
164 -- member along with another sequence. So, this is a potentially
165 -- recursive definition. This type corresponds to \"v4sequence\" in
166 -- the postscreen parser.
169 IPv4SequenceSingleMember IPv4SequenceMember
170 | IPv4SequenceOptions IPv4SequenceMember IPv4Sequence
174 instance Pretty IPv4Sequence where
175 pretty_show (IPv4SequenceSingleMember member) = pretty_show member
176 pretty_show (IPv4SequenceOptions member subsequence) =
177 (pretty_show member) ++ ";" ++ (pretty_show subsequence)
180 -- | Parse an IPv4 \"sequence\". A sequence is whatever is allowed
181 -- within square brackets. Basically it can be three things:
183 -- * An octet (number).
184 -- * A range of addresses in start..end format.
185 -- * An alternative, separated by a semicolon, where each side
186 -- contains one of the previous two options.
190 -- >>> import Text.Parsec ( parseTest )
191 -- >>> parseTest v4sequence "1"
192 -- IPv4SequenceSingleMember (IPv4SequenceMemberOctet (IPv4Octet 1))
194 -- >>> pretty_print $ parse v4sequence "" "1..2"
197 -- >>> pretty_print $ parse v4sequence "" "1..2;8"
200 v4sequence :: Parser IPv4Sequence
201 v4sequence = try both <|> just_one
207 return $ IPv4SequenceOptions sm s
209 just_one = fmap IPv4SequenceSingleMember v4seq_member
215 data IPv4Field = IPv4FieldOctet IPv4Octet | IPv4FieldSequence IPv4Sequence
219 instance Pretty IPv4Field where
220 pretty_show (IPv4FieldOctet octet) = pretty_show octet
221 pretty_show (IPv4FieldSequence s) = "[" ++ (pretty_show s) ++ "]"
224 -- | Parse an IPv4 \"field\", which is either a boring old octet, or a
225 -- 'v4sequence' within square brackets.
229 -- >>> import Text.Parsec ( parseTest )
230 -- >>> parseTest v4field "127"
231 -- IPv4FieldOctet (IPv4Octet 127)
233 -- >>> pretty_print $ parse v4field "" "[127]"
236 v4field :: Parser IPv4Field
237 v4field = just_octet <|> brackets
239 just_octet = fmap IPv4FieldOctet v4octet
245 return $ IPv4FieldSequence s
252 IPv4Pattern IPv4Field IPv4Field IPv4Field IPv4Field
256 instance Pretty IPv4Pattern where
257 pretty_show (IPv4Pattern f1 f2 f3 f4) =
258 (pretty_show f1) ++ "."
266 -- | Parse an ipv4 address pattern. This consists of four fields,
267 -- separated by periods, where a field is either a simple octet or a
270 -- See also: 'v4field', 'v4sequence'.
274 -- >>> pretty_print $ parse v4pattern "" "127.0.0.1"
277 -- >>> pretty_print $ parse v4pattern "" "127.0.[1..3].1"
280 -- >>> pretty_print $ parse v4pattern "" "127.0.[1..3;8].1"
283 -- In the module intro, it is mentioned that this is invalid:
285 -- >>> import Text.Parsec ( parseTest )
286 -- >>> parseTest v4pattern "1.2.[3.4]"
287 -- parse error at (line 1, column 7):
289 -- expecting digit or "]"
291 -- This one is /also/ invalid; however, we'll parse the valid part off
294 -- >>> pretty_print $ parse v4pattern "" "1.2.3.3[6..9]"
297 v4pattern :: Parser IPv4Pattern
306 return $ IPv4Pattern field1 field2 field3 field4
312 -- | Enumerate the members of an 'IPv4SequenceMember'. A sequence
313 -- member is either an octet, which is easy to enumerate -- we just
314 -- print it -- or an octet range whose members can be enumerated
315 -- from least to greatest.
317 -- We enumerate strings instead of integers because the big picture
318 -- is that we will be listing out patterns of ipv4 addresses, and
319 -- those are represented as strings (dotted quad format).
323 -- >>> let (Right r) = parse v4seq_member "" "127"
324 -- >>> sequence_members r
327 -- >>> let (Right r) = parse v4seq_member "" "127..135"
328 -- >>> sequence_members r
329 -- ["127","128","129","130","131","132","133","134","135"]
331 sequence_members :: IPv4SequenceMember -> [String]
332 sequence_members (IPv4SequenceMemberOctet (IPv4Octet i)) = [show i]
333 sequence_members (IPv4SequenceMemberOctetRange (IPv4Octet s) (IPv4Octet t)) =
334 [show x | x <- [s .. t]]
337 -- | Enumerate the members of an ipv4 sequence. These consist of
338 -- either a single sequence member (in which case we delegate to
339 -- 'sequence_members'), or an \"option\" which is enumerated
344 -- >>> let (Right r) = parse v4sequence "" "1"
348 -- >>> let (Right r) = parse v4sequence "" "1..2"
352 -- >>> let (Right r) = parse v4sequence "" "1..3;4;5..9"
354 -- ["1","2","3","4","5","6","7","8","9"]
356 sequences :: IPv4Sequence -> [String]
357 sequences (IPv4SequenceSingleMember sm) =
359 sequences (IPv4SequenceOptions sm s) =
360 (sequence_members sm) ++ (sequences s)
363 -- | Enumerate the members of an 'IPv4Field'. If the field contains a
364 -- single 'IPv4Octet', we simply 'show' it. Otherwise it contains an
365 -- 'IPv4FieldSequence', and we enumerate that recursively using
370 -- >>> let (Right r) = parse v4field "" "1"
374 -- >>> let (Right r) = parse v4field "" "[127..135]"
376 -- ["127","128","129","130","131","132","133","134","135"]
378 fields :: IPv4Field -> [String]
379 fields (IPv4FieldOctet (IPv4Octet i)) = [show i]
380 fields (IPv4FieldSequence s) = sequences s
383 -- | Enumerate the addresses represented by a given 'IPv4Pattern'.
385 -- A pattern contains four fields, sepearated by period
386 -- characters. We want to list all possible combinations of
387 -- addresses where the first octet comes from the first field, the
388 -- second octet comes from the second field... and so on. To do
389 -- this, we take advantage of the List monad and the fact that
390 -- 'fields' returns a list of 'String's.
396 -- >>> let (Right r) = parse v4pattern "" "127.0.0.1"
400 -- Anything between 127.0.0.2 and 127.0.0.4, and either 127.0.0.10
403 -- >>> let (Right r) = parse v4pattern "" "127.0.0.[2..4;10;11]"
405 -- ["127.0.0.2","127.0.0.3","127.0.0.4","127.0.0.10","127.0.0.11"]
407 addresses :: IPv4Pattern -> [String]
408 addresses (IPv4Pattern field1 field2 field3 field4) = do
413 return $ f1 ++ "." ++ f2 ++ "." ++ f3 ++ "." ++ f4
419 v4octet_tests :: TestTree
423 [ test_v4octet_single_digit_parsed ]
425 test_v4octet_single_digit_parsed :: TestTree
426 test_v4octet_single_digit_parsed =
427 testCase "a single digit is parsed as a v4octet" $ do
428 -- Whatever, it's a test.
429 let (Right actual) = parse v4octet "" "1"
430 let expected = IPv4Octet 1