{-# LANGUAGE EmptyDataDecls #-}
module Network.TLS.Record.Types
( Header(..)
, ProtocolType(..)
, packetType
, Record(..)
, Fragment
, fragmentPlaintext
, fragmentCiphertext
, Plaintext
, Compressed
, Ciphertext
, onRecordFragment
, fragmentCompress
, fragmentCipher
, fragmentUncipher
, fragmentUncompress
, rawToRecord
, recordToRaw
, recordToHeader
) where
import Network.TLS.Struct
import Network.TLS.Record.State
import qualified Data.ByteString as B
import Data.Byteable
import Control.Applicative ((<$>))
data Record a = Record !ProtocolType !Version !(Fragment a) deriving (Show,Eq)
newtype Fragment a = Fragment Bytes deriving (Show,Eq)
data Plaintext
data Compressed
data Ciphertext
fragmentPlaintext :: Bytes -> Fragment Plaintext
fragmentPlaintext bytes = Fragment bytes
fragmentCiphertext :: Bytes -> Fragment Ciphertext
fragmentCiphertext bytes = Fragment bytes
instance Byteable (Fragment a) where
toBytes (Fragment b) = b
onRecordFragment :: Record a -> (Fragment a -> RecordM (Fragment b)) -> RecordM (Record b)
onRecordFragment (Record pt ver frag) f = Record pt ver <$> f frag
fragmentMap :: (Bytes -> RecordM Bytes) -> Fragment a -> RecordM (Fragment b)
fragmentMap f (Fragment b) = Fragment <$> f b
fragmentCompress :: (Bytes -> RecordM Bytes) -> Fragment Plaintext -> RecordM (Fragment Compressed)
fragmentCompress f = fragmentMap f
fragmentCipher :: (Bytes -> RecordM Bytes) -> Fragment Compressed -> RecordM (Fragment Ciphertext)
fragmentCipher f = fragmentMap f
fragmentUncipher :: (Bytes -> RecordM Bytes) -> Fragment Ciphertext -> RecordM (Fragment Compressed)
fragmentUncipher f = fragmentMap f
fragmentUncompress :: (Bytes -> RecordM Bytes) -> Fragment Compressed -> RecordM (Fragment Plaintext)
fragmentUncompress f = fragmentMap f
recordToRaw :: Record a -> (Header, Bytes)
recordToRaw (Record pt ver (Fragment bytes)) = (Header pt ver (fromIntegral $ B.length bytes), bytes)
rawToRecord :: Header -> Fragment a -> Record a
rawToRecord (Header pt ver _) fragment = Record pt ver fragment
recordToHeader :: Record a -> Header
recordToHeader (Record pt ver (Fragment bytes)) = Header pt ver (fromIntegral $ B.length bytes)