{-# LANGUAGE EmptyDataDecls #-}
module Network.TLS.Record.Types
( Header(..)
, ProtocolType(..)
, packetType
, Record(..)
, Fragment
, fragmentGetBytes
, fragmentPlaintext
, fragmentCompressed
, fragmentCiphertext
, Plaintext
, Compressed
, Ciphertext
, onRecordFragment
, fragmentCompress
, fragmentCipher
, fragmentUncipher
, fragmentUncompress
, rawToRecord
, recordToRaw
, recordToHeader
) where
import Network.TLS.Struct
import Network.TLS.Imports
import Network.TLS.Record.State
import qualified Data.ByteString as B
data Record a = Record !ProtocolType !Version !(Fragment a) deriving (Show,Eq)
newtype Fragment a = Fragment { fragmentGetBytes :: ByteString } deriving (Show,Eq)
data Plaintext
data Compressed
data Ciphertext
fragmentPlaintext :: ByteString -> Fragment Plaintext
fragmentPlaintext bytes = Fragment bytes
fragmentCompressed :: ByteString -> Fragment Compressed
fragmentCompressed bytes = Fragment bytes
fragmentCiphertext :: ByteString -> Fragment Ciphertext
fragmentCiphertext bytes = Fragment bytes
onRecordFragment :: Record a -> (Fragment a -> RecordM (Fragment b)) -> RecordM (Record b)
onRecordFragment (Record pt ver frag) f = Record pt ver <$> f frag
fragmentMap :: (ByteString -> RecordM ByteString) -> Fragment a -> RecordM (Fragment b)
fragmentMap f (Fragment b) = Fragment <$> f b
fragmentCompress :: (ByteString -> RecordM ByteString) -> Fragment Plaintext -> RecordM (Fragment Compressed)
fragmentCompress f = fragmentMap f
fragmentCipher :: (ByteString -> RecordM ByteString) -> Fragment Compressed -> RecordM (Fragment Ciphertext)
fragmentCipher f = fragmentMap f
fragmentUncipher :: (ByteString -> RecordM ByteString) -> Fragment Ciphertext -> RecordM (Fragment Compressed)
fragmentUncipher f = fragmentMap f
fragmentUncompress :: (ByteString -> RecordM ByteString) -> Fragment Compressed -> RecordM (Fragment Plaintext)
fragmentUncompress f = fragmentMap f
recordToRaw :: Record a -> (Header, ByteString)
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)