Skip to content

Commit 23aff6b

Browse files
committed
Revert "smp server: remove dependency of message size on the version (#1627)"
This reverts commit 8fea152.
1 parent 3335cd5 commit 23aff6b

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

src/Simplex/Messaging/Protocol.hs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,18 @@ currentSMPClientVersion = VersionSMPC 4
292292
supportedSMPClientVRange :: VersionRangeSMPC
293293
supportedSMPClientVRange = mkVersionRange initialSMPClientVersion currentSMPClientVersion
294294

295-
maxMessageLength :: Int
296-
maxMessageLength = 16048
295+
-- TODO v6.0 remove dependency on version
296+
maxMessageLength :: VersionSMP -> Int
297+
maxMessageLength v
298+
| v >= encryptedBlockSMPVersion = 16048 -- max 16048
299+
| v >= sendingProxySMPVersion = 16064 -- max 16067
300+
| otherwise = 16088 -- 16048 - always use this size to determine allowed ranges
297301

298302
paddedProxiedTLength :: Int
299303
paddedProxiedTLength = 16226 -- 16225 .. 16227
300304

301-
type MaxMessageLen = 16048
305+
-- TODO v7.0 change to 16048
306+
type MaxMessageLen = 16088
302307

303308
-- 16 extra bytes: 8 for timestamp and 8 for flags (7 flags and the space, only 1 flag is currently used)
304309
type MaxRcvMessageLen = MaxMessageLen + 16 -- 16104, the padded size is 16106
@@ -1472,7 +1477,7 @@ data ErrorType
14721477
STORE {storeErr :: Text}
14731478
| -- | ACK command is sent without message to be acknowledged
14741479
NO_MSG
1475-
| -- | sent message is too large (> maxMessageLength = 16048 bytes)
1480+
| -- | sent message is too large (> maxMessageLength = 16088 bytes)
14761481
LARGE_MSG
14771482
| -- | relay public key is expired
14781483
EXPIRED

src/Simplex/Messaging/Server.hs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,10 +1349,11 @@ client
13491349
ms
13501350
clnt@Client {clientId, ntfSubscriptions, ntfServiceSubscribed, serviceSubsCount = _todo', ntfServiceSubsCount, rcvQ, sndQ, clientTHParams = thParams'@THandleParams {sessionId}, procThreads} = do
13511351
labelMyThread . B.unpack $ "client $" <> encode sessionId <> " commands"
1352-
let clntServiceId = (\THClientService {serviceId} -> serviceId) <$> (peerClientService =<< thAuth thParams')
1352+
let THandleParams {thVersion} = thParams'
1353+
clntServiceId = (\THClientService {serviceId} -> serviceId) <$> (peerClientService =<< thAuth thParams')
13531354
process t acc@(rs, msgs) =
13541355
(maybe acc (\(!r, !msg_) -> (r : rs, maybe msgs (: msgs) msg_)))
1355-
<$> processCommand clntServiceId t
1356+
<$> processCommand clntServiceId thVersion t
13561357
forever $
13571358
atomically (readTBQueue rcvQ)
13581359
>>= foldrM process ([], [])
@@ -1438,8 +1439,8 @@ client
14381439
mkIncProxyStats ps psOwn own sel = do
14391440
incStat $ sel ps
14401441
when own $ incStat $ sel psOwn
1441-
processCommand :: Maybe ServiceId -> VerifiedTransmission s -> M s (Maybe ResponseAndMessage)
1442-
processCommand clntServiceId (q_, (corrId, entId, cmd)) = case cmd of
1442+
processCommand :: Maybe ServiceId -> VersionSMP -> VerifiedTransmission s -> M s (Maybe ResponseAndMessage)
1443+
processCommand clntServiceId clntVersion (q_, (corrId, entId, cmd)) = case cmd of
14431444
Cmd SProxiedClient command -> processProxiedCmd (corrId, entId, command)
14441445
Cmd SSender command -> case command of
14451446
SKEY k -> withQueue $ \q qr -> checkMode QMMessaging qr $ secureQueue_ q k
@@ -1828,7 +1829,7 @@ client
18281829

18291830
sendMessage :: MsgFlags -> MsgBody -> StoreQueue s -> QueueRec -> M s (Transmission BrokerMsg)
18301831
sendMessage msgFlags msgBody q qr
1831-
| B.length msgBody > maxMessageLength = do
1832+
| B.length msgBody > maxMessageLength clntVersion = do
18321833
stats <- asks serverStats
18331834
incStat $ msgSentLarge stats
18341835
pure $ err LARGE_MSG
@@ -1981,7 +1982,7 @@ client
19811982
-- rejectOrVerify filters allowed commands, no need to repeat it here.
19821983
-- INTERNAL is used because processCommand never returns Nothing for sender commands (could be extracted for better types).
19831984
-- `fst` removes empty message that is only returned for `SUB` command
1984-
Right t''@(_, (corrId', entId', _)) -> maybe (corrId', entId', ERR INTERNAL) fst <$> lift (processCommand Nothing t'')
1985+
Right t''@(_, (corrId', entId', _)) -> maybe (corrId', entId', ERR INTERNAL) fst <$> lift (processCommand Nothing fwdVersion t'')
19851986
-- encode response
19861987
r' <- case batchTransmissions clntTHParams [Right (Nothing, encodeTransmission clntTHParams r)] of
19871988
[] -> throwE INTERNAL -- at least 1 item is guaranteed from NonEmpty/Right

tests/CoreTests/BatchingTests.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ randomMSG = do
389389
corrId <- atomically $ C.randomBytes 24 g
390390
rId <- atomically $ C.randomBytes 24 g
391391
msgId <- atomically $ C.randomBytes 24 g
392-
msg <- atomically $ C.randomBytes maxMessageLength g
392+
msg <- atomically $ C.randomBytes (maxMessageLength currentClientSMPRelayVersion) g
393393
pure (CorrId corrId, EntityId rId, MSG RcvMessage {msgId, msgBody = EncRcvMsgBody msg})
394394

395395
randomSENDv6 :: ByteString -> Int -> IO (Either TransportError (Maybe TAuthorizations, ByteString))

tests/SMPProxyTests.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ smpProxyTests = do
6969
let srv1 = SMPServer testHost testPort testKeyHash
7070
srv2 = SMPServer testHost2 testPort2 testKeyHash
7171
describe "client API" $ do
72+
let maxLen = maxMessageLength encryptedBlockSMPVersion
7273
describe "one server" $ do
7374
it "deliver via proxy" . oneServer $ do
7475
deliverMessageViaProxy srv1 srv1 C.SEd448 "hello 1" "hello 2"
@@ -77,7 +78,7 @@ smpProxyTests = do
7778
relayServ = srv2
7879
(msg1, msg2) <- runIO $ do
7980
g <- C.newRandom
80-
atomically $ (,) <$> C.randomBytes maxMessageLength g <*> C.randomBytes maxMessageLength g
81+
atomically $ (,) <$> C.randomBytes maxLen g <*> C.randomBytes maxLen g
8182
it "deliver via proxy" . twoServersFirstProxy $
8283
deliverMessageViaProxy proxyServ relayServ C.SEd448 "hello 1" "hello 2"
8384
it "max message size, Ed448 keys" . twoServersFirstProxy $

tests/ServerTests.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,12 @@ testCreateSecure =
232232
Resp "dabc" _ err5 <- sendRecv s ("", "dabc", sId, _SEND "hello")
233233
(err5, ERR AUTH) #== "rejects unsigned SEND"
234234

235-
let maxAllowedMessage = B.replicate maxMessageLength '-'
235+
let maxAllowedMessage = B.replicate (maxMessageLength currentClientSMPRelayVersion) '-'
236236
Resp "bcda" _ OK <- signSendRecv s sKey ("bcda", sId, _SEND maxAllowedMessage)
237237
Resp "" _ (Msg mId3 msg3) <- tGet1 r
238238
(dec mId3 msg3, Right maxAllowedMessage) #== "delivers message of max size"
239239

240-
let biggerMessage = B.replicate (maxMessageLength + 1) '-'
240+
let biggerMessage = B.replicate (maxMessageLength currentClientSMPRelayVersion + 1) '-'
241241
Resp "bcda" _ (ERR LARGE_MSG) <- signSendRecv s sKey ("bcda", sId, _SEND biggerMessage)
242242
pure ()
243243

@@ -279,12 +279,12 @@ testCreateSndSecure =
279279
Resp "dabc" _ err5 <- sendRecv s ("", "dabc", sId, _SEND "hello")
280280
(err5, ERR AUTH) #== "rejects unsigned SEND"
281281

282-
let maxAllowedMessage = B.replicate maxMessageLength '-'
282+
let maxAllowedMessage = B.replicate (maxMessageLength currentClientSMPRelayVersion) '-'
283283
Resp "bcda" _ OK <- signSendRecv s sKey ("bcda", sId, _SEND maxAllowedMessage)
284284
Resp "" _ (Msg mId3 msg3) <- tGet1 r
285285
(dec mId3 msg3, Right maxAllowedMessage) #== "delivers message of max size"
286286

287-
let biggerMessage = B.replicate (maxMessageLength + 1) '-'
287+
let biggerMessage = B.replicate (maxMessageLength currentClientSMPRelayVersion + 1) '-'
288288
Resp "bcda" _ (ERR LARGE_MSG) <- signSendRecv s sKey ("bcda", sId, _SEND biggerMessage)
289289
pure ()
290290

0 commit comments

Comments
 (0)