module Net.IPv4
(
fromOctets
, toOctets
, any
, loopback
, broadcast
, private
, reserved
, public
) where
import Prelude hiding (any)
import Net.Types (IPv4(..))
import Data.Bits ((.&.),(.|.),shiftR,shiftL,complement)
import Data.Word
import Data.Int
import Data.Hashable
import Data.Aeson (FromJSON(..),ToJSON(..))
import GHC.Generics (Generic)
import Net.Internal (attoparsecParseJSON,rightToMaybe)
import Control.Monad
import Data.Text.Internal (Text(..))
import Data.Coerce (coerce)
import Unsafe.Coerce (unsafeCoerce)
import Data.ByteString (ByteString)
import Data.Vector.Generic.Mutable (MVector(..))
import Control.Monad.Primitive (PrimMonad,PrimState)
import qualified Net.Internal as Internal
import qualified Data.Text.Lazy as LText
import qualified Data.Text.IO as Text
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Unsafe as ByteString
fromOctets :: Word8 -> Word8 -> Word8 -> Word8 -> IPv4
fromOctets a b c d = IPv4 $ Internal.fromOctets'
(fromIntegral a) (fromIntegral b) (fromIntegral c) (fromIntegral d)
toOctets :: IPv4 -> (Word8,Word8,Word8,Word8)
toOctets (IPv4 w) =
( fromIntegral (shiftR w 24)
, fromIntegral (shiftR w 16)
, fromIntegral (shiftR w 8)
, fromIntegral w
)
any :: IPv4
any = IPv4 0
loopback :: IPv4
loopback = fromOctets 127 0 0 1
broadcast :: IPv4
broadcast = fromOctets 255 255 255 255
private :: IPv4 -> Bool
private (IPv4 w) =
Internal.mask8 .&. w == Internal.p24
|| Internal.mask12 .&. w == Internal.p20
|| Internal.mask16 .&. w == Internal.p16
reserved :: IPv4 -> Bool
reserved =
let a = Internal.fromOctets' 0 0 0 0
b = Internal.fromOctets' 100 64 0 0
c = Internal.fromOctets' 127 0 0 0
d = Internal.fromOctets' 169 254 0 0
e = Internal.fromOctets' 192 0 0 0
f = Internal.fromOctets' 192 0 2 0
g = Internal.fromOctets' 192 88 99 0
h = Internal.fromOctets' 198 18 0 0
i = Internal.fromOctets' 198 51 100 0
j = Internal.fromOctets' 203 0 113 0
k = Internal.fromOctets' 224 0 0 0
l = Internal.fromOctets' 240 0 0 0
m = Internal.fromOctets' 255 255 255 255
in \(IPv4 w) -> Internal.mask8 .&. w == Internal.p24
|| Internal.mask12 .&. w == Internal.p20
|| Internal.mask16 .&. w == Internal.p16
|| Internal.mask8 .&. w == a
|| Internal.mask10 .&. w == b
|| Internal.mask16 .&. w == d
|| Internal.mask24 .&. w == e
|| Internal.mask24 .&. w == g
|| Internal.mask15 .&. w == h
|| Internal.mask24 .&. w == i
|| Internal.mask24 .&. w == j
|| Internal.mask4 .&. w == k
|| Internal.mask4 .&. w == l
|| Internal.mask32 .&. w == m
public :: IPv4 -> Bool
public = not . reserved