summaryrefslogtreecommitdiff
path: root/Tox.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-07-13 16:53:05 -0400
committerjoe <joe@jerkface.net>2017-07-13 16:53:05 -0400
commit36562749e2204da4500742c7f62676c19f0ce999 (patch)
tree1233136f067bf40cdefb23901680b12d36fc357e /Tox.hs
parent0ba49d7ba71f634cc7692124874a72dfad14d425 (diff)
TOX rewrite: Response nonces are now distinct from query nonces.
Diffstat (limited to 'Tox.hs')
-rw-r--r--Tox.hs22
1 files changed, 14 insertions, 8 deletions
diff --git a/Tox.hs b/Tox.hs
index e1a1bf8a..77cd0ae0 100644
--- a/Tox.hs
+++ b/Tox.hs
@@ -240,16 +240,16 @@ encodePacket :: SecretKey -> SecretsCache -> Message ByteString -> NodeInfo -> (
240encodePacket sk cache msg ni = ( S.runPut . putMessage $ encryptMessage sk cache (nodeId ni) msg 240encodePacket sk cache msg ni = ( S.runPut . putMessage $ encryptMessage sk cache (nodeId ni) msg
241 , nodeAddr ni ) 241 , nodeAddr ni )
242 242
243newClient :: SockAddr -> IO (Client String Method TransactionId NodeInfo (Message ByteString) ()) 243newClient :: SockAddr -> IO (Client String Method TransactionId NodeInfo (Message ByteString))
244newClient addr = do 244newClient addr = do
245 udp <- udpTransport addr 245 udp <- udpTransport addr
246 secret <- generateSecretKey 246 secret <- generateSecretKey
247 let pubkey = key2id $ toPublic secret 247 let pubkey = key2id $ toPublic secret
248 cache <- newEmptyCache 248 cache <- newEmptyCache
249 drg <- getSystemDRG 249 drg <- getSystemDRG
250 let me = NodeInfo pubkey (fromMaybe (toEnum 0) $ fromSockAddr addr) 250 self <- atomically $ newTVar
251 (fromMaybe 0 $ sockAddrPort addr) 251 $ NodeInfo pubkey (fromMaybe (toEnum 0) $ fromSockAddr addr)
252 tox <- atomically $ newTVar (me,()) 252 (fromMaybe 0 $ sockAddrPort addr)
253 let net = layerTransport (parsePacket secret cache) 253 let net = layerTransport (parsePacket secret cache)
254 (encodePacket secret cache) 254 (encodePacket secret cache)
255 udp 255 udp
@@ -258,12 +258,18 @@ newClient addr = do
258 , lookupHandler = handlers 258 , lookupHandler = handlers
259 , tableMethods = tbl 259 , tableMethods = tbl
260 } 260 }
261 genNonce24 var (TransactionId nonce8 _) = atomically $ do
262 (g,pending) <- readTVar var
263 let (bs, g') = randomBytesGenerate 24 g
264 writeTVar var (g',pending)
265 return $ TransactionId nonce8 (Nonce24 bs)
261 client tbl var = Client 266 client tbl var = Client
262 { clientNet = net 267 { clientNet = net
263 , clientDispatcher = dispatch tbl 268 , clientDispatcher = dispatch tbl
264 , clientErrorReporter = ignoreErrors -- TODO 269 , clientErrorReporter = ignoreErrors -- TODO
265 , clientPending = var 270 , clientPending = var
266 , clientContext = atomically (readTVar tox) 271 , clientAddress = atomically (readTVar self)
272 , clientResponseId = genNonce24 var
267 } 273 }
268 if fitsInInt (Proxy :: Proxy Word64) 274 if fitsInInt (Proxy :: Proxy Word64)
269 then do 275 then do
@@ -300,7 +306,7 @@ classify (Message { msgType = typ
300 PongType -> IsResponse 306 PongType -> IsResponse
301 SendNodesType -> IsResponse 307 SendNodesType -> IsResponse
302 308
303encodePayload typ _ (TransactionId (Nonce8 tid) nonce) self dest b 309encodePayload typ (TransactionId (Nonce8 tid) nonce) self dest b
304 = Message { msgType = typ 310 = Message { msgType = typ
305 , msgOrigin = nodeId self 311 , msgOrigin = nodeId self
306 , msgNonce = nonce 312 , msgNonce = nonce
@@ -312,8 +318,8 @@ decodePayload msg = S.decode $ dropEnd8 $ msgPayload msg
312 318
313handler typ f = Just $ MethodHandler decodePayload (encodePayload typ) f 319handler typ f = Just $ MethodHandler decodePayload (encodePayload typ) f
314 320
315handlers :: Method -> Maybe (MethodHandler String TransactionId NodeInfo (Message ByteString) ()) 321handlers :: Method -> Maybe (MethodHandler String TransactionId NodeInfo (Message ByteString))
316handlers PingType = error "handler PingType pingH" 322handlers PingType = handler PingType pingH
317handlers GetNodesType = error "find_node" 323handlers GetNodesType = error "find_node"
318handlers _ = Nothing 324handlers _ = Nothing
319 325