-- | TLS record layer in Tx direction
module Network.TLS.Record.Send (
    encodeRecord12,
    encodeRecord13,
    sendBytes,
) where

import Control.Concurrent.MVar
import qualified Control.Exception as E
import Control.Monad.State.Strict
import qualified Data.ByteString as B

import Network.TLS.Cipher
import Network.TLS.Context.Internal
import Network.TLS.Hooks
import Network.TLS.Imports
import Network.TLS.Packet
import Network.TLS.Record
import Network.TLS.Struct

encodeRecordM :: Record Plaintext -> RecordM ByteString
encodeRecordM :: Record Plaintext -> RecordM ByteString
encodeRecordM Record Plaintext
record = do
    erecord <- Record Plaintext -> RecordM (Record Ciphertext)
encryptRecord Record Plaintext
record
    let (hdr, content) = recordToRaw erecord
    return $ B.concat [encodeHeader hdr, content]

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

encodeRecord12 :: Context -> Record Plaintext -> IO (Either TLSError ByteString)
encodeRecord12 :: Context -> Record Plaintext -> IO (Either TLSError ByteString)
encodeRecord12 Context
ctx = Context -> RecordM ByteString -> IO (Either TLSError ByteString)
forall a. Context -> RecordM a -> IO (Either TLSError a)
prepareRecord12 Context
ctx (RecordM ByteString -> IO (Either TLSError ByteString))
-> (Record Plaintext -> RecordM ByteString)
-> Record Plaintext
-> IO (Either TLSError ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Record Plaintext -> RecordM ByteString
encodeRecordM

-- before TLS 1.1, the block cipher IV is made of the residual of the previous block,
-- so we use cstIV as is, however in other case we generate an explicit IV
prepareRecord12 :: Context -> RecordM a -> IO (Either TLSError a)
prepareRecord12 :: forall a. Context -> RecordM a -> IO (Either TLSError a)
prepareRecord12 Context
ctx RecordM a
f = do
    txState <- MVar RecordState -> IO RecordState
forall a. MVar a -> IO a
readMVar (MVar RecordState -> IO RecordState)
-> MVar RecordState -> IO RecordState
forall a b. (a -> b) -> a -> b
$ Context -> MVar RecordState
ctxTxRecordState Context
ctx
    let sz = case RecordState -> Maybe Cipher
stCipher RecordState
txState of
            Maybe Cipher
Nothing -> Int
0
            Just Cipher
cipher ->
                if BulkFunctions -> Bool
hasRecordIV (BulkFunctions -> Bool) -> BulkFunctions -> Bool
forall a b. (a -> b) -> a -> b
$ Bulk -> BulkFunctions
bulkF (Bulk -> BulkFunctions) -> Bulk -> BulkFunctions
forall a b. (a -> b) -> a -> b
$ Cipher -> Bulk
cipherBulk Cipher
cipher
                    then Bulk -> Int
bulkIVSize (Bulk -> Int) -> Bulk -> Int
forall a b. (a -> b) -> a -> b
$ Cipher -> Bulk
cipherBulk Cipher
cipher
                    else Int
0 -- to not generate IV
    if sz > 0
        then do
            newIV <- getStateRNG ctx sz
            runTxRecordState ctx (modify' (setRecordIV newIV) >> f)
        else runTxRecordState ctx f

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

encodeRecord13 :: Context -> Record Plaintext -> IO (Either TLSError ByteString)
encodeRecord13 :: Context -> Record Plaintext -> IO (Either TLSError ByteString)
encodeRecord13 Context
ctx = Context -> RecordM ByteString -> IO (Either TLSError ByteString)
forall a. Context -> RecordM a -> IO (Either TLSError a)
prepareRecord13 Context
ctx (RecordM ByteString -> IO (Either TLSError ByteString))
-> (Record Plaintext -> RecordM ByteString)
-> Record Plaintext
-> IO (Either TLSError ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Record Plaintext -> RecordM ByteString
encodeRecordM

prepareRecord13 :: Context -> RecordM a -> IO (Either TLSError a)
prepareRecord13 :: forall a. Context -> RecordM a -> IO (Either TLSError a)
prepareRecord13 = Context -> RecordM a -> IO (Either TLSError a)
forall a. Context -> RecordM a -> IO (Either TLSError a)
runTxRecordState

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

sendBytes :: Context -> ByteString -> IO ()
sendBytes :: Context -> ByteString -> IO ()
sendBytes Context
ctx ByteString
dataToSend = do
    -- The record state has advanced already, so a failed send leaves the
    -- peer unable to decrypt any later record.  Later sends throw the same
    -- exception, except an asynchronous one, which is not thrown again.
    Context -> IO (Maybe SomeException)
ctxTxBrokenBy Context
ctx IO (Maybe SomeException) -> (Maybe SomeException -> IO ()) -> IO ()
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (SomeException -> IO (ZonkAny 0)) -> Maybe SomeException -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (String -> SomeException -> IO (ZonkAny 0)
forall a. String -> SomeException -> IO a
throwBroken String
"a previous send failed")
    Context -> (Logging -> IO ()) -> IO ()
withLog Context
ctx ((Logging -> IO ()) -> IO ()) -> (Logging -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Logging
logging -> Logging -> ByteString -> IO ()
loggingIOSent Logging
logging ByteString
dataToSend
    Context -> ByteString -> IO ()
contextSend Context
ctx ByteString
dataToSend IO () -> (SomeException -> IO ()) -> IO ()
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`E.catch` \SomeException
e -> Context -> SomeException -> IO ()
setTxBroken Context
ctx SomeException
e IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> SomeException -> IO ()
forall e a. (HasCallStack, Exception e) => e -> IO a
E.throwIO SomeException
e