ip (empty) → 0.1
raw patch · 5 files changed
+205/−0 lines, 5 filesdep +aesondep +attoparsecdep +basesetup-changed
Dependencies added: aeson, attoparsec, base, hashable, text
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- ip.cabal +30/−0
- src/Net/IPv4.hs +138/−0
- src/Net/IPv4/Text.hs +5/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Andrew Martin (c) 2016++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Andrew Martin nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ ip.cabal view
@@ -0,0 +1,30 @@+name: ip+version: 0.1+synopsis: Initial project template from stack+description: Please see README.md+homepage: https://github.com/andrewthad/ip#readme+license: BSD3+license-file: LICENSE+author: Andrew Martin+maintainer: andrew.thaddeus@gmail.com+copyright: 2016 Andrew Martin+category: web+build-type: Simple+cabal-version: >=1.10++library+ hs-source-dirs: src+ exposed-modules: + Net.IPv4+ Net.IPv4.Text+ build-depends: + base >= 4.7 && < 5+ , attoparsec+ , aeson+ , hashable+ , text+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/andrewthad/ip
+ src/Net/IPv4.hs view
@@ -0,0 +1,138 @@+ {-# LANGUAGE DeriveGeneric #-}+ {-# LANGUAGE GeneralizedNewtypeDeriving #-}++module Net.IPv4 where++import Data.Text (Text)+import qualified Data.Text.Lazy as LText+import qualified Data.Text.Lazy.Builder as TBuilder+import Data.Text.Lazy.Builder.Int (decimal)+import Data.Monoid ((<>))+import Data.Bits ((.&.),(.|.),shiftR,shiftL,complement)+import Data.Word+import Data.Hashable+import Data.Aeson (FromJSON(..),ToJSON(..))+import GHC.Generics (Generic)+import qualified Data.Aeson as Aeson+import qualified Data.Aeson.Types as Aeson+import qualified Data.Attoparsec.Text as Attoparsec++newtype IPv4 = IPv4 { getIPv4 :: Word32 }+ deriving (Eq,Ord,Show,Read,Enum,Bounded,Hashable,Generic)++data IPv4Range = IPv4Range + { ipv4RangeBase :: {-# UNPACK #-} !IPv4+ , ipv4RangeLength :: {-# UNPACK #-} !Int+ } deriving (Eq,Ord,Show,Read,Generic)++instance Hashable IPv4Range++instance ToJSON IPv4 where+ toJSON addr = Aeson.String (toDotDecimalText addr)++instance FromJSON IPv4 where+ parseJSON (Aeson.String t) = + case fromDotDecimalText' t of+ Left err -> fail err+ Right res -> return res++instance ToJSON IPv4Range where+ toJSON addrRange = Aeson.String (rangeToDotDecimalText addrRange)++instance FromJSON IPv4Range where+ parseJSON (Aeson.String t) = + case rangeFromDotDecimalText' t of+ Left err -> fail err+ Right res -> return res++rightToMaybe :: Either a b -> Maybe b+rightToMaybe = either (const Nothing) Just++-- mask :: Int -> IPv4+-- mask w = IPv4 $ complement $ 0xffffffff `shiftR` w++fromDotDecimalText' :: Text -> Either String IPv4+fromDotDecimalText' t = + Attoparsec.parseOnly (dotDecimalParser <* Attoparsec.endOfInput) t++fromDotDecimalText :: Text -> Maybe IPv4+fromDotDecimalText = rightToMaybe . fromDotDecimalText'++rangeFromDotDecimalText' :: Text -> Either String IPv4Range+rangeFromDotDecimalText' t = + Attoparsec.parseOnly (dotDecimalRangeParser <* Attoparsec.endOfInput) t++rangeFromDotDecimalText :: Text -> Maybe IPv4Range+rangeFromDotDecimalText = rightToMaybe . rangeFromDotDecimalText'++dotDecimalRangeParser :: Attoparsec.Parser IPv4Range+dotDecimalRangeParser = IPv4Range+ <$> dotDecimalParser+ <* Attoparsec.char '/'+ <*> (Attoparsec.decimal >>= limitSize)+ where+ limitSize i = + if i > 32+ then fail "An IP range length must be between 0 and 32"+ else return i++-- This does not do an endOfInput check because it is+-- reused in the range parser implementation.+dotDecimalParser :: Attoparsec.Parser IPv4+dotDecimalParser = fromOctets'+ <$> (Attoparsec.decimal >>= limitSize)+ <* Attoparsec.char '.'+ <*> (Attoparsec.decimal >>= limitSize)+ <* Attoparsec.char '.'+ <*> (Attoparsec.decimal >>= limitSize)+ <* Attoparsec.char '.'+ <*> (Attoparsec.decimal >>= limitSize)+ where+ limitSize i = + if i > 255 + then fail "All octets in an ip address must be between 0 and 255"+ else return i++fromOctets :: Word8 -> Word8 -> Word8 -> Word8 -> IPv4+fromOctets a b c d = fromOctets' + (fromIntegral a) (fromIntegral b) (fromIntegral c) (fromIntegral d)++-- | This is sort of a misnomer. It takes Word32 to make+-- dotDecimalParser probably perform better.+fromOctets' :: Word32 -> Word32 -> Word32 -> Word32 -> IPv4+fromOctets' a b c d = IPv4 + ( 0+ .|. shiftL a 24+ .|. shiftL b 16+ .|. shiftL c 8+ .|. d+ )++toDotDecimalText :: IPv4 -> Text+toDotDecimalText = LText.toStrict . TBuilder.toLazyText . toDotDecimalBuilder++-- It should be possible to write a more efficient version that initially+-- allocates a block of strict text of length 15 and then starts filling +-- it in.+toDotDecimalBuilder :: IPv4 -> TBuilder.Builder+toDotDecimalBuilder (IPv4 w) = + decimal (255 .&. shiftR w 24 )+ <> dot+ <> decimal (255 .&. shiftR w 16 )+ <> dot+ <> decimal (255 .&. shiftR w 8 )+ <> dot+ <> decimal (255 .&. w)+ where dot = TBuilder.singleton '.'++rangeToDotDecimalText :: IPv4Range -> Text+rangeToDotDecimalText = LText.toStrict . TBuilder.toLazyText . rangeToDotDecimalBuilder++rangeToDotDecimalBuilder :: IPv4Range -> TBuilder.Builder+rangeToDotDecimalBuilder (IPv4Range addr len) = + toDotDecimalBuilder addr+ <> TBuilder.singleton '/'+ <> decimal len+++
+ src/Net/IPv4/Text.hs view
@@ -0,0 +1,5 @@+ {-# LANGUAGE DeriveGeneric #-}+ {-# LANGUAGE GeneralizedNewtypeDeriving #-}++module Net.IPv4.Text where+