From 189885c50d433bf79555d3dfedd091b95c02af3a Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Mon, 5 Feb 2024 21:01:20 +0000 Subject: [PATCH] protocol: ignore tail bytes in handshake blocks to allow future extension (#983) * protocol: ignore tail bytes in handshake blocks to allow future extension * cleanup --- src/Simplex/Messaging/Transport.hs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Simplex/Messaging/Transport.hs b/src/Simplex/Messaging/Transport.hs index c67c812af..d0f371cef 100644 --- a/src/Simplex/Messaging/Transport.hs +++ b/src/Simplex/Messaging/Transport.hs @@ -65,6 +65,7 @@ import Control.Monad.Except import Control.Monad.Trans.Except (throwE) import qualified Data.Aeson.TH as J import Data.Attoparsec.ByteString.Char8 (Parser) +import qualified Data.Attoparsec.ByteString.Char8 as A import Data.Bifunctor (first) import Data.Bitraversable (bimapM) import Data.ByteString.Char8 (ByteString) @@ -80,7 +81,7 @@ import qualified Network.TLS.Extra as TE import qualified Paths_simplexmq as SMQ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding -import Simplex.Messaging.Parsers (dropPrefix, parse, parseRead1, sumTypeJSON) +import Simplex.Messaging.Parsers (dropPrefix, parseRead1, sumTypeJSON) import Simplex.Messaging.Transport.Buffer import Simplex.Messaging.Util (bshow, catchAll, catchAll_) import Simplex.Messaging.Version @@ -358,8 +359,9 @@ smpThHandle th v = (th :: THandle c) {thVersion = v, batch = v >= 4} sendHandshake :: (Transport c, Encoding smp) => THandle c -> smp -> ExceptT TransportError IO () sendHandshake th = ExceptT . tPutBlock th . smpEncode +-- ignores tail bytes to allow future extensions getHandshake :: (Transport c, Encoding smp) => THandle c -> ExceptT TransportError IO smp -getHandshake th = ExceptT $ (parse smpP (TEHandshake PARSE) =<<) <$> tGetBlock th +getHandshake th = ExceptT $ (first (\_ -> TEHandshake PARSE) . A.parseOnly smpP =<<) <$> tGetBlock th smpTHandle :: Transport c => c -> THandle c smpTHandle c = THandle {connection = c, sessionId = tlsUnique c, blockSize = smpBlockSize, thVersion = 0, batch = False}