summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Network/Tox/Crypto/Transport.hs30
1 files changed, 29 insertions, 1 deletions
diff --git a/src/Network/Tox/Crypto/Transport.hs b/src/Network/Tox/Crypto/Transport.hs
index b89bde46..3133ee9b 100644
--- a/src/Network/Tox/Crypto/Transport.hs
+++ b/src/Network/Tox/Crypto/Transport.hs
@@ -154,10 +154,38 @@ data CryptoData = CryptoData
154 -- | [ uint32_t packet number if lossless 154 -- | [ uint32_t packet number if lossless
155 -- , sendbuffer buffer_end if lossy , (big endian)] 155 -- , sendbuffer buffer_end if lossy , (big endian)]
156 , bufferEnd :: Word32 156 , bufferEnd :: Word32
157 -- | [data] 157 -- | [data] (TODO See Note [Padding])
158 , bufferData :: CryptoMessage 158 , bufferData :: CryptoMessage
159 } 159 }
160 160
161{-
162Note [Padding]
163
164TODO: The 'bufferData' field of 'CryptoData' should probably be something like
165/Padded CryptoMessage/ because c-toxcore strips leading zeros on incoming and
166pads leading zeros on outgoing packets.
167
168After studying c-toxcore (at commit c49a6e7f5bc245a51a3c85cc2c8b7f881c412998),
169I've determined the following behavior.
170
171Incoming: All leading zero bytes are stripped until possibly the whole packet
172is consumed (in which case it is discarded). This happens at
173toxcore/net_crypto.c:1366:handle_data_packet_core().
174
175Outgoing: The number of zeros added is:
176
177 padding_length len = (1373 - len) `mod` 8 where
178
179where /len/ is the size of the non-padded CryptoMessage. This happens at
180toxcore/net_crypto.c:936:send_data_packet_helper()
181
182The number 1373 is written in C as MAX_CRYPTO_DATA_SIZE which is defined in
183terms of the max /NetCrypto/ packet size (1400) minus the minimum possible size
184of an id-byte (1) and a /CryptoPacket Encrypted/ ( 2 + 4 + 4 + 16 ).
185
186One effect of this is that short messages will be padded to at least 5 bytes.
187-}
188
161instance Serialize CryptoData where 189instance Serialize CryptoData where
162 get = CryptoData <$> get <*> get <*> get 190 get = CryptoData <$> get <*> get <*> get
163 put (CryptoData start end dta) = put start >> put end >> put dta 191 put (CryptoData start end dta) = put start >> put end >> put dta