improve message error handling (#286)

* message envelope sizes and protocol doc corrections

* change error handling in message delivery loop
This commit is contained in:
Evgeny Poberezkin
2022-01-12 10:36:22 +00:00
committed by GitHub
parent d1eba1ef2f
commit bfa4911217
6 changed files with 65 additions and 58 deletions
+16 -12
View File
@@ -458,18 +458,20 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} connId sq = do
withStore (\st -> E.try $ getPendingMsgData st connId msgId) >>= \case
Left (e :: E.SomeException) ->
notify $ MERR mId (INTERNAL $ show e)
Right (rq_, (msgType, msgBody)) -> do
withRetryInterval ri $ \loop -> do
Right (rq_, (msgType, msgBody)) ->
withRetryInterval ri $ \loop ->
tryError (sendAgentMessage c sq msgBody) >>= \case
Left e -> case e of
SMP SMP.QUOTA -> loop
SMP SMP.AUTH -> case msgType of
HELLO_ -> loop
REPLY_ -> notify $ ERR e
A_MSG_ -> notify $ MERR mId e
SMP {} -> notify $ MERR mId e
CMD {} -> notify $ MERR mId e
_ -> loop
Left e -> do
case e of
SMP SMP.QUOTA -> loop
SMP SMP.AUTH -> case msgType of
HELLO_ -> loop
REPLY_ -> notify (ERR e) >> delMsg msgId
A_MSG_ -> notify (MERR mId e) >> delMsg msgId
SMP (SMP.CMD _) -> notify (MERR mId e) >> delMsg msgId
SMP SMP.LARGE_MSG -> notify (MERR mId e) >> delMsg msgId
SMP {} -> notify (MERR mId e) >> loop
_ -> loop
Right () -> do
case msgType of
HELLO_ -> do
@@ -483,8 +485,10 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} connId sq = do
_ -> createReplyQueue c connId sq
A_MSG_ -> notify $ SENT mId
_ -> pure ()
withStore $ \st -> deleteMsg st connId msgId
delMsg msgId
where
delMsg :: InternalId -> m ()
delMsg msgId = withStore $ \st -> deleteMsg st connId msgId
notify :: ACommand 'Agent -> m ()
notify cmd = atomically $ writeTBQueue subQ ("", connId, cmd)
+4 -4
View File
@@ -161,7 +161,7 @@ e2eEncConnInfoLength :: Int
e2eEncConnInfoLength = 14848
e2eEncUserMsgLength :: Int
e2eEncUserMsgLength = 15876
e2eEncUserMsgLength = 15856
-- | Raw (unparsed) SMP agent protocol transmission.
type ARawTransmission = (ByteString, ByteString, ByteString)
@@ -304,9 +304,9 @@ data AgentMsgEnvelope
encAgentMessage :: ByteString
}
| AgentInvitation -- the connInfo in contactInvite is only encrypted with per-queue E2E, not with double ratchet,
{ agentVersion :: !Version,
connReq :: !(ConnectionRequestUri 'CMInvitation),
connInfo :: !ByteString -- this message is only encrypted with per-queue E2E, not with double ratchet,
{ agentVersion :: Version,
connReq :: (ConnectionRequestUri 'CMInvitation),
connInfo :: ByteString -- this message is only encrypted with per-queue E2E, not with double ratchet,
}
deriving (Show)
+8 -8
View File
@@ -285,22 +285,22 @@ instance AlgorithmI a => Encoding (MsgHeader a) where
data EncMessageHeader = EncMessageHeader
{ ehVersion :: Version,
ehBody :: ByteString,
ehIV :: IV,
ehAuthTag :: AuthTag,
ehIV :: IV
ehBody :: ByteString
}
instance Encoding EncMessageHeader where
smpEncode EncMessageHeader {ehVersion, ehBody, ehAuthTag, ehIV} =
smpEncode (ehVersion, ehBody, ehAuthTag, ehIV)
smpEncode EncMessageHeader {ehVersion, ehIV, ehAuthTag, ehBody} =
smpEncode (ehVersion, ehIV, ehAuthTag, ehBody)
smpP = do
(ehVersion, ehBody, ehAuthTag, ehIV) <- smpP
pure EncMessageHeader {ehVersion, ehBody, ehAuthTag, ehIV}
(ehVersion, ehIV, ehAuthTag, ehBody) <- smpP
pure EncMessageHeader {ehVersion, ehIV, ehAuthTag, ehBody}
data EncRatchetMessage = EncRatchetMessage
{ emHeader :: ByteString,
emBody :: ByteString,
emAuthTag :: AuthTag
emAuthTag :: AuthTag,
emBody :: ByteString
}
instance Encoding EncRatchetMessage where
+4 -4
View File
@@ -108,10 +108,6 @@ instance Encoding SystemTime where
smpEncode = smpEncode . systemSeconds
smpP = MkSystemTime <$> smpP <*> pure 0
instance (Encoding a, Encoding b) => Encoding (a, b) where
smpEncode (a, b) = smpEncode a <> smpEncode b
smpP = (,) <$> smpP <*> smpP
-- lists encode/parse as a sequence of items prefixed with list length (as 1 byte)
smpEncodeList :: Encoding a => [a] -> ByteString
smpEncodeList xs = B.cons (lenEncode $ length xs) . B.concat $ map smpEncode xs
@@ -130,6 +126,10 @@ instance Encoding a => Encoding (L.NonEmpty a) where
0 -> fail "empty list"
n -> L.fromList <$> A.count n smpP
instance (Encoding a, Encoding b) => Encoding (a, b) where
smpEncode (a, b) = smpEncode a <> smpEncode b
smpP = (,) <$> smpP <*> smpP
instance (Encoding a, Encoding b, Encoding c) => Encoding (a, b, c) where
smpEncode (a, b, c) = smpEncode a <> smpEncode b <> smpEncode c
smpP = (,,) <$> smpP <*> smpP <*> smpP
+3 -3
View File
@@ -118,14 +118,14 @@ smpClientVRange :: VersionRange
smpClientVRange = mkVersionRange 1 smpClientVersion
maxMessageLength :: Int
maxMessageLength = 16078
maxMessageLength = 16088
-- it is shorter to allow per-queue e2e encryption DH key in the "public" header
e2eEncConfirmationLength :: Int
e2eEncConfirmationLength = 15942
e2eEncConfirmationLength = 15936
e2eEncMessageLength :: Int
e2eEncMessageLength = 16030
e2eEncMessageLength = 16032
-- | SMP protocol clients
data Party = Recipient | Sender | Notifier