{-# OPTIONS_HADDOCK hide #-}

module Network.DNS.Decode.Internal (
    -- ** Internal message component decoders for tests
    decodeDNSHeader
  , decodeDNSFlags
  , decodeDomain
  , decodeMailbox
  , decodeResourceRecordAt
  , decodeResourceRecord
  ) where

import Network.DNS.Decode.Parsers
import Network.DNS.Imports
import Network.DNS.StateBinary
import Network.DNS.Types.Internal

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

-- | Decode the 'DNSFlags' field of 'DNSHeader'.  This is an internal function
-- exposed only for testing.
--
decodeDNSFlags :: ByteString -> Either DNSError DNSFlags
decodeDNSFlags :: ByteString -> Either DNSError DNSFlags
decodeDNSFlags ByteString
bs = forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. SGet a -> ByteString -> Either DNSError (a, PState)
runSGet SGet DNSFlags
getDNSFlags ByteString
bs

-- | Decode the 'DNSHeader' of a message.  This is an internal function.
-- exposed only for testing.
--
decodeDNSHeader :: ByteString -> Either DNSError DNSHeader
decodeDNSHeader :: ByteString -> Either DNSError DNSHeader
decodeDNSHeader ByteString
bs = forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. SGet a -> ByteString -> Either DNSError (a, PState)
runSGet SGet DNSHeader
getHeader ByteString
bs

-- | Decode a domain name.  Since DNS names may use name compression, it is not
-- generally possible to decode the names separately from the enclosing DNS
-- message.  This is an internal function exposed only for testing.
--
decodeDomain :: ByteString -> Either DNSError Domain
decodeDomain :: ByteString -> Either DNSError ByteString
decodeDomain ByteString
bs = forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. SGet a -> ByteString -> Either DNSError (a, PState)
runSGet SGet ByteString
getDomain ByteString
bs

-- | Decode a mailbox name (e.g. the SOA record /rname/ field).  Since DNS names
-- may use name compression, it is not generally possible to decode the names
-- separately from the enclosing DNS message.  This is an internal function.
--
decodeMailbox :: ByteString -> Either DNSError Mailbox
decodeMailbox :: ByteString -> Either DNSError ByteString
decodeMailbox ByteString
bs = forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. SGet a -> ByteString -> Either DNSError (a, PState)
runSGet SGet ByteString
getMailbox ByteString
bs

-- | Decoding resource records.

-- | Decode a resource record (RR) with any DNS timestamps interpreted at the
-- nominal epoch time (see 'decodeAt').  Since RRs may use name compression,
-- it is not generally possible to decode resource record separately from the
-- enclosing DNS message.  This is an internal function.
--
decodeResourceRecord :: ByteString -> Either DNSError ResourceRecord
decodeResourceRecord :: ByteString -> Either DNSError ResourceRecord
decodeResourceRecord ByteString
bs = forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. SGet a -> ByteString -> Either DNSError (a, PState)
runSGet SGet ResourceRecord
getResourceRecord ByteString
bs

-- | Decode a resource record (RR) with DNS timestamps interpreted at the
-- supplied epoch time.  Since RRs may use DNS name compression, it is not
-- generally possible to decode resource record separately from the enclosing
-- DNS message.  This is an internal function.
--
decodeResourceRecordAt :: Int64      -- ^ current epoch time
                       -> ByteString -- ^ encoded resource record
                       -> Either DNSError ResourceRecord
decodeResourceRecordAt :: Int64 -> ByteString -> Either DNSError ResourceRecord
decodeResourceRecordAt Int64
t ByteString
bs = forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a.
Int64 -> SGet a -> ByteString -> Either DNSError (a, PState)
runSGetAt Int64
t SGet ResourceRecord
getResourceRecord ByteString
bs