1 {-# LANGUAGE FlexibleInstances #-}
3 -- | An IPv4 address pattern has four fields separated by ".". Each
4 -- field is either a decimal number, or a sequence inside "[]" that
5 -- contains one or more ";"-separated decimal numbers or
6 -- number..number ranges.
8 -- Thus, any pattern field can be a sequence inside "[]", but a "[]"
9 -- sequence cannot span multiple address fields, and a pattern field
10 -- cannot contain both a number and a "[]" sequence at the same
13 -- This means that the pattern 1.2.[3.4] is not valid (the sequence
14 -- [3.4] cannot span two address fields) and the pattern
15 -- 1.2.3.3[6..9] is also not valid (the last field cannot be both
16 -- number 3 and sequence [6..9] at the same time).
18 -- The syntax for IPv4 patterns is as follows:
20 -- v4pattern = v4field "." v4field "." v4field "." v4field
21 -- v4field = v4octet | "[" v4sequence "]"
22 -- v4octet = any decimal number in the range 0 through 255
23 -- v4sequence = v4seq_member | v4sequence ";" v4seq_member
24 -- v4seq_member = v4octet | v4octet ".." v4octet
29 import Test.Tasty ( TestTree, testGroup )
30 import Test.Tasty.HUnit ( (@?=), testCase )
32 import Text.Parsec.String ( Parser )
33 import Text.Read ( readMaybe )
37 -- | Obtain a pretty 'String' representation of the given thingy.
38 prettyshow :: a -> String
40 -- | Pretty-print the given thingy.
42 pp = putStrLn . prettyshow
45 -- | Define a 'Pretty' instance for the result of 'parse'. This lets
46 -- us pretty-print the result of a parse attempt without worrying
47 -- about whether or not it failed. If the parse failed, you get the
48 -- same output that you usually would. Otherwise we pretty-print the
51 instance Pretty a => Pretty (Either ParseError a) where
52 prettyshow (Left err) = show err
53 prettyshow (Right v) = prettyshow v
58 -- | An ipv4 octet; that is, an integer between @0@ and @255@
59 -- inclusive. This is the data type corresponding to a \"v4octet\"
60 -- in the postscreen parser.
62 newtype IPv4Octet = IPv4Octet Int
66 instance Pretty IPv4Octet where
67 prettyshow (IPv4Octet x) = show x
70 -- | Parse an IPv4 octet, which should contain a string of digits.
71 -- Should fail if the parsed integer does not lie between @0@ and
76 -- Standard octets are parsed correctly:
78 -- >>> parseTest v4octet "0"
81 -- >>> parseTest v4octet "127"
84 -- >>> parseTest v4octet "255"
87 -- Non-digit input throws an error:
89 -- >>> parseTest v4octet "Hello, World!"
90 -- parse error at (line 1, column 1):
94 -- If we're given an integer outside the range @0..255@ (i.e. not a
95 -- valid octet), we fail:
97 -- >>> parseTest v4octet "9000"
98 -- parse error at (line 1, column 5):
99 -- unexpected end of input
101 -- Octet "9000" must be between 0 and 255.
103 v4octet :: Parser IPv4Octet
106 case ( readMaybe s :: Maybe Int ) of
107 -- If "many1 digit" gives us a list of digits, we should be able
108 -- to convert that to an Int! It will overflow rather than fail
109 -- if the input is too big/small, so it should really always
111 Nothing -> unexpected "readMaybe failed on a sequence of digits!"
113 -- If we got an Int, make sure it's actually a representation of
115 Just k -> if 0 <= k && k <= 255
116 then return (IPv4Octet k)
117 else fail ("Octet \"" ++ (show k)
118 ++ "\" must be between 0 and 255.")
123 -- * Sequence members
126 -- | An ipv4 \"sequence member\". A sequence member is either an
127 -- integer (an octet) or a range of integers (contained in an
128 -- octet). This data type corresponds to \"v4seq_member\" in the
129 -- postscreen parser.
131 data IPv4SequenceMember =
132 IPv4SequenceMemberOctet IPv4Octet
133 | IPv4SequenceMemberOctetRange IPv4Octet IPv4Octet
137 instance Pretty IPv4SequenceMember where
138 prettyshow (IPv4SequenceMemberOctet octet) = prettyshow octet
139 prettyshow (IPv4SequenceMemberOctetRange octet1 octet2) =
140 (prettyshow octet1) ++ ".." ++ (prettyshow octet2)
143 -- | Parse an IPv4 \"sequence member\". A sequence member is either an
144 -- octet, or a start..end sequence (like an enumeration, in Haskell).
148 -- >>> parseTest v4seq_member "127"
149 -- IPv4SequenceMemberOctet (IPv4Octet 127)
151 -- >>> parseTest v4seq_member "1..5"
152 -- IPv4SequenceMemberOctetRange (IPv4Octet 1) (IPv4Octet 5)
154 v4seq_member :: Parser IPv4SequenceMember
155 v4seq_member = try both <|> just_one
161 return $ IPv4SequenceMemberOctetRange oct1 oct2
163 just_one = fmap IPv4SequenceMemberOctet v4octet
169 -- | An ipv4 \"sequence\". A sequence contains either a single
170 -- \"sequence member\" (see 'IPv4SequenceMember'), or a sequence
171 -- member along with another sequence. So, this is a potentially
172 -- recursive definition. This type corresponds to \"v4sequence\" in
173 -- the postscreen parser.
176 IPv4SequenceSingleMember IPv4SequenceMember
177 | IPv4SequenceOptions IPv4SequenceMember IPv4Sequence
181 instance Pretty IPv4Sequence where
182 prettyshow (IPv4SequenceSingleMember member) = prettyshow member
183 prettyshow (IPv4SequenceOptions member subsequence) =
184 (prettyshow member) ++ ";" ++ (prettyshow subsequence)
187 -- | Parse an IPv4 \"sequence\". A sequence is whatever is allowed
188 -- within square brackets. Basically it can be three things:
190 -- * An octet (number).
191 -- * A range of addresses in start..end format.
192 -- * An alternative, separated by a semicolon, where each side
193 -- contains one of the previous two options.
197 -- >>> parseTest v4sequence "1"
198 -- IPv4SequenceSingleMember (IPv4SequenceMemberOctet (IPv4Octet 1))
200 -- >>> pp $ parse v4sequence "" "1..2"
203 -- >>> pp $ parse v4sequence "" "1..2;8"
206 v4sequence :: Parser IPv4Sequence
207 v4sequence = try both <|> just_one -- Maybe sepBy is appropriate here?
213 return $ IPv4SequenceOptions sm s
215 just_one = fmap IPv4SequenceSingleMember v4seq_member
221 data IPv4Field = IPv4FieldOctet IPv4Octet | IPv4FieldSequence IPv4Sequence
225 instance Pretty IPv4Field where
226 prettyshow (IPv4FieldOctet octet) = prettyshow octet
227 prettyshow (IPv4FieldSequence seq) = "[" ++ (prettyshow seq) ++ "]"
230 -- | Parse an IPv4 \"field\", which is either a boring old octet, or a
231 -- 'v4sequence' within square brackets.
235 -- >>> parseTest v4field "127"
236 -- IPv4FieldOctet (IPv4Octet 127)
238 -- >>> pp $ parse v4field "" "[127]"
241 v4field :: Parser IPv4Field
242 v4field = just_octet <|> brackets
244 just_octet = fmap IPv4FieldOctet v4octet
250 return $ IPv4FieldSequence s
257 IPv4Pattern IPv4Field IPv4Field IPv4Field IPv4Field
261 instance Pretty IPv4Pattern where
262 prettyshow (IPv4Pattern f1 f2 f3 f4) =
263 (prettyshow f1) ++ "."
271 -- | Parse an ipv4 address pattern. This consists of four fields,
272 -- separated by periods, where a field is either a simple octet or a
275 -- See also: 'v4field', 'v4sequence'.
279 -- >>> pp $ parse v4pattern "" "127.0.0.1"
282 -- >>> pp $ parse v4pattern "" "127.0.[1..3].1"
285 -- >>> pp $ parse v4pattern "" "127.0.[1..3;8].1"
288 -- In the module intro, it is mentioned that this is invalid:
290 -- >>> parseTest v4pattern "1.2.[3.4]"
291 -- parse error at (line 1, column 7):
293 -- expecting digit or "]"
295 -- This one is /also/ invalid; however, we'll parse the valid part off
298 -- >>> pp $ parse v4pattern "" "1.2.3.3[6..9]"
301 v4pattern :: Parser IPv4Pattern
310 return $ IPv4Pattern field1 field2 field3 field4
316 -- | Enumerate the members of an 'IPv4SequenceMember'. A sequence
317 -- member is either an octet, which is easy to enumerate -- we just
318 -- print it -- or an octet range whose members can be enumerated
319 -- from least to greatest.
321 -- We enumerate strings instead of integers because the big picture
322 -- is that we will be listing out patterns of ipv4 addresses, and
323 -- those are represented as strings (dotted quad format).
327 -- >>> let (Right r) = parse v4seq_member "" "127"
328 -- >>> sequence_members r
331 -- >>> let (Right r) = parse v4seq_member "" "127..135"
332 -- >>> sequence_members r
333 -- ["127","128","129","130","131","132","133","134","135"]
335 sequence_members :: IPv4SequenceMember -> [String]
336 sequence_members (IPv4SequenceMemberOctet (IPv4Octet i)) = [show i]
337 sequence_members (IPv4SequenceMemberOctetRange (IPv4Octet s) (IPv4Octet t)) =
338 [show x | x <- [s .. t]]
341 -- | Enumerate the members of an ipv4 sequence. These consist of
342 -- either a single sequence member (in which case we delegate to
343 -- 'sequence_members'), or an \"option\" which is enumerated
348 -- >>> let (Right r) = parse v4sequence "" "1"
352 -- >>> let (Right r) = parse v4sequence "" "1..2"
356 -- >>> let (Right r) = parse v4sequence "" "1..3;4;5..9"
358 -- ["1","2","3","4","5","6","7","8","9"]
360 sequences :: IPv4Sequence -> [String]
361 sequences (IPv4SequenceSingleMember sm) =
363 sequences (IPv4SequenceOptions sm s) =
364 (sequence_members sm) ++ (sequences s)
367 -- | Enumerate the members of an 'IPv4Field'. If the field contains a
368 -- single 'IPv4Octet', we simply 'show' it. Otherwise it contains an
369 -- 'IPv4FieldSequence', and we enumerate that recursively using
374 -- >>> let (Right r) = parse v4field "" "1"
378 -- >>> let (Right r) = parse v4field "" "[127..135]"
380 -- ["127","128","129","130","131","132","133","134","135"]
382 fields :: IPv4Field -> [String]
383 fields (IPv4FieldOctet (IPv4Octet i)) = [show i]
384 fields (IPv4FieldSequence s) = sequences s
387 -- | Enumerate the addresses represented by a given 'IPv4Pattern'.
389 -- A pattern contains four fields, sepearated by period
390 -- characters. We want to list all possible combinations of
391 -- addresses where the first octet comes from the first field, the
392 -- second octet comes from the second field... and so on. To do
393 -- this, we take advantage of the List monad and the fact that
394 -- 'fields' returns a list of 'String's.
400 -- >>> let (Right r) = parse v4pattern "" "127.0.0.1"
404 -- Anything between 127.0.0.2 and 127.0.0.4, and either 127.0.0.10
407 -- >>> let (Right r) = parse v4pattern "" "127.0.0.[2..4;10;11]"
409 -- ["127.0.0.2","127.0.0.3","127.0.0.4","127.0.0.10","127.0.0.11"]
411 addresses :: IPv4Pattern -> [String]
412 addresses (IPv4Pattern field1 field2 field3 field4) = do
417 return $ f1 ++ "." ++ f2 ++ "." ++ f3 ++ "." ++ f4
423 v4octet_tests :: TestTree
427 [ test_v4octet_single_digit_parsed ]
429 test_v4octet_single_digit_parsed :: TestTree
430 test_v4octet_single_digit_parsed =
431 testCase "a single digit is parsed as a v4octet" $ do
432 -- Whatever, it's a test.
433 let (Right actual) = parse v4octet "" "1"
434 let expected = IPv4Octet 1