module Network.Subnet.Ip where
import Network.Subnet.Binary
import Data.List
import Data.List.Split
newtype SubnetMask = SubnetMask { getSubnetMask :: String }
deriving (Ord, Show, Eq)
newtype LogIp = LogIp { getLogIp :: String }
deriving (Ord, Show, Eq)
mkSubnetMask :: String -> SubnetMask
mkSubnetMask x | (&&) ((length (readIp x)) == 4) $ all (==True) $ map (\x -> x >= "0" || x <= "255") $ readIp x = SubnetMask x
| otherwise = error $ x ++ " NOT A SUBNETMASK JON"
mkLogIp :: String -> LogIp
mkLogIp x | (&&) ((length (readIp x)) == 4) $ all (==True) $ map (\x -> x <= "255" || x >= "0") $ readIp x = LogIp x
| otherwise = error "stfu jon"
showIp = intercalate "."
readIp = splitOn "."
readLogIp :: LogIp -> Binary
readLogIp = fixets . mkBinary . concat . map (getBinary . fixets . toBinary . read) . readIp . getLogIp
readSubnetMask :: SubnetMask -> Binary
readSubnetMask = fixets . mkBinary . concat . map (getBinary . fixets . toBinary . read) . readIp . getSubnetMask
showLogIp :: Binary -> LogIp
showLogIp = mkLogIp . intercalate "." . map (show . fromBinary . mkBinary) . chunksOf 8 . getBinary
showSubnetMask :: Binary -> SubnetMask
showSubnetMask = mkSubnetMask . intercalate "." . map (show . fromBinary . mkBinary) . chunksOf 8 . getBinary
modifyLogIp :: (String -> String) -> LogIp -> LogIp
modifyLogIp f = mkLogIp . f . getLogIp
addOneLogIp :: LogIp -> LogIp
addOneLogIp = showLogIp . addOne . readLogIp