{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

module Network.QUIC.Info where

import qualified Data.ByteString.Char8 as C8
import qualified Network.Socket as NS
import Network.TLS hiding (HandshakeFailed, Version)
import Network.UDP (UDPSocket (..))

import Network.QUIC.Connection
import Network.QUIC.Imports
import Network.QUIC.Types

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

-- | Information about a connection.
data ConnectionInfo = ConnectionInfo
    { ConnectionInfo -> Version
version :: Version
    , ConnectionInfo -> Cipher
cipher :: Cipher
    , ConnectionInfo -> Maybe ByteString
alpn :: Maybe ByteString
    , ConnectionInfo -> HandshakeMode13
handshakeMode :: HandshakeMode13
    , ConnectionInfo -> Bool
retry :: Bool
    , ConnectionInfo -> SockAddr
localSockAddr :: NS.SockAddr
    , ConnectionInfo -> SockAddr
remoteSockAddr :: NS.SockAddr
    , ConnectionInfo -> CID
localCID :: CID
    , ConnectionInfo -> CID
remoteCID :: CID
    }

-- | Getting information about a connection.
getConnectionInfo :: Connection -> IO ConnectionInfo
getConnectionInfo :: Connection -> IO ConnectionInfo
getConnectionInfo Connection
conn = do
    UDPSocket{Bool
SockAddr
Socket
udpSocket :: UDPSocket -> Socket
peerSockAddr :: UDPSocket -> SockAddr
connected :: UDPSocket -> Bool
connected :: Bool
peerSockAddr :: SockAddr
udpSocket :: Socket
..} <- Connection -> IO UDPSocket
getSocket Connection
conn
    SockAddr
mysa <- Socket -> IO SockAddr
NS.getSocketName Socket
udpSocket
    CID
mycid <- Connection -> IO CID
getMyCID Connection
conn
    CID
peercid <- Connection -> IO CID
getPeerCID Connection
conn
    Cipher
c <- Connection -> EncryptionLevel -> IO Cipher
getCipher Connection
conn EncryptionLevel
RTT1Level
    Maybe ByteString
mproto <- Connection -> IO (Maybe ByteString)
getApplicationProtocol Connection
conn
    HandshakeMode13
mode <- Connection -> IO HandshakeMode13
getTLSMode Connection
conn
    Bool
r <- Connection -> IO Bool
getRetried Connection
conn
    Version
v <- Connection -> IO Version
getVersion Connection
conn
    forall (m :: * -> *) a. Monad m => a -> m a
return
        ConnectionInfo
            { version :: Version
version = Version
v
            , cipher :: Cipher
cipher = Cipher
c
            , alpn :: Maybe ByteString
alpn = Maybe ByteString
mproto
            , handshakeMode :: HandshakeMode13
handshakeMode = HandshakeMode13
mode
            , retry :: Bool
retry = Bool
r
            , localSockAddr :: SockAddr
localSockAddr = SockAddr
mysa
            , remoteSockAddr :: SockAddr
remoteSockAddr = SockAddr
peerSockAddr
            , localCID :: CID
localCID = CID
mycid
            , remoteCID :: CID
remoteCID = CID
peercid
            }

instance Show ConnectionInfo where
    show :: ConnectionInfo -> String
show ConnectionInfo{Bool
Maybe ByteString
SockAddr
HandshakeMode13
Cipher
CID
Version
remoteCID :: CID
localCID :: CID
remoteSockAddr :: SockAddr
localSockAddr :: SockAddr
retry :: Bool
handshakeMode :: HandshakeMode13
alpn :: Maybe ByteString
cipher :: Cipher
version :: Version
remoteCID :: ConnectionInfo -> CID
localCID :: ConnectionInfo -> CID
remoteSockAddr :: ConnectionInfo -> SockAddr
localSockAddr :: ConnectionInfo -> SockAddr
retry :: ConnectionInfo -> Bool
handshakeMode :: ConnectionInfo -> HandshakeMode13
alpn :: ConnectionInfo -> Maybe ByteString
cipher :: ConnectionInfo -> Cipher
version :: ConnectionInfo -> Version
..} =
        String
"Version: "
            forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Version
version
            forall a. [a] -> [a] -> [a]
++ String
"\n"
            forall a. [a] -> [a] -> [a]
++ String
"Cipher: "
            forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Cipher
cipher
            forall a. [a] -> [a] -> [a]
++ String
"\n"
            forall a. [a] -> [a] -> [a]
++ String
"ALPN: "
            forall a. [a] -> [a] -> [a]
++ forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"none" ByteString -> String
C8.unpack Maybe ByteString
alpn
            forall a. [a] -> [a] -> [a]
++ String
"\n"
            forall a. [a] -> [a] -> [a]
++ String
"Mode: "
            forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show HandshakeMode13
handshakeMode
            forall a. [a] -> [a] -> [a]
++ String
"\n"
            forall a. [a] -> [a] -> [a]
++ String
"Local CID: "
            forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show CID
localCID
            forall a. [a] -> [a] -> [a]
++ String
"\n"
            forall a. [a] -> [a] -> [a]
++ String
"Remote CID: "
            forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show CID
remoteCID
            forall a. [a] -> [a] -> [a]
++ String
"\n"
            forall a. [a] -> [a] -> [a]
++ String
"Local SockAddr: "
            forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show SockAddr
localSockAddr
            forall a. [a] -> [a] -> [a]
++ String
"\n"
            forall a. [a] -> [a] -> [a]
++ String
"Remote SockAddr: "
            forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show SockAddr
remoteSockAddr
            forall a. [a] -> [a] -> [a]
++ if Bool
retry then String
"\nQUIC retry" else String
""

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

-- | Statistics of a connection.
data ConnectionStats = ConnectionStats
    { ConnectionStats -> Int
txBytes :: Int
    , ConnectionStats -> Int
rxBytes :: Int
    }
    deriving (ConnectionStats -> ConnectionStats -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ConnectionStats -> ConnectionStats -> Bool
$c/= :: ConnectionStats -> ConnectionStats -> Bool
== :: ConnectionStats -> ConnectionStats -> Bool
$c== :: ConnectionStats -> ConnectionStats -> Bool
Eq, Int -> ConnectionStats -> ShowS
[ConnectionStats] -> ShowS
ConnectionStats -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ConnectionStats] -> ShowS
$cshowList :: [ConnectionStats] -> ShowS
show :: ConnectionStats -> String
$cshow :: ConnectionStats -> String
showsPrec :: Int -> ConnectionStats -> ShowS
$cshowsPrec :: Int -> ConnectionStats -> ShowS
Show)

-- | Getting statistics of a connection.
getConnectionStats :: Connection -> IO ConnectionStats
getConnectionStats :: Connection -> IO ConnectionStats
getConnectionStats Connection
conn = do
    Int
tx <- Connection -> IO Int
getTxBytes Connection
conn
    Int
rx <- Connection -> IO Int
getRxBytes Connection
conn
    forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
        ConnectionStats
            { txBytes :: Int
txBytes = Int
tx
            , rxBytes :: Int
rxBytes = Int
rx
            }