From 041d33c0e5ac9c0e9733f42cd96aa2fba07a043f Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Mon, 26 Dec 2011 13:04:41 -0500 Subject: [PATCH 01/16] Cabalize the project. --- Setup.hs | 2 ++ hath.cabal | 46 ++++++++++++++++++++++++++++++++++++++++++++++ makefile | 26 ++++++++------------------ 3 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 Setup.hs create mode 100644 hath.cabal diff --git a/Setup.hs b/Setup.hs new file mode 100644 index 0000000..9a994af --- /dev/null +++ b/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain diff --git a/hath.cabal b/hath.cabal new file mode 100644 index 0000000..ca43858 --- /dev/null +++ b/hath.cabal @@ -0,0 +1,46 @@ +name: hath +version: 0.0 +cabal-version: >= 1.8 +author: Michael Orlitzky +maintainer: Michael Orlitzky +synopsis: + Hath manipulates network blocks in CIDR notation. +build-type: Simple + + +executable hath + build-depends: + base == 4.4.*, + HUnit == 1.2.*, + QuickCheck == 2.4.* + + main-is: + Main.hs + + hs-source-dirs: + src/ + + ghc-options: + -Wall + -fwarn-hi-shadowing + -fwarn-missing-signatures + -fwarn-name-shadowing + -fwarn-orphans + -fwarn-type-defaults + -fwarn-tabs + -fwarn-incomplete-record-updates + -fwarn-monomorphism-restriction + -fwarn-unused-do-bind + -funbox-strict-fields + -fexcess-precision + -fno-spec-constr-count + -rtsopts + -threaded + -optc-O3 + -optc-march=native + -O2 + + ghc-prof-options: + -prof + -auto-all + -caf-all diff --git a/makefile b/makefile index 8605a89..7eb2938 100644 --- a/makefile +++ b/makefile @@ -1,27 +1,17 @@ -GHC_WARNINGS := -Wall -GHC_WARNINGS += -fwarn-hi-shadowing -GHC_WARNINGS += -fwarn-missing-signatures -GHC_WARNINGS += -fwarn-name-shadowing -GHC_WARNINGS += -fwarn-orphans -GHC_WARNINGS += -fwarn-type-defaults - -BIN=hath - .PHONY : test -all: $(BIN) - -$(BIN): src/*.hs - ghc -O2 $(GHC_WARNINGS) --make -o bin/${BIN} src/*.hs +hath: src/*.hs + runghc Setup.hs clean + runghc Setup.hs configure --user + runghc Setup.hs build profile: src/*.hs - ghc -O2 $(GHC_WARNINGS) -prof -auto-all --make -o bin/$(BIN) src/*.hs + runghc Setup.hs clean + runghc Setup.hs configure --user --enable-executable-profiling + runghc Setup.hs build clean: - rm -f bin/$(BIN) - rm -f src/*.hi - rm -f src/*.o - rm -f *.prof + runghc Setup.hs clean test: runghc -i"src" test/TestSuite.hs -- 2.43.2 From e8b41784eaa68049f7a7354916c9c9920050db70 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Mon, 26 Dec 2011 22:33:25 -0500 Subject: [PATCH 02/16] Rewrite everything to use Maybe instead of None constructors. Convert comments to Haddock-style docs in many places. Whitespace cleanup. --- src/Bit.hs | 36 +++-- src/Cidr.hs | 389 +++++++++++++++++++++++++-------------------- src/IPv4Address.hs | 272 ++++++++++++++++--------------- src/Main.hs | 40 ++--- src/Maskable.hs | 25 +-- src/Maskbits.hs | 290 +++++++++++++++++---------------- src/Octet.hs | 206 ++++++++++++++---------- 7 files changed, 668 insertions(+), 590 deletions(-) diff --git a/src/Bit.hs b/src/Bit.hs index a4241bc..128b6f6 100644 --- a/src/Bit.hs +++ b/src/Bit.hs @@ -1,29 +1,35 @@ -module Bit where +-- | The Bit module contains the Bit data type, which is essentially a +-- renamed Boolean, and some convenience functions. +module Bit +where -import Test.QuickCheck +import Test.QuickCheck ( + Arbitrary, + arbitrary, + elements + ) -data Bit = None | Zero | One - deriving (Eq) +data Bit = Zero | One + deriving (Eq) instance Show Bit where - show None = "None" - show Zero = "0" - show One = "1" + show Zero = "0" + show One = "1" instance Arbitrary Bit where - arbitrary = elements [ Zero, One ] + arbitrary = elements [ Zero, One ] +-- | Convert a Bit to an Int. bit_to_int :: Bit -> Int -bit_to_int None = -1 bit_to_int Zero = 0 bit_to_int One = 1 --- If we are passed a '0' or '1', convert it appropriately. Otherwise, --- default to None. -bit_from_char :: Char -> Bit -bit_from_char '0' = Zero -bit_from_char '1' = One -bit_from_char _ = None +-- | If we are passed a '0' or '1', convert it +-- appropriately. Otherwise, default to Nothing. +bit_from_char :: Char -> Maybe Bit +bit_from_char '0' = Just Zero +bit_from_char '1' = Just One +bit_from_char _ = Nothing diff --git a/src/Cidr.hs b/src/Cidr.hs index 422a0ad..826c862 100644 --- a/src/Cidr.hs +++ b/src/Cidr.hs @@ -18,6 +18,7 @@ module Cidr ) where import Data.List (nubBy) +import Data.Maybe (catMaybes, fromJust) import Test.HUnit import Test.QuickCheck @@ -29,13 +30,12 @@ import Maskbits import Octet -data Cidr = None | Cidr { ipv4address :: IPv4Address, - maskbits :: Maskbits } +data Cidr = Cidr { ipv4address :: IPv4Address, + maskbits :: Maskbits } instance Show Cidr where - show Cidr.None = "None" show cidr = (show (ipv4address cidr)) ++ "/" ++ (show (maskbits cidr)) @@ -53,56 +53,45 @@ instance Eq Cidr where -- Two CIDR ranges are equivalent if they have the same network bits -- and the masks are the same. equivalent :: Cidr -> Cidr -> Bool -equivalent Cidr.None Cidr.None = True -equivalent Cidr.None _ = False -equivalent _ Cidr.None = False equivalent (Cidr addr1 mbits1) (Cidr addr2 mbits2) = (mbits1 == mbits2) && ((apply_mask addr1 mbits1 B.Zero) == (apply_mask addr2 mbits2 B.Zero)) -- Returns the mask portion of a CIDR address. That is, everything -- after the trailing slash. -maskbits_from_cidr_string :: String -> Maskbits +maskbits_from_cidr_string :: String -> Maybe Maskbits maskbits_from_cidr_string s - | length partlist == 2 = maskbits_from_string (partlist !! 1) - | otherwise = Maskbits.None - where - partlist = (splitWith (`elem` "/") s) + | length partlist == 2 = maskbits_from_string (partlist !! 1) + | otherwise = Nothing + where + partlist = (splitWith (`elem` "/") s) --- Takes an IP address String in CIDR notation, and returns a list of --- its octets (as Ints). +-- | Takes an IP address String in CIDR notation, and returns a list +-- of its octets (as Ints). octets_from_cidr_string :: String -> [Octet] octets_from_cidr_string s = - map octet_from_string (take 4 (splitWith (`elem` "./") s)) + catMaybes $ map octet_from_string (take 4 (splitWith (`elem` "./") s)) -cidr_from_string :: String -> Cidr -cidr_from_string s - | addr == IPv4Address.None = Cidr.None - | mbits == Maskbits.None = Cidr.None - | otherwise = Cidr addr mbits - where - addr = ipv4address_from_octets (oct1) (oct2) (oct3) (oct4) - oct1 = (octs !! 0) - oct2 = (octs !! 1) - oct3 = (octs !! 2) - oct4 = (octs !! 3) - octs = octets_from_cidr_string s - mbits = maskbits_from_cidr_string s +-- | Return Nothing if we can't parse both maskbits and octets from +-- the string. +cidr_from_string :: String -> Maybe Cidr +cidr_from_string s = + case (octets_from_cidr_string s) of + [oct1, oct2, oct3, oct4] -> + case (maskbits_from_cidr_string s) of + Just mbits -> + Just $ Cidr (IPv4Address oct1 oct2 oct3 oct4) mbits + _ -> Nothing + _ -> Nothing min_host :: Cidr -> IPv4Address -min_host Cidr.None = IPv4Address.None -min_host (Cidr IPv4Address.None _) = IPv4Address.None -min_host (Cidr _ Maskbits.None) = IPv4Address.None min_host (Cidr addr mask) = apply_mask addr mask B.Zero max_host :: Cidr -> IPv4Address -max_host Cidr.None = IPv4Address.None -max_host (Cidr IPv4Address.None _) = IPv4Address.None -max_host (Cidr _ Maskbits.None) = IPv4Address.None max_host (Cidr addr mask) = apply_mask addr mask B.One @@ -132,51 +121,45 @@ max_octet4 cidr = octet4 (max_host cidr) --- Return true if the first argument (a CIDR range) contains the --- second (another CIDR range). There are a lot of ways we can be fed --- junk here. For lack of a better alternative, just return False when --- we are given nonsense. -contains :: Cidr -> Cidr -> Bool -contains Cidr.None _ = False -contains _ Cidr.None = False -contains (Cidr _ Maskbits.None) _ = False -contains (Cidr IPv4Address.None _) _ = False -contains _ (Cidr _ Maskbits.None) = False -contains _ (Cidr IPv4Address.None _) = False - --- If the number of bits in the network part of the first address is --- larger than the number of bits in the second, there is no way that --- the first range can contain the second. For, if the number of --- network bits is larger, then the number of host bits must be --- smaller, and if cidr1 has fewer hosts than cidr2, cidr1 most --- certainly does not contain cidr2. +-- | Return true if the first argument (a CIDR range) contains the +-- second (another CIDR range). There are a lot of ways we can be +-- fed junk here. For lack of a better alternative, just return +-- False when we are given nonsense. +-- +-- If the number of bits in the network part of the first address is +-- larger than the number of bits in the second, there is no way +-- that the first range can contain the second. For, if the number +-- of network bits is larger, then the number of host bits must be +-- smaller, and if cidr1 has fewer hosts than cidr2, cidr1 most +-- certainly does not contain cidr2. -- --- On the other hand, if the first argument (cidr1) has fewer (or the --- same number of) network bits as the second, it can contain the --- second. In this case, we need to check that every host in cidr2 is --- contained in cidr1. If a host in cidr2 is contained in cidr1, then --- at least mbits1 of an address in cidr2 will match cidr1. For --- example, +-- On the other hand, if the first argument (cidr1) has fewer (or +-- the same number of) network bits as the second, it can contain +-- the second. In this case, we need to check that every host in +-- cidr2 is contained in cidr1. If a host in cidr2 is contained in +-- cidr1, then at least mbits1 of an address in cidr2 will match +-- cidr1. For example, -- --- cidr1 = 192.168.1.0/23, cidr2 = 192.168.1.100/24 +-- cidr1 = 192.168.1.0/23, cidr2 = 192.168.1.100/24 -- --- Here, cidr2 contains all of 192.168.1.0 through --- 192.168.1.255. However, cidr1 contains BOTH 192.168.0.0 through --- 192.168.0.255 and 192.168.1.0 through 192.168.1.255. In essence, --- what we want to check is that cidr2 "begins with" something that --- cidr1 CAN begin with. Since cidr1 can begin with 192.168.1, and --- cidr2 DOES, cidr1 contains cidr2.. +-- Here, cidr2 contains all of 192.168.1.0 through +-- 192.168.1.255. However, cidr1 contains BOTH 192.168.0.0 through +-- 192.168.0.255 and 192.168.1.0 through 192.168.1.255. In essence, +-- what we want to check is that cidr2 "begins with" something that +-- cidr1 CAN begin with. Since cidr1 can begin with 192.168.1, and +-- cidr2 DOES, cidr1 contains cidr2.. -- --- The way that we check this is to apply cidr1's mask to cidr2's --- address and see if the result is the same as cidr1's mask applied --- to cidr1's address. +-- The way that we check this is to apply cidr1's mask to cidr2's +-- address and see if the result is the same as cidr1's mask applied +-- to cidr1's address. -- +contains :: Cidr -> Cidr -> Bool contains (Cidr addr1 mbits1) (Cidr addr2 mbits2) - | mbits1 > mbits2 = False - | otherwise = addr1masked == addr2masked - where - addr1masked = apply_mask addr1 mbits1 B.Zero - addr2masked = apply_mask addr2 mbits1 B.Zero + | mbits1 > mbits2 = False + | otherwise = addr1masked == addr2masked + where + addr1masked = apply_mask addr1 mbits1 B.Zero + addr2masked = apply_mask addr2 mbits1 B.Zero contains_proper :: Cidr -> Cidr -> Bool @@ -184,59 +167,56 @@ contains_proper cidr1 cidr2 = (cidr1 `contains` cidr2) && (not (cidr2 `contains` cidr1)) --- A CIDR range is redundant (with respect to the given list) if --- another CIDR range in that list properly contains it. +-- | A CIDR range is redundant (with respect to the given list) if +-- another CIDR range in that list properly contains it. redundant :: [Cidr] -> Cidr -> Bool redundant cidrlist cidr = any ((flip contains_proper) cidr) cidrlist --- First, we look at all possible pairs of cidrs, and combine the --- adjacent ones in to a new list. Then, we concatenate that list with --- the original one, and filter out all of the redundancies. If two --- adjacent Cidrs are combined into a larger one, they will be removed --- in the second step since the larger Cidr must contain the smaller --- two. +-- | First, we look at all possible pairs of cidrs, and combine the +-- adjacent ones in to a new list. Then, we concatenate that list +-- with the original one, and filter out all of the redundancies. If +-- two adjacent Cidrs are combined into a larger one, they will be +-- removed in the second step since the larger Cidr must contain the +-- smaller two. -- --- Once this is done, we see whether or not the result is different --- than the argument that was passed in. If nothing changed, we're --- done and return the list that was passed to us. However, if --- something changed, we recurse and try to combine the list again. +-- Once this is done, we see whether or not the result is different +-- than the argument that was passed in. If nothing changed, we're +-- done and return the list that was passed to us. However, if +-- something changed, we recurse and try to combine the list again. combine_all :: [Cidr] -> [Cidr] combine_all cidrs | cidrs == (combine_contained unique_cidrs) = cidrs | otherwise = combine_all (combine_contained unique_cidrs) where - unique_cidrs = nubBy equivalent valid_cidr_combinations - valid_cidr_combinations = filter (/= Cidr.None) cidr_combinations + unique_cidrs = nubBy equivalent cidr_combinations cidr_combinations = - cidrs ++ [ (combine_adjacent x y) | x <- cidrs, y <- cidrs ] + cidrs ++ (catMaybes [ (combine_adjacent x y) | x <- cidrs, y <- cidrs ]) --- Take a list of CIDR ranges and filter out all of the ones that are --- contained entirelt within some other range in the list. +-- | Take a list of CIDR ranges and filter out all of the ones that +-- are contained entirelt within some other range in the list. combine_contained :: [Cidr] -> [Cidr] combine_contained cidrs = - filter (not . (redundant cidrs)) cidrs + filter (not . (redundant cidrs)) cidrs --- If the two Cidrs are not adjacent, return Cidr.None. Otherwise, --- decrement the maskbits of cidr1 and return that; it will contain --- both cidr1 and cidr2. -combine_adjacent :: Cidr -> Cidr -> Cidr +-- | If the two Cidrs are not adjacent, return Cidr.None. Otherwise, +-- decrement the maskbits of cidr1 and return that; it will contain +-- both cidr1 and cidr2. +combine_adjacent :: Cidr -> Cidr -> Maybe Cidr combine_adjacent cidr1 cidr2 - | not (adjacent cidr1 cidr2) = Cidr.None - | (maskbits cidr1 == Zero) = Cidr.None - | otherwise = cidr1 { maskbits = decrement (maskbits cidr1) } + | not (adjacent cidr1 cidr2) = Nothing + | (maskbits cidr1 == Zero) = Nothing + | otherwise = Just $ cidr1 { maskbits = decrement (maskbits cidr1) } --- Determine whether or not two CIDR ranges are adjacent. If two --- ranges lie consecutively within the IP space, they can be --- combined. For example, 10.1.0.0/24 and 10.0.1.0/24 are adjacent, --- and can be combined in to 10.1.0.0/23. +-- | Determine whether or not two CIDR ranges are adjacent. If two +-- ranges lie consecutively within the IP space, they can be +-- combined. For example, 10.1.0.0/24 and 10.0.1.0/24 are adjacent, +-- and can be combined in to 10.1.0.0/23. adjacent :: Cidr -> Cidr -> Bool -adjacent Cidr.None _ = False -adjacent _ Cidr.None = False adjacent cidr1 cidr2 | mbits1 /= mbits2 = False | mbits1 == Maskbits.Zero = False -- They're equal. @@ -255,140 +235,201 @@ adjacent cidr1 cidr2 test_min_host1 :: Test test_min_host1 = - TestCase $ assertEqual "The minimum host in 10.0.0.0/24 is 10.0.0.0" expected actual - where - actual = show $ min_host (cidr_from_string "10.0.0.0/24") - expected = "10.0.0.0" + TestCase $ assertEqual "The minimum host in 10.0.0.0/24 is 10.0.0.0" + expected + actual + where + actual = show $ min_host (fromJust $ cidr_from_string "10.0.0.0/24") + expected = "10.0.0.0" test_max_host1 :: Test test_max_host1 = - TestCase $ assertEqual "The maximum host in 10.0.0.0/24 is 10.0.0.255" expected actual - where - actual = show $ max_host (cidr_from_string "10.0.0.0/24") - expected = "10.0.0.255" + TestCase $ assertEqual "The maximum host in 10.0.0.0/24 is 10.0.0.255" + expected + actual + where + actual = show $ max_host (fromJust $ cidr_from_string "10.0.0.0/24") + expected = "10.0.0.255" test_equality1 :: Test test_equality1 = - TestCase $ assertEqual "10.1.1.0/23 equals itself" True (cidr1 == cidr1) - where - cidr1 = cidr_from_string "10.1.1.0/23" + TestCase $ + assertEqual + "10.1.1.0/23 equals itself" + True + (cidr1 == cidr1) + where + cidr1 = fromJust $ cidr_from_string "10.1.1.0/23" test_contains1 :: Test test_contains1 = - TestCase $ assertEqual "10.1.1.0/23 contains 10.1.1.0/24" True (cidr1 `contains` cidr2) - where - cidr1 = cidr_from_string "10.1.1.0/23" - cidr2 = cidr_from_string "10.1.1.0/24" + TestCase $ + assertEqual + "10.1.1.0/23 contains 10.1.1.0/24" + True + (cidr1 `contains` cidr2) + where + cidr1 = fromJust $ cidr_from_string "10.1.1.0/23" + cidr2 = fromJust $ cidr_from_string "10.1.1.0/24" test_contains2 :: Test test_contains2 = - TestCase $ assertEqual "10.1.1.0/23 contains itself" True (cidr1 `contains` cidr1) - where - cidr1 = cidr_from_string "10.1.1.0/23" + TestCase $ + assertEqual + "10.1.1.0/23 contains itself" + True + (cidr1 `contains` cidr1) + where + cidr1 = fromJust $ cidr_from_string "10.1.1.0/23" test_contains_proper1 :: Test test_contains_proper1 = - TestCase $ assertEqual "10.1.1.0/23 contains 10.1.1.0/24 properly" True (cidr1 `contains_proper` cidr2) - where - cidr1 = cidr_from_string "10.1.1.0/23" - cidr2 = cidr_from_string "10.1.1.0/24" + TestCase $ + assertEqual + "10.1.1.0/23 contains 10.1.1.0/24 properly" + True + (cidr1 `contains_proper` cidr2) + where + cidr1 = fromJust $ cidr_from_string "10.1.1.0/23" + cidr2 = fromJust $ cidr_from_string "10.1.1.0/24" test_contains_proper2 :: Test test_contains_proper2 = - TestCase $ assertEqual "10.1.1.0/23 does not contain itself properly" False (cidr1 `contains_proper` cidr1) - where - cidr1 = cidr_from_string "10.1.1.0/23" + TestCase $ + assertEqual + "10.1.1.0/23 does not contain itself properly" + False + (cidr1 `contains_proper` cidr1) + where + cidr1 = fromJust $ cidr_from_string "10.1.1.0/23" test_adjacent1 :: Test test_adjacent1 = - TestCase $ assertEqual "10.1.0.0/24 is adjacent to 10.1.1.0/24" True (cidr1 `adjacent` cidr2) - where - cidr1 = cidr_from_string "10.1.0.0/24" - cidr2 = cidr_from_string "10.1.1.0/24" + TestCase $ + assertEqual + "10.1.0.0/24 is adjacent to 10.1.1.0/24" + True + (cidr1 `adjacent` cidr2) + where + cidr1 = fromJust $ cidr_from_string "10.1.0.0/24" + cidr2 = fromJust $ cidr_from_string "10.1.1.0/24" test_adjacent2 :: Test test_adjacent2 = - TestCase $ assertEqual "10.1.0.0/23 is not adjacent to 10.1.0.0/24" False (cidr1 `adjacent` cidr2) - where - cidr1 = cidr_from_string "10.1.0.0/23" - cidr2 = cidr_from_string "10.1.0.0/24" + TestCase $ + assertEqual + "10.1.0.0/23 is not adjacent to 10.1.0.0/24" + False + (cidr1 `adjacent` cidr2) + where + cidr1 = fromJust $ cidr_from_string "10.1.0.0/23" + cidr2 = fromJust $ cidr_from_string "10.1.0.0/24" test_adjacent3 :: Test test_adjacent3 = - TestCase $ assertEqual "10.1.0.0/24 is not adjacent to 10.2.5.0/24" False (cidr1 `adjacent` cidr2) - where - cidr1 = cidr_from_string "10.1.0.0/24" - cidr2 = cidr_from_string "10.2.5.0/24" + TestCase $ + assertEqual + "10.1.0.0/24 is not adjacent to 10.2.5.0/24" + False + (cidr1 `adjacent` cidr2) + where + cidr1 = fromJust $ cidr_from_string "10.1.0.0/24" + cidr2 = fromJust $ cidr_from_string "10.2.5.0/24" test_adjacent4 :: Test test_adjacent4 = - TestCase $ assertEqual "10.1.1.0/24 is not adjacent to 10.1.2.0/24" False (cidr1 `adjacent` cidr2) - where - cidr1 = cidr_from_string "10.1.1.0/24" - cidr2 = cidr_from_string "10.1.2.0/24" + TestCase $ + assertEqual + "10.1.1.0/24 is not adjacent to 10.1.2.0/24" + False + (cidr1 `adjacent` cidr2) + where + cidr1 = fromJust $ cidr_from_string "10.1.1.0/24" + cidr2 = fromJust $ cidr_from_string "10.1.2.0/24" test_combine_contained1 :: Test test_combine_contained1 = - TestCase $ assertEqual "10.0.0.0/8, 10.1.0.0/16, and 10.1.1.0/24 combine to 10.0.0.0/8" expected_cidrs (combine_contained test_cidrs) - where - cidr1 = cidr_from_string "10.0.0.0/8" - cidr2 = cidr_from_string "10.1.0.0/16" - cidr3 = cidr_from_string "10.1.1.0/24" - expected_cidrs = [cidr1] - test_cidrs = [cidr1, cidr2, cidr3] + TestCase $ + assertEqual + "10.0.0.0/8, 10.1.0.0/16, and 10.1.1.0/24 combine to 10.0.0.0/8" + expected_cidrs + (combine_contained test_cidrs) + where + cidr1 = fromJust $ cidr_from_string "10.0.0.0/8" + cidr2 = fromJust $ cidr_from_string "10.1.0.0/16" + cidr3 = fromJust $ cidr_from_string "10.1.1.0/24" + expected_cidrs = [cidr1] + test_cidrs = [cidr1, cidr2, cidr3] test_combine_contained2 :: Test test_combine_contained2 = - TestCase $ assertEqual "192.168.3.0/23 does not contain 192.168.1.0/24" [cidr1, cidr2] (combine_contained [cidr1, cidr2]) - where - cidr1 = cidr_from_string "192.168.3.0/23" - cidr2 = cidr_from_string "192.168.1.0/24" + TestCase $ + assertEqual + "192.168.3.0/23 does not contain 192.168.1.0/24" + [cidr1, cidr2] + (combine_contained [cidr1, cidr2]) + where + cidr1 = fromJust $ cidr_from_string "192.168.3.0/23" + cidr2 = fromJust $ cidr_from_string "192.168.1.0/24" test_combine_all1 :: Test test_combine_all1 = - TestCase $ assertEqual "10.0.0.0/24 is adjacent to 10.0.1.0/24 and 10.0.3.0/23 contains 10.0.2.0/24" expected_cidrs (combine_all test_cidrs) - where - cidr1 = cidr_from_string "10.0.0.0/24" - cidr2 = cidr_from_string "10.0.1.0/24" - cidr3 = cidr_from_string "10.0.2.0/24" - cidr4 = cidr_from_string "10.0.3.0/23" - cidr5 = cidr_from_string "10.0.0.0/23" - expected_cidrs = [cidr_from_string "10.0.0.0/22"] - test_cidrs = [cidr1, cidr2, cidr3, cidr4, cidr5] + TestCase $ + assertEqual + "10.0.0.0/24 is adjacent to 10.0.1.0/24 and 10.0.3.0/23 contains 10.0.2.0/24" + expected_cidrs + (combine_all test_cidrs) + where + cidr1 = fromJust $ cidr_from_string "10.0.0.0/24" + cidr2 = fromJust $ cidr_from_string "10.0.1.0/24" + cidr3 = fromJust $ cidr_from_string "10.0.2.0/24" + cidr4 = fromJust $ cidr_from_string "10.0.3.0/23" + cidr5 = fromJust $ cidr_from_string "10.0.0.0/23" + expected_cidrs = [fromJust $ cidr_from_string "10.0.0.0/22"] + test_cidrs = [cidr1, cidr2, cidr3, cidr4, cidr5] test_combine_all2 :: Test test_combine_all2 = - TestCase $ assertEqual "127.0.0.1/32 combines with itself recursively" expected_cidrs (combine_all test_cidrs) + TestCase $ + assertEqual + "127.0.0.1/32 combines with itself recursively" + expected_cidrs + (combine_all test_cidrs) where - cidr1 = cidr_from_string "127.0.0.1/32" + cidr1 = fromJust $ cidr_from_string "127.0.0.1/32" expected_cidrs = [cidr1] test_cidrs = [cidr1, cidr1, cidr1, cidr1, cidr1] test_combine_all3 :: Test test_combine_all3 = - TestCase $ assertEqual "10.0.0.16, 10.0.0.17, 10.0.0.18, and 10.0.0.19 get combined into 10.0.0.16/30" expected_cidrs (combine_all test_cidrs) - where - cidr1 = cidr_from_string "10.0.0.16/32" - cidr2 = cidr_from_string "10.0.0.17/32" - cidr3 = cidr_from_string "10.0.0.18/32" - cidr4 = cidr_from_string "10.0.0.19/32" - expected_cidrs = [cidr_from_string "10.0.0.16/30"] - test_cidrs = [cidr1, cidr2, cidr3, cidr4] + TestCase $ + assertEqual + "10.0.0.16, 10.0.0.17, 10.0.0.18, and 10.0.0.19 get combined into 10.0.0.16/30" + expected_cidrs + (combine_all test_cidrs) + where + cidr1 = fromJust $ cidr_from_string "10.0.0.16/32" + cidr2 = fromJust $ cidr_from_string "10.0.0.17/32" + + cidr3 = fromJust $ cidr_from_string "10.0.0.18/32" + cidr4 = fromJust $ cidr_from_string "10.0.0.19/32" + expected_cidrs = [fromJust $ cidr_from_string "10.0.0.16/30"] + test_cidrs = [cidr1, cidr2, cidr3, cidr4] cidr_tests :: [Test] diff --git a/src/IPv4Address.hs b/src/IPv4Address.hs index e72dd59..6153d28 100644 --- a/src/IPv4Address.hs +++ b/src/IPv4Address.hs @@ -1,16 +1,12 @@ module IPv4Address -( ipv4address_from_octets, - ipv4address_tests, - IPv4Address(None), +( ipv4address_tests, + IPv4Address(..), max_address, min_address, most_sig_bit_different, - octet1, - octet2, - octet3, - octet4 ) where +import Data.Maybe (fromJust) import Test.HUnit import Test.QuickCheck @@ -18,154 +14,142 @@ import Maskable import Maskbits import Octet -data IPv4Address = None | IPv4Address { octet1 :: Octet, - octet2 :: Octet, - octet3 :: Octet, - octet4 :: Octet } - deriving (Eq) +data IPv4Address = + IPv4Address { octet1 :: Octet, + octet2 :: Octet, + octet3 :: Octet, + octet4 :: Octet } + deriving (Eq) instance Show IPv4Address where - show IPv4Address.None = "None" - show addr = concat [(show oct1) ++ ".", - (show oct2) ++ ".", - (show oct3) ++ ".", - (show oct4)] - where - oct1 = (octet1 addr) - oct2 = (octet2 addr) - oct3 = (octet3 addr) - oct4 = (octet4 addr) + show addr = concat [(show oct1) ++ ".", + (show oct2) ++ ".", + (show oct3) ++ ".", + (show oct4)] + where + oct1 = (octet1 addr) + oct2 = (octet2 addr) + oct3 = (octet3 addr) + oct4 = (octet4 addr) instance Arbitrary IPv4Address where - arbitrary = do - oct1 <- arbitrary :: Gen Octet - oct2 <- arbitrary :: Gen Octet - oct3 <- arbitrary :: Gen Octet - oct4 <- arbitrary :: Gen Octet - return (IPv4Address oct1 oct2 oct3 oct4) + arbitrary = do + oct1 <- arbitrary :: Gen Octet + oct2 <- arbitrary :: Gen Octet + oct3 <- arbitrary :: Gen Octet + oct4 <- arbitrary :: Gen Octet + return (IPv4Address oct1 oct2 oct3 oct4) instance Maskable IPv4Address where - apply_mask _ Maskbits.None _ = IPv4Address.None - apply_mask addr mask bit - | mask == ThirtyTwo = addr - | mask == ThirtyOne = addr { octet4 = (apply_mask oct4 Seven bit) } - | mask == Thirty = addr { octet4 = (apply_mask oct4 Six bit) } - | mask == TwentyNine = addr { octet4 = (apply_mask oct4 Five bit) } - | mask == TwentyEight = addr { octet4 = (apply_mask oct4 Four bit) } - | mask == TwentySeven = addr { octet4 = (apply_mask oct4 Three bit) } - | mask == TwentySix = addr { octet4 = (apply_mask oct4 Two bit) } - | mask == TwentyFive = addr { octet4 = (apply_mask oct4 One bit) } - | mask == TwentyFour = addr { octet4 = (apply_mask oct4 Zero bit) } - | mask == TwentyThree = addr { octet3 = (apply_mask oct3 Seven bit), - octet4 = (apply_mask oct4 Zero bit) } - | mask == TwentyTwo = addr { octet3 = (apply_mask oct3 Six bit), - octet4 = (apply_mask oct4 Zero bit) } - | mask == TwentyOne = addr { octet3 = (apply_mask oct3 Five bit), - octet4 = (apply_mask oct4 Zero bit) } - | mask == Twenty = addr { octet3 = (apply_mask oct3 Four bit), - octet4 = (apply_mask oct4 Zero bit) } - | mask == Nineteen = addr { octet3 = (apply_mask oct3 Three bit), - octet4 = (apply_mask oct4 Zero bit) } - | mask == Eighteen = addr { octet3 = (apply_mask oct3 Two bit), + + apply_mask addr mask bit + | mask == ThirtyTwo = addr + | mask == ThirtyOne = addr { octet4 = (apply_mask oct4 Seven bit) } + | mask == Thirty = addr { octet4 = (apply_mask oct4 Six bit) } + | mask == TwentyNine = addr { octet4 = (apply_mask oct4 Five bit) } + | mask == TwentyEight = addr { octet4 = (apply_mask oct4 Four bit) } + | mask == TwentySeven = addr { octet4 = (apply_mask oct4 Three bit) } + | mask == TwentySix = addr { octet4 = (apply_mask oct4 Two bit) } + | mask == TwentyFive = addr { octet4 = (apply_mask oct4 One bit) } + | mask == TwentyFour = addr { octet4 = (apply_mask oct4 Zero bit) } + | mask == TwentyThree = addr { octet3 = (apply_mask oct3 Seven bit), octet4 = (apply_mask oct4 Zero bit) } - | mask == Seventeen = addr { octet3 = (apply_mask oct3 One bit), - octet4 = (apply_mask oct4 Zero bit) } - | mask == Sixteen = addr { octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit) } - | mask == Fifteen = addr { octet2 = (apply_mask oct2 Seven bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit) } - | mask == Fourteen = addr { octet2 = (apply_mask oct2 Six bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Thirteen = addr { octet2 = (apply_mask oct2 Five bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Twelve = addr { octet2 = (apply_mask oct2 Four bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Eleven = addr { octet2 = (apply_mask oct2 Three bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Ten = addr { octet2 = (apply_mask oct2 Two bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Nine = addr { octet2 = (apply_mask oct2 One bit), + | mask == TwentyTwo = addr { octet3 = (apply_mask oct3 Six bit), + octet4 = (apply_mask oct4 Zero bit) } + | mask == TwentyOne = addr { octet3 = (apply_mask oct3 Five bit), + octet4 = (apply_mask oct4 Zero bit) } + | mask == Twenty = addr { octet3 = (apply_mask oct3 Four bit), + octet4 = (apply_mask oct4 Zero bit) } + | mask == Nineteen = addr { octet3 = (apply_mask oct3 Three bit), + octet4 = (apply_mask oct4 Zero bit) } + | mask == Eighteen = addr { octet3 = (apply_mask oct3 Two bit), + octet4 = (apply_mask oct4 Zero bit) } + | mask == Seventeen = addr { octet3 = (apply_mask oct3 One bit), + octet4 = (apply_mask oct4 Zero bit) } + | mask == Sixteen = addr { octet3 = (apply_mask oct3 Zero bit), + octet4 = (apply_mask oct4 Zero bit) } + | mask == Fifteen = addr { octet2 = (apply_mask oct2 Seven bit), octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Eight = addr { octet2 = (apply_mask oct2 Zero bit), + octet4 = (apply_mask oct4 Zero bit) } + | mask == Fourteen = addr { octet2 = (apply_mask oct2 Six bit), octet3 = (apply_mask oct3 Zero bit), octet4 = (apply_mask oct4 Zero bit)} - | mask == Seven = addr { octet1 = (apply_mask oct1 Seven bit), - octet2 = (apply_mask oct2 Zero bit), + | mask == Thirteen = addr { octet2 = (apply_mask oct2 Five bit), octet3 = (apply_mask oct3 Zero bit), octet4 = (apply_mask oct4 Zero bit)} - | mask == Six = addr { octet1 = (apply_mask oct1 Six bit), - octet2 = (apply_mask oct2 Zero bit), + | mask == Twelve = addr { octet2 = (apply_mask oct2 Four bit), octet3 = (apply_mask oct3 Zero bit), octet4 = (apply_mask oct4 Zero bit)} - | mask == Five = addr { octet1 = (apply_mask oct1 Five bit), - octet2 = (apply_mask oct2 Zero bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Four = addr { octet1 = (apply_mask oct1 Four bit), - octet2 = (apply_mask oct2 Zero bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Three = addr { octet1 = (apply_mask oct1 Three bit), - octet2 = (apply_mask oct2 Zero bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Two = addr { octet1 = (apply_mask oct1 Two bit), - octet2 = (apply_mask oct2 Zero bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == One = addr { octet1 = (apply_mask oct1 One bit), - octet2 = (apply_mask oct2 Zero bit), + | mask == Eleven = addr { octet2 = (apply_mask oct2 Three bit), octet3 = (apply_mask oct3 Zero bit), octet4 = (apply_mask oct4 Zero bit)} - | mask == Zero = addr { octet1 = (apply_mask oct1 Zero bit), - octet2 = (apply_mask oct2 Zero bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | otherwise = IPv4Address.None - where - oct1 = (octet1 addr) - oct2 = (octet2 addr) - oct3 = (octet3 addr) - oct4 = (octet4 addr) - + | mask == Ten = addr { octet2 = (apply_mask oct2 Two bit), + octet3 = (apply_mask oct3 Zero bit), + octet4 = (apply_mask oct4 Zero bit)} + | mask == Nine = addr { octet2 = (apply_mask oct2 One bit), + octet3 = (apply_mask oct3 Zero bit), + octet4 = (apply_mask oct4 Zero bit)} + | mask == Eight = addr { octet2 = (apply_mask oct2 Zero bit), + octet3 = (apply_mask oct3 Zero bit), + octet4 = (apply_mask oct4 Zero bit)} + | mask == Seven = addr { octet1 = (apply_mask oct1 Seven bit), + octet2 = (apply_mask oct2 Zero bit), + octet3 = (apply_mask oct3 Zero bit), + octet4 = (apply_mask oct4 Zero bit)} + | mask == Six = addr { octet1 = (apply_mask oct1 Six bit), + octet2 = (apply_mask oct2 Zero bit), + octet3 = (apply_mask oct3 Zero bit), + octet4 = (apply_mask oct4 Zero bit)} + | mask == Five = addr { octet1 = (apply_mask oct1 Five bit), + octet2 = (apply_mask oct2 Zero bit), + octet3 = (apply_mask oct3 Zero bit), + octet4 = (apply_mask oct4 Zero bit)} + | mask == Four = addr { octet1 = (apply_mask oct1 Four bit), + octet2 = (apply_mask oct2 Zero bit), + octet3 = (apply_mask oct3 Zero bit), + octet4 = (apply_mask oct4 Zero bit)} + | mask == Three = addr { octet1 = (apply_mask oct1 Three bit), + octet2 = (apply_mask oct2 Zero bit), + octet3 = (apply_mask oct3 Zero bit), + octet4 = (apply_mask oct4 Zero bit)} + | mask == Two = addr { octet1 = (apply_mask oct1 Two bit), + octet2 = (apply_mask oct2 Zero bit), + octet3 = (apply_mask oct3 Zero bit), + octet4 = (apply_mask oct4 Zero bit)} + | mask == One = addr { octet1 = (apply_mask oct1 One bit), + octet2 = (apply_mask oct2 Zero bit), + octet3 = (apply_mask oct3 Zero bit), + octet4 = (apply_mask oct4 Zero bit)} + | mask == Zero = addr { octet1 = (apply_mask oct1 Zero bit), + octet2 = (apply_mask oct2 Zero bit), + octet3 = (apply_mask oct3 Zero bit), + octet4 = (apply_mask oct4 Zero bit)} + where + oct1 = (octet1 addr) + oct2 = (octet2 addr) + oct3 = (octet3 addr) + oct4 = (octet4 addr) --- We don't export our constructor so this function is the only --- way to construct an address from octets. As a result, we can --- return IPv4Address.None in response to being passed one of more --- Octet.None octets. -ipv4address_from_octets :: Octet -> Octet -> Octet -> Octet -> IPv4Address -ipv4address_from_octets oct1 oct2 oct3 oct4 - | or [oct1 == Octet.None, - oct2 == Octet.None, - oct3 == Octet.None, - oct4 == Octet.None] = IPv4Address.None - | otherwise = IPv4Address oct1 oct2 oct3 oct4 - --- The minimum possible IPv4 address, 0.0.0.0. +-- | The minimum possible IPv4 address, 0.0.0.0. min_address :: IPv4Address -min_address = IPv4Address min_octet min_octet min_octet min_octet +min_address = + IPv4Address min_octet min_octet min_octet min_octet --- The maximum possible IPv4 address, 255.255.255.255. +-- | The maximum possible IPv4 address, 255.255.255.255. max_address :: IPv4Address -max_address = IPv4Address max_octet max_octet max_octet max_octet +max_address = + IPv4Address max_octet max_octet max_octet max_octet --- Given two addresses, find the number of the most significant bit --- where they differ. If the addresses are the same, return --- Maskbits.Zero. +-- | Given two addresses, find the number of the most significant bit +-- where they differ. If the addresses are the same, return +-- Maskbits.Zero. most_sig_bit_different :: IPv4Address -> IPv4Address -> Maskbits most_sig_bit_different addr1 addr2 | addr1 == addr2 = Maskbits.Zero @@ -281,24 +265,38 @@ most_sig_bit_different addr1 addr2 -- HUnit Tests mk_testaddr :: Int -> Int -> Int -> Int -> IPv4Address mk_testaddr a b c d = - IPv4Address oct1 oct2 oct3 oct4 - where - oct1 = octet_from_int a - oct2 = octet_from_int b - oct3 = octet_from_int c - oct4 = octet_from_int d + IPv4Address oct1 oct2 oct3 oct4 + where + oct1 = fromJust $ octet_from_int a + oct2 = fromJust $ octet_from_int b + oct3 = fromJust $ octet_from_int c + oct4 = fromJust $ octet_from_int d test_most_sig_bit_different1 :: Test test_most_sig_bit_different1 = - TestCase $ assertEqual "10.1.1.0 and 10.1.0.0 differ in bit 24" TwentyFour (most_sig_bit_different (mk_testaddr 10 1 1 0) (mk_testaddr 10 1 0 0)) + TestCase $ assertEqual "10.1.1.0 and 10.1.0.0 differ in bit 24" + TwentyFour + bit + where + addr1 = mk_testaddr 10 1 1 0 + addr2 = (mk_testaddr 10 1 0 0) + bit = most_sig_bit_different addr1 addr2 + test_most_sig_bit_different2 :: Test test_most_sig_bit_different2 = - TestCase $ assertEqual "10.1.2.0 and 10.1.1.0 differ in bit 23" TwentyThree (most_sig_bit_different (mk_testaddr 10 1 2 0) (mk_testaddr 10 1 1 0)) + TestCase $ assertEqual "10.1.2.0 and 10.1.1.0 differ in bit 23" + TwentyThree + bit + where + addr1 = mk_testaddr 10 1 2 0 + addr2 = mk_testaddr 10 1 1 0 + bit = most_sig_bit_different addr1 addr2 ipv4address_tests :: [Test] -ipv4address_tests = [ test_most_sig_bit_different1, - test_most_sig_bit_different2 ] +ipv4address_tests = + [ test_most_sig_bit_different1, + test_most_sig_bit_different2 ] diff --git a/src/Main.hs b/src/Main.hs index 6fa7da9..b2fc64b 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,4 +1,5 @@ import Data.List ((\\), intercalate, intersperse) +import Data.Maybe (catMaybes, isNothing) import System.Exit (ExitCode(..), exitWith) import System.IO (stderr, hPutStrLn) @@ -114,35 +115,38 @@ main = do input <- inputfunc let cidr_strings = lines input - let cidrs = map Cidr.cidr_from_string cidr_strings + let cidrs = map cidr_from_string cidr_strings - if (any (== Cidr.None) cidrs) + if (any isNothing cidrs) then do putStrLn "Error: not valid CIDR notation." exitWith (ExitFailure exit_invalid_cidr) else do -- Nothing + -- Filter out only the valid ones. + let valid_cidrs = catMaybes cidrs + -- Get the mode of operation. mode <- CommandLine.parse_mode case mode of Regex -> do - let regexes = map cidr_to_regex cidrs - putStrLn $ alternate regexes + let regexes = map cidr_to_regex valid_cidrs + putStrLn $ alternate regexes Reduce -> do - _ <- mapM (putStrLn . show) (combine_all cidrs) - return () + _ <- mapM (putStrLn . show) (combine_all valid_cidrs) + return () Dupe -> do - _ <- mapM (putStrLn . show) dupes - return () - where - dupes = cidrs \\ (combine_all cidrs) + _ <- mapM (putStrLn . show) dupes + return () + where + dupes = valid_cidrs \\ (combine_all valid_cidrs) Diff -> do - _ <- mapM putStrLn deletions - _ <- mapM putStrLn additions - return () - where - dupes = cidrs \\ (combine_all cidrs) - deletions = map (\s -> "-" ++ (show s)) dupes - newcidrs = (combine_all cidrs) \\ cidrs - additions = map (\s -> "+" ++ (show s)) newcidrs + _ <- mapM putStrLn deletions + _ <- mapM putStrLn additions + return () + where + dupes = valid_cidrs \\ (combine_all valid_cidrs) + deletions = map (\s -> "-" ++ (show s)) dupes + newcidrs = (combine_all valid_cidrs) \\ valid_cidrs + additions = map (\s -> "+" ++ (show s)) newcidrs diff --git a/src/Maskable.hs b/src/Maskable.hs index 9e72b8e..d05ff6f 100644 --- a/src/Maskable.hs +++ b/src/Maskable.hs @@ -3,19 +3,20 @@ module Maskable where import Bit import Maskbits --- Any string of bits should be maskable by some number of netmask --- bits. The convention of the Maskable typeclass follows CIDR --- notation, where the number of mask bits (the number after the --- slash) denotes how many bits are reserved for the network. +-- | Any string of bits should be maskable by some number of netmask +-- bits. The convention of the Maskable typeclass follows CIDR +-- notation, where the number of mask bits (the number after the +-- slash) denotes how many bits are reserved for the network. -- --- So, a mask of 32 applied to an address of 127.0.0.1 will again --- return 127.0.0.1. Likewise, 31 mask bits applied to 127.0.0.1 --- should return 127.0.0.0, since 127.0.0.1/31 matches both 127.0.0.0 --- and 127.0.0.1. In this case, the final '0' or '1' is the host --- part of the address. The '127.0.0' is thus the network part. +-- So, a mask of 32 applied to an address of 127.0.0.1 will again +-- return 127.0.0.1. Likewise, 31 mask bits applied to 127.0.0.1 +-- should return 127.0.0.0, since 127.0.0.1/31 matches both +-- 127.0.0.0 and 127.0.0.1. In this case, the final '0' or '1' is +-- the host part of the address. The '127.0.0' is thus the network +-- part. +-- +-- The Bit argument allows us to specify whether the host bits +-- should be replaced with either Zero or One. -- --- The Bit argument allows us to specify whether the host bits --- should be replaced with either Zero or One. class Maskable a where apply_mask :: a -> Maskbits -> Bit -> a - diff --git a/src/Maskbits.hs b/src/Maskbits.hs index 06314e3..d7064d8 100644 --- a/src/Maskbits.hs +++ b/src/Maskbits.hs @@ -7,167 +7,165 @@ module Maskbits import Test.QuickCheck -- A type representing the number of bits in a CIDR netmask. -data Maskbits = None - | Zero - | One - | Two - | Three - | Four - | Five - | Six - | Seven - | Eight - | Nine - | Ten - | Eleven - | Twelve - | Thirteen - | Fourteen - | Fifteen - | Sixteen - | Seventeen - | Eighteen - | Nineteen - | Twenty - | TwentyOne - | TwentyTwo - | TwentyThree - | TwentyFour - | TwentyFive - | TwentySix - | TwentySeven - | TwentyEight - | TwentyNine - | Thirty - | ThirtyOne - | ThirtyTwo - deriving (Eq, Ord) +data Maskbits = + Zero + | One + | Two + | Three + | Four + | Five + | Six + | Seven + | Eight + | Nine + | Ten + | Eleven + | Twelve + | Thirteen + | Fourteen + | Fifteen + | Sixteen + | Seventeen + | Eighteen + | Nineteen + | Twenty + | TwentyOne + | TwentyTwo + | TwentyThree + | TwentyFour + | TwentyFive + | TwentySix + | TwentySeven + | TwentyEight + | TwentyNine + | Thirty + | ThirtyOne + | ThirtyTwo + deriving (Eq, Ord) instance Show Maskbits where - show None = "None" - show Zero = "0" - show One = "1" - show Two = "2" - show Three = "3" - show Four = "4" - show Five = "5" - show Six = "6" - show Seven = "7" - show Eight = "8" - show Nine = "9" - show Ten = "10" - show Eleven = "11" - show Twelve = "12" - show Thirteen = "13" - show Fourteen = "14" - show Fifteen = "15" - show Sixteen = "16" - show Seventeen = "17" - show Eighteen = "18" - show Nineteen = "19" - show Twenty = "20" - show TwentyOne = "21" - show TwentyTwo = "22" - show TwentyThree = "23" - show TwentyFour = "24" - show TwentyFive = "25" - show TwentySix = "26" - show TwentySeven = "27" - show TwentyEight = "28" - show TwentyNine = "29" - show Thirty = "30" - show ThirtyOne = "31" - show ThirtyTwo = "32" + show Zero = "0" + show One = "1" + show Two = "2" + show Three = "3" + show Four = "4" + show Five = "5" + show Six = "6" + show Seven = "7" + show Eight = "8" + show Nine = "9" + show Ten = "10" + show Eleven = "11" + show Twelve = "12" + show Thirteen = "13" + show Fourteen = "14" + show Fifteen = "15" + show Sixteen = "16" + show Seventeen = "17" + show Eighteen = "18" + show Nineteen = "19" + show Twenty = "20" + show TwentyOne = "21" + show TwentyTwo = "22" + show TwentyThree = "23" + show TwentyFour = "24" + show TwentyFive = "25" + show TwentySix = "26" + show TwentySeven = "27" + show TwentyEight = "28" + show TwentyNine = "29" + show Thirty = "30" + show ThirtyOne = "31" + show ThirtyTwo = "32" instance Arbitrary Maskbits where - arbitrary = elements [ Zero, - One, - Two, - Three, - Four, - Five, - Six, - Seven, - Eight, - Nine, - Ten, - Eleven, - Twelve, - Thirteen, - Fourteen, - Fifteen, - Sixteen, - Seventeen, - Eighteen, - Nineteen, - Twenty, - TwentyOne, - TwentyTwo, - TwentyThree, - TwentyFour, - TwentyFive, - TwentySix, - TwentySeven, - TwentyEight, - TwentyNine, - Thirty, - ThirtyOne, - ThirtyTwo ] + arbitrary = + elements [ Zero, + One, + Two, + Three, + Four, + Five, + Six, + Seven, + Eight, + Nine, + Ten, + Eleven, + Twelve, + Thirteen, + Fourteen, + Fifteen, + Sixteen, + Seventeen, + Eighteen, + Nineteen, + Twenty, + TwentyOne, + TwentyTwo, + TwentyThree, + TwentyFour, + TwentyFive, + TwentySix, + TwentySeven, + TwentyEight, + TwentyNine, + Thirty, + ThirtyOne, + ThirtyTwo ] --- There are only 32 bits in an IPv4 address, so there --- can't be more bits than that in the mask. -maskbits_from_int :: Int -> Maskbits -maskbits_from_int 0 = Zero -maskbits_from_int 1 = One -maskbits_from_int 2 = Two -maskbits_from_int 3 = Three -maskbits_from_int 4 = Four -maskbits_from_int 5 = Five -maskbits_from_int 6 = Six -maskbits_from_int 7 = Seven -maskbits_from_int 8 = Eight -maskbits_from_int 9 = Nine -maskbits_from_int 10 = Ten -maskbits_from_int 11 = Eleven -maskbits_from_int 12 = Twelve -maskbits_from_int 13 = Thirteen -maskbits_from_int 14 = Fourteen -maskbits_from_int 15 = Fifteen -maskbits_from_int 16 = Sixteen -maskbits_from_int 17 = Seventeen -maskbits_from_int 18 = Eighteen -maskbits_from_int 19 = Nineteen -maskbits_from_int 20 = Twenty -maskbits_from_int 21 = TwentyOne -maskbits_from_int 22 = TwentyTwo -maskbits_from_int 23 = TwentyThree -maskbits_from_int 24 = TwentyFour -maskbits_from_int 25 = TwentyFive -maskbits_from_int 26 = TwentySix -maskbits_from_int 27 = TwentySeven -maskbits_from_int 28 = TwentyEight -maskbits_from_int 29 = TwentyNine -maskbits_from_int 30 = Thirty -maskbits_from_int 31 = ThirtyOne -maskbits_from_int 32 = ThirtyTwo -maskbits_from_int _ = None +-- | There are only 32 bits in an IPv4 address, so there +-- can't be more bits than that in the mask. +maskbits_from_int :: Int -> Maybe Maskbits +maskbits_from_int 0 = Just Zero +maskbits_from_int 1 = Just One +maskbits_from_int 2 = Just Two +maskbits_from_int 3 = Just Three +maskbits_from_int 4 = Just Four +maskbits_from_int 5 = Just Five +maskbits_from_int 6 = Just Six +maskbits_from_int 7 = Just Seven +maskbits_from_int 8 = Just Eight +maskbits_from_int 9 = Just Nine +maskbits_from_int 10 = Just Ten +maskbits_from_int 11 = Just Eleven +maskbits_from_int 12 = Just Twelve +maskbits_from_int 13 = Just Thirteen +maskbits_from_int 14 = Just Fourteen +maskbits_from_int 15 = Just Fifteen +maskbits_from_int 16 = Just Sixteen +maskbits_from_int 17 = Just Seventeen +maskbits_from_int 18 = Just Eighteen +maskbits_from_int 19 = Just Nineteen +maskbits_from_int 20 = Just Twenty +maskbits_from_int 21 = Just TwentyOne +maskbits_from_int 22 = Just TwentyTwo +maskbits_from_int 23 = Just TwentyThree +maskbits_from_int 24 = Just TwentyFour +maskbits_from_int 25 = Just TwentyFive +maskbits_from_int 26 = Just TwentySix +maskbits_from_int 27 = Just TwentySeven +maskbits_from_int 28 = Just TwentyEight +maskbits_from_int 29 = Just TwentyNine +maskbits_from_int 30 = Just Thirty +maskbits_from_int 31 = Just ThirtyOne +maskbits_from_int 32 = Just ThirtyTwo +maskbits_from_int _ = Nothing - -maskbits_from_string :: String -> Maskbits +maskbits_from_string :: String -> Maybe Maskbits maskbits_from_string s = - case (reads s :: [(Int, String)]) of - [] -> None - x:_ -> maskbits_from_int (fst x) + case (reads s :: [(Int, String)]) of + [] -> Nothing + x:_ -> maskbits_from_int (fst x) decrement :: Maskbits -> Maskbits -decrement None = None -decrement Zero = None +decrement Zero = Zero decrement One = Zero decrement Two = One decrement Three = Two diff --git a/src/Octet.hs b/src/Octet.hs index 7faf1d7..2e372fd 100644 --- a/src/Octet.hs +++ b/src/Octet.hs @@ -1,5 +1,7 @@ -module Octet where +module Octet +where +import Data.Maybe (fromJust) import Test.HUnit import Test.QuickCheck @@ -7,128 +9,156 @@ import Bit as B import Maskable import Maskbits --- An Octet consists of eight bits. For our purposes, the most --- significant bit will come "first." That is, b1 is in the 2^7 --- place while b8 is in the 2^0 place. -data Octet = None | Octet { b1 :: Bit, - b2 :: Bit, - b3 :: Bit, - b4 :: Bit, - b5 :: Bit, - b6 :: Bit, - b7 :: Bit, - b8 :: Bit } - deriving (Eq) +-- | An Octet consists of eight bits. For our purposes, the most +-- significant bit will come "first." That is, b1 is in the 2^7 +-- place while b8 is in the 2^0 place. +data Octet = + Octet { b1 :: Bit, + b2 :: Bit, + b3 :: Bit, + b4 :: Bit, + b5 :: Bit, + b6 :: Bit, + b7 :: Bit, + b8 :: Bit } + deriving (Eq) instance Show Octet where - show Octet.None = "None" - show oct = show (octet_to_int oct) + show oct = show (octet_to_int oct) instance Arbitrary Octet where - arbitrary = do - a1 <- arbitrary :: Gen Bit - a2 <- arbitrary :: Gen Bit - a3 <- arbitrary :: Gen Bit - a4 <- arbitrary :: Gen Bit - a5 <- arbitrary :: Gen Bit - a6 <- arbitrary :: Gen Bit - a7 <- arbitrary :: Gen Bit - a8 <- arbitrary :: Gen Bit - return (Octet a1 a2 a3 a4 a5 a6 a7 a8) + arbitrary = do + a1 <- arbitrary :: Gen Bit + a2 <- arbitrary :: Gen Bit + a3 <- arbitrary :: Gen Bit + a4 <- arbitrary :: Gen Bit + a5 <- arbitrary :: Gen Bit + a6 <- arbitrary :: Gen Bit + a7 <- arbitrary :: Gen Bit + a8 <- arbitrary :: Gen Bit + return (Octet a1 a2 a3 a4 a5 a6 a7 a8) instance Maskable Octet where - apply_mask _ Maskbits.None _ = Octet.None - apply_mask Octet.None _ _ = Octet.None - apply_mask oct mask bit - | mask == Eight = oct - | mask == Seven = oct { b8 = bit } - | mask == Six = oct { b8 = bit, b7 = bit } - | mask == Five = oct { b8 = bit, b7 = bit, b6 = bit } - | mask == Four = oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit } - | mask == Three = oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit, b4 = bit } - | mask == Two = oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit, b4 = bit, b3 = bit } - | mask == Maskbits.One = oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit, b4 = bit, b3 = bit, b2 = bit } - | mask == Maskbits.Zero = oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit, b4 = bit, b3 = bit, b2 = bit, b1 = bit } - | otherwise = Octet.None - - --- Convert each bit to its integer value, and multiply by the --- appropriate power of two. Sum them up, and we should get an integer --- between 0 and 255. + apply_mask oct Eight _ = oct + + apply_mask oct Seven bit = + oct { b8 = bit } + + apply_mask oct Six bit = + oct { b8 = bit, b7 = bit } + + apply_mask oct Five bit = + oct { b8 = bit, b7 = bit, b6 = bit } + + apply_mask oct Four bit = + oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit } + + apply_mask oct Three bit = + oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit, b4 = bit } + + apply_mask oct Two bit = + oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit, b4 = bit, b3 = bit } + + apply_mask oct Maskbits.One bit = + oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit, + b4 = bit, b3 = bit, b2 = bit } + + apply_mask oct Maskbits.Zero bit = + oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit, + b4 = bit, b3 = bit, b2 = bit, b1 = bit } + + -- The Maskbits must be in [Eight..ThirtyTwo]. + apply_mask oct _ _ = oct + + +-- | Convert each bit to its integer value, and multiply by the +-- appropriate power of two. Sum them up, and we should get an integer +-- between 0 and 255. octet_to_int :: Octet -> Int octet_to_int x = - 128 * (bit_to_int (b1 x)) + - 64 * (bit_to_int (b2 x)) + - 32 * (bit_to_int (b3 x)) + - 16 * (bit_to_int (b4 x)) + - 8 * (bit_to_int (b5 x)) + - 4 * (bit_to_int (b6 x)) + - 2 * (bit_to_int (b7 x)) + - 1 * (bit_to_int (b8 x)) + 128 * (bit_to_int (b1 x)) + + 64 * (bit_to_int (b2 x)) + + 32 * (bit_to_int (b3 x)) + + 16 * (bit_to_int (b4 x)) + + 8 * (bit_to_int (b5 x)) + + 4 * (bit_to_int (b6 x)) + + 2 * (bit_to_int (b7 x)) + + 1 * (bit_to_int (b8 x)) -octet_from_int :: Int -> Octet +octet_from_int :: Int -> Maybe Octet octet_from_int x - | (x < 0) || (x > 255) = Octet.None - | otherwise = (Octet a1 a2 a3 a4 a5 a6 a7 a8) - where - a1 = if (x >= 128) then B.One else B.Zero - a2 = if ((x `mod` 128) >= 64) then B.One else B.Zero - a3 = if ((x `mod` 64) >= 32) then B.One else B.Zero - a4 = if ((x `mod` 32) >= 16) then B.One else B.Zero - a5 = if ((x `mod` 16) >= 8) then B.One else B.Zero - a6 = if ((x `mod` 8) >= 4) then B.One else B.Zero - a7 = if ((x `mod` 4) >= 2) then B.One else B.Zero - a8 = if ((x `mod` 2) == 1) then B.One else B.Zero - - -octet_from_string :: String -> Octet + | (x < 0) || (x > 255) = Nothing + | otherwise = Just (Octet a1 a2 a3 a4 a5 a6 a7 a8) + where + a1 = if (x >= 128) then B.One else B.Zero + a2 = if ((x `mod` 128) >= 64) then B.One else B.Zero + a3 = if ((x `mod` 64) >= 32) then B.One else B.Zero + a4 = if ((x `mod` 32) >= 16) then B.One else B.Zero + a5 = if ((x `mod` 16) >= 8) then B.One else B.Zero + a6 = if ((x `mod` 8) >= 4) then B.One else B.Zero + a7 = if ((x `mod` 4) >= 2) then B.One else B.Zero + a8 = if ((x `mod` 2) == 1) then B.One else B.Zero + + +octet_from_string :: String -> Maybe Octet octet_from_string s = - case (reads s :: [(Int, String)]) of - [] -> Octet.None - x:_ -> octet_from_int (fst x) + case (reads s :: [(Int, String)]) of + [] -> Nothing + x:_ -> octet_from_int (fst x) --- The octet with the least possible value. +-- | The octet with the least possible value. min_octet :: Octet -min_octet = Octet B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero +min_octet = + Octet B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero --- The octet with the greatest possible value. +-- | The octet with the greatest possible value. max_octet :: Octet -max_octet = Octet B.One B.One B.One B.One B.One B.One B.One B.One +max_octet = + Octet B.One B.One B.One B.One B.One B.One B.One B.One -- HUnit Tests test_octet_from_int1 :: Test test_octet_from_int1 = - TestCase $ assertEqual "octet_from_int 128 should parse as 10000000" oct1 (octet_from_int 128) - where - oct1 = Octet B.One B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero - + TestCase $ assertEqual "octet_from_int 128 should parse as 10000000" oct1 oct2 + where + oct1 = Octet B.One B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero + oct2 = fromJust $ octet_from_int 128 test_octet_mask1 :: Test test_octet_mask1 = - TestCase $ assertEqual "The network bits of 255/4 should equal 240" oct2 (apply_mask oct1 Four B.Zero) - where - oct1 = octet_from_int 255 - oct2 = octet_from_int 240 + TestCase $ + assertEqual + "The network bits of 255/4 should equal 240" + oct2 + (apply_mask oct1 Four B.Zero) + where + oct1 = fromJust $ octet_from_int 255 + oct2 = fromJust $ octet_from_int 240 test_octet_mask2 :: Test test_octet_mask2 = - TestCase $ assertEqual "The network bits of 255/1 should equal 128" oct2 (apply_mask oct1 Maskbits.One B.Zero) - where - oct1 = octet_from_int 255 - oct2 = octet_from_int 128 + TestCase $ + assertEqual + "The network bits of 255/1 should equal 128" + oct2 + (apply_mask oct1 Maskbits.One B.Zero) + where + oct1 = fromJust $ octet_from_int 255 + oct2 = fromJust $ octet_from_int 128 octet_tests :: [Test] -octet_tests = [ test_octet_from_int1, - test_octet_mask1, - test_octet_mask2 ] +octet_tests = + [ test_octet_from_int1, + test_octet_mask1, + test_octet_mask2 ] -- 2.43.2 From b4397616550eff1141b094f3fe0c38a1b3caffb7 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Mon, 26 Dec 2011 22:38:46 -0500 Subject: [PATCH 03/16] Remove an accidental line break and indentation. --- src/Cidr.hs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Cidr.hs b/src/Cidr.hs index 826c862..a07117d 100644 --- a/src/Cidr.hs +++ b/src/Cidr.hs @@ -425,8 +425,7 @@ test_combine_all3 = where cidr1 = fromJust $ cidr_from_string "10.0.0.16/32" cidr2 = fromJust $ cidr_from_string "10.0.0.17/32" - - cidr3 = fromJust $ cidr_from_string "10.0.0.18/32" + cidr3 = fromJust $ cidr_from_string "10.0.0.18/32" cidr4 = fromJust $ cidr_from_string "10.0.0.19/32" expected_cidrs = [fromJust $ cidr_from_string "10.0.0.16/30"] test_cidrs = [cidr1, cidr2, cidr3, cidr4] -- 2.43.2 From a6569af040d40db73dfd32677893916638c0d56a Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Mon, 26 Dec 2011 22:52:39 -0500 Subject: [PATCH 04/16] Greatly simplify the definition of IPv4Address.apply_mask. --- src/IPv4Address.hs | 203 ++++++++++++++++++++++++++------------------- 1 file changed, 116 insertions(+), 87 deletions(-) diff --git a/src/IPv4Address.hs b/src/IPv4Address.hs index 6153d28..b46f18c 100644 --- a/src/IPv4Address.hs +++ b/src/IPv4Address.hs @@ -46,93 +46,122 @@ instance Arbitrary IPv4Address where instance Maskable IPv4Address where - apply_mask addr mask bit - | mask == ThirtyTwo = addr - | mask == ThirtyOne = addr { octet4 = (apply_mask oct4 Seven bit) } - | mask == Thirty = addr { octet4 = (apply_mask oct4 Six bit) } - | mask == TwentyNine = addr { octet4 = (apply_mask oct4 Five bit) } - | mask == TwentyEight = addr { octet4 = (apply_mask oct4 Four bit) } - | mask == TwentySeven = addr { octet4 = (apply_mask oct4 Three bit) } - | mask == TwentySix = addr { octet4 = (apply_mask oct4 Two bit) } - | mask == TwentyFive = addr { octet4 = (apply_mask oct4 One bit) } - | mask == TwentyFour = addr { octet4 = (apply_mask oct4 Zero bit) } - | mask == TwentyThree = addr { octet3 = (apply_mask oct3 Seven bit), - octet4 = (apply_mask oct4 Zero bit) } - | mask == TwentyTwo = addr { octet3 = (apply_mask oct3 Six bit), - octet4 = (apply_mask oct4 Zero bit) } - | mask == TwentyOne = addr { octet3 = (apply_mask oct3 Five bit), - octet4 = (apply_mask oct4 Zero bit) } - | mask == Twenty = addr { octet3 = (apply_mask oct3 Four bit), - octet4 = (apply_mask oct4 Zero bit) } - | mask == Nineteen = addr { octet3 = (apply_mask oct3 Three bit), - octet4 = (apply_mask oct4 Zero bit) } - | mask == Eighteen = addr { octet3 = (apply_mask oct3 Two bit), - octet4 = (apply_mask oct4 Zero bit) } - | mask == Seventeen = addr { octet3 = (apply_mask oct3 One bit), - octet4 = (apply_mask oct4 Zero bit) } - | mask == Sixteen = addr { octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit) } - | mask == Fifteen = addr { octet2 = (apply_mask oct2 Seven bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit) } - | mask == Fourteen = addr { octet2 = (apply_mask oct2 Six bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Thirteen = addr { octet2 = (apply_mask oct2 Five bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Twelve = addr { octet2 = (apply_mask oct2 Four bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Eleven = addr { octet2 = (apply_mask oct2 Three bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Ten = addr { octet2 = (apply_mask oct2 Two bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Nine = addr { octet2 = (apply_mask oct2 One bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Eight = addr { octet2 = (apply_mask oct2 Zero bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Seven = addr { octet1 = (apply_mask oct1 Seven bit), - octet2 = (apply_mask oct2 Zero bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Six = addr { octet1 = (apply_mask oct1 Six bit), - octet2 = (apply_mask oct2 Zero bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Five = addr { octet1 = (apply_mask oct1 Five bit), - octet2 = (apply_mask oct2 Zero bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Four = addr { octet1 = (apply_mask oct1 Four bit), - octet2 = (apply_mask oct2 Zero bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Three = addr { octet1 = (apply_mask oct1 Three bit), - octet2 = (apply_mask oct2 Zero bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Two = addr { octet1 = (apply_mask oct1 Two bit), - octet2 = (apply_mask oct2 Zero bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == One = addr { octet1 = (apply_mask oct1 One bit), - octet2 = (apply_mask oct2 Zero bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - | mask == Zero = addr { octet1 = (apply_mask oct1 Zero bit), - octet2 = (apply_mask oct2 Zero bit), - octet3 = (apply_mask oct3 Zero bit), - octet4 = (apply_mask oct4 Zero bit)} - where - oct1 = (octet1 addr) - oct2 = (octet2 addr) - oct3 = (octet3 addr) - oct4 = (octet4 addr) + apply_mask addr mask bit = + apply_mask' mask + where + oct1 = octet1 addr + oct2 = octet2 addr + oct3 = octet3 addr + oct4 = octet4 addr + + -- A copy of 'addr' with the fourth octet zeroed (or oned). + new_addr1 = addr { octet4 = (apply_mask oct4 Zero bit) } + + -- Likewise for new_addr1's third octet. + new_addr2 = new_addr1 { octet3 = (apply_mask oct3 Zero bit) } + + -- And new_addr2's second octet. + new_addr3 = new_addr2 { octet2 = (apply_mask oct2 Zero bit) } + + -- This helper function allows us to pattern-match cleanly. + apply_mask' :: Maskbits -> IPv4Address + + apply_mask' ThirtyTwo = addr + + apply_mask' ThirtyOne = addr { octet4 = (apply_mask oct4 Seven bit) } + + apply_mask' Thirty = + addr { octet4 = (apply_mask oct4 Six bit) } + + apply_mask' TwentyNine = + addr { octet4 = (apply_mask oct4 Five bit) } + + apply_mask' TwentyEight = + addr { octet4 = (apply_mask oct4 Four bit) } + + apply_mask' TwentySeven = + addr { octet4 = (apply_mask oct4 Three bit) } + + apply_mask' TwentySix = + addr { octet4 = (apply_mask oct4 Two bit) } + + apply_mask' TwentyFive = + addr { octet4 = (apply_mask oct4 One bit) } + + apply_mask' TwentyFour = new_addr1 + + apply_mask' TwentyThree = + new_addr1 { octet3 = (apply_mask oct3 Seven bit) } + + apply_mask' TwentyTwo = + new_addr1 { octet3 = (apply_mask oct3 Six bit) } + + apply_mask' TwentyOne = + new_addr1 { octet3 = (apply_mask oct3 Five bit) } + + apply_mask' Twenty = + new_addr1 { octet3 = (apply_mask oct3 Four bit) } + + apply_mask' Nineteen = + new_addr1 { octet3 = (apply_mask oct3 Three bit) } + + apply_mask' Eighteen = + new_addr1 { octet3 = (apply_mask oct3 Two bit) } + + apply_mask' Seventeen = + new_addr1 { octet3 = (apply_mask oct3 One bit) } + + apply_mask' Sixteen = + new_addr2 + + apply_mask' Fifteen = + new_addr2 { octet2 = (apply_mask oct2 Seven bit) } + + apply_mask' Fourteen = + new_addr2 { octet2 = (apply_mask oct2 Six bit) } + + apply_mask' Thirteen = + new_addr2 { octet2 = (apply_mask oct2 Five bit) } + + apply_mask' Twelve = + new_addr2 { octet2 = (apply_mask oct2 Four bit) } + + apply_mask' Eleven = + new_addr2 { octet2 = (apply_mask oct2 Three bit) } + + apply_mask' Ten = + new_addr2 { octet2 = (apply_mask oct2 Two bit) } + + apply_mask' Nine = + new_addr2 { octet2 = (apply_mask oct2 One bit) } + + apply_mask' Eight = + new_addr3 { octet2 = (apply_mask oct2 Zero bit) } + + apply_mask' Seven = + new_addr3 { octet1 = (apply_mask oct1 Seven bit) } + + apply_mask' Six = + new_addr3 { octet1 = (apply_mask oct1 Six bit) } + + apply_mask' Five = + new_addr3 { octet1 = (apply_mask oct1 Five bit) } + + apply_mask' Four = + new_addr3 { octet1 = (apply_mask oct1 Four bit) } + + apply_mask' Three = + new_addr3 { octet1 = (apply_mask oct1 Three bit) } + + apply_mask' Two = + new_addr3 { octet1 = (apply_mask oct1 Two bit) } + + apply_mask' One = + new_addr3 { octet1 = (apply_mask oct1 One bit) } + + apply_mask' Zero = + new_addr3 { octet1 = (apply_mask oct1 Zero bit) } + -- | The minimum possible IPv4 address, 0.0.0.0. -- 2.43.2 From 22b9c60a6badcc6cb68902965d6f810634e30693 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Thu, 12 Apr 2012 13:21:59 -0400 Subject: [PATCH 05/16] Relax the base version requirement. --- hath.cabal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hath.cabal b/hath.cabal index ca43858..79639ce 100644 --- a/hath.cabal +++ b/hath.cabal @@ -10,7 +10,7 @@ build-type: Simple executable hath build-depends: - base == 4.4.*, + base == 4.*, HUnit == 1.2.*, QuickCheck == 2.4.* -- 2.43.2 From d32fe2f0a6c83ba3046a405eda40f83c794c000d Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 21 Apr 2013 12:24:14 -0400 Subject: [PATCH 06/16] Use test-framework for the tests, and bump some dependencies. --- hath.cabal | 5 +- src/Cidr.hs | 146 ++++++++++++++++++++++++++++----------------- src/IPv4Address.hs | 17 ++++-- src/Octet.hs | 32 +++++----- test/TestSuite.hs | 56 ++++++++++------- 5 files changed, 159 insertions(+), 97 deletions(-) diff --git a/hath.cabal b/hath.cabal index 79639ce..2ea92c0 100644 --- a/hath.cabal +++ b/hath.cabal @@ -12,7 +12,10 @@ executable hath build-depends: base == 4.*, HUnit == 1.2.*, - QuickCheck == 2.4.* + QuickCheck == 2.6.*, + test-framework == 0.8.*, + test-framework-hunit == 0.3.*, + test-framework-quickcheck2 == 0.3.* main-is: Main.hs diff --git a/src/Cidr.hs b/src/Cidr.hs index a07117d..d41ba11 100644 --- a/src/Cidr.hs +++ b/src/Cidr.hs @@ -1,6 +1,7 @@ module Cidr ( Cidr(..), cidr_from_string, + cidr_properties, cidr_tests, combine_all, contains, @@ -19,8 +20,12 @@ module Cidr import Data.List (nubBy) import Data.Maybe (catMaybes, fromJust) -import Test.HUnit -import Test.QuickCheck + +import Test.HUnit (assertEqual) +import Test.Framework (Test, testGroup) +import Test.Framework.Providers.HUnit (testCase) +import Test.Framework.Providers.QuickCheck2 (testProperty) +import Test.QuickCheck (Arbitrary(..), Gen, Property, (==>)) import qualified Bit as B import IPv4Address @@ -235,137 +240,151 @@ adjacent cidr1 cidr2 test_min_host1 :: Test test_min_host1 = - TestCase $ assertEqual "The minimum host in 10.0.0.0/24 is 10.0.0.0" - expected - actual + testCase desc $ + assertEqual desc + expected + actual where + desc = "The minimum host in 10.0.0.0/24 is 10.0.0.0" actual = show $ min_host (fromJust $ cidr_from_string "10.0.0.0/24") expected = "10.0.0.0" test_max_host1 :: Test test_max_host1 = - TestCase $ assertEqual "The maximum host in 10.0.0.0/24 is 10.0.0.255" - expected - actual + testCase desc $ + assertEqual desc + expected + actual where + desc = "The maximum host in 10.0.0.0/24 is 10.0.0.255" actual = show $ max_host (fromJust $ cidr_from_string "10.0.0.0/24") expected = "10.0.0.255" test_equality1 :: Test test_equality1 = - TestCase $ + testCase desc $ assertEqual - "10.1.1.0/23 equals itself" + desc True (cidr1 == cidr1) where + desc = "10.1.1.0/23 equals itself" cidr1 = fromJust $ cidr_from_string "10.1.1.0/23" test_contains1 :: Test test_contains1 = - TestCase $ + testCase desc $ assertEqual - "10.1.1.0/23 contains 10.1.1.0/24" + desc True (cidr1 `contains` cidr2) where + desc = "10.1.1.0/23 contains 10.1.1.0/24" cidr1 = fromJust $ cidr_from_string "10.1.1.0/23" cidr2 = fromJust $ cidr_from_string "10.1.1.0/24" test_contains2 :: Test test_contains2 = - TestCase $ + testCase desc $ assertEqual - "10.1.1.0/23 contains itself" + desc True (cidr1 `contains` cidr1) where + desc = "10.1.1.0/23 contains itself" cidr1 = fromJust $ cidr_from_string "10.1.1.0/23" test_contains_proper1 :: Test test_contains_proper1 = - TestCase $ + testCase desc $ assertEqual - "10.1.1.0/23 contains 10.1.1.0/24 properly" + desc True (cidr1 `contains_proper` cidr2) where + desc = "10.1.1.0/23 contains 10.1.1.0/24 properly" cidr1 = fromJust $ cidr_from_string "10.1.1.0/23" cidr2 = fromJust $ cidr_from_string "10.1.1.0/24" test_contains_proper2 :: Test test_contains_proper2 = - TestCase $ + testCase desc $ assertEqual - "10.1.1.0/23 does not contain itself properly" + desc False (cidr1 `contains_proper` cidr1) where + desc = "10.1.1.0/23 does not contain itself properly" cidr1 = fromJust $ cidr_from_string "10.1.1.0/23" test_adjacent1 :: Test test_adjacent1 = - TestCase $ + testCase desc $ assertEqual - "10.1.0.0/24 is adjacent to 10.1.1.0/24" + desc True (cidr1 `adjacent` cidr2) where + desc = "10.1.0.0/24 is adjacent to 10.1.1.0/24" cidr1 = fromJust $ cidr_from_string "10.1.0.0/24" cidr2 = fromJust $ cidr_from_string "10.1.1.0/24" test_adjacent2 :: Test test_adjacent2 = - TestCase $ + testCase desc $ assertEqual - "10.1.0.0/23 is not adjacent to 10.1.0.0/24" + desc False (cidr1 `adjacent` cidr2) where + desc = "10.1.0.0/23 is not adjacent to 10.1.0.0/24" cidr1 = fromJust $ cidr_from_string "10.1.0.0/23" cidr2 = fromJust $ cidr_from_string "10.1.0.0/24" test_adjacent3 :: Test test_adjacent3 = - TestCase $ + testCase desc $ assertEqual - "10.1.0.0/24 is not adjacent to 10.2.5.0/24" + desc False (cidr1 `adjacent` cidr2) where + desc = "10.1.0.0/24 is not adjacent to 10.2.5.0/24" cidr1 = fromJust $ cidr_from_string "10.1.0.0/24" cidr2 = fromJust $ cidr_from_string "10.2.5.0/24" test_adjacent4 :: Test test_adjacent4 = - TestCase $ + testCase desc $ assertEqual - "10.1.1.0/24 is not adjacent to 10.1.2.0/24" + desc False (cidr1 `adjacent` cidr2) where + desc = "10.1.1.0/24 is not adjacent to 10.1.2.0/24" cidr1 = fromJust $ cidr_from_string "10.1.1.0/24" cidr2 = fromJust $ cidr_from_string "10.1.2.0/24" test_combine_contained1 :: Test test_combine_contained1 = - TestCase $ + testCase desc $ assertEqual - "10.0.0.0/8, 10.1.0.0/16, and 10.1.1.0/24 combine to 10.0.0.0/8" + desc expected_cidrs (combine_contained test_cidrs) where + desc = "10.0.0.0/8, 10.1.0.0/16, and 10.1.1.0/24 combine to 10.0.0.0/8" cidr1 = fromJust $ cidr_from_string "10.0.0.0/8" cidr2 = fromJust $ cidr_from_string "10.1.0.0/16" cidr3 = fromJust $ cidr_from_string "10.1.1.0/24" @@ -375,24 +394,27 @@ test_combine_contained1 = test_combine_contained2 :: Test test_combine_contained2 = - TestCase $ + testCase desc $ assertEqual - "192.168.3.0/23 does not contain 192.168.1.0/24" + desc [cidr1, cidr2] (combine_contained [cidr1, cidr2]) where + desc = "192.168.3.0/23 does not contain 192.168.1.0/24" cidr1 = fromJust $ cidr_from_string "192.168.3.0/23" cidr2 = fromJust $ cidr_from_string "192.168.1.0/24" test_combine_all1 :: Test test_combine_all1 = - TestCase $ + testCase desc $ assertEqual - "10.0.0.0/24 is adjacent to 10.0.1.0/24 and 10.0.3.0/23 contains 10.0.2.0/24" + desc expected_cidrs (combine_all test_cidrs) where + desc = "10.0.0.0/24 is adjacent to 10.0.1.0/24 " + ++ "and 10.0.3.0/23 contains 10.0.2.0/24" cidr1 = fromJust $ cidr_from_string "10.0.0.0/24" cidr2 = fromJust $ cidr_from_string "10.0.1.0/24" cidr3 = fromJust $ cidr_from_string "10.0.2.0/24" @@ -404,12 +426,13 @@ test_combine_all1 = test_combine_all2 :: Test test_combine_all2 = - TestCase $ + testCase desc $ assertEqual - "127.0.0.1/32 combines with itself recursively" + desc expected_cidrs (combine_all test_cidrs) where + desc = "127.0.0.1/32 combines with itself recursively" cidr1 = fromJust $ cidr_from_string "127.0.0.1/32" expected_cidrs = [cidr1] test_cidrs = [cidr1, cidr1, cidr1, cidr1, cidr1] @@ -417,12 +440,14 @@ test_combine_all2 = test_combine_all3 :: Test test_combine_all3 = - TestCase $ + testCase desc $ assertEqual - "10.0.0.16, 10.0.0.17, 10.0.0.18, and 10.0.0.19 get combined into 10.0.0.16/30" + desc expected_cidrs (combine_all test_cidrs) where + desc = "10.0.0.16, 10.0.0.17, 10.0.0.18, and " + ++ "10.0.0.19 get combined into 10.0.0.16/30" cidr1 = fromJust $ cidr_from_string "10.0.0.16/32" cidr2 = fromJust $ cidr_from_string "10.0.0.17/32" cidr3 = fromJust $ cidr_from_string "10.0.0.18/32" @@ -431,24 +456,25 @@ test_combine_all3 = test_cidrs = [cidr1, cidr2, cidr3, cidr4] -cidr_tests :: [Test] -cidr_tests = [ test_min_host1, - test_max_host1, - test_equality1, - test_contains1, - test_contains2, - test_contains_proper1, - test_contains_proper2, - test_adjacent1, - test_adjacent2, - test_adjacent3, - test_adjacent4, - test_combine_contained1, - test_combine_contained2, - test_combine_all1, - test_combine_all2, - test_combine_all3 - ] +cidr_tests :: Test +cidr_tests = + testGroup "CIDR Tests" [ + test_min_host1, + test_max_host1, + test_equality1, + test_contains1, + test_contains2, + test_contains_proper1, + test_contains_proper2, + test_adjacent1, + test_adjacent2, + test_adjacent3, + test_adjacent4, + test_combine_contained1, + test_combine_contained2, + test_combine_all1, + test_combine_all2, + test_combine_all3 ] -- QuickCheck Tests @@ -462,3 +488,15 @@ prop_contains_proper_intransitive :: Cidr -> Cidr -> Property prop_contains_proper_intransitive cidr1 cidr2 = (cidr1 `contains_proper` cidr2) ==> (not (cidr2 `contains_proper` cidr1)) + +cidr_properties :: Test +cidr_properties = + testGroup "CIDR Properties" [ + testProperty + "All CIDRs contain themselves" + prop_all_cidrs_contain_themselves, + + testProperty + "contains_proper is intransitive" + prop_contains_proper_intransitive + ] diff --git a/src/IPv4Address.hs b/src/IPv4Address.hs index b46f18c..dac7dda 100644 --- a/src/IPv4Address.hs +++ b/src/IPv4Address.hs @@ -7,8 +7,10 @@ module IPv4Address ) where import Data.Maybe (fromJust) -import Test.HUnit -import Test.QuickCheck +import Test.HUnit (assertEqual) +import Test.Framework (Test, testGroup) +import Test.Framework.Providers.HUnit (testCase) +import Test.QuickCheck (Arbitrary(..), Gen) import Maskable import Maskbits @@ -304,10 +306,11 @@ mk_testaddr a b c d = test_most_sig_bit_different1 :: Test test_most_sig_bit_different1 = - TestCase $ assertEqual "10.1.1.0 and 10.1.0.0 differ in bit 24" + testCase desc $ assertEqual desc TwentyFour bit where + desc = "10.1.1.0 and 10.1.0.0 differ in bit 24" addr1 = mk_testaddr 10 1 1 0 addr2 = (mk_testaddr 10 1 0 0) bit = most_sig_bit_different addr1 addr2 @@ -316,16 +319,18 @@ test_most_sig_bit_different1 = test_most_sig_bit_different2 :: Test test_most_sig_bit_different2 = - TestCase $ assertEqual "10.1.2.0 and 10.1.1.0 differ in bit 23" + testCase desc $ assertEqual desc TwentyThree bit where + desc = "10.1.2.0 and 10.1.1.0 differ in bit 23" addr1 = mk_testaddr 10 1 2 0 addr2 = mk_testaddr 10 1 1 0 bit = most_sig_bit_different addr1 addr2 -ipv4address_tests :: [Test] +ipv4address_tests :: Test ipv4address_tests = - [ test_most_sig_bit_different1, + testGroup "IPv4 Address Tests" [ + test_most_sig_bit_different1, test_most_sig_bit_different2 ] diff --git a/src/Octet.hs b/src/Octet.hs index 2e372fd..78413e6 100644 --- a/src/Octet.hs +++ b/src/Octet.hs @@ -2,8 +2,12 @@ module Octet where import Data.Maybe (fromJust) -import Test.HUnit -import Test.QuickCheck + +import Test.HUnit (assertEqual) +import Test.Framework (Test, testGroup) +import Test.Framework.Providers.HUnit (testCase) + +import Test.QuickCheck (Arbitrary(..), Gen) import Bit as B import Maskable @@ -128,37 +132,35 @@ max_octet = -- HUnit Tests test_octet_from_int1 :: Test test_octet_from_int1 = - TestCase $ assertEqual "octet_from_int 128 should parse as 10000000" oct1 oct2 + testCase desc $ assertEqual desc oct1 oct2 where + desc = "octet_from_int 128 should parse as 10000000" oct1 = Octet B.One B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero oct2 = fromJust $ octet_from_int 128 test_octet_mask1 :: Test test_octet_mask1 = - TestCase $ - assertEqual - "The network bits of 255/4 should equal 240" - oct2 - (apply_mask oct1 Four B.Zero) + testCase desc $ + assertEqual desc oct2 (apply_mask oct1 Four B.Zero) where + desc = "The network bits of 255/4 should equal 240" oct1 = fromJust $ octet_from_int 255 oct2 = fromJust $ octet_from_int 240 test_octet_mask2 :: Test test_octet_mask2 = - TestCase $ - assertEqual - "The network bits of 255/1 should equal 128" - oct2 - (apply_mask oct1 Maskbits.One B.Zero) + testCase desc $ + assertEqual desc oct2 (apply_mask oct1 Maskbits.One B.Zero) where + desc = "The network bits of 255/1 should equal 128" oct1 = fromJust $ octet_from_int 255 oct2 = fromJust $ octet_from_int 128 -octet_tests :: [Test] +octet_tests :: Test octet_tests = - [ test_octet_from_int1, + testGroup "Octet Tests" [ + test_octet_from_int1, test_octet_mask1, test_octet_mask2 ] diff --git a/test/TestSuite.hs b/test/TestSuite.hs index d7cc5ff..3085c1b 100644 --- a/test/TestSuite.hs +++ b/test/TestSuite.hs @@ -1,32 +1,46 @@ {-# LANGUAGE NoMonomorphismRestriction #-} +import Data.Monoid (mempty) +import Test.Framework ( + RunnerOptions(), + Test, + TestName, + TestOptions(), + defaultMainWithOpts, + testGroup + ) +import Test.Framework.Options +import Test.Framework.Runners.Options +import Test.Framework.Providers.API (TestName) +import Test.Framework.Providers.HUnit (testCase) +import Test.Framework.Providers.QuickCheck2 (testProperty) import Test.HUnit -import Test.QuickCheck (Args(..), quickCheckWith, stdArgs) +import Test.QuickCheck (Testable ()) -import Cidr (cidr_tests, - prop_all_cidrs_contain_themselves, - prop_contains_proper_intransitive) +import Cidr (cidr_properties, cidr_tests) import IPv4Address (ipv4address_tests) import Octet (octet_tests) --- The list of HUnit tests. -test_suite = TestList (concat [cidr_tests, - ipv4address_tests, - octet_tests]) +tests :: [Test.Framework.Test] +tests = [ cidr_properties, + cidr_tests, + ipv4address_tests, + octet_tests ] main :: IO () main = do - putStrLn "HUnit" - putStrLn "-----" - runTestTT test_suite + let empty_test_opts = mempty :: TestOptions + let my_test_opts = empty_test_opts { + -- + -- Increase to 5000 when, + -- https://github.com/batterseapower/test-framework/issues/34 + -- is fixed. + -- + topt_maximum_generated_tests = Just 1000 + } + let empty_runner_opts = mempty :: RunnerOptions + let my_runner_opts = empty_runner_opts { + ropt_test_options = Just my_test_opts + } - putStrLn "" - - putStrLn "QuickCheck" - putStrLn "----------" - qc prop_all_cidrs_contain_themselves - qc prop_contains_proper_intransitive - where - args :: Args - args = stdArgs { maxDiscard = 5000 } - qc = quickCheckWith args + defaultMainWithOpts tests my_runner_opts -- 2.43.2 From b30aa6a3d9163c4fad3b5200cb2fe552fd709d46 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 21 Apr 2013 12:33:45 -0400 Subject: [PATCH 07/16] Prepare for version 0.0.1. --- hath.cabal | 2 +- makefile | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/hath.cabal b/hath.cabal index 2ea92c0..ee3615a 100644 --- a/hath.cabal +++ b/hath.cabal @@ -1,5 +1,5 @@ name: hath -version: 0.0 +version: 0.0.1 cabal-version: >= 1.8 author: Michael Orlitzky maintainer: Michael Orlitzky diff --git a/makefile b/makefile index 7eb2938..50dda0f 100644 --- a/makefile +++ b/makefile @@ -15,3 +15,7 @@ clean: test: runghc -i"src" test/TestSuite.hs + +dist: + runghc Setup.hs configure + runghc Setup.hs sdist -- 2.43.2 From 909e781852e3566fcaa16e156437ebe5bfc89e02 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 21 Apr 2013 12:41:57 -0400 Subject: [PATCH 08/16] Use Cabal test integration. --- hath.cabal | 39 +++++++++++++++++++++++++++++++++++++++ makefile | 14 +++++++++++--- test/TestSuite.hs | 9 --------- 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/hath.cabal b/hath.cabal index ee3615a..23f6cc4 100644 --- a/hath.cabal +++ b/hath.cabal @@ -47,3 +47,42 @@ executable hath -prof -auto-all -caf-all + + +test-suite testsuite + type: exitcode-stdio-1.0 + hs-source-dirs: src test + main-is: TestSuite.hs + build-depends: + base == 4.*, + HUnit == 1.2.*, + QuickCheck == 2.6.*, + test-framework == 0.8.*, + test-framework-hunit == 0.3.*, + test-framework-quickcheck2 == 0.3.* + + -- It's not entirely clear to me why I have to reproduce all of this. + ghc-options: + -Wall + -fwarn-hi-shadowing + -fwarn-missing-signatures + -fwarn-name-shadowing + -fwarn-orphans + -fwarn-type-defaults + -fwarn-tabs + -fwarn-incomplete-record-updates + -fwarn-monomorphism-restriction + -fwarn-unused-do-bind + -funbox-strict-fields + -fexcess-precision + -fno-spec-constr-count + -rtsopts + -threaded + -optc-O3 + -optc-march=native + -O2 + +source-repository head + type: git + location: http://michael.orlitzky.com/git/hath.git + branch: master diff --git a/makefile b/makefile index 50dda0f..2d46895 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,9 @@ +BIN = dist/build/hath/hath +TESTSUITE_BIN = dist/build/testsuite/testsuite + .PHONY : test -hath: src/*.hs +$(BIN): src/*.hs runghc Setup.hs clean runghc Setup.hs configure --user runghc Setup.hs build @@ -13,8 +16,13 @@ profile: src/*.hs clean: runghc Setup.hs clean -test: - runghc -i"src" test/TestSuite.hs + +$(TESTSUITE_BIN): src/*.hs test/TestSuite.hs + runghc Setup.hs configure --user --flags=${FLAGS} --enable-tests + runghc Setup.hs build + +test: $(BIN) $(TESTSUITE_BIN) + runghc Setup.hs test dist: runghc Setup.hs configure diff --git a/test/TestSuite.hs b/test/TestSuite.hs index 3085c1b..406fec5 100644 --- a/test/TestSuite.hs +++ b/test/TestSuite.hs @@ -1,20 +1,11 @@ {-# LANGUAGE NoMonomorphismRestriction #-} import Data.Monoid (mempty) import Test.Framework ( - RunnerOptions(), Test, - TestName, - TestOptions(), defaultMainWithOpts, - testGroup ) import Test.Framework.Options import Test.Framework.Runners.Options -import Test.Framework.Providers.API (TestName) -import Test.Framework.Providers.HUnit (testCase) -import Test.Framework.Providers.QuickCheck2 (testProperty) -import Test.HUnit -import Test.QuickCheck (Testable ()) import Cidr (cidr_properties, cidr_tests) -- 2.43.2 From e1d16438b44ecd962565756a828c9ed8014cf894 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Fri, 7 Jun 2013 18:18:10 -0400 Subject: [PATCH 09/16] Add more Haddock comments. Replace a few custom functions with library ones. Allow input CIDRs to be separated by any whitespace, not just newlines. Split the exit codes out into their own module. Remove now-unused ListUtils modules. Update deps in cabal file. Add a man page. --- doc/man/hath.1 | 99 ++++++++++++++++++++++++++++++++++++++++++++++ hath.cabal | 10 ++--- src/Bit.hs | 2 +- src/Cidr.hs | 39 +++++++++++++----- src/CommandLine.hs | 63 ++++++++++++++--------------- src/ExitCodes.hs | 11 ++++++ src/ListUtils.hs | 37 ----------------- src/Main.hs | 73 +++++++++++++++------------------- src/Maskbits.hs | 5 ++- 9 files changed, 212 insertions(+), 127 deletions(-) create mode 100644 doc/man/hath.1 create mode 100644 src/ExitCodes.hs delete mode 100644 src/ListUtils.hs diff --git a/doc/man/hath.1 b/doc/man/hath.1 new file mode 100644 index 0000000..7006ac3 --- /dev/null +++ b/doc/man/hath.1 @@ -0,0 +1,99 @@ +.TH hath 1 + +.SH NAME +hath \- Manipulate network blocks in CIDR notation + +.SH SYNOPSIS + +\fBhath\fR [\fBregexed|reduced|duped|diffed\fR] [\fB\-h\fR] [\fB-i \fIFILE\fR] \fI\fR + +.SH INPUT + +.P +The \fIinput\fR (default: stdin) should be a list of CIDR blocks, +separated by whitespace. Empty lines will be ignored, but otherwise, +malformed entries will cause an error to be displayed. + +.SH DESCRIPTION + +.P +Hath is a Haskell program for working with network blocks in CIDR +notation. When dealing with blocks of network addresses, there are a +few things that one usually wants to do with them: + +.IP \[bu] 2 +Create a regular expression matching the CIDR block(s). This is +because grep will throw up if you feed it CIDR. + +.IP \[bu] +Combine small blocks into larger ones. For example, if you have two +consecutive /24s, they might combine into a larger /23. + +.IP \[bu] +View the result of block combination in a useful way. + +.P +Hath does just that. It takes as its input (via stdin, or a file with +the -i parameter) a list of CIDR blocks. + +.SH MODES + +.P +Hath currently has four modes: + +.IP \[bu] 2 +\fBRegexed\fR + +This computes a (Perl-compatible) regular expression matching +the input CIDR blocks. It's the default mode of operation. + +.nf +.B $ hath <<< \(dq10.0.0.0/24 10.0.1.0/24\(dq +([^\.0-9](10)\.(0)\.(0)\.(0)[^\.0-9]|[^\.0-9](10)\.(0)\.(1) +\.(0)[^\.0-9]) + +.IP \[bu] +\fBReduced\fR + +This combines small blocks into larger ones where possible, and +eliminates redundant blocks. The output should be equivalent to +the input, though. + +.nf +.B $ hath reduced <<< \(dq10.0.0.0/24 10.0.1.0/24\(dq +10.0.0.0/23 + +.IP \[bu] +\fBDuped\fR + +Shows only the blocks that would be removed by reduce; that is, it +shows the ones that would get combined into larger blocks or are +simply redundant. + +.nf +.B $ hath duped <<< \(dq10.0.0.0/24 10.0.1.0/24\(dq +10.0.0.0/24 +10.0.1.0/24 + +.IP \[bu] +\fBDiffed\fR + +Shows what would change if you used reduce. Uses diff-like +notation. + +.nf +.B $ hath diffed <<< \(dq10.0.0.0/24 10.0.1.0/24\(dq +-10.0.0.0/24 +-10.0.1.0/24 ++10.0.0.0/23 + +.P +Each of the modes also supports a present-tense flavor; the following +are equivalent to their counterparts: \fBregex\fR, \fBreduce\fR, +\fBdupe\fR, \fBdiff\fR. + +.SH OPTIONS + +.IP \fB\-\-input\fR,\ \fB\-i\fR +Specify the input file containing a list of CIDRs, rather than using +stdin (the default). diff --git a/hath.cabal b/hath.cabal index 23f6cc4..12f811a 100644 --- a/hath.cabal +++ b/hath.cabal @@ -13,6 +13,8 @@ executable hath base == 4.*, HUnit == 1.2.*, QuickCheck == 2.6.*, + MissingH == 1.2.*, + split == 0.2.*, test-framework == 0.8.*, test-framework-hunit == 0.3.*, test-framework-quickcheck2 == 0.3.* @@ -34,9 +36,6 @@ executable hath -fwarn-incomplete-record-updates -fwarn-monomorphism-restriction -fwarn-unused-do-bind - -funbox-strict-fields - -fexcess-precision - -fno-spec-constr-count -rtsopts -threaded -optc-O3 @@ -57,6 +56,8 @@ test-suite testsuite base == 4.*, HUnit == 1.2.*, QuickCheck == 2.6.*, + MissingH == 1.2.*, + split == 0.2.*, test-framework == 0.8.*, test-framework-hunit == 0.3.*, test-framework-quickcheck2 == 0.3.* @@ -73,9 +74,6 @@ test-suite testsuite -fwarn-incomplete-record-updates -fwarn-monomorphism-restriction -fwarn-unused-do-bind - -funbox-strict-fields - -fexcess-precision - -fno-spec-constr-count -rtsopts -threaded -optc-O3 diff --git a/src/Bit.hs b/src/Bit.hs index 128b6f6..5c8c5aa 100644 --- a/src/Bit.hs +++ b/src/Bit.hs @@ -28,7 +28,7 @@ bit_to_int Zero = 0 bit_to_int One = 1 -- | If we are passed a '0' or '1', convert it --- appropriately. Otherwise, default to Nothing. +-- appropriately. Otherwise, return Nothing. bit_from_char :: Char -> Maybe Bit bit_from_char '0' = Just Zero bit_from_char '1' = Just One diff --git a/src/Cidr.hs b/src/Cidr.hs index d41ba11..002ec0b 100644 --- a/src/Cidr.hs +++ b/src/Cidr.hs @@ -1,3 +1,5 @@ +-- | The CIDR modules contains most of the functions used for working +-- with the CIDR type. module Cidr ( Cidr(..), cidr_from_string, @@ -19,6 +21,7 @@ module Cidr ) where import Data.List (nubBy) +import Data.List.Split (splitOneOf) import Data.Maybe (catMaybes, fromJust) import Test.HUnit (assertEqual) @@ -29,7 +32,6 @@ import Test.QuickCheck (Arbitrary(..), Gen, Property, (==>)) import qualified Bit as B import IPv4Address -import ListUtils import Maskable import Maskbits import Octet @@ -55,27 +57,27 @@ instance Eq Cidr where cidr1 == cidr2 = (cidr1 `equivalent` cidr2) --- Two CIDR ranges are equivalent if they have the same network bits --- and the masks are the same. +-- | Two CIDR ranges are equivalent if they have the same network bits +-- and the masks are the same. equivalent :: Cidr -> Cidr -> Bool equivalent (Cidr addr1 mbits1) (Cidr addr2 mbits2) = (mbits1 == mbits2) && ((apply_mask addr1 mbits1 B.Zero) == (apply_mask addr2 mbits2 B.Zero)) --- Returns the mask portion of a CIDR address. That is, everything --- after the trailing slash. +-- | Returns the mask portion of a CIDR address. That is, everything +-- after the trailing slash. maskbits_from_cidr_string :: String -> Maybe Maskbits maskbits_from_cidr_string s | length partlist == 2 = maskbits_from_string (partlist !! 1) | otherwise = Nothing where - partlist = (splitWith (`elem` "/") s) + partlist = splitOneOf "/" s -- | Takes an IP address String in CIDR notation, and returns a list -- of its octets (as Ints). octets_from_cidr_string :: String -> [Octet] octets_from_cidr_string s = - catMaybes $ map octet_from_string (take 4 (splitWith (`elem` "./") s)) + catMaybes $ map octet_from_string (take 4 (splitOneOf "./" s)) -- | Return Nothing if we can't parse both maskbits and octets from @@ -92,35 +94,53 @@ cidr_from_string s = +-- | Given a CIDR, return the minimum valid IPv4 address contained +-- within it. min_host :: Cidr -> IPv4Address min_host (Cidr addr mask) = apply_mask addr mask B.Zero - +-- | Given a CIDR, return the maximum valid IPv4 address contained +-- within it. max_host :: Cidr -> IPv4Address max_host (Cidr addr mask) = apply_mask addr mask B.One - +-- | Given a CIDR, return the first octet of the minimum valid IPv4 +-- address contained within it. min_octet1 :: Cidr -> Octet min_octet1 cidr = octet1 (min_host cidr) +-- | Given a CIDR, return the second octet of the minimum valid IPv4 +-- address contained within it. min_octet2 :: Cidr -> Octet min_octet2 cidr = octet2 (min_host cidr) +-- | Given a CIDR, return the third octet of the minimum valid IPv4 +-- address contained within it. min_octet3 :: Cidr -> Octet min_octet3 cidr = octet3 (min_host cidr) +-- | Given a CIDR, return the fourth octet of the minimum valid IPv4 +-- address contained within it. min_octet4 :: Cidr -> Octet min_octet4 cidr = octet4 (min_host cidr) +-- | Given a CIDR, return the first octet of the maximum valid IPv4 +-- address contained within it. max_octet1 :: Cidr -> Octet max_octet1 cidr = octet1 (max_host cidr) +-- | Given a CIDR, return the second octet of the maximum valid IPv4 +-- address contained within it. max_octet2 :: Cidr -> Octet max_octet2 cidr = octet2 (max_host cidr) +-- | Given a CIDR, return the third octet of the maximum valid IPv4 +-- address contained within it. max_octet3 :: Cidr -> Octet max_octet3 cidr = octet3 (max_host cidr) +-- | Given a CIDR, return the fourth octet of the maximum valid IPv4 +-- address contained within it. max_octet4 :: Cidr -> Octet max_octet4 cidr = octet4 (max_host cidr) @@ -167,6 +187,7 @@ contains (Cidr addr1 mbits1) (Cidr addr2 mbits2) addr2masked = apply_mask addr2 mbits1 B.Zero +-- | Contains but is not equal to. contains_proper :: Cidr -> Cidr -> Bool contains_proper cidr1 cidr2 = (cidr1 `contains` cidr2) && (not (cidr2 `contains` cidr1)) diff --git a/src/CommandLine.hs b/src/CommandLine.hs index 68957b1..c2bbe0c 100644 --- a/src/CommandLine.hs +++ b/src/CommandLine.hs @@ -16,71 +16,72 @@ import System.Console.GetOpt import System.Environment (getArgs) --- Dark magic. +-- | Lowercase an entire string. lowercase :: String -> String lowercase = map toLower --- The application currently has four modes. The default, Regex, will --- compute a regular expression matching the input CIDRs. Reduce, on --- the other hand, will combine any redundant/adjacent CIDR blocks --- into one. Dupe will show you what would be removed by Reduce, and --- Diff will show both additions and deletions in a diff-like format. +-- | The application currently has four modes. The default, Regex, +-- will compute a regular expression matching the input +-- CIDRs. Reduce, on the other hand, will combine any +-- redundant/adjacent CIDR blocks into one. Dupe will show you what +-- would be removed by Reduce, and Diff will show both additions and +-- deletions in a diff-like format. data Mode = Regex | Reduce | Dupe | Diff --- A record containing values for all available options. +-- | A record containing values for all available options. data Options = Options { opt_help :: Bool, opt_input :: IO String } --- This constructs an instance of Options, with each of its members --- set to default values. +-- | This constructs an instance of Options, with each of its members +-- set to default values. default_options :: Options default_options = Options { opt_help = False, opt_input = getContents } --- The options list that we construct associates a function with each --- option. This function is responsible for updating an Options record --- with the appropriate value. +-- | The options list that we construct associates a function with +-- each option. This function is responsible for updating an Options +-- record with the appropriate value. -- --- For more information and an example of this idiom, see, +-- For more information and an example of this idiom, see, -- --- http://www.haskell.org/haskellwiki/High-level_option_handling_with_GetOpt +-- http://www.haskell.org/haskellwiki/High-level_option_handling_with_GetOpt -- options :: [OptDescr (Options -> IO Options)] options = - [ Option ['h'][] (NoArg set_help) "Prints this help message.", - Option ['i'][] (ReqArg set_input "FILE") "Read FILE instead of stdin." ] + [ Option ['h']["help"] (NoArg set_help) "Prints this help message.", + Option ['i']["input"] (ReqArg set_input "FILE") "Read FILE instead of stdin." ] --- Takes an Options as an argument, and sets its opt_help member to --- True. +-- | Takes an Options as an argument, and sets its opt_help member to +-- True. set_help :: Options -> IO Options set_help opts = do return opts { opt_help = True } --- If the input file option is set, this function will update the --- passed Options record with a new function for opt_input. The --- default opt_input is to read from stdin, but if this option is set, --- we replace that with readFile. +-- | If the input file option is set, this function will update the +-- passed Options record with a new function for opt_input. The +-- default opt_input is to read from stdin, but if this option is +-- set, we replace that with readFile. set_input :: String -> Options -> IO Options set_input arg opts = do return opts { opt_input = readFile arg } --- The usage header +-- | The usage header. usage :: String -usage = "Usage: hath [regexed|reduced|duped|diffed] [-h] [-i FILE]" +usage = "Usage: hath [regexed|reduced|duped|diffed] [-h] [-i FILE] " --- The usage header, and all available flags (as generated by GetOpt) +-- | The usage header, and all available flags (as generated by GetOpt). help_text :: String help_text = usageInfo usage options --- Return a list of options. +-- | Return a list of options. parse_options :: IO Options parse_options = do argv <- getArgs @@ -95,7 +96,7 @@ parse_options = do return opts --- Return the mode if one was given. +-- | Return the mode if one was given. parse_mode :: IO Mode parse_mode = do argv <- getArgs @@ -120,7 +121,7 @@ parse_mode = do --- Return a list of errors. +-- | Return a list of errors. parse_errors :: IO [String] parse_errors = do argv <- getArgs @@ -129,15 +130,15 @@ parse_errors = do --- Is the help option set? +-- | Is the help option set? help_set :: IO Bool help_set = do opts <- parse_options return (opt_help opts) --- Return our input function, getContents by default, or readFile if --- the input file option was set. +-- | Return our input function, getContents by default, or readFile if +-- the input file option was set. input_function :: IO (IO String) input_function = do opts <- parse_options diff --git a/src/ExitCodes.hs b/src/ExitCodes.hs new file mode 100644 index 0000000..4257191 --- /dev/null +++ b/src/ExitCodes.hs @@ -0,0 +1,11 @@ +-- | Some exit codes, used in the ExitFailure constructor. +module ExitCodes +where + +-- | One of the CIDRs is invalid (malformed, not a CIDR at all, etc). +exit_invalid_cidr :: Int +exit_invalid_cidr = 1 + +-- | We were unable to parse the command-line arguments. +exit_args_parse_failed :: Int +exit_args_parse_failed = 2 diff --git a/src/ListUtils.hs b/src/ListUtils.hs deleted file mode 100644 index 5ec9120..0000000 --- a/src/ListUtils.hs +++ /dev/null @@ -1,37 +0,0 @@ -module ListUtils -( pad_left_to, - pad_right_to, - splitWith -) where - - --- Stolen from ByteString. Splits a list at each element satisfying --- the predicate p. -splitWith :: (a -> Bool) -> [a] -> [[a]] -splitWith p xs = - ys : case zs of - [] -> [] - _:ws -> splitWith p ws - where (ys,zs) = break p xs - - --- Pads a list (on the left) to length len by prepending pad_elem. -pad_left_to :: Int -> a -> [a] -> [a] -pad_left_to len pad_elem xs = - if (length xs) >= len then - xs - else - (replicate padcount pad_elem) ++ xs - where - padcount = len - (length xs) - - --- Pads a list (on the right) to length len by appending pad_elem. -pad_right_to :: Int -> a -> [a] -> [a] -pad_right_to len pad_elem xs = - if (length xs) >= len then - xs - else - xs ++ (replicate padcount pad_elem) - where - padcount = len - (length xs) diff --git a/src/Main.hs b/src/Main.hs index b2fc64b..dd7eefe 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,5 +1,7 @@ +import Control.Monad (when) import Data.List ((\\), intercalate, intersperse) import Data.Maybe (catMaybes, isNothing) +import Data.String.Utils (splitWs) import System.Exit (ExitCode(..), exitWith) import System.IO (stderr, hPutStrLn) @@ -21,39 +23,32 @@ import CommandLine (help_set, Mode(..), parse_errors, parse_mode) - + +import ExitCodes import Octet --- Some exit codes, used in the ExitFailure constructor. -exit_invalid_cidr :: Int -exit_invalid_cidr = 1 - -exit_args_parse_failed :: Int -exit_args_parse_failed = 2 - --- A regular expression that matches a non-address character. +-- | A regular expression that matches a non-address character. non_addr_char :: String non_addr_char = "[^\\.0-9]" --- Add non_addr_chars on either side of the given String. This --- prevents (for example) the regex '127.0.0.1' from matching --- '127.0.0.100'. +-- | Add non_addr_chars on either side of the given String. This +-- prevents (for example) the regex '127.0.0.1' from matching +-- '127.0.0.100'. addr_barrier :: String -> String addr_barrier x = non_addr_char ++ x ++ non_addr_char --- The magic happens here. We take a CIDR String as an argument, and --- return the equivalent regular expression. We do this as follows: +-- | The magic happens here. We take a CIDR String as an argument, and +-- return the equivalent regular expression. We do this as follows: -- --- 1. Compute the minimum possible value of each octet. --- 2. Compute the maximum possible value of each octet. --- 3. Generate a regex matching every value between those min and --- max values. --- 4. Join the regexes from step 3 with regexes matching periods. --- 5. Stick an address boundary on either side of the result. ---cidr_to_regex :: String -> String +-- 1. Compute the minimum possible value of each octet. +-- 2. Compute the maximum possible value of each octet. +-- 3. Generate a regex matching every value between those min and +-- max values. +-- 4. Join the regexes from step 3 with regexes matching periods. +-- 5. Stick an address boundary on either side of the result. cidr_to_regex :: Cidr.Cidr -> String cidr_to_regex cidr = addr_barrier (intercalate "\\." [range1, range2, range3, range4]) @@ -73,14 +68,14 @@ cidr_to_regex cidr = --- Take a list of Strings, and return a regular expression matching --- any of them. +-- | Take a list of Strings, and return a regular expression matching +-- any of them. alternate :: [String] -> String alternate terms = "(" ++ (concat (intersperse "|" terms)) ++ ")" --- Take two Ints as parameters, and return a regex matching any --- integer between them (inclusive). +-- | Take two Ints as parameters, and return a regex matching any +-- integer between them (inclusive). numeric_range :: Int -> Int -> String numeric_range x y = alternate (map show [lower..upper]) @@ -94,34 +89,28 @@ main = do -- First, check for any errors that occurred while parsing -- the command line options. errors <- CommandLine.parse_errors - if not (null errors) - then do - hPutStrLn stderr (concat errors) - putStrLn CommandLine.help_text - exitWith (ExitFailure exit_args_parse_failed) - else do -- Nothing + when ((not . null) errors) $ do + hPutStrLn stderr (concat errors) + putStrLn CommandLine.help_text + exitWith (ExitFailure exit_args_parse_failed) -- Next, check to see if the 'help' option was passed to the -- program. If it was, display the help, and exit successfully. help_opt_set <- CommandLine.help_set - if help_opt_set - then do - putStrLn CommandLine.help_text - exitWith ExitSuccess - else do -- Nothing + when help_opt_set $ do + putStrLn CommandLine.help_text + exitWith ExitSuccess -- The input function we receive here should know what to read. inputfunc <- (CommandLine.input_function) input <- inputfunc - let cidr_strings = lines input + let cidr_strings = splitWs input let cidrs = map cidr_from_string cidr_strings - if (any isNothing cidrs) - then do - putStrLn "Error: not valid CIDR notation." - exitWith (ExitFailure exit_invalid_cidr) - else do -- Nothing + when (any isNothing cidrs) $ do + putStrLn "Error: not valid CIDR notation." + exitWith (ExitFailure exit_invalid_cidr) -- Filter out only the valid ones. let valid_cidrs = catMaybes cidrs diff --git a/src/Maskbits.hs b/src/Maskbits.hs index d7064d8..ac46ccf 100644 --- a/src/Maskbits.hs +++ b/src/Maskbits.hs @@ -6,7 +6,7 @@ module Maskbits import Test.QuickCheck --- A type representing the number of bits in a CIDR netmask. +-- | A type representing the number of bits in a CIDR netmask. data Maskbits = Zero | One @@ -156,6 +156,8 @@ maskbits_from_int 31 = Just ThirtyOne maskbits_from_int 32 = Just ThirtyTwo maskbits_from_int _ = Nothing + +-- | Convert a String to Maskbits, if possible. maskbits_from_string :: String -> Maybe Maskbits maskbits_from_string s = case (reads s :: [(Int, String)]) of @@ -164,6 +166,7 @@ maskbits_from_string s = +-- | Maskbits are just natural numbers, this returns the previous one. decrement :: Maskbits -> Maskbits decrement Zero = Zero decrement One = Zero -- 2.43.2 From 01c7ddcee2f36236e9a10f244260501a8c3caf4f Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Fri, 7 Jun 2013 18:46:43 -0400 Subject: [PATCH 10/16] Add a 'doc' makefile target. --- makefile | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/makefile b/makefile index 2d46895..14f4c7d 100644 --- a/makefile +++ b/makefile @@ -13,6 +13,13 @@ profile: src/*.hs runghc Setup.hs configure --user --enable-executable-profiling runghc Setup.hs build +doc: src/*.hs + runghc Setup.hs configure --user + runghc Setup.hs hscolour --executables + runghc Setup.hs haddock --internal \ + --executables \ + --hyperlink-source + clean: runghc Setup.hs clean -- 2.43.2 From bacd73f540f87c5441a1ec8ca8c26b34659f55a2 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Fri, 7 Jun 2013 18:48:51 -0400 Subject: [PATCH 11/16] Fix escaping in a comment. --- src/Cidr.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cidr.hs b/src/Cidr.hs index 002ec0b..7143a80 100644 --- a/src/Cidr.hs +++ b/src/Cidr.hs @@ -165,7 +165,7 @@ max_octet4 cidr = octet4 (max_host cidr) -- cidr1, then at least mbits1 of an address in cidr2 will match -- cidr1. For example, -- --- cidr1 = 192.168.1.0/23, cidr2 = 192.168.1.100/24 +-- cidr1 = 192.168.1.0\/23, cidr2 = 192.168.1.100\/24 -- -- Here, cidr2 contains all of 192.168.1.0 through -- 192.168.1.255. However, cidr1 contains BOTH 192.168.0.0 through -- 2.43.2 From 70dce336e3261027dc135faebed85ea443525974 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sat, 8 Jun 2013 12:20:18 -0400 Subject: [PATCH 12/16] Add a description to the cabal file. --- hath.cabal | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/hath.cabal b/hath.cabal index 12f811a..4431b84 100644 --- a/hath.cabal +++ b/hath.cabal @@ -3,10 +3,56 @@ version: 0.0.1 cabal-version: >= 1.8 author: Michael Orlitzky maintainer: Michael Orlitzky +build-type: Simple synopsis: Hath manipulates network blocks in CIDR notation. -build-type: Simple - +description: + Hath is a Haskell program for working with network blocks in CIDR + notation. When dealing with blocks of network addresses, there are a + few things that one usually wants to do with them: + . + * Create a regular expression matching the CIDR block(s). This is + because grep will throw up if you feed it CIDR. + . + * Combine small blocks into larger ones. For example, if you have two + consecutive \/24s, they might combine into a larger \/23. + . + * View the result of block combination in a useful way. + . + Hath has four modes to perform these functions: + . + [@Regexed@] + This computes a (Perl-compatible) regular expression matching + the input CIDR blocks. It's the default mode of operation. + . + [@Reduced@] + This combines small blocks into larger ones where possible, and + eliminates redundant blocks. The output should be equivalent to + the input, though. + . + [@Duped@] + Shows only the blocks that would be removed by reduce; that is, it + shows the ones that would get combined into larger blocks or are + simply redundant. + . + [@Diffed@] + Shows what would change if you used reduce. Uses diff-like + notation. + . + /Examples/: + . + Combine two \/24s into a \/23: + . + @ + $ hath reduced <<< \"10.0.0.0\/24 10.0.1.0\/24\" + 10.0.0.0/23 + @ + . + Create a perl-compatible regex to be fed to grep: + . + @ + $ grep -P `hath regexed -i cidrs.txt` mail.log + @ executable hath build-depends: -- 2.43.2 From a3726f89f2c7116a4e8329f8ecbe90b8ac5dc220 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sat, 8 Jun 2013 12:28:15 -0400 Subject: [PATCH 13/16] PHONY the dist make target. Change license to GPL3. Add license field to cabal file. --- doc/LICENSE | 623 +++++++++++++++++++++++++++++++++++++++++++++++++++- hath.cabal | 2 + makefile | 2 +- 3 files changed, 617 insertions(+), 10 deletions(-) diff --git a/doc/LICENSE b/doc/LICENSE index 5a8e332..bc08fe2 100644 --- a/doc/LICENSE +++ b/doc/LICENSE @@ -1,14 +1,619 @@ - DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE - Version 2, December 2004 + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 - Copyright (C) 2004 Sam Hocevar + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. - Everyone is permitted to copy and distribute verbatim or modified - copies of this license document, and changing it is allowed as long - as the name is changed. + Preamble - DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + The GNU General Public License is a free, copyleft license for +software and other kinds of works. - 0. You just DO WHAT THE FUCK YOU WANT TO. + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. diff --git a/hath.cabal b/hath.cabal index 4431b84..e0d16a1 100644 --- a/hath.cabal +++ b/hath.cabal @@ -3,6 +3,8 @@ version: 0.0.1 cabal-version: >= 1.8 author: Michael Orlitzky maintainer: Michael Orlitzky +license: GPL-3 +license-file: doc/LICENSE build-type: Simple synopsis: Hath manipulates network blocks in CIDR notation. diff --git a/makefile b/makefile index 14f4c7d..2061f44 100644 --- a/makefile +++ b/makefile @@ -1,7 +1,7 @@ BIN = dist/build/hath/hath TESTSUITE_BIN = dist/build/testsuite/testsuite -.PHONY : test +.PHONY : test dist $(BIN): src/*.hs runghc Setup.hs clean -- 2.43.2 From ea257ba2e191df27cf23feb98a8110157987bfb9 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sat, 8 Jun 2013 12:34:40 -0400 Subject: [PATCH 14/16] Add category: Utils to cabal file. --- hath.cabal | 1 + 1 file changed, 1 insertion(+) diff --git a/hath.cabal b/hath.cabal index e0d16a1..5354d42 100644 --- a/hath.cabal +++ b/hath.cabal @@ -3,6 +3,7 @@ version: 0.0.1 cabal-version: >= 1.8 author: Michael Orlitzky maintainer: Michael Orlitzky +category: Utils license: GPL-3 license-file: doc/LICENSE build-type: Simple -- 2.43.2 From 3c9316fed6fd100be9a5e1f8d72db6534fb163cd Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sat, 8 Jun 2013 13:38:06 -0400 Subject: [PATCH 15/16] Add hlint makefile target. Fix hlint suggestions. --- makefile | 8 +++++++- src/Cidr.hs | 4 ++-- src/CommandLine.hs | 24 +++++++++++------------- src/Main.hs | 20 ++++++++++---------- 4 files changed, 30 insertions(+), 26 deletions(-) diff --git a/makefile b/makefile index 2061f44..276c512 100644 --- a/makefile +++ b/makefile @@ -1,7 +1,7 @@ BIN = dist/build/hath/hath TESTSUITE_BIN = dist/build/testsuite/testsuite -.PHONY : test dist +.PHONY : test dist hlint $(BIN): src/*.hs runghc Setup.hs clean @@ -34,3 +34,9 @@ test: $(BIN) $(TESTSUITE_BIN) dist: runghc Setup.hs configure runghc Setup.hs sdist + +hlint: + hlint --ignore="Use camelCase" \ + --ignore="Redundant bracket" \ + --color \ + src diff --git a/src/Cidr.hs b/src/Cidr.hs index 7143a80..cdfef9a 100644 --- a/src/Cidr.hs +++ b/src/Cidr.hs @@ -22,7 +22,7 @@ module Cidr import Data.List (nubBy) import Data.List.Split (splitOneOf) -import Data.Maybe (catMaybes, fromJust) +import Data.Maybe (catMaybes, fromJust, mapMaybe) import Test.HUnit (assertEqual) import Test.Framework (Test, testGroup) @@ -77,7 +77,7 @@ maskbits_from_cidr_string s -- of its octets (as Ints). octets_from_cidr_string :: String -> [Octet] octets_from_cidr_string s = - catMaybes $ map octet_from_string (take 4 (splitOneOf "./" s)) + mapMaybe octet_from_string (take 4 (splitOneOf "./" s)) -- | Return Nothing if we can't parse both maskbits and octets from diff --git a/src/CommandLine.hs b/src/CommandLine.hs index c2bbe0c..181eb6f 100644 --- a/src/CommandLine.hs +++ b/src/CommandLine.hs @@ -52,13 +52,13 @@ default_options = Options { opt_help = False, -- options :: [OptDescr (Options -> IO Options)] options = - [ Option ['h']["help"] (NoArg set_help) "Prints this help message.", - Option ['i']["input"] (ReqArg set_input "FILE") "Read FILE instead of stdin." ] + [ Option "h" ["help"] (NoArg set_help) "Prints this help message.", + Option "i" ["input"] (ReqArg set_input "FILE") "Read FILE instead of stdin." ] -- | Takes an Options as an argument, and sets its opt_help member to -- True. set_help :: Options -> IO Options -set_help opts = do +set_help opts = return opts { opt_help = True } @@ -67,7 +67,7 @@ set_help opts = do -- default opt_input is to read from stdin, but if this option is -- set, we replace that with readFile. set_input :: String -> Options -> IO Options -set_input arg opts = do +set_input arg opts = return opts { opt_input = readFile arg } @@ -91,9 +91,8 @@ parse_options = do -- list, one after another, on a default_options record. The end -- result should be an Options instance with all of its members set -- correctly. - opts <- foldl (>>=) (return default_options) actions + foldl (>>=) (return default_options) actions - return opts -- | Return the mode if one was given. @@ -101,13 +100,12 @@ parse_mode :: IO Mode parse_mode = do argv <- getArgs let (_, non_options, _) = getOpt Permute options argv - if (null non_options) - then do - -- Default - return Regex - else do - -- Some non-option was given, but were any of them modes? - case (lowercase (non_options !! 0)) of + case non_options of + -- Default + [] -> return Regex + -- Some non-option was given, but were any of them modes? + (x:_) -> + case (lowercase x) of "regex" -> return Regex "regexed" -> return Regex "reduce" -> return Reduce diff --git a/src/Main.hs b/src/Main.hs index dd7eefe..12511b6 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,8 +1,8 @@ -import Control.Monad (when) -import Data.List ((\\), intercalate, intersperse) +import Control.Monad (unless, when) +import Data.List ((\\), intercalate) import Data.Maybe (catMaybes, isNothing) import Data.String.Utils (splitWs) -import System.Exit (ExitCode(..), exitWith) +import System.Exit (ExitCode(..), exitSuccess, exitWith) import System.IO (stderr, hPutStrLn) import Cidr (Cidr(..), @@ -71,7 +71,7 @@ cidr_to_regex cidr = -- | Take a list of Strings, and return a regular expression matching -- any of them. alternate :: [String] -> String -alternate terms = "(" ++ (concat (intersperse "|" terms)) ++ ")" +alternate terms = "(" ++ (intercalate "|" terms) ++ ")" -- | Take two Ints as parameters, and return a regex matching any @@ -89,7 +89,7 @@ main = do -- First, check for any errors that occurred while parsing -- the command line options. errors <- CommandLine.parse_errors - when ((not . null) errors) $ do + unless (null errors) $ do hPutStrLn stderr (concat errors) putStrLn CommandLine.help_text exitWith (ExitFailure exit_args_parse_failed) @@ -99,7 +99,7 @@ main = do help_opt_set <- CommandLine.help_set when help_opt_set $ do putStrLn CommandLine.help_text - exitWith ExitSuccess + exitSuccess -- The input function we receive here should know what to read. inputfunc <- (CommandLine.input_function) @@ -123,10 +123,10 @@ main = do let regexes = map cidr_to_regex valid_cidrs putStrLn $ alternate regexes Reduce -> do - _ <- mapM (putStrLn . show) (combine_all valid_cidrs) + _ <- mapM print (combine_all valid_cidrs) return () Dupe -> do - _ <- mapM (putStrLn . show) dupes + _ <- mapM print dupes return () where dupes = valid_cidrs \\ (combine_all valid_cidrs) @@ -136,6 +136,6 @@ main = do return () where dupes = valid_cidrs \\ (combine_all valid_cidrs) - deletions = map (\s -> "-" ++ (show s)) dupes + deletions = map (\s -> '-' : (show s)) dupes newcidrs = (combine_all valid_cidrs) \\ valid_cidrs - additions = map (\s -> "+" ++ (show s)) newcidrs + additions = map (\s -> '+' : (show s)) newcidrs -- 2.43.2 From 7622c66d1bb0592ae6cd5b9c6092ff2e0490c4a1 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sat, 8 Jun 2013 14:58:53 -0400 Subject: [PATCH 16/16] Remove useless --flags in makefile. Version bump and add other-modules to cabal file. --- hath.cabal | 12 +++++++++++- makefile | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/hath.cabal b/hath.cabal index 5354d42..7799a7e 100644 --- a/hath.cabal +++ b/hath.cabal @@ -1,5 +1,5 @@ name: hath -version: 0.0.1 +version: 0.0.2 cabal-version: >= 1.8 author: Michael Orlitzky maintainer: Michael Orlitzky @@ -74,6 +74,16 @@ executable hath hs-source-dirs: src/ + other-modules: + Bit + Cidr + CommandLine + ExitCodes + IPv4Address + Maskable + Maskbits + Octet + ghc-options: -Wall -fwarn-hi-shadowing diff --git a/makefile b/makefile index 276c512..5d42778 100644 --- a/makefile +++ b/makefile @@ -25,7 +25,7 @@ clean: $(TESTSUITE_BIN): src/*.hs test/TestSuite.hs - runghc Setup.hs configure --user --flags=${FLAGS} --enable-tests + runghc Setup.hs configure --user --enable-tests runghc Setup.hs build test: $(BIN) $(TESTSUITE_BIN) -- 2.43.2