File: Hooks.hs

package info (click to toggle)
haskell-tls 2.1.8-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,056 kB
  • sloc: haskell: 15,695; makefile: 3
file content (58 lines) | stat: -rw-r--r-- 1,671 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module Network.TLS.Hooks (
    Logging (..),
    defaultLogging,
    Hooks (..),
    defaultHooks,
) where

import qualified Data.ByteString as B
import Data.Default (Default (def))
import Network.TLS.Struct (Handshake, Header)
import Network.TLS.Struct13 (Handshake13)
import Network.TLS.X509 (CertificateChain)

-- | Hooks for logging
--
-- This is called when sending and receiving packets and IO
data Logging = Logging
    { loggingPacketSent :: String -> IO ()
    , loggingPacketRecv :: String -> IO ()
    , loggingIOSent :: B.ByteString -> IO ()
    , loggingIORecv :: Header -> B.ByteString -> IO ()
    }

defaultLogging :: Logging
defaultLogging =
    Logging
        { loggingPacketSent = \_ -> return ()
        , loggingPacketRecv = \_ -> return ()
        , loggingIOSent = \_ -> return ()
        , loggingIORecv = \_ _ -> return ()
        }

instance Default Logging where
    def = defaultLogging

-- | A collection of hooks actions.
data Hooks = Hooks
    { hookRecvHandshake :: Handshake -> IO Handshake
    -- ^ called at each handshake message received
    , hookRecvHandshake13 :: Handshake13 -> IO Handshake13
    -- ^ called at each handshake message received for TLS 1.3
    , hookRecvCertificates :: CertificateChain -> IO ()
    -- ^ called at each certificate chain message received
    , hookLogging :: Logging
    -- ^ hooks on IO and packets, receiving and sending.
    }

defaultHooks :: Hooks
defaultHooks =
    Hooks
        { hookRecvHandshake = return
        , hookRecvHandshake13 = return
        , hookRecvCertificates = return . const ()
        , hookLogging = def
        }

instance Default Hooks where
    def = defaultHooks