-- | Transport abstraction for libp2p.
--
-- Defines the record-of-functions pattern for transport-agnostic
-- connection management. Stream transports produce a byte stream for the
-- standard security/muxer upgrade; native multiplexed transports such as QUIC
-- provide an already authenticated stream multiplexer.
module LibP2P.Transport
  ( ConnectionEndpoint (..)
  , NativeMuxer (..)
  , RelayCircuitLimit (..)
  , relayCircuitIsLimited
  , RawConnection (..)
  , Listener (..)
  , Transport (..)
  ) where

import Data.Word (Word32, Word64)
import LibP2P.Crypto.PeerId (PeerId)
import LibP2P.Multiaddr (Multiaddr)
import LibP2P.MultistreamSelect.Negotiation (ProtocolId, StreamIO)

-- | Budget a relay advertised for one relayed circuit (specs/relay/circuit-v2).
--
-- A component of 0, or an absent component, means that component is
-- unlimited. 'Nothing' on a connection means it is not a relay circuit.
data RelayCircuitLimit = RelayCircuitLimit
  { RelayCircuitLimit -> Maybe Word32
rclDurationSeconds :: !(Maybe Word32)
  , RelayCircuitLimit -> Maybe Word64
rclDataBytes       :: !(Maybe Word64)
  } deriving (Int -> RelayCircuitLimit -> ShowS
[RelayCircuitLimit] -> ShowS
RelayCircuitLimit -> String
(Int -> RelayCircuitLimit -> ShowS)
-> (RelayCircuitLimit -> String)
-> ([RelayCircuitLimit] -> ShowS)
-> Show RelayCircuitLimit
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> RelayCircuitLimit -> ShowS
showsPrec :: Int -> RelayCircuitLimit -> ShowS
$cshow :: RelayCircuitLimit -> String
show :: RelayCircuitLimit -> String
$cshowList :: [RelayCircuitLimit] -> ShowS
showList :: [RelayCircuitLimit] -> ShowS
Show, RelayCircuitLimit -> RelayCircuitLimit -> Bool
(RelayCircuitLimit -> RelayCircuitLimit -> Bool)
-> (RelayCircuitLimit -> RelayCircuitLimit -> Bool)
-> Eq RelayCircuitLimit
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RelayCircuitLimit -> RelayCircuitLimit -> Bool
== :: RelayCircuitLimit -> RelayCircuitLimit -> Bool
$c/= :: RelayCircuitLimit -> RelayCircuitLimit -> Bool
/= :: RelayCircuitLimit -> RelayCircuitLimit -> Bool
Eq)

-- | True when the relay imposed a non-zero duration or a non-zero
-- per-direction data cap. A zero or omitted component does not count.
relayCircuitIsLimited :: Maybe RelayCircuitLimit -> Bool
relayCircuitIsLimited :: Maybe RelayCircuitLimit -> Bool
relayCircuitIsLimited = Bool
-> (RelayCircuitLimit -> Bool) -> Maybe RelayCircuitLimit -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False RelayCircuitLimit -> Bool
active
  where
    active :: RelayCircuitLimit -> Bool
active RelayCircuitLimit
lim = Maybe Word32 -> Bool
forall {a}. (Ord a, Num a) => Maybe a -> Bool
positive (RelayCircuitLimit -> Maybe Word32
rclDurationSeconds RelayCircuitLimit
lim) Bool -> Bool -> Bool
|| Maybe Word64 -> Bool
forall {a}. (Ord a, Num a) => Maybe a -> Bool
positive (RelayCircuitLimit -> Maybe Word64
rclDataBytes RelayCircuitLimit
lim)
    positive :: Maybe a -> Bool
positive (Just a
n) = a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
0
    positive Maybe a
Nothing = Bool
False

-- | A transport-native authenticated stream multiplexer.
data NativeMuxer = NativeMuxer
  { NativeMuxer -> PeerId
nativePeerId :: !PeerId
  , NativeMuxer -> ProtocolId
nativeSecurity :: !ProtocolId
  , NativeMuxer -> ProtocolId
nativeMuxerProtocol :: !ProtocolId
  , NativeMuxer -> IO StreamIO
nativeOpenStream :: !(IO StreamIO)
  , NativeMuxer -> IO StreamIO
nativeAcceptStream :: !(IO StreamIO)
  , NativeMuxer -> IO ()
nativeClose :: !(IO ())
  }

-- | The I/O endpoint established by a transport.
data ConnectionEndpoint
  = ByteStreamEndpoint !StreamIO
  | NativeMuxerEndpoint !NativeMuxer

-- | A connection established by a transport.
--
-- TCP and relayed connections carry an unencrypted byte stream. QUIC carries
-- a TLS-authenticated native multiplexer and therefore bypasses Noise/Yamux.
data RawConnection = RawConnection
  { RawConnection -> ConnectionEndpoint
rcEndpoint :: !ConnectionEndpoint
  , RawConnection -> Multiaddr
rcLocalAddr :: !Multiaddr
  , RawConnection -> Multiaddr
rcRemoteAddr :: !Multiaddr
  , RawConnection -> IO ()
rcClose :: !(IO ())
  , RawConnection -> Maybe RelayCircuitLimit
rcRelayLimit :: !(Maybe RelayCircuitLimit)
  }

-- | A listener that accepts inbound connections.
data Listener = Listener
  { Listener -> IO RawConnection
listenerAccept :: !(IO RawConnection)
  , Listener -> IO ()
listenerClose :: !(IO ())
  , Listener -> Multiaddr
listenerAddr :: !Multiaddr
  }

-- | Transport provides dial/listen capabilities for a specific protocol.
data Transport = Transport
  { Transport -> Multiaddr -> IO RawConnection
transportDial :: !(Multiaddr -> IO RawConnection)
  , Transport -> Maybe Multiaddr -> Multiaddr -> IO RawConnection
transportDialFrom :: !(Maybe Multiaddr -> Multiaddr -> IO RawConnection)
  , Transport -> Multiaddr -> IO Listener
transportListen :: !(Multiaddr -> IO Listener)
  , Transport -> Multiaddr -> Bool
transportCanDial :: !(Multiaddr -> Bool)
  }