{-# 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 = (DNSFlags, PState) -> DNSFlags
forall a b. (a, b) -> a
fst ((DNSFlags, PState) -> DNSFlags)
-> Either DNSError (DNSFlags, PState) -> Either DNSError DNSFlags
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SGet DNSFlags -> ByteString -> Either DNSError (DNSFlags, PState)
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 = (DNSHeader, PState) -> DNSHeader
forall a b. (a, b) -> a
fst ((DNSHeader, PState) -> DNSHeader)
-> Either DNSError (DNSHeader, PState) -> Either DNSError DNSHeader
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SGet DNSHeader -> ByteString -> Either DNSError (DNSHeader, PState)
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 = (ByteString, PState) -> ByteString
forall a b. (a, b) -> a
fst ((ByteString, PState) -> ByteString)
-> Either DNSError (ByteString, PState)
-> Either DNSError ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SGet ByteString
-> ByteString -> Either DNSError (ByteString, PState)
forall a. SGet a -> ByteString -> Either DNSError (a, PState)
runSGet SGet ByteString
getDomain ByteString
bs

-- | Decode a mailbox name (the SOA record /mrname/ 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 = (ByteString, PState) -> ByteString
forall a b. (a, b) -> a
fst ((ByteString, PState) -> ByteString)
-> Either DNSError (ByteString, PState)
-> Either DNSError ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SGet ByteString
-> ByteString -> Either DNSError (ByteString, PState)
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 = (ResourceRecord, PState) -> ResourceRecord
forall a b. (a, b) -> a
fst ((ResourceRecord, PState) -> ResourceRecord)
-> Either DNSError (ResourceRecord, PState)
-> Either DNSError ResourceRecord
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SGet ResourceRecord
-> ByteString -> Either DNSError (ResourceRecord, PState)
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 = (ResourceRecord, PState) -> ResourceRecord
forall a b. (a, b) -> a
fst ((ResourceRecord, PState) -> ResourceRecord)
-> Either DNSError (ResourceRecord, PState)
-> Either DNSError ResourceRecord
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int64
-> SGet ResourceRecord
-> ByteString
-> Either DNSError (ResourceRecord, PState)
forall a.
Int64 -> SGet a -> ByteString -> Either DNSError (a, PState)
runSGetAt Int64
t SGet ResourceRecord
getResourceRecord ByteString
bs