diff options
author | joe <joe@jerkface.net> | 2017-07-13 16:53:05 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-07-13 16:53:05 -0400 |
commit | 36562749e2204da4500742c7f62676c19f0ce999 (patch) | |
tree | 1233136f067bf40cdefb23901680b12d36fc357e /Tox.hs | |
parent | 0ba49d7ba71f634cc7692124874a72dfad14d425 (diff) |
TOX rewrite: Response nonces are now distinct from query nonces.
Diffstat (limited to 'Tox.hs')
-rw-r--r-- | Tox.hs | 22 |
1 files changed, 14 insertions, 8 deletions
@@ -240,16 +240,16 @@ encodePacket :: SecretKey -> SecretsCache -> Message ByteString -> NodeInfo -> ( | |||
240 | encodePacket sk cache msg ni = ( S.runPut . putMessage $ encryptMessage sk cache (nodeId ni) msg | 240 | encodePacket sk cache msg ni = ( S.runPut . putMessage $ encryptMessage sk cache (nodeId ni) msg |
241 | , nodeAddr ni ) | 241 | , nodeAddr ni ) |
242 | 242 | ||
243 | newClient :: SockAddr -> IO (Client String Method TransactionId NodeInfo (Message ByteString) ()) | 243 | newClient :: SockAddr -> IO (Client String Method TransactionId NodeInfo (Message ByteString)) |
244 | newClient addr = do | 244 | newClient 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 | ||
303 | encodePayload typ _ (TransactionId (Nonce8 tid) nonce) self dest b | 309 | encodePayload 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 | ||
313 | handler typ f = Just $ MethodHandler decodePayload (encodePayload typ) f | 319 | handler typ f = Just $ MethodHandler decodePayload (encodePayload typ) f |
314 | 320 | ||
315 | handlers :: Method -> Maybe (MethodHandler String TransactionId NodeInfo (Message ByteString) ()) | 321 | handlers :: Method -> Maybe (MethodHandler String TransactionId NodeInfo (Message ByteString)) |
316 | handlers PingType = error "handler PingType pingH" | 322 | handlers PingType = handler PingType pingH |
317 | handlers GetNodesType = error "find_node" | 323 | handlers GetNodesType = error "find_node" |
318 | handlers _ = Nothing | 324 | handlers _ = Nothing |
319 | 325 | ||