Add two new modes: 'List' and 'Reversed' to list and perform a PTR lookup on a CIDR's addresses respectively.
Add a .ghci file which loads some modules automatically.
Add Bounded and Enum instances for Bit, Octet, IPv4Address.
Add some tests for the new functionality.
--- /dev/null
+-- Set the include path.
+:set -isrc
+
+-- Load the stuff we want to play with.
+:{
+:load src/Bit.hs
+ src/Octet.hs
+ src/IPv4Address.hs
+ src/Cidr.hs
+ src/DNS.hs
+:}
+
+:set prompt "hath> "
name: hath
-version: 0.0.3
+version: 0.0.4
cabal-version: >= 1.8
author: Michael Orlitzky
maintainer: Michael Orlitzky <michael@orlitzky.com>
executable hath
build-depends:
base == 4.*,
+ bytestring == 0.10.*,
+ dns == 0.3.*,
HUnit == 1.2.*,
QuickCheck == 2.6.*,
MissingH == 1.2.*,
+ parallel-io == 0.3.*,
split == 0.2.*,
test-framework == 0.8.*,
test-framework-hunit == 0.3.*,
Bit
Cidr
CommandLine
+ DNS
ExitCodes
IPv4Address
Maskable
Maskbits
Octet
-
+
ghc-options:
-Wall
-fwarn-hi-shadowing
main-is: TestSuite.hs
build-depends:
base == 4.*,
+ bytestring == 0.10.*,
+ dns == 0.3.*,
HUnit == 1.2.*,
QuickCheck == 2.6.*,
MissingH == 1.2.*,
+ parallel-io == 0.3.*,
split == 0.2.*,
test-framework == 0.8.*,
test-framework-hunit == 0.3.*,
data Bit = Zero | One
- deriving (Eq)
+ deriving (Enum, Eq)
instance Show Bit where
show Zero = "0"
arbitrary = elements [ Zero, One ]
+instance Ord Bit where
+ Zero <= Zero = True
+ Zero <= One = True
+ One <= Zero = False
+ One <= One = True
+
+instance Bounded Bit where
+ minBound = Zero
+ maxBound = One
+
+
-- | Convert a Bit to an Int.
bit_to_int :: Bit -> Int
bit_to_int Zero = 0
combine_all,
contains,
contains_proper,
+ enumerate,
max_octet1,
max_octet2,
max_octet3,
mbits2 = maskbits cidr2
-
+enumerate :: Cidr -> [IPv4Address]
+enumerate cidr = [(min_host cidr)..(max_host cidr)]
-- HUnit Tests
lowercase = map toLower
--- | The application currently has four modes. The default, Regex,
+-- | The application currently has six 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
+-- 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.
+--
+-- Diff will show both additions and deletions in a diff-like
+-- format.
+--
+-- List will enumerate the IP addresses contained within the input
+-- CIDRs.
+--
+-- Reverse will perform a reverse DNS (PTR) lookup on each IP
+-- address contained within the input CIDRs.
+--
+data Mode = Regex | Reduce | Dupe | Diff | List | Reverse
-- | A record containing values for all available options.
-- | Takes an Options as an argument, and sets its opt_help member to
-- True.
set_help :: Options -> IO Options
-set_help opts =
+set_help opts =
return opts { opt_help = True }
-- | The usage header.
usage :: String
-usage = "Usage: hath [regexed|reduced|duped|diffed] [-h] [-i FILE] <input>"
+usage =
+ "Usage: hath " ++
+ "[regexed|reduced|duped|diffed|listed|reversed] " ++
+ "[-h] " ++
+ "[-i FILE] " ++
+ "<input>"
-- | The usage header, and all available flags (as generated by GetOpt).
parse_mode = do
argv <- getArgs
let (_, non_options, _) = getOpt Permute options argv
- case non_options of
+ return $ case non_options of
-- Default
- [] -> return Regex
- -- Some non-option was given, but were any of them modes?
+ [] -> 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
- "reduced" -> return Reduce
- "dupe" -> return Dupe
- "duped" -> return Dupe
- "diff" -> return Diff
- "diffed" -> return Diff
- _ -> return Regex
+ "regex" -> Regex
+ "regexed" -> Regex
+ "reduce" -> Reduce
+ "reduced" -> Reduce
+ "dupe" -> Dupe
+ "duped" -> Dupe
+ "diff" -> Diff
+ "diffed" -> Diff
+ "list" -> List
+ "listed" -> List
+ "reverse" -> Reverse
+ "reversed" -> Reverse
+ _ -> Regex
--- /dev/null
+-- | Helpers to perform DNS queries.
+module DNS (
+ Domain,
+ lookup_ptrs
+ )
+where
+
+import qualified Data.ByteString.Char8 as BS (
+ append,
+ intercalate,
+ pack,
+ split )
+import Network.DNS (
+ Domain,
+ ResolvConf(..),
+ defaultResolvConf,
+ lookupPTR,
+ makeResolvSeed,
+ withResolver
+ )
+
+
+-- | Convert the given IP address (as a ByteString) to the format
+-- required for a PTR lookup. For example, "192.168.0.0" should be
+-- converted to "0.0.168.192.in-addr.arpa".
+ip_to_in_addr_arpa :: Domain -> Domain
+ip_to_in_addr_arpa ip =
+ rev_ip `BS.append` suffix
+ where
+ dot = BS.pack "."
+ suffix = BS.pack ".in-addr.arpa"
+ rev_ip = BS.intercalate dot (reverse (BS.split '.' ip))
+
+-- | Take the default ResolvConf and increase the timeout to 15
+-- seconds.
+our_resolv_conf :: ResolvConf
+our_resolv_conf =
+ defaultResolvConf { resolvTimeout = 15*1000*1000 } -- 15s
+
+
+-- | Takes a list of IP addresses (as ByteStrings) and performs
+-- reverse (PTR) lookups on each of them.
+lookup_ptrs :: [Domain] -> IO [Maybe [Domain]]
+lookup_ptrs ips = do
+ rs <- makeResolvSeed our_resolv_conf
+ withResolver rs $ \resolver ->
+ mapM (lookupPTR resolver) in_addrs
+ where
+ in_addrs = map ip_to_in_addr_arpa ips
-module IPv4Address
-( ipv4address_tests,
+module IPv4Address(
+ ipv4address_properties,
+ ipv4address_tests,
IPv4Address(..),
- max_address,
- min_address,
most_sig_bit_different,
) where
import Test.HUnit (assertEqual)
import Test.Framework (Test, testGroup)
import Test.Framework.Providers.HUnit (testCase)
-import Test.QuickCheck (Arbitrary(..), Gen)
+import Test.Framework.Providers.QuickCheck2 (testProperty)
+import Test.QuickCheck (Arbitrary(..), Gen, Property, (==>))
import Maskable
import Maskbits
new_addr3 { octet1 = (apply_mask oct1 Zero bit) }
+instance Bounded IPv4Address where
+ -- | The minimum possible IPv4 address, 0.0.0.0.
+ minBound = IPv4Address minBound minBound minBound minBound
--- | The minimum possible IPv4 address, 0.0.0.0.
-min_address :: IPv4Address
-min_address =
- IPv4Address min_octet min_octet min_octet min_octet
+ -- | The maximum possible IPv4 address, 255.255.255.255.
+ maxBound = IPv4Address maxBound maxBound maxBound maxBound
--- | The maximum possible IPv4 address, 255.255.255.255.
-max_address :: IPv4Address
-max_address =
- IPv4Address max_octet max_octet max_octet max_octet
+-- | Convert @addr@ to an 'Int' by converting each octet to an 'Int'
+-- and shifting the result to the left by 0,8.16, or 24 bits.
+ipv4address_to_int :: IPv4Address -> Int
+ipv4address_to_int addr =
+ (shifted_oct1) + (shifted_oct2) + (shifted_oct3) + oct4
+ where
+ oct1 = octet_to_int (octet1 addr)
+ oct2 = octet_to_int (octet2 addr)
+ oct3 = octet_to_int (octet3 addr)
+ oct4 = octet_to_int (octet4 addr)
+
+ shifted_oct1 = oct1 * 2^(24 :: Integer)
+ shifted_oct2 = oct2 * 2^(16 :: Integer)
+ shifted_oct3 = oct3 * 2^(8 :: Integer)
+
+
+
+-- | Convert an 'Int' @x@ to an 'IPv4Address'. Each octet of @x@ is
+-- right-shifted by the appropriate number of bits, and the fractional
+-- part is dropped.
+ipv4address_from_int :: Int -> Maybe IPv4Address
+ipv4address_from_int x
+ | (x < 0) || (x > 2^(32 :: Integer) - 1) = Nothing
+ | otherwise = do
+ -- If the algebra is right, none of these octet_from_int calls
+ -- below can fail since 0 <= x <= 2^32 - 1.
+ oct1 <- octet_from_int shifted_x1
+ oct2 <- octet_from_int shifted_x2
+ oct3 <- octet_from_int shifted_x3
+ oct4 <- octet_from_int x4
+ return $ IPv4Address oct1 oct2 oct3 oct4
+ where
+ -- Chop off the higher octets. x1 = x `mod` 2^32, would be
+ -- redundant.
+ x2 = x `mod` 2^(24 :: Integer)
+ x3 = x `mod` 2^(16 :: Integer)
+ x4 = x `mod` 2^(8 :: Integer)
+ -- Perform right-shifts. x4 doesn't need a shift.
+ shifted_x1 = x `quot` 2^(24 :: Integer)
+ shifted_x2 = x2 `quot` 2^(16 :: Integer)
+ shifted_x3 = x3 `quot` 2^(8 :: Integer)
+
+
+instance Enum IPv4Address where
+ -- We're supposed to throw a runtime error if you call (succ
+ -- maxBound), so the fromJust here doesn't introduce any additional
+ -- badness.
+ toEnum = fromJust . ipv4address_from_int
+ fromEnum = ipv4address_to_int
-- | Given two addresses, find the number of the most significant bit
-- where they differ. If the addresses are the same, return
oct4b = (octet4 addr2)
+-- Test lists.
+ipv4address_tests :: Test
+ipv4address_tests =
+ testGroup "IPv4 Address Tests" [
+ test_enum,
+ test_maxBound,
+ test_minBound,
+ test_most_sig_bit_different1,
+ test_most_sig_bit_different2 ]
+
+ipv4address_properties :: Test
+ipv4address_properties =
+ testGroup
+ "IPv4 Address Properties "
+ [ testProperty
+ "fromEnum/toEnum are inverses"
+ prop_from_enum_to_enum_inverses ]
+
+-- QuickCheck properties
+prop_from_enum_to_enum_inverses :: Int -> Property
+prop_from_enum_to_enum_inverses x =
+ (0 <= x) && (x <= 2^(32 :: Integer) - 1) ==>
+ fromEnum (toEnum x :: IPv4Address) == x
-- HUnit Tests
mk_testaddr :: Int -> Int -> Int -> Int -> IPv4Address
oct3 = fromJust $ octet_from_int c
oct4 = fromJust $ octet_from_int d
+test_minBound :: Test
+test_minBound =
+ testCase desc $ assertEqual desc expected actual
+ where
+ desc = "minBound should be 0.0.0.0"
+ expected = mk_testaddr 0 0 0 0
+ actual = minBound :: IPv4Address
+
+test_maxBound :: Test
+test_maxBound =
+ testCase desc $ assertEqual desc expected actual
+ where
+ desc = "maxBound should be 255.255.255.255"
+ expected = mk_testaddr 255 255 255 255
+ actual = maxBound :: IPv4Address
+
+test_enum :: Test
+test_enum =
+ testCase desc $ assertEqual desc expected actual
+ where
+ desc = "enumerating a /24 gives the correct addresses"
+ expected = ["192.168.0." ++ (show x) | x <- [0..255::Int] ]
+ lb = mk_testaddr 192 168 0 0
+ ub = mk_testaddr 192 168 0 255
+ actual = map show [lb..ub]
test_most_sig_bit_different1 :: Test
test_most_sig_bit_different1 =
bit = most_sig_bit_different addr1 addr2
-ipv4address_tests :: Test
-ipv4address_tests =
- testGroup "IPv4 Address Tests" [
- test_most_sig_bit_different1,
- test_most_sig_bit_different2 ]
+import Control.Concurrent.ParallelIO.Global (
+ parallel,
+ stopGlobalPool )
import Control.Monad (unless, when)
+import qualified Data.ByteString.Char8 as BS (intercalate, pack, unpack)
import Data.List ((\\), intercalate)
import Data.Maybe (catMaybes, isNothing)
import Data.String.Utils (splitWs)
import Cidr (Cidr(..),
cidr_from_string,
combine_all,
+ enumerate,
max_octet1,
max_octet2,
max_octet3,
parse_errors,
parse_mode)
+import DNS (Domain, lookup_ptrs)
import ExitCodes
import Octet
-
+
-- | A regular expression that matches a non-address character.
non_addr_char :: String
Regex -> do
let regexes = map cidr_to_regex valid_cidrs
putStrLn $ alternate regexes
- Reduce -> do
- _ <- mapM print (combine_all valid_cidrs)
- return ()
- Dupe -> do
- _ <- mapM print dupes
- return ()
+ Reduce ->
+ mapM_ print (combine_all valid_cidrs)
+ Dupe ->
+ mapM_ print dupes
where
dupes = valid_cidrs \\ (combine_all valid_cidrs)
Diff -> do
- _ <- mapM putStrLn deletions
- _ <- mapM putStrLn additions
- return ()
+ mapM_ putStrLn deletions
+ mapM_ putStrLn additions
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
+ List -> do
+ let combined_cidrs = combine_all valid_cidrs
+ let addrs = concatMap enumerate combined_cidrs
+ mapM_ print addrs
+ Reverse -> do
+ let combined_cidrs = combine_all valid_cidrs
+ let addrs = concatMap enumerate combined_cidrs
+ let addr_bytestrings = map (BS.pack . show) addrs
+ ptrs <- lookup_ptrs addr_bytestrings
+ let pairs = zip addr_bytestrings ptrs
+ _ <- parallel (map (putStrLn . show_pair) pairs)
+ return ()
+
+ stopGlobalPool
+
+ where
+ show_pair :: (Domain, Maybe [Domain]) -> String
+ show_pair (s, mds) =
+ (BS.unpack s) ++ ": " ++ results
+ where
+ space = BS.pack " "
+ results =
+ case mds of
+ Nothing -> ""
+ Just ds -> BS.unpack $ BS.intercalate space ds
apply_mask oct _ _ = oct
+instance Ord Octet where
+ (Octet x1 x2 x3 x4 x5 x6 x7 x8) <= (Octet y1 y2 y3 y4 y5 y6 y7 y8)
+ | x1 > y1 = False
+ | x2 > y2 = False
+ | x3 > y3 = False
+ | x4 > y4 = False
+ | x5 > y5 = False
+ | x6 > y6 = False
+ | x7 > y7 = False
+ | x8 > y8 = False
+ | otherwise = True
+
+
+instance Bounded Octet where
+ -- | The octet with the least possible value.
+ minBound =
+ Octet B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero
+
+ -- | The octet with the greatest possible value.
+ maxBound =
+ Octet B.One B.One B.One B.One B.One B.One B.One B.One
+
+
+instance Enum Octet where
+ -- We're supposed to throw a runtime error if you call (succ
+ -- maxBound), so the fromJust here doesn't introduce any additional
+ -- badness.
+ toEnum = fromJust . octet_from_int
+ fromEnum = octet_to_int
+
-- | 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.
x:_ -> octet_from_int (fst x)
--- | 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
-
-
--- | 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
-
-
-- HUnit Tests
test_octet_from_int1 :: Test
import Cidr (cidr_properties, cidr_tests)
-import IPv4Address (ipv4address_tests)
+import IPv4Address (ipv4address_properties, ipv4address_tests)
import Octet (octet_tests)
tests :: [Test.Framework.Test]
tests = [ cidr_properties,
cidr_tests,
+ ipv4address_properties,
ipv4address_tests,
octet_tests ]