{-# LANGUAGE BangPatterns               #-}
{-# LANGUAGE CApiFFI                    #-}
{-# LANGUAGE DeriveFoldable             #-}
{-# LANGUAGE DeriveFunctor              #-}
{-# LANGUAGE DeriveTraversable          #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE RecordWildCards            #-}

-- |
-- Copyright: © 2017 Herbert Valerio Riedel
-- SPDX-License-Identifier: GPL-2.0-or-later
--
-- Internal module
module Network.DNS.Message where

import qualified Data.ByteString.Base16 as B16

import qualified Data.ByteString        as BS
import qualified Data.ByteString.Lazy   as BSL
import           Data.Function
import           Data.List              (groupBy)
import           Data.String
import           Numeric                (showHex)
import           Prelude

import           Data.Binary
import           Data.Binary.Get
import           Data.Binary.Put
import           Data.Bits
import           Data.Map               (Map)
import qualified Data.Map               as Map
import           Data.Set               (Set)
import qualified Data.Set               as Set

import           Compat

-- | An IPv6 address
--
-- The IP address is represented in network order,
-- i.e. @2606:2800:220:1:248:1893:25c8:1946@ is
-- represented as @(IPv6 0x2606280002200001 0x248189325c81946)@.
data IPv6 = IPv6 !Word64 !Word64
          deriving (IPv6 -> IPv6 -> Bool
(IPv6 -> IPv6 -> Bool) -> (IPv6 -> IPv6 -> Bool) -> Eq IPv6
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: IPv6 -> IPv6 -> Bool
== :: IPv6 -> IPv6 -> Bool
$c/= :: IPv6 -> IPv6 -> Bool
/= :: IPv6 -> IPv6 -> Bool
Eq,Eq IPv6
Eq IPv6 =>
(IPv6 -> IPv6 -> Ordering)
-> (IPv6 -> IPv6 -> Bool)
-> (IPv6 -> IPv6 -> Bool)
-> (IPv6 -> IPv6 -> Bool)
-> (IPv6 -> IPv6 -> Bool)
-> (IPv6 -> IPv6 -> IPv6)
-> (IPv6 -> IPv6 -> IPv6)
-> Ord IPv6
IPv6 -> IPv6 -> Bool
IPv6 -> IPv6 -> Ordering
IPv6 -> IPv6 -> IPv6
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: IPv6 -> IPv6 -> Ordering
compare :: IPv6 -> IPv6 -> Ordering
$c< :: IPv6 -> IPv6 -> Bool
< :: IPv6 -> IPv6 -> Bool
$c<= :: IPv6 -> IPv6 -> Bool
<= :: IPv6 -> IPv6 -> Bool
$c> :: IPv6 -> IPv6 -> Bool
> :: IPv6 -> IPv6 -> Bool
$c>= :: IPv6 -> IPv6 -> Bool
>= :: IPv6 -> IPv6 -> Bool
$cmax :: IPv6 -> IPv6 -> IPv6
max :: IPv6 -> IPv6 -> IPv6
$cmin :: IPv6 -> IPv6 -> IPv6
min :: IPv6 -> IPv6 -> IPv6
Ord,ReadPrec [IPv6]
ReadPrec IPv6
Int -> ReadS IPv6
ReadS [IPv6]
(Int -> ReadS IPv6)
-> ReadS [IPv6] -> ReadPrec IPv6 -> ReadPrec [IPv6] -> Read IPv6
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS IPv6
readsPrec :: Int -> ReadS IPv6
$creadList :: ReadS [IPv6]
readList :: ReadS [IPv6]
$creadPrec :: ReadPrec IPv6
readPrec :: ReadPrec IPv6
$creadListPrec :: ReadPrec [IPv6]
readListPrec :: ReadPrec [IPv6]
Read)

instance Show IPv6 where
    showsPrec :: Int -> IPv6 -> ShowS
showsPrec Int
p (IPv6 Word64
hi Word64
lo) = Bool -> ShowS -> ShowS
showParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
11) (String -> ShowS
showString String
"IPv6 0x" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> ShowS
forall a. Integral a => a -> ShowS
showHex Word64
hi ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
" 0x" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> ShowS
forall a. Integral a => a -> ShowS
showHex Word64
lo)

instance Binary IPv6 where
    put :: IPv6 -> Put
put (IPv6 Word64
hi Word64
lo) = Word64 -> Put
putWord64be Word64
hi Put -> Put -> Put
forall a b. PutM a -> PutM b -> PutM b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Word64 -> Put
putWord64be Word64
lo
    get :: Get IPv6
get              = Word64 -> Word64 -> IPv6
IPv6 (Word64 -> Word64 -> IPv6) -> Get Word64 -> Get (Word64 -> IPv6)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word64
getWord64be Get (Word64 -> IPv6) -> Get Word64 -> Get IPv6
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word64
getWord64be

-- | An IPv4 address
--
-- The IP address is represented in network order, i.e. @127.0.0.1@ is
-- represented as @(IPv4 0x7f000001)@.
data IPv4 = IPv4 !Word32
          deriving (IPv4 -> IPv4 -> Bool
(IPv4 -> IPv4 -> Bool) -> (IPv4 -> IPv4 -> Bool) -> Eq IPv4
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: IPv4 -> IPv4 -> Bool
== :: IPv4 -> IPv4 -> Bool
$c/= :: IPv4 -> IPv4 -> Bool
/= :: IPv4 -> IPv4 -> Bool
Eq,Eq IPv4
Eq IPv4 =>
(IPv4 -> IPv4 -> Ordering)
-> (IPv4 -> IPv4 -> Bool)
-> (IPv4 -> IPv4 -> Bool)
-> (IPv4 -> IPv4 -> Bool)
-> (IPv4 -> IPv4 -> Bool)
-> (IPv4 -> IPv4 -> IPv4)
-> (IPv4 -> IPv4 -> IPv4)
-> Ord IPv4
IPv4 -> IPv4 -> Bool
IPv4 -> IPv4 -> Ordering
IPv4 -> IPv4 -> IPv4
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: IPv4 -> IPv4 -> Ordering
compare :: IPv4 -> IPv4 -> Ordering
$c< :: IPv4 -> IPv4 -> Bool
< :: IPv4 -> IPv4 -> Bool
$c<= :: IPv4 -> IPv4 -> Bool
<= :: IPv4 -> IPv4 -> Bool
$c> :: IPv4 -> IPv4 -> Bool
> :: IPv4 -> IPv4 -> Bool
$c>= :: IPv4 -> IPv4 -> Bool
>= :: IPv4 -> IPv4 -> Bool
$cmax :: IPv4 -> IPv4 -> IPv4
max :: IPv4 -> IPv4 -> IPv4
$cmin :: IPv4 -> IPv4 -> IPv4
min :: IPv4 -> IPv4 -> IPv4
Ord,ReadPrec [IPv4]
ReadPrec IPv4
Int -> ReadS IPv4
ReadS [IPv4]
(Int -> ReadS IPv4)
-> ReadS [IPv4] -> ReadPrec IPv4 -> ReadPrec [IPv4] -> Read IPv4
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS IPv4
readsPrec :: Int -> ReadS IPv4
$creadList :: ReadS [IPv4]
readList :: ReadS [IPv4]
$creadPrec :: ReadPrec IPv4
readPrec :: ReadPrec IPv4
$creadListPrec :: ReadPrec [IPv4]
readListPrec :: ReadPrec [IPv4]
Read)

instance Show IPv4 where
    showsPrec :: Int -> IPv4 -> ShowS
showsPrec Int
p (IPv4 Word32
n) = Bool -> ShowS -> ShowS
showParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
11) (String -> ShowS
showString String
"IPv4 0x" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> ShowS
forall a. Integral a => a -> ShowS
showHex Word32
n)

instance Binary IPv4 where
    put :: IPv4 -> Put
put (IPv4 Word32
w) = Word32 -> Put
putWord32be Word32
w
    get :: Get IPv4
get = Word32 -> IPv4
IPv4 (Word32 -> IPv4) -> Get Word32 -> Get IPv4
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word32
getWord32be

-- | @\<domain-name\>@ as per [RFC 1035, section 3.3](https://tools.ietf.org/html/rfc1035#section-3.3).
--
-- A domain-name represented as a series of labels separated by dots.
--
-- See also 'Labels' for list-based representation.
--
-- __NOTE__: The 'Labels' type is able to properly represent domain
-- names whose components contain dots which the 'Name' representation
-- cannot.
newtype Name = Name BS.ByteString
             deriving (ReadPrec [Name]
ReadPrec Name
Int -> ReadS Name
ReadS [Name]
(Int -> ReadS Name)
-> ReadS [Name] -> ReadPrec Name -> ReadPrec [Name] -> Read Name
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Name
readsPrec :: Int -> ReadS Name
$creadList :: ReadS [Name]
readList :: ReadS [Name]
$creadPrec :: ReadPrec Name
readPrec :: ReadPrec Name
$creadListPrec :: ReadPrec [Name]
readListPrec :: ReadPrec [Name]
Read,Int -> Name -> ShowS
[Name] -> ShowS
Name -> String
(Int -> Name -> ShowS)
-> (Name -> String) -> ([Name] -> ShowS) -> Show Name
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Name -> ShowS
showsPrec :: Int -> Name -> ShowS
$cshow :: Name -> String
show :: Name -> String
$cshowList :: [Name] -> ShowS
showList :: [Name] -> ShowS
Show,Name -> Name -> Bool
(Name -> Name -> Bool) -> (Name -> Name -> Bool) -> Eq Name
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Name -> Name -> Bool
== :: Name -> Name -> Bool
$c/= :: Name -> Name -> Bool
/= :: Name -> Name -> Bool
Eq,Eq Name
Eq Name =>
(Name -> Name -> Ordering)
-> (Name -> Name -> Bool)
-> (Name -> Name -> Bool)
-> (Name -> Name -> Bool)
-> (Name -> Name -> Bool)
-> (Name -> Name -> Name)
-> (Name -> Name -> Name)
-> Ord Name
Name -> Name -> Bool
Name -> Name -> Ordering
Name -> Name -> Name
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Name -> Name -> Ordering
compare :: Name -> Name -> Ordering
$c< :: Name -> Name -> Bool
< :: Name -> Name -> Bool
$c<= :: Name -> Name -> Bool
<= :: Name -> Name -> Bool
$c> :: Name -> Name -> Bool
> :: Name -> Name -> Bool
$c>= :: Name -> Name -> Bool
>= :: Name -> Name -> Bool
$cmax :: Name -> Name -> Name
max :: Name -> Name -> Name
$cmin :: Name -> Name -> Name
min :: Name -> Name -> Name
Ord)

-- | @\<character-string\>@ as per [RFC 1035, section 3.3](https://tools.ietf.org/html/rfc1035#section-3.3).
--
-- A sequence of up to 255 octets
--
-- The limit of 255 octets is caused by the encoding which uses by a
-- prefixed octet denoting the length.
newtype CharStr = CharStr BS.ByteString
                deriving (CharStr -> CharStr -> Bool
(CharStr -> CharStr -> Bool)
-> (CharStr -> CharStr -> Bool) -> Eq CharStr
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CharStr -> CharStr -> Bool
== :: CharStr -> CharStr -> Bool
$c/= :: CharStr -> CharStr -> Bool
/= :: CharStr -> CharStr -> Bool
Eq,Eq CharStr
Eq CharStr =>
(CharStr -> CharStr -> Ordering)
-> (CharStr -> CharStr -> Bool)
-> (CharStr -> CharStr -> Bool)
-> (CharStr -> CharStr -> Bool)
-> (CharStr -> CharStr -> Bool)
-> (CharStr -> CharStr -> CharStr)
-> (CharStr -> CharStr -> CharStr)
-> Ord CharStr
CharStr -> CharStr -> Bool
CharStr -> CharStr -> Ordering
CharStr -> CharStr -> CharStr
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: CharStr -> CharStr -> Ordering
compare :: CharStr -> CharStr -> Ordering
$c< :: CharStr -> CharStr -> Bool
< :: CharStr -> CharStr -> Bool
$c<= :: CharStr -> CharStr -> Bool
<= :: CharStr -> CharStr -> Bool
$c> :: CharStr -> CharStr -> Bool
> :: CharStr -> CharStr -> Bool
$c>= :: CharStr -> CharStr -> Bool
>= :: CharStr -> CharStr -> Bool
$cmax :: CharStr -> CharStr -> CharStr
max :: CharStr -> CharStr -> CharStr
$cmin :: CharStr -> CharStr -> CharStr
min :: CharStr -> CharStr -> CharStr
Ord,String -> CharStr
(String -> CharStr) -> IsString CharStr
forall a. (String -> a) -> IsString a
$cfromString :: String -> CharStr
fromString :: String -> CharStr
IsString)

instance Show CharStr where
    showsPrec :: Int -> CharStr -> ShowS
showsPrec Int
p (CharStr Label
bs) = Int -> Label -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
p Label
bs

instance Read CharStr where
    readsPrec :: Int -> ReadS CharStr
readsPrec Int
p = ((Label, String) -> (CharStr, String))
-> [(Label, String)] -> [(CharStr, String)]
forall a b. (a -> b) -> [a] -> [b]
map (\(Label
x,String
y) -> (Label -> CharStr
CharStr Label
x,String
y)) ([(Label, String)] -> [(CharStr, String)])
-> (String -> [(Label, String)]) -> ReadS CharStr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> String -> [(Label, String)]
forall a. Read a => Int -> ReadS a
readsPrec Int
p

instance Binary CharStr where
    put :: CharStr -> Put
put (CharStr Label
bs)
      | Label -> Int
BS.length Label
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0xff = String -> Put
forall a. HasCallStack => String -> a
error String
"putString: string too long"
      | Bool
otherwise = do
            Word8 -> Put
putWord8 (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ Label -> Int
BS.length Label
bs)
            Label -> Put
putByteString Label
bs
    get :: Get CharStr
get = do
        Word8
len' <- Get Word8
getWord8
        Label -> CharStr
CharStr (Label -> CharStr) -> Get Label -> Get CharStr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Get Label
getByteString (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
len')

{- Resource records

 -- https://en.wikipedia.org/wiki/List_of_DNS_record_types

 RFC 1035

 A        1     a host address
 NS       2     an authoritative name server
 CNAME    5     the canonical name for an alias
 SOA      6     marks the start of a zone of authority
 PTR      12    a domain name pointer
 MX       15    mail exchange
 TXT      16    text strings

 RFC 3596

 AAAA     28    IPv6

 RFC 2782

 SRV      33    Location of services

 ----

 RFC3597            Handling of Unknown DNS Resource Record (RR) Types

-}

-- | Represents a DNS message as per [RFC 1035](https://tools.ietf.org/html/rfc1035)
data Msg l
    = Msg
      { forall l. Msg l -> MsgHeader
msgHeader           :: !MsgHeader
      , forall l. Msg l -> [MsgQuestion l]
msgQD               :: [MsgQuestion l]
      , forall l. Msg l -> [MsgRR l]
msgAN, forall l. Msg l -> [MsgRR l]
msgNS, forall l. Msg l -> [MsgRR l]
msgAR :: [MsgRR l]
      } deriving (ReadPrec [Msg l]
ReadPrec (Msg l)
Int -> ReadS (Msg l)
ReadS [Msg l]
(Int -> ReadS (Msg l))
-> ReadS [Msg l]
-> ReadPrec (Msg l)
-> ReadPrec [Msg l]
-> Read (Msg l)
forall l. Read l => ReadPrec [Msg l]
forall l. Read l => ReadPrec (Msg l)
forall l. Read l => Int -> ReadS (Msg l)
forall l. Read l => ReadS [Msg l]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall l. Read l => Int -> ReadS (Msg l)
readsPrec :: Int -> ReadS (Msg l)
$creadList :: forall l. Read l => ReadS [Msg l]
readList :: ReadS [Msg l]
$creadPrec :: forall l. Read l => ReadPrec (Msg l)
readPrec :: ReadPrec (Msg l)
$creadListPrec :: forall l. Read l => ReadPrec [Msg l]
readListPrec :: ReadPrec [Msg l]
Read,Int -> Msg l -> ShowS
[Msg l] -> ShowS
Msg l -> String
(Int -> Msg l -> ShowS)
-> (Msg l -> String) -> ([Msg l] -> ShowS) -> Show (Msg l)
forall l. Show l => Int -> Msg l -> ShowS
forall l. Show l => [Msg l] -> ShowS
forall l. Show l => Msg l -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall l. Show l => Int -> Msg l -> ShowS
showsPrec :: Int -> Msg l -> ShowS
$cshow :: forall l. Show l => Msg l -> String
show :: Msg l -> String
$cshowList :: forall l. Show l => [Msg l] -> ShowS
showList :: [Msg l] -> ShowS
Show,(forall a b. (a -> b) -> Msg a -> Msg b)
-> (forall a b. a -> Msg b -> Msg a) -> Functor Msg
forall a b. a -> Msg b -> Msg a
forall a b. (a -> b) -> Msg a -> Msg b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> Msg a -> Msg b
fmap :: forall a b. (a -> b) -> Msg a -> Msg b
$c<$ :: forall a b. a -> Msg b -> Msg a
<$ :: forall a b. a -> Msg b -> Msg a
Functor,(forall m. Monoid m => Msg m -> m)
-> (forall m a. Monoid m => (a -> m) -> Msg a -> m)
-> (forall m a. Monoid m => (a -> m) -> Msg a -> m)
-> (forall a b. (a -> b -> b) -> b -> Msg a -> b)
-> (forall a b. (a -> b -> b) -> b -> Msg a -> b)
-> (forall b a. (b -> a -> b) -> b -> Msg a -> b)
-> (forall b a. (b -> a -> b) -> b -> Msg a -> b)
-> (forall a. (a -> a -> a) -> Msg a -> a)
-> (forall a. (a -> a -> a) -> Msg a -> a)
-> (forall a. Msg a -> [a])
-> (forall a. Msg a -> Bool)
-> (forall a. Msg a -> Int)
-> (forall a. Eq a => a -> Msg a -> Bool)
-> (forall a. Ord a => Msg a -> a)
-> (forall a. Ord a => Msg a -> a)
-> (forall a. Num a => Msg a -> a)
-> (forall a. Num a => Msg a -> a)
-> Foldable Msg
forall a. Eq a => a -> Msg a -> Bool
forall a. Num a => Msg a -> a
forall a. Ord a => Msg a -> a
forall m. Monoid m => Msg m -> m
forall a. Msg a -> Bool
forall a. Msg a -> Int
forall a. Msg a -> [a]
forall a. (a -> a -> a) -> Msg a -> a
forall m a. Monoid m => (a -> m) -> Msg a -> m
forall b a. (b -> a -> b) -> b -> Msg a -> b
forall a b. (a -> b -> b) -> b -> Msg a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => Msg m -> m
fold :: forall m. Monoid m => Msg m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Msg a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Msg a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Msg a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> Msg a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> Msg a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Msg a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Msg a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Msg a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Msg a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Msg a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Msg a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> Msg a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> Msg a -> a
foldr1 :: forall a. (a -> a -> a) -> Msg a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Msg a -> a
foldl1 :: forall a. (a -> a -> a) -> Msg a -> a
$ctoList :: forall a. Msg a -> [a]
toList :: forall a. Msg a -> [a]
$cnull :: forall a. Msg a -> Bool
null :: forall a. Msg a -> Bool
$clength :: forall a. Msg a -> Int
length :: forall a. Msg a -> Int
$celem :: forall a. Eq a => a -> Msg a -> Bool
elem :: forall a. Eq a => a -> Msg a -> Bool
$cmaximum :: forall a. Ord a => Msg a -> a
maximum :: forall a. Ord a => Msg a -> a
$cminimum :: forall a. Ord a => Msg a -> a
minimum :: forall a. Ord a => Msg a -> a
$csum :: forall a. Num a => Msg a -> a
sum :: forall a. Num a => Msg a -> a
$cproduct :: forall a. Num a => Msg a -> a
product :: forall a. Num a => Msg a -> a
Foldable,Functor Msg
Foldable Msg
(Functor Msg, Foldable Msg) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> Msg a -> f (Msg b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    Msg (f a) -> f (Msg a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Msg a -> m (Msg b))
-> (forall (m :: * -> *) a. Monad m => Msg (m a) -> m (Msg a))
-> Traversable Msg
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => Msg (m a) -> m (Msg a)
forall (f :: * -> *) a. Applicative f => Msg (f a) -> f (Msg a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Msg a -> m (Msg b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Msg a -> f (Msg b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Msg a -> f (Msg b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Msg a -> f (Msg b)
$csequenceA :: forall (f :: * -> *) a. Applicative f => Msg (f a) -> f (Msg a)
sequenceA :: forall (f :: * -> *) a. Applicative f => Msg (f a) -> f (Msg a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Msg a -> m (Msg b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Msg a -> m (Msg b)
$csequence :: forall (m :: * -> *) a. Monad m => Msg (m a) -> m (Msg a)
sequence :: forall (m :: * -> *) a. Monad m => Msg (m a) -> m (Msg a)
Traversable)

-- | DNS message header section as per [RFC 1035, section 4.1.1](https://tools.ietf.org/html/rfc1035#section-4.1.1)
data MsgHeader
    = MsgHeader
      { MsgHeader -> Word16
mhId      :: !Word16

      , MsgHeader -> MsgHeaderFlags
mhFlags   :: !MsgHeaderFlags

      , MsgHeader -> Word16
mhQDCount :: !Word16
      , MsgHeader -> Word16
mhANCount :: !Word16
      , MsgHeader -> Word16
mhNSCount :: !Word16
      , MsgHeader -> Word16
mhARCount :: !Word16
      } deriving (ReadPrec [MsgHeader]
ReadPrec MsgHeader
Int -> ReadS MsgHeader
ReadS [MsgHeader]
(Int -> ReadS MsgHeader)
-> ReadS [MsgHeader]
-> ReadPrec MsgHeader
-> ReadPrec [MsgHeader]
-> Read MsgHeader
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS MsgHeader
readsPrec :: Int -> ReadS MsgHeader
$creadList :: ReadS [MsgHeader]
readList :: ReadS [MsgHeader]
$creadPrec :: ReadPrec MsgHeader
readPrec :: ReadPrec MsgHeader
$creadListPrec :: ReadPrec [MsgHeader]
readListPrec :: ReadPrec [MsgHeader]
Read,Int -> MsgHeader -> ShowS
[MsgHeader] -> ShowS
MsgHeader -> String
(Int -> MsgHeader -> ShowS)
-> (MsgHeader -> String)
-> ([MsgHeader] -> ShowS)
-> Show MsgHeader
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MsgHeader -> ShowS
showsPrec :: Int -> MsgHeader -> ShowS
$cshow :: MsgHeader -> String
show :: MsgHeader -> String
$cshowList :: [MsgHeader] -> ShowS
showList :: [MsgHeader] -> ShowS
Show)

-- | DNS message header section as per [RFC 1035, section 4.1.2](https://tools.ietf.org/html/rfc1035#section-4.1.2)
data MsgQuestion l
    = MsgQuestion !l !Type !Class
    deriving (MsgQuestion l -> MsgQuestion l -> Bool
(MsgQuestion l -> MsgQuestion l -> Bool)
-> (MsgQuestion l -> MsgQuestion l -> Bool) -> Eq (MsgQuestion l)
forall l. Eq l => MsgQuestion l -> MsgQuestion l -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall l. Eq l => MsgQuestion l -> MsgQuestion l -> Bool
== :: MsgQuestion l -> MsgQuestion l -> Bool
$c/= :: forall l. Eq l => MsgQuestion l -> MsgQuestion l -> Bool
/= :: MsgQuestion l -> MsgQuestion l -> Bool
Eq,ReadPrec [MsgQuestion l]
ReadPrec (MsgQuestion l)
Int -> ReadS (MsgQuestion l)
ReadS [MsgQuestion l]
(Int -> ReadS (MsgQuestion l))
-> ReadS [MsgQuestion l]
-> ReadPrec (MsgQuestion l)
-> ReadPrec [MsgQuestion l]
-> Read (MsgQuestion l)
forall l. Read l => ReadPrec [MsgQuestion l]
forall l. Read l => ReadPrec (MsgQuestion l)
forall l. Read l => Int -> ReadS (MsgQuestion l)
forall l. Read l => ReadS [MsgQuestion l]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall l. Read l => Int -> ReadS (MsgQuestion l)
readsPrec :: Int -> ReadS (MsgQuestion l)
$creadList :: forall l. Read l => ReadS [MsgQuestion l]
readList :: ReadS [MsgQuestion l]
$creadPrec :: forall l. Read l => ReadPrec (MsgQuestion l)
readPrec :: ReadPrec (MsgQuestion l)
$creadListPrec :: forall l. Read l => ReadPrec [MsgQuestion l]
readListPrec :: ReadPrec [MsgQuestion l]
Read,Int -> MsgQuestion l -> ShowS
[MsgQuestion l] -> ShowS
MsgQuestion l -> String
(Int -> MsgQuestion l -> ShowS)
-> (MsgQuestion l -> String)
-> ([MsgQuestion l] -> ShowS)
-> Show (MsgQuestion l)
forall l. Show l => Int -> MsgQuestion l -> ShowS
forall l. Show l => [MsgQuestion l] -> ShowS
forall l. Show l => MsgQuestion l -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall l. Show l => Int -> MsgQuestion l -> ShowS
showsPrec :: Int -> MsgQuestion l -> ShowS
$cshow :: forall l. Show l => MsgQuestion l -> String
show :: MsgQuestion l -> String
$cshowList :: forall l. Show l => [MsgQuestion l] -> ShowS
showList :: [MsgQuestion l] -> ShowS
Show,(forall a b. (a -> b) -> MsgQuestion a -> MsgQuestion b)
-> (forall a b. a -> MsgQuestion b -> MsgQuestion a)
-> Functor MsgQuestion
forall a b. a -> MsgQuestion b -> MsgQuestion a
forall a b. (a -> b) -> MsgQuestion a -> MsgQuestion b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> MsgQuestion a -> MsgQuestion b
fmap :: forall a b. (a -> b) -> MsgQuestion a -> MsgQuestion b
$c<$ :: forall a b. a -> MsgQuestion b -> MsgQuestion a
<$ :: forall a b. a -> MsgQuestion b -> MsgQuestion a
Functor,(forall m. Monoid m => MsgQuestion m -> m)
-> (forall m a. Monoid m => (a -> m) -> MsgQuestion a -> m)
-> (forall m a. Monoid m => (a -> m) -> MsgQuestion a -> m)
-> (forall a b. (a -> b -> b) -> b -> MsgQuestion a -> b)
-> (forall a b. (a -> b -> b) -> b -> MsgQuestion a -> b)
-> (forall b a. (b -> a -> b) -> b -> MsgQuestion a -> b)
-> (forall b a. (b -> a -> b) -> b -> MsgQuestion a -> b)
-> (forall a. (a -> a -> a) -> MsgQuestion a -> a)
-> (forall a. (a -> a -> a) -> MsgQuestion a -> a)
-> (forall a. MsgQuestion a -> [a])
-> (forall a. MsgQuestion a -> Bool)
-> (forall a. MsgQuestion a -> Int)
-> (forall a. Eq a => a -> MsgQuestion a -> Bool)
-> (forall a. Ord a => MsgQuestion a -> a)
-> (forall a. Ord a => MsgQuestion a -> a)
-> (forall a. Num a => MsgQuestion a -> a)
-> (forall a. Num a => MsgQuestion a -> a)
-> Foldable MsgQuestion
forall a. Eq a => a -> MsgQuestion a -> Bool
forall a. Num a => MsgQuestion a -> a
forall a. Ord a => MsgQuestion a -> a
forall m. Monoid m => MsgQuestion m -> m
forall a. MsgQuestion a -> Bool
forall a. MsgQuestion a -> Int
forall a. MsgQuestion a -> [a]
forall a. (a -> a -> a) -> MsgQuestion a -> a
forall m a. Monoid m => (a -> m) -> MsgQuestion a -> m
forall b a. (b -> a -> b) -> b -> MsgQuestion a -> b
forall a b. (a -> b -> b) -> b -> MsgQuestion a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => MsgQuestion m -> m
fold :: forall m. Monoid m => MsgQuestion m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> MsgQuestion a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> MsgQuestion a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> MsgQuestion a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> MsgQuestion a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> MsgQuestion a -> b
foldr :: forall a b. (a -> b -> b) -> b -> MsgQuestion a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> MsgQuestion a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> MsgQuestion a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> MsgQuestion a -> b
foldl :: forall b a. (b -> a -> b) -> b -> MsgQuestion a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> MsgQuestion a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> MsgQuestion a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> MsgQuestion a -> a
foldr1 :: forall a. (a -> a -> a) -> MsgQuestion a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> MsgQuestion a -> a
foldl1 :: forall a. (a -> a -> a) -> MsgQuestion a -> a
$ctoList :: forall a. MsgQuestion a -> [a]
toList :: forall a. MsgQuestion a -> [a]
$cnull :: forall a. MsgQuestion a -> Bool
null :: forall a. MsgQuestion a -> Bool
$clength :: forall a. MsgQuestion a -> Int
length :: forall a. MsgQuestion a -> Int
$celem :: forall a. Eq a => a -> MsgQuestion a -> Bool
elem :: forall a. Eq a => a -> MsgQuestion a -> Bool
$cmaximum :: forall a. Ord a => MsgQuestion a -> a
maximum :: forall a. Ord a => MsgQuestion a -> a
$cminimum :: forall a. Ord a => MsgQuestion a -> a
minimum :: forall a. Ord a => MsgQuestion a -> a
$csum :: forall a. Num a => MsgQuestion a -> a
sum :: forall a. Num a => MsgQuestion a -> a
$cproduct :: forall a. Num a => MsgQuestion a -> a
product :: forall a. Num a => MsgQuestion a -> a
Foldable,Functor MsgQuestion
Foldable MsgQuestion
(Functor MsgQuestion, Foldable MsgQuestion) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> MsgQuestion a -> f (MsgQuestion b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    MsgQuestion (f a) -> f (MsgQuestion a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> MsgQuestion a -> m (MsgQuestion b))
-> (forall (m :: * -> *) a.
    Monad m =>
    MsgQuestion (m a) -> m (MsgQuestion a))
-> Traversable MsgQuestion
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
MsgQuestion (m a) -> m (MsgQuestion a)
forall (f :: * -> *) a.
Applicative f =>
MsgQuestion (f a) -> f (MsgQuestion a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> MsgQuestion a -> m (MsgQuestion b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> MsgQuestion a -> f (MsgQuestion b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> MsgQuestion a -> f (MsgQuestion b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> MsgQuestion a -> f (MsgQuestion b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
MsgQuestion (f a) -> f (MsgQuestion a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
MsgQuestion (f a) -> f (MsgQuestion a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> MsgQuestion a -> m (MsgQuestion b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> MsgQuestion a -> m (MsgQuestion b)
$csequence :: forall (m :: * -> *) a.
Monad m =>
MsgQuestion (m a) -> m (MsgQuestion a)
sequence :: forall (m :: * -> *) a.
Monad m =>
MsgQuestion (m a) -> m (MsgQuestion a)
Traversable)

-- | DNS message header flags as per [RFC 1035, section 4.1.1](https://tools.ietf.org/html/rfc1035#section-4.1.1)
data MsgHeaderFlags
    = MsgHeaderFlags
      { MsgHeaderFlags -> QR
mhQR     :: !QR
      , MsgHeaderFlags -> Word8
mhOpcode :: !Word8 -- actually Word4
      , MsgHeaderFlags -> Bool
mhAA     :: !Bool
      , MsgHeaderFlags -> Bool
mhTC     :: !Bool
      , MsgHeaderFlags -> Bool
mhRD     :: !Bool
      , MsgHeaderFlags -> Bool
mhRA     :: !Bool
      , MsgHeaderFlags -> Bool
mhZ      :: !Bool -- reserved/unused bit
      , MsgHeaderFlags -> Bool
mhAD     :: !Bool -- RFC4035
      , MsgHeaderFlags -> Bool
mhCD     :: !Bool -- RFC4035
      , MsgHeaderFlags -> Word8
mhRCode  :: !Word8 -- Word4
      } deriving (ReadPrec [MsgHeaderFlags]
ReadPrec MsgHeaderFlags
Int -> ReadS MsgHeaderFlags
ReadS [MsgHeaderFlags]
(Int -> ReadS MsgHeaderFlags)
-> ReadS [MsgHeaderFlags]
-> ReadPrec MsgHeaderFlags
-> ReadPrec [MsgHeaderFlags]
-> Read MsgHeaderFlags
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS MsgHeaderFlags
readsPrec :: Int -> ReadS MsgHeaderFlags
$creadList :: ReadS [MsgHeaderFlags]
readList :: ReadS [MsgHeaderFlags]
$creadPrec :: ReadPrec MsgHeaderFlags
readPrec :: ReadPrec MsgHeaderFlags
$creadListPrec :: ReadPrec [MsgHeaderFlags]
readListPrec :: ReadPrec [MsgHeaderFlags]
Read,Int -> MsgHeaderFlags -> ShowS
[MsgHeaderFlags] -> ShowS
MsgHeaderFlags -> String
(Int -> MsgHeaderFlags -> ShowS)
-> (MsgHeaderFlags -> String)
-> ([MsgHeaderFlags] -> ShowS)
-> Show MsgHeaderFlags
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MsgHeaderFlags -> ShowS
showsPrec :: Int -> MsgHeaderFlags -> ShowS
$cshow :: MsgHeaderFlags -> String
show :: MsgHeaderFlags -> String
$cshowList :: [MsgHeaderFlags] -> ShowS
showList :: [MsgHeaderFlags] -> ShowS
Show)

-- | DNS resource record section as per [RFC 1035, section 4.1.3](https://tools.ietf.org/html/rfc1035#section-4.1.3)
data MsgRR l
    = MsgRR
      { forall l. MsgRR l -> l
rrName  :: !l
      , forall l. MsgRR l -> Class
rrClass :: !Class
      , forall l. MsgRR l -> TTL
rrTTL   :: !TTL
      , forall l. MsgRR l -> RData l
rrData  :: !(RData l)
      } deriving (MsgRR l -> MsgRR l -> Bool
(MsgRR l -> MsgRR l -> Bool)
-> (MsgRR l -> MsgRR l -> Bool) -> Eq (MsgRR l)
forall l. Eq l => MsgRR l -> MsgRR l -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall l. Eq l => MsgRR l -> MsgRR l -> Bool
== :: MsgRR l -> MsgRR l -> Bool
$c/= :: forall l. Eq l => MsgRR l -> MsgRR l -> Bool
/= :: MsgRR l -> MsgRR l -> Bool
Eq,ReadPrec [MsgRR l]
ReadPrec (MsgRR l)
Int -> ReadS (MsgRR l)
ReadS [MsgRR l]
(Int -> ReadS (MsgRR l))
-> ReadS [MsgRR l]
-> ReadPrec (MsgRR l)
-> ReadPrec [MsgRR l]
-> Read (MsgRR l)
forall l. Read l => ReadPrec [MsgRR l]
forall l. Read l => ReadPrec (MsgRR l)
forall l. Read l => Int -> ReadS (MsgRR l)
forall l. Read l => ReadS [MsgRR l]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall l. Read l => Int -> ReadS (MsgRR l)
readsPrec :: Int -> ReadS (MsgRR l)
$creadList :: forall l. Read l => ReadS [MsgRR l]
readList :: ReadS [MsgRR l]
$creadPrec :: forall l. Read l => ReadPrec (MsgRR l)
readPrec :: ReadPrec (MsgRR l)
$creadListPrec :: forall l. Read l => ReadPrec [MsgRR l]
readListPrec :: ReadPrec [MsgRR l]
Read,Int -> MsgRR l -> ShowS
[MsgRR l] -> ShowS
MsgRR l -> String
(Int -> MsgRR l -> ShowS)
-> (MsgRR l -> String) -> ([MsgRR l] -> ShowS) -> Show (MsgRR l)
forall l. Show l => Int -> MsgRR l -> ShowS
forall l. Show l => [MsgRR l] -> ShowS
forall l. Show l => MsgRR l -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall l. Show l => Int -> MsgRR l -> ShowS
showsPrec :: Int -> MsgRR l -> ShowS
$cshow :: forall l. Show l => MsgRR l -> String
show :: MsgRR l -> String
$cshowList :: forall l. Show l => [MsgRR l] -> ShowS
showList :: [MsgRR l] -> ShowS
Show,(forall a b. (a -> b) -> MsgRR a -> MsgRR b)
-> (forall a b. a -> MsgRR b -> MsgRR a) -> Functor MsgRR
forall a b. a -> MsgRR b -> MsgRR a
forall a b. (a -> b) -> MsgRR a -> MsgRR b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> MsgRR a -> MsgRR b
fmap :: forall a b. (a -> b) -> MsgRR a -> MsgRR b
$c<$ :: forall a b. a -> MsgRR b -> MsgRR a
<$ :: forall a b. a -> MsgRR b -> MsgRR a
Functor,(forall m. Monoid m => MsgRR m -> m)
-> (forall m a. Monoid m => (a -> m) -> MsgRR a -> m)
-> (forall m a. Monoid m => (a -> m) -> MsgRR a -> m)
-> (forall a b. (a -> b -> b) -> b -> MsgRR a -> b)
-> (forall a b. (a -> b -> b) -> b -> MsgRR a -> b)
-> (forall b a. (b -> a -> b) -> b -> MsgRR a -> b)
-> (forall b a. (b -> a -> b) -> b -> MsgRR a -> b)
-> (forall a. (a -> a -> a) -> MsgRR a -> a)
-> (forall a. (a -> a -> a) -> MsgRR a -> a)
-> (forall a. MsgRR a -> [a])
-> (forall a. MsgRR a -> Bool)
-> (forall a. MsgRR a -> Int)
-> (forall a. Eq a => a -> MsgRR a -> Bool)
-> (forall a. Ord a => MsgRR a -> a)
-> (forall a. Ord a => MsgRR a -> a)
-> (forall a. Num a => MsgRR a -> a)
-> (forall a. Num a => MsgRR a -> a)
-> Foldable MsgRR
forall a. Eq a => a -> MsgRR a -> Bool
forall a. Num a => MsgRR a -> a
forall a. Ord a => MsgRR a -> a
forall m. Monoid m => MsgRR m -> m
forall a. MsgRR a -> Bool
forall a. MsgRR a -> Int
forall a. MsgRR a -> [a]
forall a. (a -> a -> a) -> MsgRR a -> a
forall m a. Monoid m => (a -> m) -> MsgRR a -> m
forall b a. (b -> a -> b) -> b -> MsgRR a -> b
forall a b. (a -> b -> b) -> b -> MsgRR a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => MsgRR m -> m
fold :: forall m. Monoid m => MsgRR m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> MsgRR a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> MsgRR a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> MsgRR a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> MsgRR a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> MsgRR a -> b
foldr :: forall a b. (a -> b -> b) -> b -> MsgRR a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> MsgRR a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> MsgRR a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> MsgRR a -> b
foldl :: forall b a. (b -> a -> b) -> b -> MsgRR a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> MsgRR a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> MsgRR a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> MsgRR a -> a
foldr1 :: forall a. (a -> a -> a) -> MsgRR a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> MsgRR a -> a
foldl1 :: forall a. (a -> a -> a) -> MsgRR a -> a
$ctoList :: forall a. MsgRR a -> [a]
toList :: forall a. MsgRR a -> [a]
$cnull :: forall a. MsgRR a -> Bool
null :: forall a. MsgRR a -> Bool
$clength :: forall a. MsgRR a -> Int
length :: forall a. MsgRR a -> Int
$celem :: forall a. Eq a => a -> MsgRR a -> Bool
elem :: forall a. Eq a => a -> MsgRR a -> Bool
$cmaximum :: forall a. Ord a => MsgRR a -> a
maximum :: forall a. Ord a => MsgRR a -> a
$cminimum :: forall a. Ord a => MsgRR a -> a
minimum :: forall a. Ord a => MsgRR a -> a
$csum :: forall a. Num a => MsgRR a -> a
sum :: forall a. Num a => MsgRR a -> a
$cproduct :: forall a. Num a => MsgRR a -> a
product :: forall a. Num a => MsgRR a -> a
Foldable,Functor MsgRR
Foldable MsgRR
(Functor MsgRR, Foldable MsgRR) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> MsgRR a -> f (MsgRR b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    MsgRR (f a) -> f (MsgRR a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> MsgRR a -> m (MsgRR b))
-> (forall (m :: * -> *) a. Monad m => MsgRR (m a) -> m (MsgRR a))
-> Traversable MsgRR
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => MsgRR (m a) -> m (MsgRR a)
forall (f :: * -> *) a. Applicative f => MsgRR (f a) -> f (MsgRR a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> MsgRR a -> m (MsgRR b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> MsgRR a -> f (MsgRR b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> MsgRR a -> f (MsgRR b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> MsgRR a -> f (MsgRR b)
$csequenceA :: forall (f :: * -> *) a. Applicative f => MsgRR (f a) -> f (MsgRR a)
sequenceA :: forall (f :: * -> *) a. Applicative f => MsgRR (f a) -> f (MsgRR a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> MsgRR a -> m (MsgRR b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> MsgRR a -> m (MsgRR b)
$csequence :: forall (m :: * -> *) a. Monad m => MsgRR (m a) -> m (MsgRR a)
sequence :: forall (m :: * -> *) a. Monad m => MsgRR (m a) -> m (MsgRR a)
Traversable)

-- | DNS resource record data (see also 'MsgRR' and 'TypeSym')
data RData l
    = RDataA      !IPv4
    | RDataAAAA   !IPv6
    | RDataCNAME  !l
    | RDataPTR    !l
    | RDataHINFO  !CharStr !CharStr
    | RDataNS     !l
    | RDataMX     !Word16 !l
    | RDataTXT    ![CharStr]
    | RDataSPF    ![CharStr]
    | RDataSOA    !l !l !Word32 !Word32 !Word32 !Word32 !Word32
    | RDataSRV    !(SRV l)

    -- RFC 1183
    | RDataAFSDB  !Word16 !l

    -- RFC 2915
    | RDataNAPTR  !Word16 !Word16 !CharStr !CharStr !CharStr !l

    -- RFC 7553
    | RDataURI    !Word16 !Word16 !BS.ByteString

    -- RFC 4034
    | RDataRRSIG  !Word16 !Word8 !Word8 !Word32 !Word32 !Word32 !Word16 !l !BS.ByteString
    | RDataDNSKEY !Word16 !Word8 !Word8 !BS.ByteString
    | RDataDS     !Word16 !Word8 !Word8 !BS.ByteString
    | RDataNSEC   !l !(Set Type)

    -- RFC 4255
    | RDataSSHFP  !Word8 !Word8 !BS.ByteString

    -- RFC 5155
    | RDataNSEC3PARAM !Word8 !Word8 !Word16 !CharStr
    | RDataNSEC3      !Word8 !Word8 !Word16 !CharStr  !CharStr !(Set Type)

    -- RFC 6844
    | RDataCAA !Word8 !CharStr !BS.ByteString

    -- pseudo-record
    | RDataOPT !BS.ByteString -- FIXME

    -- unknown/unsupported
    | RData    !Type !BS.ByteString -- ^ Unknown/undecoded resource record type
    deriving (RData l -> RData l -> Bool
(RData l -> RData l -> Bool)
-> (RData l -> RData l -> Bool) -> Eq (RData l)
forall l. Eq l => RData l -> RData l -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall l. Eq l => RData l -> RData l -> Bool
== :: RData l -> RData l -> Bool
$c/= :: forall l. Eq l => RData l -> RData l -> Bool
/= :: RData l -> RData l -> Bool
Eq,ReadPrec [RData l]
ReadPrec (RData l)
Int -> ReadS (RData l)
ReadS [RData l]
(Int -> ReadS (RData l))
-> ReadS [RData l]
-> ReadPrec (RData l)
-> ReadPrec [RData l]
-> Read (RData l)
forall l. Read l => ReadPrec [RData l]
forall l. Read l => ReadPrec (RData l)
forall l. Read l => Int -> ReadS (RData l)
forall l. Read l => ReadS [RData l]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall l. Read l => Int -> ReadS (RData l)
readsPrec :: Int -> ReadS (RData l)
$creadList :: forall l. Read l => ReadS [RData l]
readList :: ReadS [RData l]
$creadPrec :: forall l. Read l => ReadPrec (RData l)
readPrec :: ReadPrec (RData l)
$creadListPrec :: forall l. Read l => ReadPrec [RData l]
readListPrec :: ReadPrec [RData l]
Read,Int -> RData l -> ShowS
[RData l] -> ShowS
RData l -> String
(Int -> RData l -> ShowS)
-> (RData l -> String) -> ([RData l] -> ShowS) -> Show (RData l)
forall l. Show l => Int -> RData l -> ShowS
forall l. Show l => [RData l] -> ShowS
forall l. Show l => RData l -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall l. Show l => Int -> RData l -> ShowS
showsPrec :: Int -> RData l -> ShowS
$cshow :: forall l. Show l => RData l -> String
show :: RData l -> String
$cshowList :: forall l. Show l => [RData l] -> ShowS
showList :: [RData l] -> ShowS
Show,(forall a b. (a -> b) -> RData a -> RData b)
-> (forall a b. a -> RData b -> RData a) -> Functor RData
forall a b. a -> RData b -> RData a
forall a b. (a -> b) -> RData a -> RData b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> RData a -> RData b
fmap :: forall a b. (a -> b) -> RData a -> RData b
$c<$ :: forall a b. a -> RData b -> RData a
<$ :: forall a b. a -> RData b -> RData a
Functor,(forall m. Monoid m => RData m -> m)
-> (forall m a. Monoid m => (a -> m) -> RData a -> m)
-> (forall m a. Monoid m => (a -> m) -> RData a -> m)
-> (forall a b. (a -> b -> b) -> b -> RData a -> b)
-> (forall a b. (a -> b -> b) -> b -> RData a -> b)
-> (forall b a. (b -> a -> b) -> b -> RData a -> b)
-> (forall b a. (b -> a -> b) -> b -> RData a -> b)
-> (forall a. (a -> a -> a) -> RData a -> a)
-> (forall a. (a -> a -> a) -> RData a -> a)
-> (forall a. RData a -> [a])
-> (forall a. RData a -> Bool)
-> (forall a. RData a -> Int)
-> (forall a. Eq a => a -> RData a -> Bool)
-> (forall a. Ord a => RData a -> a)
-> (forall a. Ord a => RData a -> a)
-> (forall a. Num a => RData a -> a)
-> (forall a. Num a => RData a -> a)
-> Foldable RData
forall a. Eq a => a -> RData a -> Bool
forall a. Num a => RData a -> a
forall a. Ord a => RData a -> a
forall m. Monoid m => RData m -> m
forall a. RData a -> Bool
forall a. RData a -> Int
forall a. RData a -> [a]
forall a. (a -> a -> a) -> RData a -> a
forall m a. Monoid m => (a -> m) -> RData a -> m
forall b a. (b -> a -> b) -> b -> RData a -> b
forall a b. (a -> b -> b) -> b -> RData a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => RData m -> m
fold :: forall m. Monoid m => RData m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> RData a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> RData a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> RData a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> RData a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> RData a -> b
foldr :: forall a b. (a -> b -> b) -> b -> RData a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> RData a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> RData a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> RData a -> b
foldl :: forall b a. (b -> a -> b) -> b -> RData a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> RData a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> RData a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> RData a -> a
foldr1 :: forall a. (a -> a -> a) -> RData a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> RData a -> a
foldl1 :: forall a. (a -> a -> a) -> RData a -> a
$ctoList :: forall a. RData a -> [a]
toList :: forall a. RData a -> [a]
$cnull :: forall a. RData a -> Bool
null :: forall a. RData a -> Bool
$clength :: forall a. RData a -> Int
length :: forall a. RData a -> Int
$celem :: forall a. Eq a => a -> RData a -> Bool
elem :: forall a. Eq a => a -> RData a -> Bool
$cmaximum :: forall a. Ord a => RData a -> a
maximum :: forall a. Ord a => RData a -> a
$cminimum :: forall a. Ord a => RData a -> a
minimum :: forall a. Ord a => RData a -> a
$csum :: forall a. Num a => RData a -> a
sum :: forall a. Num a => RData a -> a
$cproduct :: forall a. Num a => RData a -> a
product :: forall a. Num a => RData a -> a
Foldable,Functor RData
Foldable RData
(Functor RData, Foldable RData) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> RData a -> f (RData b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    RData (f a) -> f (RData a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> RData a -> m (RData b))
-> (forall (m :: * -> *) a. Monad m => RData (m a) -> m (RData a))
-> Traversable RData
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => RData (m a) -> m (RData a)
forall (f :: * -> *) a. Applicative f => RData (f a) -> f (RData a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> RData a -> m (RData b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> RData a -> f (RData b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> RData a -> f (RData b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> RData a -> f (RData b)
$csequenceA :: forall (f :: * -> *) a. Applicative f => RData (f a) -> f (RData a)
sequenceA :: forall (f :: * -> *) a. Applicative f => RData (f a) -> f (RData a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> RData a -> m (RData b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> RData a -> m (RData b)
$csequence :: forall (m :: * -> *) a. Monad m => RData (m a) -> m (RData a)
sequence :: forall (m :: * -> *) a. Monad m => RData (m a) -> m (RData a)
Traversable)


-- | @SRV@ Record data as per [RFC 2782](https://tools.ietf.org/html/rfc2782)
data SRV l = SRV { forall l. SRV l -> Word16
srvPriority :: !Word16
                 , forall l. SRV l -> Word16
srvWeight   :: !Word16
                 , forall l. SRV l -> Word16
srvPort     :: !Word16
                 , forall l. SRV l -> l
srvTarget   :: !l
                 } deriving (SRV l -> SRV l -> Bool
(SRV l -> SRV l -> Bool) -> (SRV l -> SRV l -> Bool) -> Eq (SRV l)
forall l. Eq l => SRV l -> SRV l -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall l. Eq l => SRV l -> SRV l -> Bool
== :: SRV l -> SRV l -> Bool
$c/= :: forall l. Eq l => SRV l -> SRV l -> Bool
/= :: SRV l -> SRV l -> Bool
Eq,ReadPrec [SRV l]
ReadPrec (SRV l)
Int -> ReadS (SRV l)
ReadS [SRV l]
(Int -> ReadS (SRV l))
-> ReadS [SRV l]
-> ReadPrec (SRV l)
-> ReadPrec [SRV l]
-> Read (SRV l)
forall l. Read l => ReadPrec [SRV l]
forall l. Read l => ReadPrec (SRV l)
forall l. Read l => Int -> ReadS (SRV l)
forall l. Read l => ReadS [SRV l]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall l. Read l => Int -> ReadS (SRV l)
readsPrec :: Int -> ReadS (SRV l)
$creadList :: forall l. Read l => ReadS [SRV l]
readList :: ReadS [SRV l]
$creadPrec :: forall l. Read l => ReadPrec (SRV l)
readPrec :: ReadPrec (SRV l)
$creadListPrec :: forall l. Read l => ReadPrec [SRV l]
readListPrec :: ReadPrec [SRV l]
Read,Int -> SRV l -> ShowS
[SRV l] -> ShowS
SRV l -> String
(Int -> SRV l -> ShowS)
-> (SRV l -> String) -> ([SRV l] -> ShowS) -> Show (SRV l)
forall l. Show l => Int -> SRV l -> ShowS
forall l. Show l => [SRV l] -> ShowS
forall l. Show l => SRV l -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall l. Show l => Int -> SRV l -> ShowS
showsPrec :: Int -> SRV l -> ShowS
$cshow :: forall l. Show l => SRV l -> String
show :: SRV l -> String
$cshowList :: forall l. Show l => [SRV l] -> ShowS
showList :: [SRV l] -> ShowS
Show,(forall a b. (a -> b) -> SRV a -> SRV b)
-> (forall a b. a -> SRV b -> SRV a) -> Functor SRV
forall a b. a -> SRV b -> SRV a
forall a b. (a -> b) -> SRV a -> SRV b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> SRV a -> SRV b
fmap :: forall a b. (a -> b) -> SRV a -> SRV b
$c<$ :: forall a b. a -> SRV b -> SRV a
<$ :: forall a b. a -> SRV b -> SRV a
Functor,(forall m. Monoid m => SRV m -> m)
-> (forall m a. Monoid m => (a -> m) -> SRV a -> m)
-> (forall m a. Monoid m => (a -> m) -> SRV a -> m)
-> (forall a b. (a -> b -> b) -> b -> SRV a -> b)
-> (forall a b. (a -> b -> b) -> b -> SRV a -> b)
-> (forall b a. (b -> a -> b) -> b -> SRV a -> b)
-> (forall b a. (b -> a -> b) -> b -> SRV a -> b)
-> (forall a. (a -> a -> a) -> SRV a -> a)
-> (forall a. (a -> a -> a) -> SRV a -> a)
-> (forall a. SRV a -> [a])
-> (forall a. SRV a -> Bool)
-> (forall a. SRV a -> Int)
-> (forall a. Eq a => a -> SRV a -> Bool)
-> (forall a. Ord a => SRV a -> a)
-> (forall a. Ord a => SRV a -> a)
-> (forall a. Num a => SRV a -> a)
-> (forall a. Num a => SRV a -> a)
-> Foldable SRV
forall a. Eq a => a -> SRV a -> Bool
forall a. Num a => SRV a -> a
forall a. Ord a => SRV a -> a
forall m. Monoid m => SRV m -> m
forall a. SRV a -> Bool
forall a. SRV a -> Int
forall a. SRV a -> [a]
forall a. (a -> a -> a) -> SRV a -> a
forall m a. Monoid m => (a -> m) -> SRV a -> m
forall b a. (b -> a -> b) -> b -> SRV a -> b
forall a b. (a -> b -> b) -> b -> SRV a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => SRV m -> m
fold :: forall m. Monoid m => SRV m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> SRV a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> SRV a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> SRV a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> SRV a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> SRV a -> b
foldr :: forall a b. (a -> b -> b) -> b -> SRV a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> SRV a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> SRV a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> SRV a -> b
foldl :: forall b a. (b -> a -> b) -> b -> SRV a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> SRV a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> SRV a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> SRV a -> a
foldr1 :: forall a. (a -> a -> a) -> SRV a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> SRV a -> a
foldl1 :: forall a. (a -> a -> a) -> SRV a -> a
$ctoList :: forall a. SRV a -> [a]
toList :: forall a. SRV a -> [a]
$cnull :: forall a. SRV a -> Bool
null :: forall a. SRV a -> Bool
$clength :: forall a. SRV a -> Int
length :: forall a. SRV a -> Int
$celem :: forall a. Eq a => a -> SRV a -> Bool
elem :: forall a. Eq a => a -> SRV a -> Bool
$cmaximum :: forall a. Ord a => SRV a -> a
maximum :: forall a. Ord a => SRV a -> a
$cminimum :: forall a. Ord a => SRV a -> a
minimum :: forall a. Ord a => SRV a -> a
$csum :: forall a. Num a => SRV a -> a
sum :: forall a. Num a => SRV a -> a
$cproduct :: forall a. Num a => SRV a -> a
product :: forall a. Num a => SRV a -> a
Foldable,Functor SRV
Foldable SRV
(Functor SRV, Foldable SRV) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> SRV a -> f (SRV b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    SRV (f a) -> f (SRV a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> SRV a -> m (SRV b))
-> (forall (m :: * -> *) a. Monad m => SRV (m a) -> m (SRV a))
-> Traversable SRV
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => SRV (m a) -> m (SRV a)
forall (f :: * -> *) a. Applicative f => SRV (f a) -> f (SRV a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SRV a -> m (SRV b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SRV a -> f (SRV b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SRV a -> f (SRV b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SRV a -> f (SRV b)
$csequenceA :: forall (f :: * -> *) a. Applicative f => SRV (f a) -> f (SRV a)
sequenceA :: forall (f :: * -> *) a. Applicative f => SRV (f a) -> f (SRV a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SRV a -> m (SRV b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SRV a -> m (SRV b)
$csequence :: forall (m :: * -> *) a. Monad m => SRV (m a) -> m (SRV a)
sequence :: forall (m :: * -> *) a. Monad m => SRV (m a) -> m (SRV a)
Traversable)

----------------------------------------------------------------------------

decodeMessage' :: BS.ByteString -> Maybe (Msg Labels)
decodeMessage' :: Label -> Maybe (Msg Labels)
decodeMessage' Label
bs = do
    (ByteString
rest, ByteOffset
_, Msg LabelsPtr
v) <- ((ByteString, ByteOffset, String)
 -> Maybe (ByteString, ByteOffset, Msg LabelsPtr))
-> ((ByteString, ByteOffset, Msg LabelsPtr)
    -> Maybe (ByteString, ByteOffset, Msg LabelsPtr))
-> Either
     (ByteString, ByteOffset, String)
     (ByteString, ByteOffset, Msg LabelsPtr)
-> Maybe (ByteString, ByteOffset, Msg LabelsPtr)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (ByteString, ByteOffset, String)
-> Maybe (ByteString, ByteOffset, Msg LabelsPtr)
forall {a} {b} {a}. (Show a, Show b) => (ByteString, b, a) -> a
handleParseFail (ByteString, ByteOffset, Msg LabelsPtr)
-> Maybe (ByteString, ByteOffset, Msg LabelsPtr)
forall a. a -> Maybe a
Just (Either
   (ByteString, ByteOffset, String)
   (ByteString, ByteOffset, Msg LabelsPtr)
 -> Maybe (ByteString, ByteOffset, Msg LabelsPtr))
-> Either
     (ByteString, ByteOffset, String)
     (ByteString, ByteOffset, Msg LabelsPtr)
-> Maybe (ByteString, ByteOffset, Msg LabelsPtr)
forall a b. (a -> b) -> a -> b
$
                    ByteString
-> Either
     (ByteString, ByteOffset, String)
     (ByteString, ByteOffset, Msg LabelsPtr)
forall a.
Binary a =>
ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
decodeOrFail (Label -> ByteString
fromStrict Label
bs)

    -- don't allow trailing garbage
    Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (ByteString -> Bool
BSL.null ByteString
rest)

    let ofss :: Set Word16
ofss = [Word16] -> Set Word16
forall a. Ord a => [a] -> Set a
Set.fromList ([Word16] -> Set Word16) -> [Word16] -> Set Word16
forall a b. (a -> b) -> a -> b
$ (LabelsPtr -> Maybe Word16) -> [LabelsPtr] -> [Word16]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe LabelsPtr -> Maybe Word16
labelsPtr (Msg LabelsPtr -> [LabelsPtr]
forall a. Msg a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Msg LabelsPtr
v)
    Map Word16 LabelsPtr
ofsmap <- Label -> Set Word16 -> Maybe (Map Word16 LabelsPtr)
retrieveLabelPtrs Label
bs Set Word16
ofss

    (LabelsPtr -> Maybe Labels) -> Msg LabelsPtr -> Maybe (Msg Labels)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Msg a -> f (Msg b)
traverse (Map Word16 LabelsPtr -> LabelsPtr -> Maybe Labels
resolveLabelPtr Map Word16 LabelsPtr
ofsmap) Msg LabelsPtr
v
  where
    -- handleParseFail _ = Nothing
    handleParseFail :: (ByteString, b, a) -> a
handleParseFail (ByteString
rest, b
n, a
e) = String -> a
forall a. HasCallStack => String -> a
error (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$ (a, b, ByteOffset, Int) -> String
forall a. Show a => a -> String
show (a
e, b
n, ByteString -> ByteOffset
BSL.length ByteString
rest, Label -> Int
BS.length Label
bs) String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"\n" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Label -> String
forall a. Show a => a -> String
show (Label -> Label
B16.encode (Label -> Label) -> Label -> Label
forall a b. (a -> b) -> a -> b
$ ByteString -> Label
toStrict ByteString
rest)

-- | Decode a raw DNS message (query or response)
--
-- Returns 'Nothing' on decoding failures.
decodeMessage :: IsLabels n => BS.ByteString -> Maybe (Msg n)
decodeMessage :: forall n. IsLabels n => Label -> Maybe (Msg n)
decodeMessage = (Msg Labels -> Msg n) -> Maybe (Msg Labels) -> Maybe (Msg n)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Labels -> n) -> Msg Labels -> Msg n
forall a b. (a -> b) -> Msg a -> Msg b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Labels -> n
forall s. IsLabels s => Labels -> s
fromLabels) (Maybe (Msg Labels) -> Maybe (Msg n))
-> (Label -> Maybe (Msg Labels)) -> Label -> Maybe (Msg n)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Label -> Maybe (Msg Labels)
decodeMessage'

encodeMessage' :: Msg Labels -> BS.ByteString
encodeMessage' :: Msg Labels -> Label
encodeMessage' Msg Labels
m = ByteString -> Label
toStrict (ByteString -> Label) -> ByteString -> Label
forall a b. (a -> b) -> a -> b
$ Msg LabelsPtr -> ByteString
forall a. Binary a => a -> ByteString
encode ((Labels -> LabelsPtr) -> Msg Labels -> Msg LabelsPtr
forall a b. (a -> b) -> Msg a -> Msg b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Labels -> LabelsPtr
labels2labelsPtr Msg Labels
m)

-- | Construct a raw DNS message (query or response)
--
-- May return 'Nothing' in input parameters are detected to be invalid.
encodeMessage :: IsLabels n => Msg n -> Maybe BS.ByteString
encodeMessage :: forall n. IsLabels n => Msg n -> Maybe Label
encodeMessage Msg n
m = Msg Labels -> Label
encodeMessage' (Msg Labels -> Label) -> Maybe (Msg Labels) -> Maybe Label
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (n -> Maybe Labels) -> Msg n -> Maybe (Msg Labels)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Msg a -> f (Msg b)
traverse n -> Maybe Labels
forall s. IsLabels s => s -> Maybe Labels
toLabels Msg n
m


instance Binary l => Binary (Msg l) where
    get :: Get (Msg l)
get = do
        hdr :: MsgHeader
hdr@MsgHeader{Word16
MsgHeaderFlags
mhId :: MsgHeader -> Word16
mhFlags :: MsgHeader -> MsgHeaderFlags
mhQDCount :: MsgHeader -> Word16
mhANCount :: MsgHeader -> Word16
mhNSCount :: MsgHeader -> Word16
mhARCount :: MsgHeader -> Word16
mhId :: Word16
mhFlags :: MsgHeaderFlags
mhQDCount :: Word16
mhANCount :: Word16
mhNSCount :: Word16
mhARCount :: Word16
..} <- Get MsgHeader
forall t. Binary t => Get t
get

        MsgHeader
-> [MsgQuestion l] -> [MsgRR l] -> [MsgRR l] -> [MsgRR l] -> Msg l
forall l.
MsgHeader
-> [MsgQuestion l] -> [MsgRR l] -> [MsgRR l] -> [MsgRR l] -> Msg l
Msg MsgHeader
hdr ([MsgQuestion l] -> [MsgRR l] -> [MsgRR l] -> [MsgRR l] -> Msg l)
-> Get [MsgQuestion l]
-> Get ([MsgRR l] -> [MsgRR l] -> [MsgRR l] -> Msg l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Get (MsgQuestion l) -> Get [MsgQuestion l]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (Word16 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
mhQDCount) Get (MsgQuestion l)
forall t. Binary t => Get t
get
                Get ([MsgRR l] -> [MsgRR l] -> [MsgRR l] -> Msg l)
-> Get [MsgRR l] -> Get ([MsgRR l] -> [MsgRR l] -> Msg l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Int -> Get (MsgRR l) -> Get [MsgRR l]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (Word16 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
mhANCount) Get (MsgRR l)
forall t. Binary t => Get t
get
                Get ([MsgRR l] -> [MsgRR l] -> Msg l)
-> Get [MsgRR l] -> Get ([MsgRR l] -> Msg l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Int -> Get (MsgRR l) -> Get [MsgRR l]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (Word16 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
mhNSCount) Get (MsgRR l)
forall t. Binary t => Get t
get
                Get ([MsgRR l] -> Msg l) -> Get [MsgRR l] -> Get (Msg l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Int -> Get (MsgRR l) -> Get [MsgRR l]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM (Word16 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
mhARCount) Get (MsgRR l)
forall t. Binary t => Get t
get

    put :: Msg l -> Put
put (Msg MsgHeader
hdr [MsgQuestion l]
qds [MsgRR l]
ans [MsgRR l]
nss [MsgRR l]
ars) = do
        MsgHeader -> Put
forall t. Binary t => t -> Put
put MsgHeader
hdr
        (MsgQuestion l -> Put) -> [MsgQuestion l] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ MsgQuestion l -> Put
forall t. Binary t => t -> Put
put [MsgQuestion l]
qds
        (MsgRR l -> Put) -> [MsgRR l] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ MsgRR l -> Put
forall t. Binary t => t -> Put
put [MsgRR l]
ans
        (MsgRR l -> Put) -> [MsgRR l] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ MsgRR l -> Put
forall t. Binary t => t -> Put
put [MsgRR l]
nss
        (MsgRR l -> Put) -> [MsgRR l] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ MsgRR l -> Put
forall t. Binary t => t -> Put
put [MsgRR l]
ars

instance Binary MsgHeader where
    get :: Get MsgHeader
get = Word16
-> MsgHeaderFlags
-> Word16
-> Word16
-> Word16
-> Word16
-> MsgHeader
MsgHeader (Word16
 -> MsgHeaderFlags
 -> Word16
 -> Word16
 -> Word16
 -> Word16
 -> MsgHeader)
-> Get Word16
-> Get
     (MsgHeaderFlags
      -> Word16 -> Word16 -> Word16 -> Word16 -> MsgHeader)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word16
getWord16be
                    Get
  (MsgHeaderFlags
   -> Word16 -> Word16 -> Word16 -> Word16 -> MsgHeader)
-> Get MsgHeaderFlags
-> Get (Word16 -> Word16 -> Word16 -> Word16 -> MsgHeader)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get MsgHeaderFlags
forall t. Binary t => Get t
get
                    Get (Word16 -> Word16 -> Word16 -> Word16 -> MsgHeader)
-> Get Word16 -> Get (Word16 -> Word16 -> Word16 -> MsgHeader)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word16
getWord16be
                    Get (Word16 -> Word16 -> Word16 -> MsgHeader)
-> Get Word16 -> Get (Word16 -> Word16 -> MsgHeader)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word16
getWord16be
                    Get (Word16 -> Word16 -> MsgHeader)
-> Get Word16 -> Get (Word16 -> MsgHeader)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word16
getWord16be
                    Get (Word16 -> MsgHeader) -> Get Word16 -> Get MsgHeader
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word16
getWord16be

    put :: MsgHeader -> Put
put (MsgHeader{Word16
MsgHeaderFlags
mhId :: MsgHeader -> Word16
mhFlags :: MsgHeader -> MsgHeaderFlags
mhQDCount :: MsgHeader -> Word16
mhANCount :: MsgHeader -> Word16
mhNSCount :: MsgHeader -> Word16
mhARCount :: MsgHeader -> Word16
mhId :: Word16
mhFlags :: MsgHeaderFlags
mhQDCount :: Word16
mhANCount :: Word16
mhNSCount :: Word16
mhARCount :: Word16
..}) = do
        Word16 -> Put
putWord16be Word16
mhId
        MsgHeaderFlags -> Put
forall t. Binary t => t -> Put
put MsgHeaderFlags
mhFlags
        Word16 -> Put
putWord16be Word16
mhQDCount
        Word16 -> Put
putWord16be Word16
mhANCount
        Word16 -> Put
putWord16be Word16
mhNSCount
        Word16 -> Put
putWord16be Word16
mhARCount

instance Binary MsgHeaderFlags where
    put :: MsgHeaderFlags -> Put
put = Word16 -> Put
putWord16be (Word16 -> Put)
-> (MsgHeaderFlags -> Word16) -> MsgHeaderFlags -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MsgHeaderFlags -> Word16
encodeFlags
    get :: Get MsgHeaderFlags
get = Word16 -> MsgHeaderFlags
decodeFlags (Word16 -> MsgHeaderFlags) -> Get Word16 -> Get MsgHeaderFlags
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word16
getWord16be

-- | Decode message header flag field
--
-- >  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
-- >  |QR|   Opcode  |AA|TC|RD|RA|??|AD|CD|   RCODE   |
-- >  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
--
decodeFlags :: Word16 -> MsgHeaderFlags
decodeFlags :: Word16 -> MsgHeaderFlags
decodeFlags Word16
w = MsgHeaderFlags{Bool
Word8
QR
mhQR :: QR
mhOpcode :: Word8
mhAA :: Bool
mhTC :: Bool
mhRD :: Bool
mhRA :: Bool
mhZ :: Bool
mhAD :: Bool
mhCD :: Bool
mhRCode :: Word8
mhQR :: QR
mhOpcode :: Word8
mhAA :: Bool
mhTC :: Bool
mhRD :: Bool
mhRA :: Bool
mhZ :: Bool
mhAD :: Bool
mhCD :: Bool
mhRCode :: Word8
..}
  where
    mhQR :: QR
mhQR      = if Word16 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word16
w Int
15 then QR
IsResponse else QR
IsQuery
    mhOpcode :: Word8
mhOpcode  = Int -> Word8
shiftR'   Int
11 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0xf
    mhAA :: Bool
mhAA      = Word16 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word16
w Int
10
    mhTC :: Bool
mhTC      = Word16 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word16
w  Int
9
    mhRD :: Bool
mhRD      = Word16 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word16
w  Int
8
    mhRA :: Bool
mhRA      = Word16 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word16
w  Int
7
    mhZ :: Bool
mhZ       = Word16 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word16
w  Int
6
    mhAD :: Bool
mhAD      = Word16 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word16
w  Int
5
    mhCD :: Bool
mhCD      = Word16 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word16
w  Int
4
    mhRCode :: Word8
mhRCode   = Word16 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
w Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0xf

    shiftR' :: Int -> Word8
shiftR' = Word16 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word16 -> Word8) -> (Int -> Word16) -> Int -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word16 -> Int -> Word16
forall a. Bits a => a -> Int -> a
shiftR Word16
w

encodeFlags :: MsgHeaderFlags -> Word16
encodeFlags :: MsgHeaderFlags -> Word16
encodeFlags MsgHeaderFlags{Bool
Word8
QR
mhQR :: MsgHeaderFlags -> QR
mhOpcode :: MsgHeaderFlags -> Word8
mhAA :: MsgHeaderFlags -> Bool
mhTC :: MsgHeaderFlags -> Bool
mhRD :: MsgHeaderFlags -> Bool
mhRA :: MsgHeaderFlags -> Bool
mhZ :: MsgHeaderFlags -> Bool
mhAD :: MsgHeaderFlags -> Bool
mhCD :: MsgHeaderFlags -> Bool
mhRCode :: MsgHeaderFlags -> Word8
mhQR :: QR
mhOpcode :: Word8
mhAA :: Bool
mhTC :: Bool
mhRD :: Bool
mhRA :: Bool
mhZ :: Bool
mhAD :: Bool
mhCD :: Bool
mhRCode :: Word8
..} =
    (case QR
mhQR of
        QR
IsResponse -> Int -> Word16
forall a. Bits a => Int -> a
bit Int
15
        QR
IsQuery    -> Word16
0) Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
.|.
    (Word8 -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
mhOpcode Word16 -> Int -> Word16
forall a. Bits a => a -> Int -> a
`shiftL` Int
11) Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
.|.
    (if Bool
mhAA then Int -> Word16
forall a. Bits a => Int -> a
bit Int
10 else Word16
0) Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
.|.
    (if Bool
mhTC then Int -> Word16
forall a. Bits a => Int -> a
bit  Int
9 else Word16
0) Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
.|.
    (if Bool
mhRD then Int -> Word16
forall a. Bits a => Int -> a
bit  Int
8 else Word16
0) Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
.|.
    (if Bool
mhRA then Int -> Word16
forall a. Bits a => Int -> a
bit  Int
7 else Word16
0) Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
.|.
    (if Bool
mhZ  then Int -> Word16
forall a. Bits a => Int -> a
bit  Int
6 else Word16
0) Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
.|.
    (if Bool
mhAD then Int -> Word16
forall a. Bits a => Int -> a
bit  Int
5 else Word16
0) Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
.|.
    (if Bool
mhCD then Int -> Word16
forall a. Bits a => Int -> a
bit  Int
4 else Word16
0) Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
.|.
    (Word8 -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
mhRCode)

-- | Encodes whether message is a query or a response
--
-- @since 0.1.1.0
data QR = IsQuery | IsResponse
        deriving (QR -> QR -> Bool
(QR -> QR -> Bool) -> (QR -> QR -> Bool) -> Eq QR
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: QR -> QR -> Bool
== :: QR -> QR -> Bool
$c/= :: QR -> QR -> Bool
/= :: QR -> QR -> Bool
Eq,ReadPrec [QR]
ReadPrec QR
Int -> ReadS QR
ReadS [QR]
(Int -> ReadS QR)
-> ReadS [QR] -> ReadPrec QR -> ReadPrec [QR] -> Read QR
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS QR
readsPrec :: Int -> ReadS QR
$creadList :: ReadS [QR]
readList :: ReadS [QR]
$creadPrec :: ReadPrec QR
readPrec :: ReadPrec QR
$creadListPrec :: ReadPrec [QR]
readListPrec :: ReadPrec [QR]
Read,Int -> QR -> ShowS
[QR] -> ShowS
QR -> String
(Int -> QR -> ShowS)
-> (QR -> String) -> ([QR] -> ShowS) -> Show QR
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> QR -> ShowS
showsPrec :: Int -> QR -> ShowS
$cshow :: QR -> String
show :: QR -> String
$cshowList :: [QR] -> ShowS
showList :: [QR] -> ShowS
Show)

----------------------------------------------------------------------------

infixr 5 :.:

-- | A DNS Label
--
-- Must be non-empty and at most 63 octets.
type Label = BS.ByteString

-- | A @<domain-name>@ as per [RFC 1035, section 3.3](https://tools.ietf.org/html/rfc1035#section-3.3) expressed as list of 'Label's.
--
-- See also 'Name'
data Labels = !Label :.: !Labels | Root
            deriving (ReadPrec [Labels]
ReadPrec Labels
Int -> ReadS Labels
ReadS [Labels]
(Int -> ReadS Labels)
-> ReadS [Labels]
-> ReadPrec Labels
-> ReadPrec [Labels]
-> Read Labels
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Labels
readsPrec :: Int -> ReadS Labels
$creadList :: ReadS [Labels]
readList :: ReadS [Labels]
$creadPrec :: ReadPrec Labels
readPrec :: ReadPrec Labels
$creadListPrec :: ReadPrec [Labels]
readListPrec :: ReadPrec [Labels]
Read,Int -> Labels -> ShowS
[Labels] -> ShowS
Labels -> String
(Int -> Labels -> ShowS)
-> (Labels -> String) -> ([Labels] -> ShowS) -> Show Labels
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Labels -> ShowS
showsPrec :: Int -> Labels -> ShowS
$cshow :: Labels -> String
show :: Labels -> String
$cshowList :: [Labels] -> ShowS
showList :: [Labels] -> ShowS
Show,Labels -> Labels -> Bool
(Labels -> Labels -> Bool)
-> (Labels -> Labels -> Bool) -> Eq Labels
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Labels -> Labels -> Bool
== :: Labels -> Labels -> Bool
$c/= :: Labels -> Labels -> Bool
/= :: Labels -> Labels -> Bool
Eq,Eq Labels
Eq Labels =>
(Labels -> Labels -> Ordering)
-> (Labels -> Labels -> Bool)
-> (Labels -> Labels -> Bool)
-> (Labels -> Labels -> Bool)
-> (Labels -> Labels -> Bool)
-> (Labels -> Labels -> Labels)
-> (Labels -> Labels -> Labels)
-> Ord Labels
Labels -> Labels -> Bool
Labels -> Labels -> Ordering
Labels -> Labels -> Labels
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Labels -> Labels -> Ordering
compare :: Labels -> Labels -> Ordering
$c< :: Labels -> Labels -> Bool
< :: Labels -> Labels -> Bool
$c<= :: Labels -> Labels -> Bool
<= :: Labels -> Labels -> Bool
$c> :: Labels -> Labels -> Bool
> :: Labels -> Labels -> Bool
$c>= :: Labels -> Labels -> Bool
>= :: Labels -> Labels -> Bool
$cmax :: Labels -> Labels -> Labels
max :: Labels -> Labels -> Labels
$cmin :: Labels -> Labels -> Labels
min :: Labels -> Labels -> Labels
Ord)

labelsToList :: Labels -> [Label]
labelsToList :: Labels -> [Label]
labelsToList (Label
x :.: Labels
xs) = Label
x Label -> [Label] -> [Label]
forall a. a -> [a] -> [a]
: Labels -> [Label]
labelsToList Labels
xs
labelsToList Labels
Root       = [Label
""]

-- | Types that represent @<domain-name>@ as per [RFC 1035, section 3.3](https://tools.ietf.org/html/rfc1035#section-3.3) and can be converted to and from 'Labels'.
class IsLabels s where
  toLabels   :: s -> Maybe Labels
  fromLabels :: Labels -> s

instance IsLabels Labels where
  fromLabels :: Labels -> Labels
fromLabels = Labels -> Labels
forall a. a -> a
id

  toLabels :: Labels -> Maybe Labels
toLabels Labels
ls
    | (Label -> Bool) -> [Label] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Label -> Bool
isLabelValid ([Label] -> [Label]
forall a. HasCallStack => [a] -> [a]
init (Labels -> [Label]
labelsToList Labels
ls)) = Labels -> Maybe Labels
forall a. a -> Maybe a
Just Labels
ls
    | Bool
otherwise = Maybe Labels
forall a. Maybe a
Nothing
    where
      isLabelValid :: Label -> Bool
isLabelValid Label
l = Bool -> Bool
not (Label -> Bool
BS.null Label
l) Bool -> Bool -> Bool
&& Label -> Int
BS.length Label
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0x40

instance IsLabels Name where
  fromLabels :: Labels -> Name
fromLabels = Labels -> Name
labels2name
  toLabels :: Name -> Maybe Labels
toLabels   = Name -> Maybe Labels
name2labels

toName :: IsLabels n => n -> Maybe Name
toName :: forall n. IsLabels n => n -> Maybe Name
toName = (Labels -> Name) -> Maybe Labels -> Maybe Name
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Labels -> Name
forall s. IsLabels s => Labels -> s
fromLabels (Maybe Labels -> Maybe Name)
-> (n -> Maybe Labels) -> n -> Maybe Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. n -> Maybe Labels
forall s. IsLabels s => s -> Maybe Labels
toLabels

name2labels :: Name -> Maybe Labels
name2labels :: Name -> Maybe Labels
name2labels (Name Label
n)
  | (Label -> Bool) -> [Label] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (\Label
l -> Bool -> Bool
not (Label -> Bool
BS.null Label
l) Bool -> Bool -> Bool
&& Label -> Int
BS.length Label
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0x40) [Label]
n' = Labels -> Maybe Labels
forall a. a -> Maybe a
Just (Labels -> Maybe Labels) -> Labels -> Maybe Labels
forall a b. (a -> b) -> a -> b
$! (Label -> Labels -> Labels) -> Labels -> [Label] -> Labels
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Label -> Labels -> Labels
(:.:) Labels
Root [Label]
n'
  | Bool
otherwise = Maybe Labels
forall a. Maybe a
Nothing
  where
    n' :: [Label]
n' | Label -> Label -> Bool
BS.isSuffixOf Label
"." Label
n = Word8 -> Label -> [Label]
BS.split Word8
0x2e (HasCallStack => Label -> Label
Label -> Label
BS.init Label
n)
       | Bool
otherwise           = Word8 -> Label -> [Label]
BS.split Word8
0x2e Label
n

labels2name :: Labels -> Name
labels2name :: Labels -> Name
labels2name Labels
Root = Label -> Name
Name Label
"."
labels2name Labels
ls   = Label -> Name
Name (Label -> [Label] -> Label
BS.intercalate Label
"." ([Label] -> Label) -> [Label] -> Label
forall a b. (a -> b) -> a -> b
$ Labels -> [Label]
labelsToList Labels
ls)

-- | IOW, a domain-name
--
-- May contain pointers
--
-- Can be resolved into a 'Labels' without label ptrs.
data LabelsPtr = Label !Label !LabelsPtr -- ^ See RC2181: a label must be between 1-63 octets; can be arbitrary binary data
               | LPtr  !Word16
               | LNul
               deriving (LabelsPtr -> LabelsPtr -> Bool
(LabelsPtr -> LabelsPtr -> Bool)
-> (LabelsPtr -> LabelsPtr -> Bool) -> Eq LabelsPtr
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LabelsPtr -> LabelsPtr -> Bool
== :: LabelsPtr -> LabelsPtr -> Bool
$c/= :: LabelsPtr -> LabelsPtr -> Bool
/= :: LabelsPtr -> LabelsPtr -> Bool
Eq,ReadPrec [LabelsPtr]
ReadPrec LabelsPtr
Int -> ReadS LabelsPtr
ReadS [LabelsPtr]
(Int -> ReadS LabelsPtr)
-> ReadS [LabelsPtr]
-> ReadPrec LabelsPtr
-> ReadPrec [LabelsPtr]
-> Read LabelsPtr
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS LabelsPtr
readsPrec :: Int -> ReadS LabelsPtr
$creadList :: ReadS [LabelsPtr]
readList :: ReadS [LabelsPtr]
$creadPrec :: ReadPrec LabelsPtr
readPrec :: ReadPrec LabelsPtr
$creadListPrec :: ReadPrec [LabelsPtr]
readListPrec :: ReadPrec [LabelsPtr]
Read,Int -> LabelsPtr -> ShowS
[LabelsPtr] -> ShowS
LabelsPtr -> String
(Int -> LabelsPtr -> ShowS)
-> (LabelsPtr -> String)
-> ([LabelsPtr] -> ShowS)
-> Show LabelsPtr
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LabelsPtr -> ShowS
showsPrec :: Int -> LabelsPtr -> ShowS
$cshow :: LabelsPtr -> String
show :: LabelsPtr -> String
$cshowList :: [LabelsPtr] -> ShowS
showList :: [LabelsPtr] -> ShowS
Show)

labels2labelsPtr :: Labels -> LabelsPtr
labels2labelsPtr :: Labels -> LabelsPtr
labels2labelsPtr Labels
Root         = LabelsPtr
LNul
labels2labelsPtr (Label
l :.: Labels
rest) = Label -> LabelsPtr -> LabelsPtr
Label Label
l (Labels -> LabelsPtr
labels2labelsPtr Labels
rest)

instance Binary LabelsPtr where
    get :: Get LabelsPtr
get = [Label] -> Get LabelsPtr
go []
      where
        go :: [Label] -> Get LabelsPtr
go [Label]
acc = do
            Either Word16 Label
l0 <- Get (Either Word16 Label)
getLabel
            case Either Word16 Label
l0 of
              Right Label
bs | Label -> Bool
BS.null Label
bs -> LabelsPtr -> Get LabelsPtr
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Label -> LabelsPtr -> LabelsPtr)
-> LabelsPtr -> [Label] -> LabelsPtr
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Label -> LabelsPtr -> LabelsPtr
Label LabelsPtr
LNul ([Label] -> LabelsPtr) -> [Label] -> LabelsPtr
forall a b. (a -> b) -> a -> b
$ [Label] -> [Label]
forall a. [a] -> [a]
reverse [Label]
acc)
                       | Bool
otherwise  -> [Label] -> Get LabelsPtr
go (Label
bsLabel -> [Label] -> [Label]
forall a. a -> [a] -> [a]
:[Label]
acc)
              Left Word16
ofs              -> LabelsPtr -> Get LabelsPtr
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Label -> LabelsPtr -> LabelsPtr)
-> LabelsPtr -> [Label] -> LabelsPtr
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Label -> LabelsPtr -> LabelsPtr
Label (Word16 -> LabelsPtr
LPtr Word16
ofs) ([Label] -> LabelsPtr) -> [Label] -> LabelsPtr
forall a b. (a -> b) -> a -> b
$ [Label] -> [Label]
forall a. [a] -> [a]
reverse [Label]
acc)

        getLabel :: Get (Either Word16 BS.ByteString)
        getLabel :: Get (Either Word16 Label)
getLabel = do
            Word8
len <- Get Word8
getWord8

            if Word8
len Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word8
0x40
             then do
                Bool -> Get () -> Get ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Word8
len Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0xc0 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
0xc0) (Get () -> Get ()) -> Get () -> Get ()
forall a b. (a -> b) -> a -> b
$ String -> Get ()
forall a. String -> Get a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
"invalid length octet " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Word8 -> String
forall a. Show a => a -> String
show Word8
len)
                Word16
ofs <- Word8 -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> Word16) -> Get Word8 -> Get Word16
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word8
getWord8
                Either Word16 Label -> Get (Either Word16 Label)
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Word16 Label -> Get (Either Word16 Label))
-> Either Word16 Label -> Get (Either Word16 Label)
forall a b. (a -> b) -> a -> b
$ Word16 -> Either Word16 Label
forall a b. a -> Either a b
Left (Word16 -> Either Word16 Label) -> Word16 -> Either Word16 Label
forall a b. (a -> b) -> a -> b
$ (Word8 -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
len Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x3f) Word16 -> Int -> Word16
forall a. Bits a => a -> Int -> a
`shiftL` Int
8) Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
.|. Word16
ofs
             else Label -> Either Word16 Label
forall a b. b -> Either a b
Right (Label -> Either Word16 Label)
-> Get Label -> Get (Either Word16 Label)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Get Label
getByteString (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
len)

    put :: LabelsPtr -> Put
put LabelsPtr
LNul = Word8 -> Put
putWord8 Word8
0
    put (Label Label
l LabelsPtr
next)
      | Label -> Int
BS.length Label
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
1 Bool -> Bool -> Bool
|| Label -> Int
BS.length Label
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0x40 = String -> Put
forall a. HasCallStack => String -> a
error String
"put (Label {}): invalid label size"
      | Bool
otherwise = do
            Word8 -> Put
putWord8 (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Label -> Int
BS.length Label
l))
            Label -> Put
putByteString Label
l
            LabelsPtr -> Put
forall t. Binary t => t -> Put
put LabelsPtr
next
    put (LPtr Word16
ofs)
      | Word16
ofs Word16 -> Word16 -> Bool
forall a. Ord a => a -> a -> Bool
< Word16
0x4000 = Word16 -> Put
putWord16be (Word16
0xc000 Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
.|. Word16
ofs)
      | Bool
otherwise  = String -> Put
forall a. HasCallStack => String -> a
error String
"put (LPtr {}): invalid offset"

-- | Compute serialised size of 'LabelsPtr'
labelsSize :: LabelsPtr -> Word16
labelsSize :: LabelsPtr -> Word16
labelsSize = Int -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word16) -> (LabelsPtr -> Int) -> LabelsPtr -> Word16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> LabelsPtr -> Int
go Int
0
  where
    go :: Int -> LabelsPtr -> Int
go Int
n (LPtr Word16
_)        = Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
2
    go Int
n  LabelsPtr
LNul           = Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1
    go Int
n (Label Label
bs LabelsPtr
rest) = Int -> LabelsPtr -> Int
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Label -> Int
BS.length Label
bs) LabelsPtr
rest

-- | Extract pointer-offset from 'LabelsPtr' (if it exists)
labelsPtr :: LabelsPtr -> Maybe Word16
labelsPtr :: LabelsPtr -> Maybe Word16
labelsPtr (Label Label
_ LabelsPtr
ls) = LabelsPtr -> Maybe Word16
labelsPtr LabelsPtr
ls
labelsPtr LabelsPtr
LNul         = Maybe Word16
forall a. Maybe a
Nothing
labelsPtr (LPtr Word16
ofs)   = Word16 -> Maybe Word16
forall a. a -> Maybe a
Just Word16
ofs

----------------------------------------------------------------------------

instance Binary l => Binary (MsgQuestion l) where
    get :: Get (MsgQuestion l)
get = l -> Type -> Class -> MsgQuestion l
forall l. l -> Type -> Class -> MsgQuestion l
MsgQuestion (l -> Type -> Class -> MsgQuestion l)
-> Get l -> Get (Type -> Class -> MsgQuestion l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get l
forall t. Binary t => Get t
get Get (Type -> Class -> MsgQuestion l)
-> Get Type -> Get (Class -> MsgQuestion l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Type
forall t. Binary t => Get t
get Get (Class -> MsgQuestion l) -> Get Class -> Get (MsgQuestion l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Class
forall t. Binary t => Get t
get
    put :: MsgQuestion l -> Put
put (MsgQuestion l
l Type
qt Class
cls) = l -> Put
forall t. Binary t => t -> Put
put l
l Put -> Put -> Put
forall a b. PutM a -> PutM b -> PutM b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Type -> Put
forall t. Binary t => t -> Put
put Type
qt Put -> Put -> Put
forall a b. PutM a -> PutM b -> PutM b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Class -> Put
forall t. Binary t => t -> Put
put Class
cls


instance Binary l => Binary (MsgRR l) where
    get :: Get (MsgRR l)
get = do
        l
rrName  <- Get l
forall t. Binary t => Get t
get
        Type
rrType  <- Get Type
forall t. Binary t => Get t
get
        Class
rrClass <- Get Class
forall t. Binary t => Get t
get
        TTL
rrTTL   <- Get TTL
forall t. Binary t => Get t
get
        RData l
rrData  <- Type -> Get (RData l)
forall l. Binary l => Type -> Get (RData l)
getRData Type
rrType
        MsgRR l -> Get (MsgRR l)
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MsgRR {l
TTL
Class
RData l
rrName :: l
rrClass :: Class
rrTTL :: TTL
rrData :: RData l
rrName :: l
rrClass :: Class
rrTTL :: TTL
rrData :: RData l
..})

    put :: MsgRR l -> Put
put (MsgRR{l
TTL
Class
RData l
rrName :: forall l. MsgRR l -> l
rrClass :: forall l. MsgRR l -> Class
rrTTL :: forall l. MsgRR l -> TTL
rrData :: forall l. MsgRR l -> RData l
rrName :: l
rrClass :: Class
rrTTL :: TTL
rrData :: RData l
..}) = do
        l -> Put
forall t. Binary t => t -> Put
put         l
rrName
        Type -> Put
forall t. Binary t => t -> Put
put         ((Type -> Type) -> (TypeSym -> Type) -> Either Type TypeSym -> Type
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Type -> Type
forall a. a -> a
id TypeSym -> Type
typeFromSym (Either Type TypeSym -> Type) -> Either Type TypeSym -> Type
forall a b. (a -> b) -> a -> b
$ RData l -> Either Type TypeSym
forall l. RData l -> Either Type TypeSym
rdType RData l
rrData)
        Class -> Put
forall t. Binary t => t -> Put
put         Class
rrClass
        TTL -> Put
forall t. Binary t => t -> Put
put         TTL
rrTTL
        RData l -> Put
forall l. Binary l => RData l -> Put
putRData    RData l
rrData

getRData :: Binary l => Type -> Get (RData l)
getRData :: forall l. Binary l => Type -> Get (RData l)
getRData Type
qt = do
    Int
len     <- Word16 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word16 -> Int) -> Get Word16 -> Get Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word16
getWord16be

    let unknownRdata :: Get (RData l)
unknownRdata = Type -> Label -> RData l
forall l. Type -> Label -> RData l
RData Type
qt (Label -> RData l) -> Get Label -> Get (RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Get Label
getByteString Int
len

        getByteStringRest :: Get Label
getByteStringRest = (Int -> Get Label) -> Get Label
forall {b}. (Int -> Get b) -> Get b
consumeRestWith Int -> Get Label
getByteString

        consumeRestWith :: (Int -> Get b) -> Get b
consumeRestWith Int -> Get b
act = do
            Int
curofs <- ByteOffset -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset -> Int) -> Get ByteOffset -> Get Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get ByteOffset
bytesRead
            Int -> Get b
act (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
curofs)

    Int -> Get (RData l) -> Get (RData l)
forall a. Int -> Get a -> Get a
isolate Int
len (Get (RData l) -> Get (RData l)) -> Get (RData l) -> Get (RData l)
forall a b. (a -> b) -> a -> b
$
      case Type -> Maybe TypeSym
typeToSym Type
qt of
        Maybe TypeSym
Nothing -> Get (RData l)
forall {l}. Get (RData l)
unknownRdata
        Just TypeSym
ts -> case TypeSym
ts of
          TypeSym
TypeA      -> IPv4 -> RData l
forall l. IPv4 -> RData l
RDataA      (IPv4 -> RData l) -> Get IPv4 -> Get (RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get IPv4
forall t. Binary t => Get t
get

          TypeSym
TypeAFSDB  -> Word16 -> l -> RData l
forall l. Word16 -> l -> RData l
RDataAFSDB  (Word16 -> l -> RData l) -> Get Word16 -> Get (l -> RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word16
getWord16be
                                    Get (l -> RData l) -> Get l -> Get (RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get l
forall t. Binary t => Get t
get

          TypeSym
TypeNS     -> l -> RData l
forall l. l -> RData l
RDataNS     (l -> RData l) -> Get l -> Get (RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get l
forall t. Binary t => Get t
get

          TypeSym
TypeCNAME  -> l -> RData l
forall l. l -> RData l
RDataCNAME  (l -> RData l) -> Get l -> Get (RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get l
forall t. Binary t => Get t
get

          TypeSym
TypeSOA    -> l -> l -> Word32 -> Word32 -> Word32 -> Word32 -> Word32 -> RData l
forall l.
l -> l -> Word32 -> Word32 -> Word32 -> Word32 -> Word32 -> RData l
RDataSOA    (l
 -> l -> Word32 -> Word32 -> Word32 -> Word32 -> Word32 -> RData l)
-> Get l
-> Get
     (l -> Word32 -> Word32 -> Word32 -> Word32 -> Word32 -> RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get l
forall t. Binary t => Get t
get
                                    Get
  (l -> Word32 -> Word32 -> Word32 -> Word32 -> Word32 -> RData l)
-> Get l
-> Get (Word32 -> Word32 -> Word32 -> Word32 -> Word32 -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get l
forall t. Binary t => Get t
get
                                    Get (Word32 -> Word32 -> Word32 -> Word32 -> Word32 -> RData l)
-> Get Word32
-> Get (Word32 -> Word32 -> Word32 -> Word32 -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word32
getWord32be
                                    Get (Word32 -> Word32 -> Word32 -> Word32 -> RData l)
-> Get Word32 -> Get (Word32 -> Word32 -> Word32 -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word32
getWord32be
                                    Get (Word32 -> Word32 -> Word32 -> RData l)
-> Get Word32 -> Get (Word32 -> Word32 -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word32
getWord32be
                                    Get (Word32 -> Word32 -> RData l)
-> Get Word32 -> Get (Word32 -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word32
getWord32be
                                    Get (Word32 -> RData l) -> Get Word32 -> Get (RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word32
getWord32be

          TypeSym
TypePTR    -> l -> RData l
forall l. l -> RData l
RDataPTR    (l -> RData l) -> Get l -> Get (RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get l
forall t. Binary t => Get t
get

          TypeSym
TypeHINFO  -> CharStr -> CharStr -> RData l
forall l. CharStr -> CharStr -> RData l
RDataHINFO  (CharStr -> CharStr -> RData l)
-> Get CharStr -> Get (CharStr -> RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get CharStr
forall t. Binary t => Get t
get
                                    Get (CharStr -> RData l) -> Get CharStr -> Get (RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get CharStr
forall t. Binary t => Get t
get

          TypeSym
TypeMX     -> Word16 -> l -> RData l
forall l. Word16 -> l -> RData l
RDataMX     (Word16 -> l -> RData l) -> Get Word16 -> Get (l -> RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word16
getWord16be
                                    Get (l -> RData l) -> Get l -> Get (RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get l
forall t. Binary t => Get t
get

          TypeSym
TypeTXT    -> [CharStr] -> RData l
forall l. [CharStr] -> RData l
RDataTXT    ([CharStr] -> RData l) -> Get [CharStr] -> Get (RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get [CharStr]
forall a. Binary a => Get [a]
getUntilEmpty
          TypeSym
TypeSPF    -> [CharStr] -> RData l
forall l. [CharStr] -> RData l
RDataSPF    ([CharStr] -> RData l) -> Get [CharStr] -> Get (RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get [CharStr]
forall a. Binary a => Get [a]
getUntilEmpty

          TypeSym
TypeAAAA   -> IPv6 -> RData l
forall l. IPv6 -> RData l
RDataAAAA   (IPv6 -> RData l) -> Get IPv6 -> Get (RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get IPv6
forall t. Binary t => Get t
get

          TypeSym
TypeSRV    -> SRV l -> RData l
forall l. SRV l -> RData l
RDataSRV    (SRV l -> RData l) -> Get (SRV l) -> Get (RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get (SRV l)
forall t. Binary t => Get t
get

          TypeSym
TypeNAPTR  -> Word16 -> Word16 -> CharStr -> CharStr -> CharStr -> l -> RData l
forall l.
Word16 -> Word16 -> CharStr -> CharStr -> CharStr -> l -> RData l
RDataNAPTR  (Word16 -> Word16 -> CharStr -> CharStr -> CharStr -> l -> RData l)
-> Get Word16
-> Get (Word16 -> CharStr -> CharStr -> CharStr -> l -> RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word16
getWord16be -- order
                                    Get (Word16 -> CharStr -> CharStr -> CharStr -> l -> RData l)
-> Get Word16
-> Get (CharStr -> CharStr -> CharStr -> l -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word16
getWord16be --preference
                                    Get (CharStr -> CharStr -> CharStr -> l -> RData l)
-> Get CharStr -> Get (CharStr -> CharStr -> l -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get CharStr
forall t. Binary t => Get t
get -- flags
                                    Get (CharStr -> CharStr -> l -> RData l)
-> Get CharStr -> Get (CharStr -> l -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get CharStr
forall t. Binary t => Get t
get -- services
                                    Get (CharStr -> l -> RData l) -> Get CharStr -> Get (l -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get CharStr
forall t. Binary t => Get t
get -- regexp
                                    Get (l -> RData l) -> Get l -> Get (RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get l
forall t. Binary t => Get t
get -- replacement

          TypeSym
TypeRRSIG  -> Word16
-> Word8
-> Word8
-> Word32
-> Word32
-> Word32
-> Word16
-> l
-> Label
-> RData l
forall l.
Word16
-> Word8
-> Word8
-> Word32
-> Word32
-> Word32
-> Word16
-> l
-> Label
-> RData l
RDataRRSIG  (Word16
 -> Word8
 -> Word8
 -> Word32
 -> Word32
 -> Word32
 -> Word16
 -> l
 -> Label
 -> RData l)
-> Get Word16
-> Get
     (Word8
      -> Word8
      -> Word32
      -> Word32
      -> Word32
      -> Word16
      -> l
      -> Label
      -> RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word16
getWord16be
                                    Get
  (Word8
   -> Word8
   -> Word32
   -> Word32
   -> Word32
   -> Word16
   -> l
   -> Label
   -> RData l)
-> Get Word8
-> Get
     (Word8
      -> Word32 -> Word32 -> Word32 -> Word16 -> l -> Label -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word8
getWord8
                                    Get
  (Word8
   -> Word32 -> Word32 -> Word32 -> Word16 -> l -> Label -> RData l)
-> Get Word8
-> Get
     (Word32 -> Word32 -> Word32 -> Word16 -> l -> Label -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word8
getWord8
                                    Get (Word32 -> Word32 -> Word32 -> Word16 -> l -> Label -> RData l)
-> Get Word32
-> Get (Word32 -> Word32 -> Word16 -> l -> Label -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word32
getWord32be
                                    Get (Word32 -> Word32 -> Word16 -> l -> Label -> RData l)
-> Get Word32 -> Get (Word32 -> Word16 -> l -> Label -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word32
getWord32be
                                    Get (Word32 -> Word16 -> l -> Label -> RData l)
-> Get Word32 -> Get (Word16 -> l -> Label -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word32
getWord32be
                                    Get (Word16 -> l -> Label -> RData l)
-> Get Word16 -> Get (l -> Label -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word16
getWord16be
                                    Get (l -> Label -> RData l) -> Get l -> Get (Label -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get l
forall t. Binary t => Get t
get -- uncompressed
                                    Get (Label -> RData l) -> Get Label -> Get (RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Label
getByteStringRest

          TypeSym
TypeDNSKEY -> Word16 -> Word8 -> Word8 -> Label -> RData l
forall l. Word16 -> Word8 -> Word8 -> Label -> RData l
RDataDNSKEY (Word16 -> Word8 -> Word8 -> Label -> RData l)
-> Get Word16 -> Get (Word8 -> Word8 -> Label -> RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word16
getWord16be
                                    Get (Word8 -> Word8 -> Label -> RData l)
-> Get Word8 -> Get (Word8 -> Label -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word8
getWord8
                                    Get (Word8 -> Label -> RData l)
-> Get Word8 -> Get (Label -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word8
getWord8
                                    Get (Label -> RData l) -> Get Label -> Get (RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Int -> Get Label
getByteString (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
4)

          TypeSym
TypeDS     -> Word16 -> Word8 -> Word8 -> Label -> RData l
forall l. Word16 -> Word8 -> Word8 -> Label -> RData l
RDataDS     (Word16 -> Word8 -> Word8 -> Label -> RData l)
-> Get Word16 -> Get (Word8 -> Word8 -> Label -> RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word16
getWord16be
                                    Get (Word8 -> Word8 -> Label -> RData l)
-> Get Word8 -> Get (Word8 -> Label -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word8
getWord8
                                    Get (Word8 -> Label -> RData l)
-> Get Word8 -> Get (Label -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word8
getWord8
                                    Get (Label -> RData l) -> Get Label -> Get (RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Int -> Get Label
getByteString (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
4)

          TypeSym
TypeNSEC   -> l -> Set Type -> RData l
forall l. l -> Set Type -> RData l
RDataNSEC   (l -> Set Type -> RData l) -> Get l -> Get (Set Type -> RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get l
forall t. Binary t => Get t
get
                                    Get (Set Type -> RData l) -> Get (Set Type) -> Get (RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get (Set Type)
decodeNsecTypeMap

          TypeSym
TypeURI    -> Word16 -> Word16 -> Label -> RData l
forall l. Word16 -> Word16 -> Label -> RData l
RDataURI    (Word16 -> Word16 -> Label -> RData l)
-> Get Word16 -> Get (Word16 -> Label -> RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word16
getWord16be -- prio
                                    Get (Word16 -> Label -> RData l)
-> Get Word16 -> Get (Label -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word16
getWord16be -- weight
                                    Get (Label -> RData l) -> Get Label -> Get (RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Int -> Get Label
getByteString (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
4)

          TypeSym
TypeSSHFP  -> Word8 -> Word8 -> Label -> RData l
forall l. Word8 -> Word8 -> Label -> RData l
RDataSSHFP  (Word8 -> Word8 -> Label -> RData l)
-> Get Word8 -> Get (Word8 -> Label -> RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word8
getWord8
                                    Get (Word8 -> Label -> RData l)
-> Get Word8 -> Get (Label -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word8
getWord8
                                    Get (Label -> RData l) -> Get Label -> Get (RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Int -> Get Label
getByteString (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2)

          TypeSym
TypeNSEC3PARAM -> Word8 -> Word8 -> Word16 -> CharStr -> RData l
forall l. Word8 -> Word8 -> Word16 -> CharStr -> RData l
RDataNSEC3PARAM (Word8 -> Word8 -> Word16 -> CharStr -> RData l)
-> Get Word8 -> Get (Word8 -> Word16 -> CharStr -> RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word8
getWord8
                                            Get (Word8 -> Word16 -> CharStr -> RData l)
-> Get Word8 -> Get (Word16 -> CharStr -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word8
getWord8
                                            Get (Word16 -> CharStr -> RData l)
-> Get Word16 -> Get (CharStr -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word16
getWord16be
                                            Get (CharStr -> RData l) -> Get CharStr -> Get (RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get CharStr
forall t. Binary t => Get t
get -- salt

          TypeSym
TypeNSEC3      -> Word8
-> Word8 -> Word16 -> CharStr -> CharStr -> Set Type -> RData l
forall l.
Word8
-> Word8 -> Word16 -> CharStr -> CharStr -> Set Type -> RData l
RDataNSEC3      (Word8
 -> Word8 -> Word16 -> CharStr -> CharStr -> Set Type -> RData l)
-> Get Word8
-> Get
     (Word8 -> Word16 -> CharStr -> CharStr -> Set Type -> RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word8
getWord8
                                            Get (Word8 -> Word16 -> CharStr -> CharStr -> Set Type -> RData l)
-> Get Word8
-> Get (Word16 -> CharStr -> CharStr -> Set Type -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word8
getWord8
                                            Get (Word16 -> CharStr -> CharStr -> Set Type -> RData l)
-> Get Word16 -> Get (CharStr -> CharStr -> Set Type -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word16
getWord16be
                                            Get (CharStr -> CharStr -> Set Type -> RData l)
-> Get CharStr -> Get (CharStr -> Set Type -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get CharStr
forall t. Binary t => Get t
get -- salt
                                            Get (CharStr -> Set Type -> RData l)
-> Get CharStr -> Get (Set Type -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get CharStr
forall t. Binary t => Get t
get -- next hashed owner name
                                            Get (Set Type -> RData l) -> Get (Set Type) -> Get (RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get (Set Type)
decodeNsecTypeMap

          TypeSym
TypeCAA        -> Word8 -> CharStr -> Label -> RData l
forall l. Word8 -> CharStr -> Label -> RData l
RDataCAA        (Word8 -> CharStr -> Label -> RData l)
-> Get Word8 -> Get (CharStr -> Label -> RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word8
getWord8 -- flags
                                            Get (CharStr -> Label -> RData l)
-> Get CharStr -> Get (Label -> RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get CharStr
forall t. Binary t => Get t
get -- tag -- TODO: must be non-empty
                                            Get (Label -> RData l) -> Get Label -> Get (RData l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Label
getByteStringRest

          TypeSym
TypeOPT -> Label -> RData l
forall l. Label -> RData l
RDataOPT (Label -> RData l) -> Get Label -> Get (RData l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Get Label
getByteString Int
len -- FIXME

          TypeSym
TypeANY    -> Get (RData l)
forall {l}. Get (RData l)
unknownRdata -- shouldn't happen

putRData :: Binary l => RData l -> Put
putRData :: forall l. Binary l => RData l -> Put
putRData RData l
rd = do
    let rdata :: ByteString
rdata = Put -> ByteString
runPut (RData l -> Put
forall l. Binary l => RData l -> Put
putRData' RData l
rd)
        rdataLen :: ByteOffset
rdataLen = ByteString -> ByteOffset
BSL.length ByteString
rdata

    Bool -> Put -> Put
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteOffset
rdataLen ByteOffset -> ByteOffset -> Bool
forall a. Ord a => a -> a -> Bool
< ByteOffset
0x10000) (Put -> Put) -> Put -> Put
forall a b. (a -> b) -> a -> b
$
        String -> Put
forall a. HasCallStack => String -> a
error String
"rdata too large"

    Word16 -> Put
putWord16be (ByteOffset -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteOffset
rdataLen)
    ByteString -> Put
putLazyByteString ByteString
rdata

putRData' :: Binary l => RData l -> Put
putRData' :: forall l. Binary l => RData l -> Put
putRData' RData l
rd = case RData l
rd of
  RDataA IPv4
ip4 -> IPv4 -> Put
forall t. Binary t => t -> Put
put IPv4
ip4
  RDataAAAA IPv6
ip6 -> IPv6 -> Put
forall t. Binary t => t -> Put
put IPv6
ip6
  RDataCNAME l
cname -> l -> Put
forall t. Binary t => t -> Put
put l
cname
  RDataOPT Label
d -> Label -> Put
putByteString Label
d
  RDataMX Word16
prio l
l -> Word16 -> Put
putWord16be Word16
prio Put -> Put -> Put
forall a b. PutM a -> PutM b -> PutM b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> l -> Put
forall t. Binary t => t -> Put
put l
l
  RDataSOA l
l1 l
l2 Word32
w1 Word32
w2 Word32
w3 Word32
w4 Word32
w5 -> do
      l -> Put
forall t. Binary t => t -> Put
put l
l1
      l -> Put
forall t. Binary t => t -> Put
put l
l2
      Word32 -> Put
putWord32be Word32
w1
      Word32 -> Put
putWord32be Word32
w2
      Word32 -> Put
putWord32be Word32
w3
      Word32 -> Put
putWord32be Word32
w4
      Word32 -> Put
putWord32be Word32
w5

  RDataPTR l
l -> l -> Put
forall t. Binary t => t -> Put
put l
l
  RDataNS  l
l -> l -> Put
forall t. Binary t => t -> Put
put l
l
  RDataTXT [CharStr]
ss -> (CharStr -> Put) -> [CharStr] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ CharStr -> Put
forall t. Binary t => t -> Put
put [CharStr]
ss
  RDataSPF [CharStr]
ss -> (CharStr -> Put) -> [CharStr] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ CharStr -> Put
forall t. Binary t => t -> Put
put [CharStr]
ss
  RDataSRV SRV l
srv -> SRV l -> Put
forall t. Binary t => t -> Put
put SRV l
srv

  RDataAFSDB Word16
w l
l -> Word16 -> Put
putWord16be Word16
w Put -> Put -> Put
forall a b. PutM a -> PutM b -> PutM b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> l -> Put
forall t. Binary t => t -> Put
put l
l

  RDataHINFO CharStr
s1 CharStr
s2 -> CharStr -> Put
forall t. Binary t => t -> Put
put CharStr
s1 Put -> Put -> Put
forall a b. PutM a -> PutM b -> PutM b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> CharStr -> Put
forall t. Binary t => t -> Put
put CharStr
s2

  RDataRRSIG Word16
w1 Word8
w2 Word8
w3 Word32
w4 Word32
w5 Word32
w6 Word16
w7 l
l Label
s -> do
      Word16 -> Put
putWord16be Word16
w1
      Word8 -> Put
putWord8    Word8
w2
      Word8 -> Put
putWord8    Word8
w3
      Word32 -> Put
putWord32be Word32
w4
      Word32 -> Put
putWord32be Word32
w5
      Word32 -> Put
putWord32be Word32
w6
      Word16 -> Put
putWord16be Word16
w7
      l -> Put
forall t. Binary t => t -> Put
put l
l
      Label -> Put
putByteString Label
s

  RDataDNSKEY Word16
w1 Word8
w2 Word8
w3 Label
s -> do
      Word16 -> Put
putWord16be Word16
w1
      Word8 -> Put
putWord8    Word8
w2
      Word8 -> Put
putWord8    Word8
w3
      Label -> Put
putByteString Label
s

  RDataNSEC3PARAM Word8
w1 Word8
w2 Word16
w3 CharStr
s -> do
      Word8 -> Put
putWord8 Word8
w1
      Word8 -> Put
putWord8 Word8
w2
      Word16 -> Put
putWord16be Word16
w3
      CharStr -> Put
forall t. Binary t => t -> Put
put CharStr
s

  RDataNSEC3 Word8
w1 Word8
w2 Word16
w3 CharStr
s1 CharStr
s2 Set Type
tm -> do
      Word8 -> Put
putWord8 Word8
w1
      Word8 -> Put
putWord8 Word8
w2
      Word16 -> Put
putWord16be Word16
w3
      CharStr -> Put
forall t. Binary t => t -> Put
put CharStr
s1
      CharStr -> Put
forall t. Binary t => t -> Put
put CharStr
s2
      Set Type -> Put
encodeNsecTypeMap Set Type
tm

  RDataCAA Word8
fl CharStr
s1 Label
s2 -> do
      Word8 -> Put
putWord8 Word8
fl
      CharStr -> Put
forall t. Binary t => t -> Put
put CharStr
s1
      Label -> Put
putByteString Label
s2

  RDataURI Word16
w1 Word16
w2 Label
s -> do
      Word16 -> Put
putWord16be Word16
w1
      Word16 -> Put
putWord16be Word16
w2
      Label -> Put
putByteString Label
s

  RDataDS Word16
w1 Word8
w2 Word8
w3 Label
s -> do
      Word16 -> Put
putWord16be Word16
w1
      Word8 -> Put
putWord8 Word8
w2
      Word8 -> Put
putWord8 Word8
w3
      Label -> Put
putByteString Label
s

  RDataNSEC l
l Set Type
tm -> do
      l -> Put
forall t. Binary t => t -> Put
put l
l
      Set Type -> Put
encodeNsecTypeMap Set Type
tm

  RDataNAPTR Word16
w1 Word16
w2 CharStr
s1 CharStr
s2 CharStr
s3 l
l -> do
      Word16 -> Put
putWord16be Word16
w1
      Word16 -> Put
putWord16be Word16
w2
      CharStr -> Put
forall t. Binary t => t -> Put
put CharStr
s1
      CharStr -> Put
forall t. Binary t => t -> Put
put CharStr
s2
      CharStr -> Put
forall t. Binary t => t -> Put
put CharStr
s3
      l -> Put
forall t. Binary t => t -> Put
put l
l

  RDataSSHFP Word8
w1 Word8
w2 Label
s -> do
      Word8 -> Put
putWord8 Word8
w1
      Word8 -> Put
putWord8 Word8
w2
      Label -> Put
putByteString Label
s

  RData Type
_ Label
raw -> Label -> Put
putByteString Label
raw

  -- _ -> error ("putRData: " ++ show rd)


instance Binary l => Binary (SRV l) where
    get :: Get (SRV l)
get = Word16 -> Word16 -> Word16 -> l -> SRV l
forall l. Word16 -> Word16 -> Word16 -> l -> SRV l
SRV (Word16 -> Word16 -> Word16 -> l -> SRV l)
-> Get Word16 -> Get (Word16 -> Word16 -> l -> SRV l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word16
getWord16be
              Get (Word16 -> Word16 -> l -> SRV l)
-> Get Word16 -> Get (Word16 -> l -> SRV l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word16
getWord16be
              Get (Word16 -> l -> SRV l) -> Get Word16 -> Get (l -> SRV l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Word16
getWord16be
              Get (l -> SRV l) -> Get l -> Get (SRV l)
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get l
forall t. Binary t => Get t
get

    put :: SRV l -> Put
put (SRV Word16
w1 Word16
w2 Word16
w3 l
l) = do
      Word16 -> Put
putWord16be Word16
w1
      Word16 -> Put
putWord16be Word16
w2
      Word16 -> Put
putWord16be Word16
w3
      l -> Put
forall t. Binary t => t -> Put
put l
l

{- NSEC type-bitmap example:

 A NS SOA TXT AAAA RRSIG NSEC DNSKEY

'00 07 62 00 80 08 00 03 80'
'00000000 00000111 01100010 00000000 10000000 00001000 00000000 00000011 10000000'
 Win=#0    len=7         ^{SOA}      ^{TXT}       ^{AAAA}                ^{DNSKEY}
                    ^^{A,NS}                                          ^^{RRSIG,NSEC}
-}

decodeNsecTypeMap :: Get (Set Type)
decodeNsecTypeMap :: Get (Set Type)
decodeNsecTypeMap = do
    [Type]
r <- [[Type]] -> [Type]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Type]] -> [Type]) -> Get [[Type]] -> Get [Type]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get [Type] -> Get [[Type]]
forall a. Get a -> Get [a]
untilEmptyWith Get [Type]
decode1
    -- TODO: enforce uniqueness
    Set Type -> Get (Set Type)
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Type] -> Set Type
forall a. Ord a => [a] -> Set a
Set.fromList [Type]
r)
  where
    -- decode single window
    decode1 :: Get [Type]
decode1 = do
        Word8
wi <- Get Word8
getWord8
        Word8
l  <- Get Word8
getWord8
        Bool -> Get () -> Get ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Word8
0 Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
< Word8
l Bool -> Bool -> Bool
&& Word8
l Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word8
32) (Get () -> Get ()) -> Get () -> Get ()
forall a b. (a -> b) -> a -> b
$
            String -> Get ()
forall a. String -> Get a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"invalid bitmap length"

        Label
bmap <- Int -> Get Label
getByteString (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
l)

        let winofs :: Int
winofs = (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
wi)Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
0x100 :: Int
            lst :: [Type]
lst = [ Word16 -> Type
Type (Int -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
winofsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
jInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
8Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
7Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
i))
                  | (Int
j,Word8
x) <- [Int] -> [Word8] -> [(Int, Word8)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..] (Label -> [Word8]
BS.unpack Label
bmap)
                  , Int
i <- [Int
7,Int
6..Int
0]
                  , Word8 -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Word8
x Int
i ]

        [Type] -> Get [Type]
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Type]
lst

encodeNsecTypeMap :: Set Type -> Put
encodeNsecTypeMap :: Set Type -> Put
encodeNsecTypeMap Set Type
bmap = do
    Bool -> Put -> Put
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Set Type -> Bool
forall a. Set a -> Bool
Set.null Set Type
bmap) (Put -> Put) -> Put -> Put
forall a b. (a -> b) -> a -> b
$ String -> Put
forall a. HasCallStack => String -> a
error String
"invalid empty type-map"
    -- when (Set.member 0 bmap) $ fail "invalid TYPE0 set in type-map"
    -- TODO: verify that Meta-TYPES and QTYPEs aren't contained in bmap

    [(Word8, [Word8])] -> ((Word8, [Word8]) -> Put) -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Map Word8 [Word8] -> [(Word8, [Word8])]
forall k a. Map k a -> [(k, a)]
Map.toList Map Word8 [Word8]
bmap') (((Word8, [Word8]) -> Put) -> Put)
-> ((Word8, [Word8]) -> Put) -> Put
forall a b. (a -> b) -> a -> b
$ \(Word8
wi, [Word8]
tm) -> do
        Word8 -> Put
putWord8 Word8
wi
        CharStr -> Put
forall t. Binary t => t -> Put
put (Label -> CharStr
CharStr (Label -> CharStr) -> Label -> CharStr
forall a b. (a -> b) -> a -> b
$ [Word8] -> Label
BS.pack [Word8]
tm)
  where
    bmap' :: Map Word8 [Word8]
bmap' = (Set Word8 -> [Word8])
-> Map Word8 (Set Word8) -> Map Word8 [Word8]
forall a b. (a -> b) -> Map Word8 a -> Map Word8 b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Set Word8 -> [Word8]
set2bitmap (Map Word8 (Set Word8) -> Map Word8 [Word8])
-> (Set Word16 -> Map Word8 (Set Word8))
-> Set Word16
-> Map Word8 [Word8]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set Word16 -> Map Word8 (Set Word8)
splitToBlocks (Set Word16 -> Map Word8 [Word8])
-> Set Word16 -> Map Word8 [Word8]
forall a b. (a -> b) -> a -> b
$ (Type -> Word16) -> Set Type -> Set Word16
forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map (\(Type Word16
w)->Word16
w) Set Type
bmap

set2bitmap :: Set Word8 -> [Word8]
set2bitmap :: Set Word8 -> [Word8]
set2bitmap = Word8 -> Word8 -> [Word8] -> [Word8]
forall {t} {a}. (Bits t, Integral a, Num t) => a -> t -> [a] -> [t]
go Word8
0 Word8
0 ([Word8] -> [Word8])
-> (Set Word8 -> [Word8]) -> Set Word8 -> [Word8]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set Word8 -> [Word8]
forall a. Set a -> [a]
Set.toList
  where
    go :: a -> t -> [a] -> [t]
go a
_ t
acc [] = if t
acc t -> t -> Bool
forall a. Eq a => a -> a -> Bool
== t
0 then [] else [t
acc]
    go a
j t
acc (a
i:[a]
is)
      | a
j'  a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
j  = t
acc t -> [t] -> [t]
forall a. a -> [a] -> [a]
: a -> t -> [a] -> [t]
go (a
ja -> a -> a
forall a. Num a => a -> a -> a
+a
1) t
0 (a
ia -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
is)
      | a
j' a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
j  = a -> t -> [a] -> [t]
go a
j' (t
acc t -> t -> t
forall a. Bits a => a -> a -> a
.|. Int -> t
forall a. Bits a => Int -> a
bit (Int
7 Int -> Int -> Int
forall a. Num a => a -> a -> a
- a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
i')) [a]
is
      | Bool
otherwise = String -> [t]
forall a. HasCallStack => String -> a
error String
"set2bitmap: the impossible happened"
      where
        (a
j',a
i') = a
i a -> a -> (a, a)
forall a. Integral a => a -> a -> (a, a)
`quotRem` a
8

splitToBlocks :: Set Word16 -> Map Word8 (Set Word8)
splitToBlocks :: Set Word16 -> Map Word8 (Set Word8)
splitToBlocks Set Word16
js = [(Word8, Set Word8)] -> Map Word8 (Set Word8)
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(Word8, Set Word8)] -> Map Word8 (Set Word8))
-> [(Word8, Set Word8)] -> Map Word8 (Set Word8)
forall a b. (a -> b) -> a -> b
$ ([(Word8, Word8)] -> (Word8, Set Word8))
-> [[(Word8, Word8)]] -> [(Word8, Set Word8)]
forall a b. (a -> b) -> [a] -> [b]
map (\[(Word8, Word8)]
xs -> ((Word8, Word8) -> Word8
forall a b. (a, b) -> a
fst ((Word8, Word8) -> Word8) -> (Word8, Word8) -> Word8
forall a b. (a -> b) -> a -> b
$ [(Word8, Word8)] -> (Word8, Word8)
forall a. HasCallStack => [a] -> a
head [(Word8, Word8)]
xs, [Word8] -> Set Word8
forall a. Ord a => [a] -> Set a
Set.fromList (((Word8, Word8) -> Word8) -> [(Word8, Word8)] -> [Word8]
forall a b. (a -> b) -> [a] -> [b]
map (Word8, Word8) -> Word8
forall a b. (a, b) -> b
snd [(Word8, Word8)]
xs))) [[(Word8, Word8)]]
js'
  where
    hi16 :: Word16 -> Word8
    hi16 :: Word16 -> Word8
hi16 = Word16 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word16 -> Word8) -> (Word16 -> Word16) -> Word16 -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word16 -> Int -> Word16) -> Int -> Word16 -> Word16
forall a b c. (a -> b -> c) -> b -> a -> c
flip Word16 -> Int -> Word16
forall a. Bits a => a -> Int -> a
shiftR Int
8

    lo16 :: Word16 -> Word8
    lo16 :: Word16 -> Word8
lo16 = Word16 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word16 -> Word8) -> (Word16 -> Word16) -> Word16 -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
.&. Word16
0xff)

    js' :: [[(Word8,Word8)]]
    js' :: [[(Word8, Word8)]]
js' = ((Word8, Word8) -> (Word8, Word8) -> Bool)
-> [(Word8, Word8)] -> [[(Word8, Word8)]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
(==) (Word8 -> Word8 -> Bool)
-> ((Word8, Word8) -> Word8)
-> (Word8, Word8)
-> (Word8, Word8)
-> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` (Word8, Word8) -> Word8
forall a b. (a, b) -> a
fst) ((Word16 -> (Word8, Word8)) -> [Word16] -> [(Word8, Word8)]
forall a b. (a -> b) -> [a] -> [b]
map ((,) (Word8 -> Word8 -> (Word8, Word8))
-> (Word16 -> Word8) -> Word16 -> Word8 -> (Word8, Word8)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Word16 -> Word8
hi16 (Word16 -> Word8 -> (Word8, Word8))
-> (Word16 -> Word8) -> Word16 -> (Word8, Word8)
forall a b. (Word16 -> a -> b) -> (Word16 -> a) -> Word16 -> b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Word16 -> Word8
lo16) (Set Word16 -> [Word16]
forall a. Set a -> [a]
Set.toList Set Word16
js))


-- | Resolves/parses label pointer used for label compressing
--
-- Returns 'Nothing' on failure
retrieveLabelPtr :: BS.ByteString -> Word16 -> Maybe LabelsPtr
retrieveLabelPtr :: Label -> Word16 -> Maybe LabelsPtr
retrieveLabelPtr Label
msg Word16
ofs
    = case ByteString
-> Either
     (ByteString, ByteOffset, String)
     (ByteString, ByteOffset, LabelsPtr)
forall a.
Binary a =>
ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
decodeOrFail (Label -> ByteString
fromStrict (Label -> ByteString) -> Label -> ByteString
forall a b. (a -> b) -> a -> b
$ Int -> Label -> Label
BS.drop (Word16 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
ofs) Label
msg) of
        Left (ByteString, ByteOffset, String)
_          -> Maybe LabelsPtr
forall a. Maybe a
Nothing
        Right (ByteString
_, ByteOffset
_, LabelsPtr
v) -> LabelsPtr -> Maybe LabelsPtr
forall a. a -> Maybe a
Just LabelsPtr
v

-- | Resolve set of label pointer offsets
--
-- Invariants (/iff/ result is not 'Nothing')
--
--  * all requested offsets will be contained in the result map
--
--  * any offsets contained in the resolved 'Labels' will be part of
--    the result map as well
--
-- NB: No cycle detection is performed, nor are 'Labels' flattened
retrieveLabelPtrs :: BS.ByteString -> Set Word16 -> Maybe (Map Word16 LabelsPtr)
retrieveLabelPtrs :: Label -> Set Word16 -> Maybe (Map Word16 LabelsPtr)
retrieveLabelPtrs Label
msg Set Word16
ofss0 = Map Word16 LabelsPtr -> Maybe (Map Word16 LabelsPtr)
go (Map Word16 LabelsPtr -> Maybe (Map Word16 LabelsPtr))
-> Maybe (Map Word16 LabelsPtr) -> Maybe (Map Word16 LabelsPtr)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Set Word16 -> Maybe (Map Word16 LabelsPtr)
lupPtrs1 Set Word16
ofss0
  where
    go :: Map Word16 LabelsPtr -> Maybe (Map Word16 LabelsPtr)
    go :: Map Word16 LabelsPtr -> Maybe (Map Word16 LabelsPtr)
go Map Word16 LabelsPtr
m0 = do
        let missingOfss :: Set Word16
missingOfss = [Word16] -> Set Word16
forall a. Ord a => [a] -> Set a
Set.fromList ((LabelsPtr -> Maybe Word16) -> [LabelsPtr] -> [Word16]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe LabelsPtr -> Maybe Word16
labelsPtr (Map Word16 LabelsPtr -> [LabelsPtr]
forall a. Map Word16 a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Map Word16 LabelsPtr
m0)) Set Word16 -> Set Word16 -> Set Word16
forall a. Ord a => Set a -> Set a -> Set a
Set.\\ Map Word16 LabelsPtr -> Set Word16
forall k a. Map k a -> Set k
Map.keysSet Map Word16 LabelsPtr
m0

        if Set Word16 -> Bool
forall a. Set a -> Bool
Set.null Set Word16
missingOfss
         then Map Word16 LabelsPtr -> Maybe (Map Word16 LabelsPtr)
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Map Word16 LabelsPtr
m0 -- fix-point reached
         else do
            Map Word16 LabelsPtr
m1 <- Set Word16 -> Maybe (Map Word16 LabelsPtr)
lupPtrs1 Set Word16
missingOfss
            Map Word16 LabelsPtr -> Maybe (Map Word16 LabelsPtr)
go (Map Word16 LabelsPtr
-> Map Word16 LabelsPtr -> Map Word16 LabelsPtr
forall k a. Ord k => Map k a -> Map k a -> Map k a
Map.union Map Word16 LabelsPtr
m0 Map Word16 LabelsPtr
m1)

    -- single lookup step
    lupPtrs1 :: Set Word16 -> Maybe (Map Word16 LabelsPtr)
    lupPtrs1 :: Set Word16 -> Maybe (Map Word16 LabelsPtr)
lupPtrs1 Set Word16
ofss1 = [(Word16, LabelsPtr)] -> Map Word16 LabelsPtr
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(Word16, LabelsPtr)] -> Map Word16 LabelsPtr)
-> ([LabelsPtr] -> [(Word16, LabelsPtr)])
-> [LabelsPtr]
-> Map Word16 LabelsPtr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word16] -> [LabelsPtr] -> [(Word16, LabelsPtr)]
forall a b. [a] -> [b] -> [(a, b)]
zip (Set Word16 -> [Word16]
forall a. Set a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Set Word16
ofss1) ([LabelsPtr] -> Map Word16 LabelsPtr)
-> Maybe [LabelsPtr] -> Maybe (Map Word16 LabelsPtr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Word16 -> Maybe LabelsPtr) -> [Word16] -> Maybe [LabelsPtr]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Label -> Word16 -> Maybe LabelsPtr
retrieveLabelPtr Label
msg) (Set Word16 -> [Word16]
forall a. Set a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Set Word16
ofss1)

-- | Checks for maximum name length (255) and (therefore indirectly) cycle-checking
resolveLabelPtr :: Map Word16 LabelsPtr -> LabelsPtr -> Maybe Labels
resolveLabelPtr :: Map Word16 LabelsPtr -> LabelsPtr -> Maybe Labels
resolveLabelPtr Map Word16 LabelsPtr
ofsmap = Int -> [Label] -> LabelsPtr -> Maybe Labels
go Int
0 []
  where
    go :: Int -> [BS.ByteString] -> LabelsPtr -> Maybe Labels
    go :: Int -> [Label] -> LabelsPtr -> Maybe Labels
go !Int
n [Label]
acc (Label Label
x LabelsPtr
ls) = Int -> [Label] -> LabelsPtr -> Maybe Labels
go (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Label -> Int
BS.length Label
x) (Label
xLabel -> [Label] -> [Label]
forall a. a -> [a] -> [a]
:[Label]
acc) LabelsPtr
ls
    go Int
n [Label]
acc LabelsPtr
LNul
        | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
255    = Labels -> Maybe Labels
forall a. a -> Maybe a
Just (Labels -> Maybe Labels) -> Labels -> Maybe Labels
forall a b. (a -> b) -> a -> b
$! (Label -> Labels -> Labels) -> Labels -> [Label] -> Labels
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Label -> Labels -> Labels
(:.:) Labels
Root ([Label] -> [Label]
forall a. [a] -> [a]
reverse [Label]
acc)
        | Bool
otherwise  = Maybe Labels
forall a. Maybe a
Nothing -- length violation
    go Int
n [Label]
acc (LPtr Word16
ofs)
        | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
255    = Int -> [Label] -> LabelsPtr -> Maybe Labels
go Int
n [Label]
acc (LabelsPtr -> Maybe Labels) -> Maybe LabelsPtr -> Maybe Labels
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Word16 -> Maybe LabelsPtr
lup Word16
ofs
        | Bool
otherwise  = Maybe Labels
forall a. Maybe a
Nothing

    lup :: Word16 -> Maybe LabelsPtr
    lup :: Word16 -> Maybe LabelsPtr
lup Word16
ofs = Word16 -> Map Word16 LabelsPtr -> Maybe LabelsPtr
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Word16
ofs Map Word16 LabelsPtr
ofsmap


{- Resource records

 -- https://en.wikipedia.org/wiki/List_of_DNS_record_types

 RFC 1035

 A        1     a host address
 NS       2     an authoritative name server
 CNAME    5     the canonical name for an alias
 SOA      6     marks the start of a zone of authority
 PTR      12    a domain name pointer
 MX       15    mail exchange
 TXT      16    text strings

 RFC 3596

 AAAA     28    IPv6

 RFC 2782

 SRV      33    Location of services

 ----

 RFC3597            Handling of Unknown DNS Resource Record (RR) Types

-}

-- | Raw DNS record type code
--
-- See also 'TypeSym'
newtype Type = Type Word16
             deriving (Type -> Type -> Bool
(Type -> Type -> Bool) -> (Type -> Type -> Bool) -> Eq Type
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Type -> Type -> Bool
== :: Type -> Type -> Bool
$c/= :: Type -> Type -> Bool
/= :: Type -> Type -> Bool
Eq,Eq Type
Eq Type =>
(Type -> Type -> Ordering)
-> (Type -> Type -> Bool)
-> (Type -> Type -> Bool)
-> (Type -> Type -> Bool)
-> (Type -> Type -> Bool)
-> (Type -> Type -> Type)
-> (Type -> Type -> Type)
-> Ord Type
Type -> Type -> Bool
Type -> Type -> Ordering
Type -> Type -> Type
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Type -> Type -> Ordering
compare :: Type -> Type -> Ordering
$c< :: Type -> Type -> Bool
< :: Type -> Type -> Bool
$c<= :: Type -> Type -> Bool
<= :: Type -> Type -> Bool
$c> :: Type -> Type -> Bool
> :: Type -> Type -> Bool
$c>= :: Type -> Type -> Bool
>= :: Type -> Type -> Bool
$cmax :: Type -> Type -> Type
max :: Type -> Type -> Type
$cmin :: Type -> Type -> Type
min :: Type -> Type -> Type
Ord,ReadPrec [Type]
ReadPrec Type
Int -> ReadS Type
ReadS [Type]
(Int -> ReadS Type)
-> ReadS [Type] -> ReadPrec Type -> ReadPrec [Type] -> Read Type
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Type
readsPrec :: Int -> ReadS Type
$creadList :: ReadS [Type]
readList :: ReadS [Type]
$creadPrec :: ReadPrec Type
readPrec :: ReadPrec Type
$creadListPrec :: ReadPrec [Type]
readListPrec :: ReadPrec [Type]
Read,Int -> Type -> ShowS
[Type] -> ShowS
Type -> String
(Int -> Type -> ShowS)
-> (Type -> String) -> ([Type] -> ShowS) -> Show Type
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Type -> ShowS
showsPrec :: Int -> Type -> ShowS
$cshow :: Type -> String
show :: Type -> String
$cshowList :: [Type] -> ShowS
showList :: [Type] -> ShowS
Show)

instance Binary Type where
    put :: Type -> Put
put (Type Word16
w) = Word16 -> Put
putWord16be Word16
w
    get :: Get Type
get = Word16 -> Type
Type (Word16 -> Type) -> Get Word16 -> Get Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word16
getWord16be

-- | DNS @CLASS@ code as per [RFC 1035, section 3.2.4](https://tools.ietf.org/html/rfc1035#section-3.2.4)
--
-- The most commonly used value is 'classIN'.
newtype Class = Class Word16
              deriving (Class -> Class -> Bool
(Class -> Class -> Bool) -> (Class -> Class -> Bool) -> Eq Class
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Class -> Class -> Bool
== :: Class -> Class -> Bool
$c/= :: Class -> Class -> Bool
/= :: Class -> Class -> Bool
Eq,Eq Class
Eq Class =>
(Class -> Class -> Ordering)
-> (Class -> Class -> Bool)
-> (Class -> Class -> Bool)
-> (Class -> Class -> Bool)
-> (Class -> Class -> Bool)
-> (Class -> Class -> Class)
-> (Class -> Class -> Class)
-> Ord Class
Class -> Class -> Bool
Class -> Class -> Ordering
Class -> Class -> Class
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Class -> Class -> Ordering
compare :: Class -> Class -> Ordering
$c< :: Class -> Class -> Bool
< :: Class -> Class -> Bool
$c<= :: Class -> Class -> Bool
<= :: Class -> Class -> Bool
$c> :: Class -> Class -> Bool
> :: Class -> Class -> Bool
$c>= :: Class -> Class -> Bool
>= :: Class -> Class -> Bool
$cmax :: Class -> Class -> Class
max :: Class -> Class -> Class
$cmin :: Class -> Class -> Class
min :: Class -> Class -> Class
Ord,ReadPrec [Class]
ReadPrec Class
Int -> ReadS Class
ReadS [Class]
(Int -> ReadS Class)
-> ReadS [Class]
-> ReadPrec Class
-> ReadPrec [Class]
-> Read Class
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Class
readsPrec :: Int -> ReadS Class
$creadList :: ReadS [Class]
readList :: ReadS [Class]
$creadPrec :: ReadPrec Class
readPrec :: ReadPrec Class
$creadListPrec :: ReadPrec [Class]
readListPrec :: ReadPrec [Class]
Read,Int -> Class -> ShowS
[Class] -> ShowS
Class -> String
(Int -> Class -> ShowS)
-> (Class -> String) -> ([Class] -> ShowS) -> Show Class
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Class -> ShowS
showsPrec :: Int -> Class -> ShowS
$cshow :: Class -> String
show :: Class -> String
$cshowList :: [Class] -> ShowS
showList :: [Class] -> ShowS
Show)

-- | The 'Class' constant for @IN@ (Internet)
classIN :: Class
classIN :: Class
classIN = Word16 -> Class
Class Word16
1

instance Binary Class where
    put :: Class -> Put
put (Class Word16
w) = Word16 -> Put
putWord16be Word16
w
    get :: Get Class
get = Word16 -> Class
Class (Word16 -> Class) -> Get Word16 -> Get Class
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word16
getWord16be

-- | Cache time-to-live expressed in seconds
newtype TTL = TTL Int32
            deriving (TTL -> TTL -> Bool
(TTL -> TTL -> Bool) -> (TTL -> TTL -> Bool) -> Eq TTL
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TTL -> TTL -> Bool
== :: TTL -> TTL -> Bool
$c/= :: TTL -> TTL -> Bool
/= :: TTL -> TTL -> Bool
Eq,Eq TTL
Eq TTL =>
(TTL -> TTL -> Ordering)
-> (TTL -> TTL -> Bool)
-> (TTL -> TTL -> Bool)
-> (TTL -> TTL -> Bool)
-> (TTL -> TTL -> Bool)
-> (TTL -> TTL -> TTL)
-> (TTL -> TTL -> TTL)
-> Ord TTL
TTL -> TTL -> Bool
TTL -> TTL -> Ordering
TTL -> TTL -> TTL
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: TTL -> TTL -> Ordering
compare :: TTL -> TTL -> Ordering
$c< :: TTL -> TTL -> Bool
< :: TTL -> TTL -> Bool
$c<= :: TTL -> TTL -> Bool
<= :: TTL -> TTL -> Bool
$c> :: TTL -> TTL -> Bool
> :: TTL -> TTL -> Bool
$c>= :: TTL -> TTL -> Bool
>= :: TTL -> TTL -> Bool
$cmax :: TTL -> TTL -> TTL
max :: TTL -> TTL -> TTL
$cmin :: TTL -> TTL -> TTL
min :: TTL -> TTL -> TTL
Ord,ReadPrec [TTL]
ReadPrec TTL
Int -> ReadS TTL
ReadS [TTL]
(Int -> ReadS TTL)
-> ReadS [TTL] -> ReadPrec TTL -> ReadPrec [TTL] -> Read TTL
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS TTL
readsPrec :: Int -> ReadS TTL
$creadList :: ReadS [TTL]
readList :: ReadS [TTL]
$creadPrec :: ReadPrec TTL
readPrec :: ReadPrec TTL
$creadListPrec :: ReadPrec [TTL]
readListPrec :: ReadPrec [TTL]
Read,Int -> TTL -> ShowS
[TTL] -> ShowS
TTL -> String
(Int -> TTL -> ShowS)
-> (TTL -> String) -> ([TTL] -> ShowS) -> Show TTL
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TTL -> ShowS
showsPrec :: Int -> TTL -> ShowS
$cshow :: TTL -> String
show :: TTL -> String
$cshowList :: [TTL] -> ShowS
showList :: [TTL] -> ShowS
Show)

instance Binary TTL where
    put :: TTL -> Put
put (TTL Int32
i) = Int32 -> Put
putInt32be Int32
i
    get :: Get TTL
get = Int32 -> TTL
TTL (Int32 -> TTL) -> Get Int32 -> Get TTL
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Int32
getInt32be

-- http://www.bind9.net/dns-parameters

-- | Symbolic DNS record type
data TypeSym
    = TypeA          -- ^ [RFC 1035](https://tools.ietf.org/html/rfc1035)
    | TypeAAAA       -- ^ [RFC 3596](https://tools.ietf.org/html/rfc3596)
    | TypeAFSDB      -- ^ [RFC 1183](https://tools.ietf.org/html/rfc1183)
    | TypeANY        -- ^ [RFC 1035](https://tools.ietf.org/html/rfc1035) (query)
    | TypeCAA        -- ^ [RFC 6844](https://tools.ietf.org/html/rfc6844)
    | TypeCNAME      -- ^ [RFC 1035](https://tools.ietf.org/html/rfc1035)
    | TypeDNSKEY     -- ^ [RFC 4034](https://tools.ietf.org/html/rfc4034)
    | TypeDS         -- ^ [RFC 4034](https://tools.ietf.org/html/rfc4034)
    | TypeHINFO      -- ^ [RFC 1035](https://tools.ietf.org/html/rfc1035)
    | TypeMX         -- ^ [RFC 1035](https://tools.ietf.org/html/rfc1035)
    | TypeNAPTR      -- ^ [RFC 2915](https://tools.ietf.org/html/rfc2915)
    | TypeNS         -- ^ [RFC 1035](https://tools.ietf.org/html/rfc1035)
    | TypeNSEC       -- ^ [RFC 4034](https://tools.ietf.org/html/rfc4034)
    | TypeNSEC3      -- ^ [RFC 5155](https://tools.ietf.org/html/rfc5155)
    | TypeNSEC3PARAM -- ^ [RFC 5155](https://tools.ietf.org/html/rfc5155)
    | TypeOPT        -- ^ [RFC 6891](https://tools.ietf.org/html/rfc6891) (meta)
    | TypePTR        -- ^ [RFC 1035](https://tools.ietf.org/html/rfc1035)
    | TypeRRSIG      -- ^ [RFC 4034](https://tools.ietf.org/html/rfc4034)
    | TypeSOA        -- ^ [RFC 1035](https://tools.ietf.org/html/rfc1035)
    | TypeSPF        -- ^ [RFC 4408](https://tools.ietf.org/html/rfc4408)
    | TypeSRV        -- ^ [RFC 2782](https://tools.ietf.org/html/rfc2782)
    | TypeSSHFP      -- ^ [RFC 4255](https://tools.ietf.org/html/rfc4255)
    | TypeTXT        -- ^ [RFC 1035](https://tools.ietf.org/html/rfc1035)
    | TypeURI        -- ^ [RFC 7553](https://tools.ietf.org/html/rfc7553)
    deriving (TypeSym -> TypeSym -> Bool
(TypeSym -> TypeSym -> Bool)
-> (TypeSym -> TypeSym -> Bool) -> Eq TypeSym
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TypeSym -> TypeSym -> Bool
== :: TypeSym -> TypeSym -> Bool
$c/= :: TypeSym -> TypeSym -> Bool
/= :: TypeSym -> TypeSym -> Bool
Eq,Eq TypeSym
Eq TypeSym =>
(TypeSym -> TypeSym -> Ordering)
-> (TypeSym -> TypeSym -> Bool)
-> (TypeSym -> TypeSym -> Bool)
-> (TypeSym -> TypeSym -> Bool)
-> (TypeSym -> TypeSym -> Bool)
-> (TypeSym -> TypeSym -> TypeSym)
-> (TypeSym -> TypeSym -> TypeSym)
-> Ord TypeSym
TypeSym -> TypeSym -> Bool
TypeSym -> TypeSym -> Ordering
TypeSym -> TypeSym -> TypeSym
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: TypeSym -> TypeSym -> Ordering
compare :: TypeSym -> TypeSym -> Ordering
$c< :: TypeSym -> TypeSym -> Bool
< :: TypeSym -> TypeSym -> Bool
$c<= :: TypeSym -> TypeSym -> Bool
<= :: TypeSym -> TypeSym -> Bool
$c> :: TypeSym -> TypeSym -> Bool
> :: TypeSym -> TypeSym -> Bool
$c>= :: TypeSym -> TypeSym -> Bool
>= :: TypeSym -> TypeSym -> Bool
$cmax :: TypeSym -> TypeSym -> TypeSym
max :: TypeSym -> TypeSym -> TypeSym
$cmin :: TypeSym -> TypeSym -> TypeSym
min :: TypeSym -> TypeSym -> TypeSym
Ord,Int -> TypeSym
TypeSym -> Int
TypeSym -> [TypeSym]
TypeSym -> TypeSym
TypeSym -> TypeSym -> [TypeSym]
TypeSym -> TypeSym -> TypeSym -> [TypeSym]
(TypeSym -> TypeSym)
-> (TypeSym -> TypeSym)
-> (Int -> TypeSym)
-> (TypeSym -> Int)
-> (TypeSym -> [TypeSym])
-> (TypeSym -> TypeSym -> [TypeSym])
-> (TypeSym -> TypeSym -> [TypeSym])
-> (TypeSym -> TypeSym -> TypeSym -> [TypeSym])
-> Enum TypeSym
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: TypeSym -> TypeSym
succ :: TypeSym -> TypeSym
$cpred :: TypeSym -> TypeSym
pred :: TypeSym -> TypeSym
$ctoEnum :: Int -> TypeSym
toEnum :: Int -> TypeSym
$cfromEnum :: TypeSym -> Int
fromEnum :: TypeSym -> Int
$cenumFrom :: TypeSym -> [TypeSym]
enumFrom :: TypeSym -> [TypeSym]
$cenumFromThen :: TypeSym -> TypeSym -> [TypeSym]
enumFromThen :: TypeSym -> TypeSym -> [TypeSym]
$cenumFromTo :: TypeSym -> TypeSym -> [TypeSym]
enumFromTo :: TypeSym -> TypeSym -> [TypeSym]
$cenumFromThenTo :: TypeSym -> TypeSym -> TypeSym -> [TypeSym]
enumFromThenTo :: TypeSym -> TypeSym -> TypeSym -> [TypeSym]
Enum,TypeSym
TypeSym -> TypeSym -> Bounded TypeSym
forall a. a -> a -> Bounded a
$cminBound :: TypeSym
minBound :: TypeSym
$cmaxBound :: TypeSym
maxBound :: TypeSym
Bounded,ReadPrec [TypeSym]
ReadPrec TypeSym
Int -> ReadS TypeSym
ReadS [TypeSym]
(Int -> ReadS TypeSym)
-> ReadS [TypeSym]
-> ReadPrec TypeSym
-> ReadPrec [TypeSym]
-> Read TypeSym
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS TypeSym
readsPrec :: Int -> ReadS TypeSym
$creadList :: ReadS [TypeSym]
readList :: ReadS [TypeSym]
$creadPrec :: ReadPrec TypeSym
readPrec :: ReadPrec TypeSym
$creadListPrec :: ReadPrec [TypeSym]
readListPrec :: ReadPrec [TypeSym]
Read,Int -> TypeSym -> ShowS
[TypeSym] -> ShowS
TypeSym -> String
(Int -> TypeSym -> ShowS)
-> (TypeSym -> String) -> ([TypeSym] -> ShowS) -> Show TypeSym
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TypeSym -> ShowS
showsPrec :: Int -> TypeSym -> ShowS
$cshow :: TypeSym -> String
show :: TypeSym -> String
$cshowList :: [TypeSym] -> ShowS
showList :: [TypeSym] -> ShowS
Show)

-- | Convert  symbolic 'TypeSym' to numeric 'Type' code
typeFromSym :: TypeSym -> Type
typeFromSym :: TypeSym -> Type
typeFromSym TypeSym
ts = Word16 -> Type
Type (Word16 -> Type) -> Word16 -> Type
forall a b. (a -> b) -> a -> b
$ case TypeSym
ts of
                  TypeSym
TypeA          -> Word16
1
                  TypeSym
TypeNS         -> Word16
2
                  TypeSym
TypeCNAME      -> Word16
5
                  TypeSym
TypeSOA        -> Word16
6
                  TypeSym
TypePTR        -> Word16
12
                  TypeSym
TypeHINFO      -> Word16
13
                  TypeSym
TypeMX         -> Word16
15
                  TypeSym
TypeTXT        -> Word16
16
                  TypeSym
TypeAFSDB      -> Word16
18
                  TypeSym
TypeAAAA       -> Word16
28
                  TypeSym
TypeSRV        -> Word16
33
                  TypeSym
TypeNAPTR      -> Word16
35
                  TypeSym
TypeOPT        -> Word16
41
                  TypeSym
TypeDS         -> Word16
43
                  TypeSym
TypeSSHFP      -> Word16
44
                  TypeSym
TypeRRSIG      -> Word16
46
                  TypeSym
TypeNSEC       -> Word16
47
                  TypeSym
TypeDNSKEY     -> Word16
48
                  TypeSym
TypeNSEC3      -> Word16
50
                  TypeSym
TypeNSEC3PARAM -> Word16
51
                  TypeSym
TypeSPF        -> Word16
99
                  TypeSym
TypeANY        -> Word16
255
                  TypeSym
TypeURI        -> Word16
256
                  TypeSym
TypeCAA        -> Word16
257

-- | Convert 'Type' code to symbolic 'TypeSym'
typeToSym :: Type -> Maybe TypeSym
typeToSym :: Type -> Maybe TypeSym
typeToSym (Type Word16
w) = case Word16
w of
                  Word16
1   -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeA
                  Word16
2   -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeNS
                  Word16
5   -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeCNAME
                  Word16
6   -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeSOA
                  Word16
12  -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypePTR
                  Word16
13  -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeHINFO
                  Word16
15  -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeMX
                  Word16
16  -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeTXT
                  Word16
18  -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeAFSDB
                  Word16
28  -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeAAAA
                  Word16
33  -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeSRV
                  Word16
35  -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeNAPTR
                  Word16
41  -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeOPT
                  Word16
43  -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeDS
                  Word16
44  -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeSSHFP
                  Word16
46  -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeRRSIG
                  Word16
47  -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeNSEC
                  Word16
48  -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeDNSKEY
                  Word16
50  -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeNSEC3
                  Word16
51  -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeNSEC3PARAM
                  Word16
99  -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeSPF
                  Word16
255 -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeANY
                  Word16
256 -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeURI
                  Word16
257 -> TypeSym -> Maybe TypeSym
forall a. a -> Maybe a
Just TypeSym
TypeCAA
                  Word16
_   -> Maybe TypeSym
forall a. Maybe a
Nothing

-- | Extract the resource record type of a 'RData' object
rdType :: RData l -> Either Type TypeSym
rdType :: forall l. RData l -> Either Type TypeSym
rdType RData l
rd = case RData l
rd of
              RDataA          {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeA
              RDataAAAA       {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeAAAA
              RDataAFSDB      {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeAFSDB
              RDataCAA        {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeCAA
              RDataCNAME      {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeCNAME
              RDataDNSKEY     {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeDNSKEY
              RDataDS         {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeDS
              RDataHINFO      {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeHINFO
              RDataMX         {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeMX
              RDataNAPTR      {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeNAPTR
              RDataNS         {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeNS
              RDataNSEC       {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeNSEC
              RDataNSEC3      {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeNSEC3
              RDataNSEC3PARAM {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeNSEC3PARAM
              RDataOPT        {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeOPT
              RDataPTR        {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypePTR
              RDataRRSIG      {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeRRSIG
              RDataSOA        {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeSOA
              RDataSRV        {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeSRV
              RDataTXT        {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeTXT
              RDataSPF        {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeSPF
              RDataURI        {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeURI
              RDataSSHFP      {} -> TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right TypeSym
TypeSSHFP
              --
              RData        Type
ty Label
_  -> Either Type TypeSym
-> (TypeSym -> Either Type TypeSym)
-> Maybe TypeSym
-> Either Type TypeSym
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Type -> Either Type TypeSym
forall a b. a -> Either a b
Left Type
ty) TypeSym -> Either Type TypeSym
forall a b. b -> Either a b
Right (Type -> Maybe TypeSym
typeToSym Type
ty)


{- TODO:


type-bitmap:

 A NS SOA TXT AAAA RRSIG NSEC DNSKEY

'00 07 62 00 80 08 00 03 80'
'00000000 00000111 01100010 00000000 10000000 00001000 00000000 00000011 10000000'
 Win=#0    len=7         ^{SOA}      ^{TXT}       ^{AAAA}                ^{DNSKEY}
                    ^^{A,NS}                                          ^^{RRSIG,NSEC}

" ".join(map("{:08b}".format,[0,7,98,0,128,8,0,3,128]))


"\NUL\a\"\NUL\NUL\NUL\NUL\ETX\128"   NS SOA RRSIG NSEC DNSKEY

[ (winofs+j*8+7-i)   | (j,x) <- zip [0..] xs, i <- [7,6..0], testBit x i ]

-}



-- helpers

getUntilEmpty :: Binary a => Get [a]
getUntilEmpty :: forall a. Binary a => Get [a]
getUntilEmpty = Get a -> Get [a]
forall a. Get a -> Get [a]
untilEmptyWith Get a
forall t. Binary t => Get t
get

untilEmptyWith :: Get a -> Get [a]
untilEmptyWith :: forall a. Get a -> Get [a]
untilEmptyWith Get a
g = [a] -> Get [a]
go []
  where
    go :: [a] -> Get [a]
go [a]
acc = do
        Bool
e <- Get Bool
isEmpty
        if Bool
e
         then [a] -> Get [a]
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([a] -> [a]
forall a. [a] -> [a]
reverse [a]
acc)
         else do
            a
v <- Get a
g
            [a] -> Get [a]
go (a
v a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
acc)



{- TODO:


   MsgRR{rrName = Name "stanford.edu.", rrClass = 1, rrTTL = 1799,
         rrData =
           RData 29
             "\NUL\DC2\SYN\DC3\136\a\244\212e\200\252\194\NUL\152\150\128"},


https://en.wikipedia.org/wiki/LOC_record


LOC record statdns.net.   IN LOC   52 22 23.000 N 4 53 32.000 E -2.00m 0.00m 10000m 10m


SW1A2AA.find.me.uk.	86399	IN	LOC	51 30 12.748 N 0 7 39.611 W 0.00m 0.00m 0.00m 0.00m


https://tools.ietf.org/html/rfc1876

-}