-- | AutoNAT v2 protobuf messages and framed stream I/O.
module LibP2P.NAT.AutoNAT.V2.Message
  ( AutoNATV2Message (..)
  , DialRequestV2 (..)
  , DialDataRequestV2 (..)
  , DialResponseV2 (..)
  , DialStatusV2 (..)
  , ResponseStatusV2 (..)
  , DialBackV2 (..)
  , encodeAutoNATV2Message
  , decodeAutoNATV2Message
  , encodeDialBackV2
  , decodeDialBackV2
  , encodeDialBackResponseV2
  , decodeDialBackResponseV2
  , writeAutoNATV2Message
  , readAutoNATV2Message
  , writeDialBackV2
  , readDialBackV2
  , writeDialBackResponseV2
  , readDialBackResponseV2
  , autoNATV2DialProtocolId
  , autoNATV2DialBackProtocolId
  , maxAutoNATV2MessageSize
  , maxAutoNATV2DialBackMessageSize
  ) where

import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import Data.Text (Text)
import Data.Word (Word32, Word64)
import LibP2P.Core.Varint (decodeUvarint, encodeUvarint)
import LibP2P.MultistreamSelect.Negotiation (StreamIO (..), readExactBounded)
import Proto3.Wire.Decode (ParseError, Parser, RawMessage, at, embedded, one, parse, repeated)
import qualified Proto3.Wire.Decode as Decode
import Proto3.Wire.Encode (MessageBuilder)
import qualified Proto3.Wire.Encode as Encode
import Proto3.Wire.Types (FieldNumber (..))

-- | Protocol used for one AutoNAT v2 reachability request.
autoNATV2DialProtocolId :: Text
autoNATV2DialProtocolId :: Text
autoNATV2DialProtocolId = Text
"/libp2p/autonat/2/dial-request"

-- | Protocol used by the server to return the request nonce.
autoNATV2DialBackProtocolId :: Text
autoNATV2DialBackProtocolId :: Text
autoNATV2DialBackProtocolId = Text
"/libp2p/autonat/2/dial-back"

-- | go-libp2p's bound for request, response, and dial-data messages.
maxAutoNATV2MessageSize :: Int
maxAutoNATV2MessageSize :: Int
maxAutoNATV2MessageSize = Int
8192

-- | go-libp2p's bound for dial-back messages.
maxAutoNATV2DialBackMessageSize :: Int
maxAutoNATV2DialBackMessageSize :: Int
maxAutoNATV2DialBackMessageSize = Int
1024

-- | One message on the dial-request protocol stream.
data AutoNATV2Message
  = V2DialRequest !DialRequestV2
  | V2DialResponse !DialResponseV2
  | V2DialDataRequest !DialDataRequestV2
  | V2DialDataResponse !ByteString
  deriving (Int -> AutoNATV2Message -> ShowS
[AutoNATV2Message] -> ShowS
AutoNATV2Message -> String
(Int -> AutoNATV2Message -> ShowS)
-> (AutoNATV2Message -> String)
-> ([AutoNATV2Message] -> ShowS)
-> Show AutoNATV2Message
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AutoNATV2Message -> ShowS
showsPrec :: Int -> AutoNATV2Message -> ShowS
$cshow :: AutoNATV2Message -> String
show :: AutoNATV2Message -> String
$cshowList :: [AutoNATV2Message] -> ShowS
showList :: [AutoNATV2Message] -> ShowS
Show, AutoNATV2Message -> AutoNATV2Message -> Bool
(AutoNATV2Message -> AutoNATV2Message -> Bool)
-> (AutoNATV2Message -> AutoNATV2Message -> Bool)
-> Eq AutoNATV2Message
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AutoNATV2Message -> AutoNATV2Message -> Bool
== :: AutoNATV2Message -> AutoNATV2Message -> Bool
$c/= :: AutoNATV2Message -> AutoNATV2Message -> Bool
/= :: AutoNATV2Message -> AutoNATV2Message -> Bool
Eq)

-- | Priority-ordered addresses and the nonce expected on dial-back.
data DialRequestV2 = DialRequestV2
  { DialRequestV2 -> [ByteString]
drv2Addrs :: ![ByteString]
  , DialRequestV2 -> Word64
drv2Nonce :: !Word64
  }
  deriving (Int -> DialRequestV2 -> ShowS
[DialRequestV2] -> ShowS
DialRequestV2 -> String
(Int -> DialRequestV2 -> ShowS)
-> (DialRequestV2 -> String)
-> ([DialRequestV2] -> ShowS)
-> Show DialRequestV2
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DialRequestV2 -> ShowS
showsPrec :: Int -> DialRequestV2 -> ShowS
$cshow :: DialRequestV2 -> String
show :: DialRequestV2 -> String
$cshowList :: [DialRequestV2] -> ShowS
showList :: [DialRequestV2] -> ShowS
Show, DialRequestV2 -> DialRequestV2 -> Bool
(DialRequestV2 -> DialRequestV2 -> Bool)
-> (DialRequestV2 -> DialRequestV2 -> Bool) -> Eq DialRequestV2
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DialRequestV2 -> DialRequestV2 -> Bool
== :: DialRequestV2 -> DialRequestV2 -> Bool
$c/= :: DialRequestV2 -> DialRequestV2 -> Bool
/= :: DialRequestV2 -> DialRequestV2 -> Bool
Eq)

-- | Cost the client must upload before a cross-IP dial-back.
data DialDataRequestV2 = DialDataRequestV2
  { DialDataRequestV2 -> Word32
ddrv2AddrIdx :: !Word32
  , DialDataRequestV2 -> Word64
ddrv2NumBytes :: !Word64
  }
  deriving (Int -> DialDataRequestV2 -> ShowS
[DialDataRequestV2] -> ShowS
DialDataRequestV2 -> String
(Int -> DialDataRequestV2 -> ShowS)
-> (DialDataRequestV2 -> String)
-> ([DialDataRequestV2] -> ShowS)
-> Show DialDataRequestV2
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DialDataRequestV2 -> ShowS
showsPrec :: Int -> DialDataRequestV2 -> ShowS
$cshow :: DialDataRequestV2 -> String
show :: DialDataRequestV2 -> String
$cshowList :: [DialDataRequestV2] -> ShowS
showList :: [DialDataRequestV2] -> ShowS
Show, DialDataRequestV2 -> DialDataRequestV2 -> Bool
(DialDataRequestV2 -> DialDataRequestV2 -> Bool)
-> (DialDataRequestV2 -> DialDataRequestV2 -> Bool)
-> Eq DialDataRequestV2
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DialDataRequestV2 -> DialDataRequestV2 -> Bool
== :: DialDataRequestV2 -> DialDataRequestV2 -> Bool
$c/= :: DialDataRequestV2 -> DialDataRequestV2 -> Bool
/= :: DialDataRequestV2 -> DialDataRequestV2 -> Bool
Eq)

-- | Outcome of dialing the selected address.
data DialStatusV2
  = DialStatusUnused
  | DialStatusError
  | DialStatusBackError
  | DialStatusOK
  | DialStatusUnknown !Word32
  deriving (Int -> DialStatusV2 -> ShowS
[DialStatusV2] -> ShowS
DialStatusV2 -> String
(Int -> DialStatusV2 -> ShowS)
-> (DialStatusV2 -> String)
-> ([DialStatusV2] -> ShowS)
-> Show DialStatusV2
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DialStatusV2 -> ShowS
showsPrec :: Int -> DialStatusV2 -> ShowS
$cshow :: DialStatusV2 -> String
show :: DialStatusV2 -> String
$cshowList :: [DialStatusV2] -> ShowS
showList :: [DialStatusV2] -> ShowS
Show, DialStatusV2 -> DialStatusV2 -> Bool
(DialStatusV2 -> DialStatusV2 -> Bool)
-> (DialStatusV2 -> DialStatusV2 -> Bool) -> Eq DialStatusV2
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DialStatusV2 -> DialStatusV2 -> Bool
== :: DialStatusV2 -> DialStatusV2 -> Bool
$c/= :: DialStatusV2 -> DialStatusV2 -> Bool
/= :: DialStatusV2 -> DialStatusV2 -> Bool
Eq)

-- | Outcome of serving the request itself.
data ResponseStatusV2
  = ResponseInternalError
  | ResponseRequestRejected
  | ResponseDialRefused
  | ResponseOK
  | ResponseStatusUnknown !Word32
  deriving (Int -> ResponseStatusV2 -> ShowS
[ResponseStatusV2] -> ShowS
ResponseStatusV2 -> String
(Int -> ResponseStatusV2 -> ShowS)
-> (ResponseStatusV2 -> String)
-> ([ResponseStatusV2] -> ShowS)
-> Show ResponseStatusV2
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ResponseStatusV2 -> ShowS
showsPrec :: Int -> ResponseStatusV2 -> ShowS
$cshow :: ResponseStatusV2 -> String
show :: ResponseStatusV2 -> String
$cshowList :: [ResponseStatusV2] -> ShowS
showList :: [ResponseStatusV2] -> ShowS
Show, ResponseStatusV2 -> ResponseStatusV2 -> Bool
(ResponseStatusV2 -> ResponseStatusV2 -> Bool)
-> (ResponseStatusV2 -> ResponseStatusV2 -> Bool)
-> Eq ResponseStatusV2
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ResponseStatusV2 -> ResponseStatusV2 -> Bool
== :: ResponseStatusV2 -> ResponseStatusV2 -> Bool
$c/= :: ResponseStatusV2 -> ResponseStatusV2 -> Bool
/= :: ResponseStatusV2 -> ResponseStatusV2 -> Bool
Eq)

-- | Final response on the dial-request stream.
data DialResponseV2 = DialResponseV2
  { DialResponseV2 -> ResponseStatusV2
drsv2Status :: !ResponseStatusV2
  , DialResponseV2 -> Word32
drsv2AddrIdx :: !Word32
  , DialResponseV2 -> DialStatusV2
drsv2DialStatus :: !DialStatusV2
  }
  deriving (Int -> DialResponseV2 -> ShowS
[DialResponseV2] -> ShowS
DialResponseV2 -> String
(Int -> DialResponseV2 -> ShowS)
-> (DialResponseV2 -> String)
-> ([DialResponseV2] -> ShowS)
-> Show DialResponseV2
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DialResponseV2 -> ShowS
showsPrec :: Int -> DialResponseV2 -> ShowS
$cshow :: DialResponseV2 -> String
show :: DialResponseV2 -> String
$cshowList :: [DialResponseV2] -> ShowS
showList :: [DialResponseV2] -> ShowS
Show, DialResponseV2 -> DialResponseV2 -> Bool
(DialResponseV2 -> DialResponseV2 -> Bool)
-> (DialResponseV2 -> DialResponseV2 -> Bool) -> Eq DialResponseV2
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DialResponseV2 -> DialResponseV2 -> Bool
== :: DialResponseV2 -> DialResponseV2 -> Bool
$c/= :: DialResponseV2 -> DialResponseV2 -> Bool
/= :: DialResponseV2 -> DialResponseV2 -> Bool
Eq)

-- | Nonce delivered over the fresh dial-back connection.
newtype DialBackV2 = DialBackV2
  { DialBackV2 -> Word64
dbv2Nonce :: Word64
  }
  deriving (Int -> DialBackV2 -> ShowS
[DialBackV2] -> ShowS
DialBackV2 -> String
(Int -> DialBackV2 -> ShowS)
-> (DialBackV2 -> String)
-> ([DialBackV2] -> ShowS)
-> Show DialBackV2
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DialBackV2 -> ShowS
showsPrec :: Int -> DialBackV2 -> ShowS
$cshow :: DialBackV2 -> String
show :: DialBackV2 -> String
$cshowList :: [DialBackV2] -> ShowS
showList :: [DialBackV2] -> ShowS
Show, DialBackV2 -> DialBackV2 -> Bool
(DialBackV2 -> DialBackV2 -> Bool)
-> (DialBackV2 -> DialBackV2 -> Bool) -> Eq DialBackV2
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DialBackV2 -> DialBackV2 -> Bool
== :: DialBackV2 -> DialBackV2 -> Bool
$c/= :: DialBackV2 -> DialBackV2 -> Bool
/= :: DialBackV2 -> DialBackV2 -> Bool
Eq)

encodeAutoNATV2Message :: AutoNATV2Message -> ByteString
encodeAutoNATV2Message :: AutoNATV2Message -> ByteString
encodeAutoNATV2Message AutoNATV2Message
message = LazyByteString -> ByteString
BL.toStrict (LazyByteString -> ByteString)
-> (MessageBuilder -> LazyByteString)
-> MessageBuilder
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MessageBuilder -> LazyByteString
Encode.toLazyByteString (MessageBuilder -> ByteString) -> MessageBuilder -> ByteString
forall a b. (a -> b) -> a -> b
$
  case AutoNATV2Message
message of
    V2DialRequest DialRequestV2
request -> FieldNumber -> MessageBuilder -> MessageBuilder
Encode.embedded (Word64 -> FieldNumber
FieldNumber Word64
1) (DialRequestV2 -> MessageBuilder
encodeDialRequest DialRequestV2
request)
    V2DialResponse DialResponseV2
response -> FieldNumber -> MessageBuilder -> MessageBuilder
Encode.embedded (Word64 -> FieldNumber
FieldNumber Word64
2) (DialResponseV2 -> MessageBuilder
encodeDialResponse DialResponseV2
response)
    V2DialDataRequest DialDataRequestV2
request -> FieldNumber -> MessageBuilder -> MessageBuilder
Encode.embedded (Word64 -> FieldNumber
FieldNumber Word64
3) (DialDataRequestV2 -> MessageBuilder
encodeDialDataRequest DialDataRequestV2
request)
    V2DialDataResponse ByteString
bytes -> FieldNumber -> MessageBuilder -> MessageBuilder
Encode.embedded (Word64 -> FieldNumber
FieldNumber Word64
4) (ByteString -> MessageBuilder
encodeDialDataResponse ByteString
bytes)

encodeDialRequest :: DialRequestV2 -> MessageBuilder
encodeDialRequest :: DialRequestV2 -> MessageBuilder
encodeDialRequest DialRequestV2
request =
  (ByteString -> MessageBuilder) -> [ByteString] -> MessageBuilder
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (FieldNumber -> ByteString -> MessageBuilder
Encode.byteString (Word64 -> FieldNumber
FieldNumber Word64
1)) (DialRequestV2 -> [ByteString]
drv2Addrs DialRequestV2
request)
    MessageBuilder -> MessageBuilder -> MessageBuilder
forall a. Semigroup a => a -> a -> a
<> FieldNumber -> Word64 -> MessageBuilder
Encode.fixed64 (Word64 -> FieldNumber
FieldNumber Word64
2) (DialRequestV2 -> Word64
drv2Nonce DialRequestV2
request)

encodeDialDataRequest :: DialDataRequestV2 -> MessageBuilder
encodeDialDataRequest :: DialDataRequestV2 -> MessageBuilder
encodeDialDataRequest DialDataRequestV2
request =
  FieldNumber -> Word32 -> MessageBuilder
Encode.uint32 (Word64 -> FieldNumber
FieldNumber Word64
1) (DialDataRequestV2 -> Word32
ddrv2AddrIdx DialDataRequestV2
request)
    MessageBuilder -> MessageBuilder -> MessageBuilder
forall a. Semigroup a => a -> a -> a
<> FieldNumber -> Word64 -> MessageBuilder
Encode.uint64 (Word64 -> FieldNumber
FieldNumber Word64
2) (DialDataRequestV2 -> Word64
ddrv2NumBytes DialDataRequestV2
request)

encodeDialDataResponse :: ByteString -> MessageBuilder
encodeDialDataResponse :: ByteString -> MessageBuilder
encodeDialDataResponse = FieldNumber -> ByteString -> MessageBuilder
Encode.byteString (Word64 -> FieldNumber
FieldNumber Word64
1)

encodeDialResponse :: DialResponseV2 -> MessageBuilder
encodeDialResponse :: DialResponseV2 -> MessageBuilder
encodeDialResponse DialResponseV2
response =
  FieldNumber -> Word32 -> MessageBuilder
Encode.uint32 (Word64 -> FieldNumber
FieldNumber Word64
1) (ResponseStatusV2 -> Word32
responseStatusToWord (DialResponseV2 -> ResponseStatusV2
drsv2Status DialResponseV2
response))
    MessageBuilder -> MessageBuilder -> MessageBuilder
forall a. Semigroup a => a -> a -> a
<> FieldNumber -> Word32 -> MessageBuilder
Encode.uint32 (Word64 -> FieldNumber
FieldNumber Word64
2) (DialResponseV2 -> Word32
drsv2AddrIdx DialResponseV2
response)
    MessageBuilder -> MessageBuilder -> MessageBuilder
forall a. Semigroup a => a -> a -> a
<> FieldNumber -> Word32 -> MessageBuilder
Encode.uint32 (Word64 -> FieldNumber
FieldNumber Word64
3) (DialStatusV2 -> Word32
dialStatusToWord (DialResponseV2 -> DialStatusV2
drsv2DialStatus DialResponseV2
response))

decodeAutoNATV2Message :: ByteString -> Either String AutoNATV2Message
decodeAutoNATV2Message :: ByteString -> Either String AutoNATV2Message
decodeAutoNATV2Message ByteString
bytes = do
  fields <- Either
  ParseError
  (Maybe DialRequestV2, Maybe DialResponseV2,
   Maybe DialDataRequestV2, Maybe ByteString)
-> Either
     String
     (Maybe DialRequestV2, Maybe DialResponseV2,
      Maybe DialDataRequestV2, Maybe ByteString)
forall a. Either ParseError a -> Either String a
firstParseError (Parser
  RawMessage
  (Maybe DialRequestV2, Maybe DialResponseV2,
   Maybe DialDataRequestV2, Maybe ByteString)
-> ByteString
-> Either
     ParseError
     (Maybe DialRequestV2, Maybe DialResponseV2,
      Maybe DialDataRequestV2, Maybe ByteString)
forall a. Parser RawMessage a -> ByteString -> Either ParseError a
parse Parser
  RawMessage
  (Maybe DialRequestV2, Maybe DialResponseV2,
   Maybe DialDataRequestV2, Maybe ByteString)
messageParser ByteString
bytes)
  case fields of
    (Just DialRequestV2
request, Maybe DialResponseV2
Nothing, Maybe DialDataRequestV2
Nothing, Maybe ByteString
Nothing) -> AutoNATV2Message -> Either String AutoNATV2Message
forall a b. b -> Either a b
Right (DialRequestV2 -> AutoNATV2Message
V2DialRequest DialRequestV2
request)
    (Maybe DialRequestV2
Nothing, Just DialResponseV2
response, Maybe DialDataRequestV2
Nothing, Maybe ByteString
Nothing) -> AutoNATV2Message -> Either String AutoNATV2Message
forall a b. b -> Either a b
Right (DialResponseV2 -> AutoNATV2Message
V2DialResponse DialResponseV2
response)
    (Maybe DialRequestV2
Nothing, Maybe DialResponseV2
Nothing, Just DialDataRequestV2
request, Maybe ByteString
Nothing) -> AutoNATV2Message -> Either String AutoNATV2Message
forall a b. b -> Either a b
Right (DialDataRequestV2 -> AutoNATV2Message
V2DialDataRequest DialDataRequestV2
request)
    (Maybe DialRequestV2
Nothing, Maybe DialResponseV2
Nothing, Maybe DialDataRequestV2
Nothing, Just ByteString
response) -> AutoNATV2Message -> Either String AutoNATV2Message
forall a b. b -> Either a b
Right (ByteString -> AutoNATV2Message
V2DialDataResponse ByteString
response)
    (Maybe DialRequestV2, Maybe DialResponseV2,
 Maybe DialDataRequestV2, Maybe ByteString)
_ -> String -> Either String AutoNATV2Message
forall a b. a -> Either a b
Left String
"AutoNAT v2 message must contain exactly one message field"

messageParser
  :: Parser RawMessage
       ( Maybe DialRequestV2
       , Maybe DialResponseV2
       , Maybe DialDataRequestV2
       , Maybe ByteString
       )
messageParser :: Parser
  RawMessage
  (Maybe DialRequestV2, Maybe DialResponseV2,
   Maybe DialDataRequestV2, Maybe ByteString)
messageParser = (,,,)
  (Maybe DialRequestV2
 -> Maybe DialResponseV2
 -> Maybe DialDataRequestV2
 -> Maybe ByteString
 -> (Maybe DialRequestV2, Maybe DialResponseV2,
     Maybe DialDataRequestV2, Maybe ByteString))
-> Parser RawMessage (Maybe DialRequestV2)
-> Parser
     RawMessage
     (Maybe DialResponseV2
      -> Maybe DialDataRequestV2
      -> Maybe ByteString
      -> (Maybe DialRequestV2, Maybe DialResponseV2,
          Maybe DialDataRequestV2, Maybe ByteString))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawField (Maybe DialRequestV2)
-> FieldNumber -> Parser RawMessage (Maybe DialRequestV2)
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
at (Parser RawMessage DialRequestV2
-> Parser RawField (Maybe DialRequestV2)
forall a. Parser RawMessage a -> Parser RawField (Maybe a)
embedded Parser RawMessage DialRequestV2
dialRequestParser) (Word64 -> FieldNumber
FieldNumber Word64
1)
  Parser
  RawMessage
  (Maybe DialResponseV2
   -> Maybe DialDataRequestV2
   -> Maybe ByteString
   -> (Maybe DialRequestV2, Maybe DialResponseV2,
       Maybe DialDataRequestV2, Maybe ByteString))
-> Parser RawMessage (Maybe DialResponseV2)
-> Parser
     RawMessage
     (Maybe DialDataRequestV2
      -> Maybe ByteString
      -> (Maybe DialRequestV2, Maybe DialResponseV2,
          Maybe DialDataRequestV2, Maybe ByteString))
forall a b.
Parser RawMessage (a -> b)
-> Parser RawMessage a -> Parser RawMessage b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser RawField (Maybe DialResponseV2)
-> FieldNumber -> Parser RawMessage (Maybe DialResponseV2)
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
at (Parser RawMessage DialResponseV2
-> Parser RawField (Maybe DialResponseV2)
forall a. Parser RawMessage a -> Parser RawField (Maybe a)
embedded Parser RawMessage DialResponseV2
dialResponseParser) (Word64 -> FieldNumber
FieldNumber Word64
2)
  Parser
  RawMessage
  (Maybe DialDataRequestV2
   -> Maybe ByteString
   -> (Maybe DialRequestV2, Maybe DialResponseV2,
       Maybe DialDataRequestV2, Maybe ByteString))
-> Parser RawMessage (Maybe DialDataRequestV2)
-> Parser
     RawMessage
     (Maybe ByteString
      -> (Maybe DialRequestV2, Maybe DialResponseV2,
          Maybe DialDataRequestV2, Maybe ByteString))
forall a b.
Parser RawMessage (a -> b)
-> Parser RawMessage a -> Parser RawMessage b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser RawField (Maybe DialDataRequestV2)
-> FieldNumber -> Parser RawMessage (Maybe DialDataRequestV2)
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
at (Parser RawMessage DialDataRequestV2
-> Parser RawField (Maybe DialDataRequestV2)
forall a. Parser RawMessage a -> Parser RawField (Maybe a)
embedded Parser RawMessage DialDataRequestV2
dialDataRequestParser) (Word64 -> FieldNumber
FieldNumber Word64
3)
  Parser
  RawMessage
  (Maybe ByteString
   -> (Maybe DialRequestV2, Maybe DialResponseV2,
       Maybe DialDataRequestV2, Maybe ByteString))
-> Parser RawMessage (Maybe ByteString)
-> Parser
     RawMessage
     (Maybe DialRequestV2, Maybe DialResponseV2,
      Maybe DialDataRequestV2, Maybe ByteString)
forall a b.
Parser RawMessage (a -> b)
-> Parser RawMessage a -> Parser RawMessage b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser RawField (Maybe ByteString)
-> FieldNumber -> Parser RawMessage (Maybe ByteString)
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
at (Parser RawMessage ByteString -> Parser RawField (Maybe ByteString)
forall a. Parser RawMessage a -> Parser RawField (Maybe a)
embedded Parser RawMessage ByteString
dialDataResponseParser) (Word64 -> FieldNumber
FieldNumber Word64
4)

dialRequestParser :: Parser RawMessage DialRequestV2
dialRequestParser :: Parser RawMessage DialRequestV2
dialRequestParser = [ByteString] -> Word64 -> DialRequestV2
DialRequestV2
  ([ByteString] -> Word64 -> DialRequestV2)
-> Parser RawMessage [ByteString]
-> Parser RawMessage (Word64 -> DialRequestV2)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawField [ByteString]
-> FieldNumber -> Parser RawMessage [ByteString]
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
at (Parser RawPrimitive ByteString -> Parser RawField [ByteString]
forall a. Parser RawPrimitive a -> Parser RawField [a]
repeated Parser RawPrimitive ByteString
Decode.byteString) (Word64 -> FieldNumber
FieldNumber Word64
1)
  Parser RawMessage (Word64 -> DialRequestV2)
-> Parser RawMessage Word64 -> Parser RawMessage DialRequestV2
forall a b.
Parser RawMessage (a -> b)
-> Parser RawMessage a -> Parser RawMessage b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser RawField Word64 -> FieldNumber -> Parser RawMessage Word64
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
at (Parser RawPrimitive Word64 -> Word64 -> Parser RawField Word64
forall a. Parser RawPrimitive a -> a -> Parser RawField a
one Parser RawPrimitive Word64
Decode.fixed64 Word64
0) (Word64 -> FieldNumber
FieldNumber Word64
2)

dialDataRequestParser :: Parser RawMessage DialDataRequestV2
dialDataRequestParser :: Parser RawMessage DialDataRequestV2
dialDataRequestParser = Word32 -> Word64 -> DialDataRequestV2
DialDataRequestV2
  (Word32 -> Word64 -> DialDataRequestV2)
-> Parser RawMessage Word32
-> Parser RawMessage (Word64 -> DialDataRequestV2)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawField Word32 -> FieldNumber -> Parser RawMessage Word32
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
at (Parser RawPrimitive Word32 -> Word32 -> Parser RawField Word32
forall a. Parser RawPrimitive a -> a -> Parser RawField a
one Parser RawPrimitive Word32
Decode.uint32 Word32
0) (Word64 -> FieldNumber
FieldNumber Word64
1)
  Parser RawMessage (Word64 -> DialDataRequestV2)
-> Parser RawMessage Word64 -> Parser RawMessage DialDataRequestV2
forall a b.
Parser RawMessage (a -> b)
-> Parser RawMessage a -> Parser RawMessage b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser RawField Word64 -> FieldNumber -> Parser RawMessage Word64
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
at (Parser RawPrimitive Word64 -> Word64 -> Parser RawField Word64
forall a. Parser RawPrimitive a -> a -> Parser RawField a
one Parser RawPrimitive Word64
Decode.uint64 Word64
0) (Word64 -> FieldNumber
FieldNumber Word64
2)

dialDataResponseParser :: Parser RawMessage ByteString
dialDataResponseParser :: Parser RawMessage ByteString
dialDataResponseParser = Parser RawField ByteString
-> FieldNumber -> Parser RawMessage ByteString
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
at (Parser RawPrimitive ByteString
-> ByteString -> Parser RawField ByteString
forall a. Parser RawPrimitive a -> a -> Parser RawField a
one Parser RawPrimitive ByteString
Decode.byteString ByteString
BS.empty) (Word64 -> FieldNumber
FieldNumber Word64
1)

dialResponseParser :: Parser RawMessage DialResponseV2
dialResponseParser :: Parser RawMessage DialResponseV2
dialResponseParser = ResponseStatusV2 -> Word32 -> DialStatusV2 -> DialResponseV2
DialResponseV2
  (ResponseStatusV2 -> Word32 -> DialStatusV2 -> DialResponseV2)
-> Parser RawMessage ResponseStatusV2
-> Parser RawMessage (Word32 -> DialStatusV2 -> DialResponseV2)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawField ResponseStatusV2
-> FieldNumber -> Parser RawMessage ResponseStatusV2
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
at ((Word32 -> ResponseStatusV2)
-> Parser RawField Word32 -> Parser RawField ResponseStatusV2
forall a b. (a -> b) -> Parser RawField a -> Parser RawField b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word32 -> ResponseStatusV2
responseStatusFromWord (Parser RawPrimitive Word32 -> Word32 -> Parser RawField Word32
forall a. Parser RawPrimitive a -> a -> Parser RawField a
one Parser RawPrimitive Word32
Decode.uint32 Word32
0)) (Word64 -> FieldNumber
FieldNumber Word64
1)
  Parser RawMessage (Word32 -> DialStatusV2 -> DialResponseV2)
-> Parser RawMessage Word32
-> Parser RawMessage (DialStatusV2 -> DialResponseV2)
forall a b.
Parser RawMessage (a -> b)
-> Parser RawMessage a -> Parser RawMessage b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser RawField Word32 -> FieldNumber -> Parser RawMessage Word32
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
at (Parser RawPrimitive Word32 -> Word32 -> Parser RawField Word32
forall a. Parser RawPrimitive a -> a -> Parser RawField a
one Parser RawPrimitive Word32
Decode.uint32 Word32
0) (Word64 -> FieldNumber
FieldNumber Word64
2)
  Parser RawMessage (DialStatusV2 -> DialResponseV2)
-> Parser RawMessage DialStatusV2
-> Parser RawMessage DialResponseV2
forall a b.
Parser RawMessage (a -> b)
-> Parser RawMessage a -> Parser RawMessage b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser RawField DialStatusV2
-> FieldNumber -> Parser RawMessage DialStatusV2
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
at ((Word32 -> DialStatusV2)
-> Parser RawField Word32 -> Parser RawField DialStatusV2
forall a b. (a -> b) -> Parser RawField a -> Parser RawField b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word32 -> DialStatusV2
dialStatusFromWord (Parser RawPrimitive Word32 -> Word32 -> Parser RawField Word32
forall a. Parser RawPrimitive a -> a -> Parser RawField a
one Parser RawPrimitive Word32
Decode.uint32 Word32
0)) (Word64 -> FieldNumber
FieldNumber Word64
3)

encodeDialBackV2 :: DialBackV2 -> ByteString
encodeDialBackV2 :: DialBackV2 -> ByteString
encodeDialBackV2 DialBackV2
dialBack = LazyByteString -> ByteString
BL.toStrict (LazyByteString -> ByteString)
-> (MessageBuilder -> LazyByteString)
-> MessageBuilder
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MessageBuilder -> LazyByteString
Encode.toLazyByteString (MessageBuilder -> ByteString) -> MessageBuilder -> ByteString
forall a b. (a -> b) -> a -> b
$
  FieldNumber -> Word64 -> MessageBuilder
Encode.fixed64 (Word64 -> FieldNumber
FieldNumber Word64
1) (DialBackV2 -> Word64
dbv2Nonce DialBackV2
dialBack)

decodeDialBackV2 :: ByteString -> Either String DialBackV2
decodeDialBackV2 :: ByteString -> Either String DialBackV2
decodeDialBackV2 ByteString
bytes = Either ParseError DialBackV2 -> Either String DialBackV2
forall a. Either ParseError a -> Either String a
firstParseError (Either ParseError DialBackV2 -> Either String DialBackV2)
-> Either ParseError DialBackV2 -> Either String DialBackV2
forall a b. (a -> b) -> a -> b
$ Parser RawMessage DialBackV2
-> ByteString -> Either ParseError DialBackV2
forall a. Parser RawMessage a -> ByteString -> Either ParseError a
parse Parser RawMessage DialBackV2
parser ByteString
bytes
  where
    parser :: Parser RawMessage DialBackV2
parser = Word64 -> DialBackV2
DialBackV2 (Word64 -> DialBackV2)
-> Parser RawMessage Word64 -> Parser RawMessage DialBackV2
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser RawField Word64 -> FieldNumber -> Parser RawMessage Word64
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
at (Parser RawPrimitive Word64 -> Word64 -> Parser RawField Word64
forall a. Parser RawPrimitive a -> a -> Parser RawField a
one Parser RawPrimitive Word64
Decode.fixed64 Word64
0) (Word64 -> FieldNumber
FieldNumber Word64
1)

-- | DialBackResponse has one enum field whose only valid value is OK=0.
encodeDialBackResponseV2 :: ByteString
encodeDialBackResponseV2 :: ByteString
encodeDialBackResponseV2 = ByteString
BS.empty

decodeDialBackResponseV2 :: ByteString -> Either String ()
decodeDialBackResponseV2 :: ByteString -> Either String ()
decodeDialBackResponseV2 ByteString
bytes = do
  status <- Either ParseError Word32 -> Either String Word32
forall a. Either ParseError a -> Either String a
firstParseError (Either ParseError Word32 -> Either String Word32)
-> Either ParseError Word32 -> Either String Word32
forall a b. (a -> b) -> a -> b
$ Parser RawMessage Word32 -> ByteString -> Either ParseError Word32
forall a. Parser RawMessage a -> ByteString -> Either ParseError a
parse Parser RawMessage Word32
parser ByteString
bytes
  if status == 0
    then Right ()
    else Left ("unknown AutoNAT v2 dial-back status: " <> show status)
  where
    parser :: Parser RawMessage Word32
parser = Parser RawField Word32 -> FieldNumber -> Parser RawMessage Word32
forall a. Parser RawField a -> FieldNumber -> Parser RawMessage a
at (Parser RawPrimitive Word32 -> Word32 -> Parser RawField Word32
forall a. Parser RawPrimitive a -> a -> Parser RawField a
one Parser RawPrimitive Word32
Decode.uint32 Word32
0) (Word64 -> FieldNumber
FieldNumber Word64
1)

writeAutoNATV2Message :: StreamIO -> AutoNATV2Message -> IO ()
writeAutoNATV2Message :: StreamIO -> AutoNATV2Message -> IO ()
writeAutoNATV2Message StreamIO
stream = StreamIO -> ByteString -> IO ()
writeFramed StreamIO
stream (ByteString -> IO ())
-> (AutoNATV2Message -> ByteString) -> AutoNATV2Message -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AutoNATV2Message -> ByteString
encodeAutoNATV2Message

readAutoNATV2Message :: StreamIO -> IO (Either String AutoNATV2Message)
readAutoNATV2Message :: StreamIO -> IO (Either String AutoNATV2Message)
readAutoNATV2Message StreamIO
stream =
  StreamIO
-> Int
-> (ByteString -> Either String AutoNATV2Message)
-> IO (Either String AutoNATV2Message)
forall a.
StreamIO
-> Int -> (ByteString -> Either String a) -> IO (Either String a)
readFramed StreamIO
stream Int
maxAutoNATV2MessageSize ByteString -> Either String AutoNATV2Message
decodeAutoNATV2Message

writeDialBackV2 :: StreamIO -> DialBackV2 -> IO ()
writeDialBackV2 :: StreamIO -> DialBackV2 -> IO ()
writeDialBackV2 StreamIO
stream = StreamIO -> ByteString -> IO ()
writeFramed StreamIO
stream (ByteString -> IO ())
-> (DialBackV2 -> ByteString) -> DialBackV2 -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DialBackV2 -> ByteString
encodeDialBackV2

readDialBackV2 :: StreamIO -> IO (Either String DialBackV2)
readDialBackV2 :: StreamIO -> IO (Either String DialBackV2)
readDialBackV2 StreamIO
stream =
  StreamIO
-> Int
-> (ByteString -> Either String DialBackV2)
-> IO (Either String DialBackV2)
forall a.
StreamIO
-> Int -> (ByteString -> Either String a) -> IO (Either String a)
readFramed StreamIO
stream Int
maxAutoNATV2DialBackMessageSize ByteString -> Either String DialBackV2
decodeDialBackV2

writeDialBackResponseV2 :: StreamIO -> IO ()
writeDialBackResponseV2 :: StreamIO -> IO ()
writeDialBackResponseV2 StreamIO
stream = StreamIO -> ByteString -> IO ()
writeFramed StreamIO
stream ByteString
encodeDialBackResponseV2

readDialBackResponseV2 :: StreamIO -> IO (Either String ())
readDialBackResponseV2 :: StreamIO -> IO (Either String ())
readDialBackResponseV2 StreamIO
stream =
  StreamIO
-> Int -> (ByteString -> Either String ()) -> IO (Either String ())
forall a.
StreamIO
-> Int -> (ByteString -> Either String a) -> IO (Either String a)
readFramed StreamIO
stream Int
maxAutoNATV2DialBackMessageSize ByteString -> Either String ()
decodeDialBackResponseV2

writeFramed :: StreamIO -> ByteString -> IO ()
writeFramed :: StreamIO -> ByteString -> IO ()
writeFramed StreamIO
stream ByteString
payload = StreamIO -> ByteString -> IO ()
streamWrite StreamIO
stream (ByteString -> IO ()) -> ByteString -> IO ()
forall a b. (a -> b) -> a -> b
$
  Word64 -> ByteString
encodeUvarint (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
BS.length ByteString
payload)) ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
payload

readFramed
  :: StreamIO
  -> Int
  -> (ByteString -> Either String a)
  -> IO (Either String a)
readFramed :: forall a.
StreamIO
-> Int -> (ByteString -> Either String a) -> IO (Either String a)
readFramed StreamIO
stream Int
maxSize ByteString -> Either String a
decoder = do
  prefix <- StreamIO -> IO ByteString
readVarintBytes StreamIO
stream
  case decodeUvarint prefix of
    Left String
err -> Either String a -> IO (Either String a)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String -> Either String a
forall a b. a -> Either a b
Left (String
"AutoNAT v2 varint decode error: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
err))
    Right (Word64
lengthWord, ByteString
_)
      | Word64
lengthWord Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
maxSize ->
          Either String a -> IO (Either String a)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String -> Either String a
forall a b. a -> Either a b
Left String
"AutoNAT v2 message exceeds the size limit")
      | Bool
otherwise -> do
          payload <- StreamIO -> Int -> Int -> IO (Either String ByteString)
readExactBounded StreamIO
stream Int
maxSize (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
lengthWord)
          pure $ payload >>= decoder

readVarintBytes :: StreamIO -> IO ByteString
readVarintBytes :: StreamIO -> IO ByteString
readVarintBytes StreamIO
stream = [Word8] -> Int -> IO ByteString
forall {t}. (Ord t, Num t) => [Word8] -> t -> IO ByteString
go [] (Int
0 :: Int)
  where
    go :: [Word8] -> t -> IO ByteString
go [Word8]
bytes t
count
      | t
count t -> t -> Bool
forall a. Ord a => a -> a -> Bool
>= t
10 = ByteString -> IO ByteString
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Word8] -> ByteString
BS.pack ([Word8] -> [Word8]
forall a. [a] -> [a]
reverse [Word8]
bytes))
      | Bool
otherwise = do
          byte <- StreamIO -> IO Word8
streamReadByte StreamIO
stream
          if byte < 0x80
            then pure (BS.pack (reverse (byte : bytes)))
            else go (byte : bytes) (count + 1)

responseStatusToWord :: ResponseStatusV2 -> Word32
responseStatusToWord :: ResponseStatusV2 -> Word32
responseStatusToWord ResponseStatusV2
ResponseInternalError = Word32
0
responseStatusToWord ResponseStatusV2
ResponseRequestRejected = Word32
100
responseStatusToWord ResponseStatusV2
ResponseDialRefused = Word32
101
responseStatusToWord ResponseStatusV2
ResponseOK = Word32
200
responseStatusToWord (ResponseStatusUnknown Word32
value) = Word32
value

responseStatusFromWord :: Word32 -> ResponseStatusV2
responseStatusFromWord :: Word32 -> ResponseStatusV2
responseStatusFromWord Word32
0 = ResponseStatusV2
ResponseInternalError
responseStatusFromWord Word32
100 = ResponseStatusV2
ResponseRequestRejected
responseStatusFromWord Word32
101 = ResponseStatusV2
ResponseDialRefused
responseStatusFromWord Word32
200 = ResponseStatusV2
ResponseOK
responseStatusFromWord Word32
value = Word32 -> ResponseStatusV2
ResponseStatusUnknown Word32
value

dialStatusToWord :: DialStatusV2 -> Word32
dialStatusToWord :: DialStatusV2 -> Word32
dialStatusToWord DialStatusV2
DialStatusUnused = Word32
0
dialStatusToWord DialStatusV2
DialStatusError = Word32
100
dialStatusToWord DialStatusV2
DialStatusBackError = Word32
101
dialStatusToWord DialStatusV2
DialStatusOK = Word32
200
dialStatusToWord (DialStatusUnknown Word32
value) = Word32
value

dialStatusFromWord :: Word32 -> DialStatusV2
dialStatusFromWord :: Word32 -> DialStatusV2
dialStatusFromWord Word32
0 = DialStatusV2
DialStatusUnused
dialStatusFromWord Word32
100 = DialStatusV2
DialStatusError
dialStatusFromWord Word32
101 = DialStatusV2
DialStatusBackError
dialStatusFromWord Word32
200 = DialStatusV2
DialStatusOK
dialStatusFromWord Word32
value = Word32 -> DialStatusV2
DialStatusUnknown Word32
value

firstParseError :: Either ParseError a -> Either String a
firstParseError :: forall a. Either ParseError a -> Either String a
firstParseError = (ParseError -> Either String a)
-> (a -> Either String a) -> Either ParseError a -> Either String a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Either String a
forall a b. a -> Either a b
Left (String -> Either String a)
-> (ParseError -> String) -> ParseError -> Either String a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseError -> String
forall a. Show a => a -> String
show) a -> Either String a
forall a b. b -> Either a b
Right