summaryrefslogtreecommitdiff
path: root/src/Network/Tox/Session.hs
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2018-09-07 12:03:18 -0400
committerJoe Crayne <joe@jerkface.net>2018-09-07 20:26:11 -0400
commitf59f8c0acd3a098d7a93b71fcf10d84cc576b7ab (patch)
tree80ae19fde474f9888ecb88dce693f74647712719 /src/Network/Tox/Session.hs
parentfe6e8bf7cd367afe785ed50914bc2a20d272e546 (diff)
Fixed handling of lossy outgoing in Network.Lossless.
Diffstat (limited to 'src/Network/Tox/Session.hs')
-rw-r--r--src/Network/Tox/Session.hs29
1 files changed, 21 insertions, 8 deletions
diff --git a/src/Network/Tox/Session.hs b/src/Network/Tox/Session.hs
index 88221b11..7b84ba80 100644
--- a/src/Network/Tox/Session.hs
+++ b/src/Network/Tox/Session.hs
@@ -15,7 +15,7 @@ import Network.Lossless
15import Network.QueryResponse 15import Network.QueryResponse
16import Network.SessionTransports 16import Network.SessionTransports
17import Network.Tox.Crypto.Transport 17import Network.Tox.Crypto.Transport
18import Network.Tox.DHT.Transport (Cookie) 18import Network.Tox.DHT.Transport (Cookie,key2id)
19import Network.Tox.Handshake 19import Network.Tox.Handshake
20 20
21type SessionKey = SecretKey 21type SessionKey = SecretKey
@@ -69,7 +69,7 @@ plainHandshakeH sp saddr skey handshake = do
69 dput XNetCrypto $ prelude ++ "handshake: " ++ show (otherCookie hd, baseNonce hd) 69 dput XNetCrypto $ prelude ++ "handshake: " ++ show (otherCookie hd, baseNonce hd)
70 sent <- spGetSentHandshake sp skey saddr (handshakeCookie handshake) (otherCookie hd) 70 sent <- spGetSentHandshake sp skey saddr (handshakeCookie handshake) (otherCookie hd)
71 -- TODO: this is always returning sent = Nothing 71 -- TODO: this is always returning sent = Nothing
72 dput XNetCrypto $ prelude ++ "plainHandshakeH: cached outgoing: " ++ show (fmap (baseNonce . snd) sent) 72 dput XNetCrypto $ " <-- (cached) handshake baseNonce" ++ show (fmap (baseNonce . snd) sent)
73 forM_ sent $ \(hd_skey,hd_sent) -> do 73 forM_ sent $ \(hd_skey,hd_sent) -> do
74 sk <- SessionKeys (spCrypto sp) 74 sk <- SessionKeys (spCrypto sp)
75 hd_skey 75 hd_skey
@@ -81,7 +81,9 @@ plainHandshakeH sp saddr skey handshake = do
81 forM_ m $ \(sid, t) -> do 81 forM_ m $ \(sid, t) -> do
82 (t2,resend,getMissing) 82 (t2,resend,getMissing)
83 <- lossless (\cp a -> return $ fmap (,a) $ checkLossless $ runIdentity $ pktData cp) 83 <- lossless (\cp a -> return $ fmap (,a) $ checkLossless $ runIdentity $ pktData cp)
84 (\seqno p _ -> encryptPacket sk $ bookKeeping seqno p) 84 (\seqno p _ -> do
85 y <- encryptPacket sk $ bookKeeping seqno p
86 return (lossyness (msgID p) == Lossy, y))
85 () 87 ()
86 t 88 t
87 let _ = t :: TransportA String () (CryptoPacket Identity) (CryptoPacket Encrypted) 89 let _ = t :: TransportA String () (CryptoPacket Identity) (CryptoPacket Encrypted)
@@ -101,16 +103,22 @@ plainHandshakeH sp saddr skey handshake = do
101 103
102decryptPacket :: SessionKeys -> SockAddr -> CryptoPacket Encrypted -> IO (Maybe (CryptoPacket Identity, ())) 104decryptPacket :: SessionKeys -> SockAddr -> CryptoPacket Encrypted -> IO (Maybe (CryptoPacket Identity, ()))
103decryptPacket sk saddr (CryptoPacket n16 ciphered) = do 105decryptPacket sk saddr (CryptoPacket n16 ciphered) = do
104 (n,δ) <- atomically $ do 106 (n24,δ) <- atomically $ do
105 n <- readTVar (skNonceIncoming sk) 107 n <- readTVar (skNonceIncoming sk)
106 let δ = n16 - nonce24ToWord16 n 108 let δ = n16 - nonce24ToWord16 n
107 return ( n `addtoNonce24` fromIntegral δ, δ ) 109 return ( n `addtoNonce24` fromIntegral δ, δ )
108 secret <- lookupSharedSecret (skCrypto sk) (skMe sk) (skThem sk) n 110 secret <- lookupSharedSecret (skCrypto sk) (skMe sk) (skThem sk) n24
109 case decodePlain =<< decrypt secret ciphered of 111 case decodePlain =<< decrypt secret ciphered of
110 Left e -> return Nothing 112 Left e -> return Nothing
111 Right x -> do 113 Right x -> do
112 when ( δ > 43690 ) 114 when ( δ > 43690 )
113 $ atomically $ writeTVar (skNonceIncoming sk) (n `addtoNonce24` 21845) 115 $ atomically $ writeTVar (skNonceIncoming sk) (n24 `addtoNonce24` 21845)
116
117 do let them = key2id $ skThem sk
118 CryptoData ack seqno _ = x
119 cm = decodeRawCryptoMsg x
120 dput XNetCrypto $ unwords [take 8 (show them),"-->",show (msgID cm),show (n24,ack,seqno)]
121
114 return $ Just ( CryptoPacket n16 (pure x), () ) 122 return $ Just ( CryptoPacket n16 (pure x), () )
115 123
116encryptPacket :: SessionKeys -> CryptoData -> IO (CryptoPacket Encrypted) 124encryptPacket :: SessionKeys -> CryptoData -> IO (CryptoPacket Encrypted)
@@ -121,12 +129,17 @@ encryptPacket sk plain = do
121 return n24 129 return n24
122 secret <- lookupSharedSecret (skCrypto sk) (skMe sk) (skThem sk) n24 130 secret <- lookupSharedSecret (skCrypto sk) (skMe sk) (skThem sk) n24
123 let ciphered = encrypt secret $ encodePlain $ plain 131 let ciphered = encrypt secret $ encodePlain $ plain
132
133 do let them = key2id $ skThem sk
134 CryptoData ack seqno cm = plain
135 dput XNetCrypto $ unwords [take 8 (show them),"<--",show (msgID cm),show (n24,ack,seqno)]
136
124 return $ CryptoPacket (nonce24ToWord16 n24) ciphered 137 return $ CryptoPacket (nonce24ToWord16 n24) ciphered
125 138
126data SessionKeys = SessionKeys 139data SessionKeys = SessionKeys
127 { skCrypto :: TransportCrypto 140 { skCrypto :: TransportCrypto
128 , skMe :: SecretKey 141 , skMe :: SecretKey -- My session key
129 , skThem :: PublicKey 142 , skThem :: PublicKey -- Their session key
130 , skNonceIncoming :: TVar Nonce24 -- +21845 when a threshold is reached. 143 , skNonceIncoming :: TVar Nonce24 -- +21845 when a threshold is reached.
131 , skNonceOutgoing :: TVar Nonce24 -- +1 on every packet 144 , skNonceOutgoing :: TVar Nonce24 -- +1 on every packet
132 } 145 }