diff options
Diffstat (limited to 'src/Network/Tox/Crypto/Handlers.hs')
-rw-r--r-- | src/Network/Tox/Crypto/Handlers.hs | 52 |
1 files changed, 43 insertions, 9 deletions
diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs index dcc63ae0..306433c1 100644 --- a/src/Network/Tox/Crypto/Handlers.hs +++ b/src/Network/Tox/Crypto/Handlers.hs | |||
@@ -62,8 +62,11 @@ data SessionView = SessionView | |||
62 | , svDownloadDir :: TVar FilePath -- ^ where to put files the user downloads | 62 | , svDownloadDir :: TVar FilePath -- ^ where to put files the user downloads |
63 | } | 63 | } |
64 | 64 | ||
65 | type SessionID = Word64 | ||
65 | 66 | ||
66 | data NetCryptoSession = NCrypto { ncState :: TVar NetCryptoSessionStatus | 67 | data NetCryptoSession = NCrypto { ncState :: TVar NetCryptoSessionStatus |
68 | , ncSessionId :: SessionID | ||
69 | , ncTheirPublicKey :: PublicKey -- Tox id w/o nospam | ||
67 | , ncTheirBaseNonce :: TVar Nonce24 -- base nonce + packet number | 70 | , ncTheirBaseNonce :: TVar Nonce24 -- base nonce + packet number |
68 | , ncMyPacketNonce :: TVar Nonce24 -- base nonce + packet number | 71 | , ncMyPacketNonce :: TVar Nonce24 -- base nonce + packet number |
69 | , ncHandShake :: TVar (Maybe (Handshake Encrypted)) | 72 | , ncHandShake :: TVar (Maybe (Handshake Encrypted)) |
@@ -86,7 +89,7 @@ data NetCryptoSession = NCrypto { ncState :: TVar NetCryptoSessionStatus | |||
86 | , ncView :: TVar SessionView | 89 | , ncView :: TVar SessionView |
87 | , ncPacketQueue :: PacketQueue CryptoData | 90 | , ncPacketQueue :: PacketQueue CryptoData |
88 | , ncBufferStart :: TVar Word32 | 91 | , ncBufferStart :: TVar Word32 |
89 | , ncDequeueThread :: ThreadId | 92 | , ncDequeueThread :: Maybe ThreadId |
90 | } | 93 | } |
91 | 94 | ||
92 | data NetCryptoSessions = NCSessions { netCryptoSessions :: TVar (Map.Map SockAddr NetCryptoSession) | 95 | data NetCryptoSessions = NCSessions { netCryptoSessions :: TVar (Map.Map SockAddr NetCryptoSession) |
@@ -97,14 +100,34 @@ data NetCryptoSessions = NCSessions { netCryptoSessions :: TVar (Map.Map SockAdd | |||
97 | , sessionView :: SessionView | 100 | , sessionView :: SessionView |
98 | , msgTypeArray :: MsgTypeArray | 101 | , msgTypeArray :: MsgTypeArray |
99 | , inboundQueueCapacity :: Word32 | 102 | , inboundQueueCapacity :: Word32 |
103 | , nextSessionId :: TVar SessionID | ||
100 | } | 104 | } |
101 | 105 | ||
106 | forgetCrypto :: TransportCrypto -> NetCryptoSessions -> NetCryptoSession -> STM () | ||
107 | forgetCrypto crypto (NCSessions {netCryptoSessions,netCryptoSessionsByKey}) session = do | ||
108 | let addr = ncSockAddr session | ||
109 | sid = ncSessionId session | ||
110 | sPubKey = ncTheirPublicKey session | ||
111 | byAddrMap <- readTVar netCryptoSessions | ||
112 | {- byKeyMap <- readTVar netCryptoSessionsByKey -} | ||
113 | case Map.lookup addr byAddrMap of | ||
114 | Nothing -> return () -- already gone | ||
115 | Just _ -> do | ||
116 | modifyTVar netCryptoSessions (Map.delete addr) | ||
117 | modifyTVar netCryptoSessionsByKey (Map.update (\xs -> case filter (\x -> ncSessionId x /= sid) xs of | ||
118 | [] -> Nothing | ||
119 | ys -> Just ys) sPubKey) | ||
120 | |||
102 | -- | initiate a netcrypto session, blocking | 121 | -- | initiate a netcrypto session, blocking |
103 | netCrypto :: TransportCrypto -> NetCryptoSessions -> SecretKey -> PublicKey -> IO NetCryptoSession | 122 | netCrypto :: TransportCrypto -> NetCryptoSessions -> SecretKey -> PublicKey -> IO NetCryptoSession |
104 | netCrypto crypto allsessions myseckey theirpubkey = do | 123 | netCrypto crypto allsessions myseckey theirpubkey = do |
105 | -- convert public key to NodeInfo check Roster | 124 | -- convert public key to NodeInfo check Roster |
106 | -- if no session: | 125 | -- if no session: |
107 | -- 1) send dht key | 126 | -- 1) send dht key, actually maybe send dht-key regardless |
127 | -- 2) send handshakes to last seen ip's, if any | ||
128 | -- | ||
129 | -- if sessions found, is it using this private key? | ||
130 | -- if not, send handshake, this is separate session | ||
108 | error "todo" | 131 | error "todo" |
109 | 132 | ||
110 | newSessionsState :: TransportCrypto | 133 | newSessionsState :: TransportCrypto |
@@ -124,6 +147,7 @@ newSessionsState crypto unrechook hooks = do | |||
124 | configdir <- sensibleVarLib pname | 147 | configdir <- sensibleVarLib pname |
125 | homedir <- getHomeDirectory | 148 | homedir <- getHomeDirectory |
126 | svDownloadDir0 <- atomically $ newTVar (homedir </> "Downloads") | 149 | svDownloadDir0 <- atomically $ newTVar (homedir </> "Downloads") |
150 | nextSessionId0 <- atomically $ newTVar 0 | ||
127 | return NCSessions { netCryptoSessions = x | 151 | return NCSessions { netCryptoSessions = x |
128 | , netCryptoSessionsByKey = x2 | 152 | , netCryptoSessionsByKey = x2 |
129 | , transportCrypto = crypto | 153 | , transportCrypto = crypto |
@@ -140,6 +164,7 @@ newSessionsState crypto unrechook hooks = do | |||
140 | } | 164 | } |
141 | , msgTypeArray = allMsgTypes -- todo make this a parameter | 165 | , msgTypeArray = allMsgTypes -- todo make this a parameter |
142 | , inboundQueueCapacity = 200 | 166 | , inboundQueueCapacity = 200 |
167 | , nextSessionId = nextSessionId0 | ||
143 | } | 168 | } |
144 | 169 | ||
145 | data HandshakeParams | 170 | data HandshakeParams |
@@ -180,6 +205,8 @@ newHandShakeData crypto basenonce (HParam {hpOtherCookie,hpMySecretKey,hpCookieR | |||
180 | }) freshCookie | 205 | }) freshCookie |
181 | 206 | ||
182 | -- | called when we recieve a crypto handshake with valid cookie | 207 | -- | called when we recieve a crypto handshake with valid cookie |
208 | -- TODO set priority on contact addr to 0 if it is older than ForgetPeriod, | ||
209 | -- then increment it regardless. (Keep addr in MinMaxPSQ in Roster.Contact) | ||
183 | freshCryptoSession :: NetCryptoSessions -> SockAddr -> HandshakeParams -> IO () | 210 | freshCryptoSession :: NetCryptoSessions -> SockAddr -> HandshakeParams -> IO () |
184 | freshCryptoSession sessions | 211 | freshCryptoSession sessions |
185 | addr | 212 | addr |
@@ -194,6 +221,10 @@ freshCryptoSession sessions | |||
194 | let crypto = transportCrypto sessions | 221 | let crypto = transportCrypto sessions |
195 | allsessions = netCryptoSessions sessions | 222 | allsessions = netCryptoSessions sessions |
196 | allsessionsByKey = netCryptoSessionsByKey sessions | 223 | allsessionsByKey = netCryptoSessionsByKey sessions |
224 | sessionId <- atomically $ do | ||
225 | x <- readTVar (nextSessionId sessions) | ||
226 | modifyTVar (nextSessionId sessions) (+1) | ||
227 | return x | ||
197 | ncState0 <- atomically $ newTVar Accepted | 228 | ncState0 <- atomically $ newTVar Accepted |
198 | ncTheirBaseNonce0 <- atomically $ newTVar theirBaseNonce | 229 | ncTheirBaseNonce0 <- atomically $ newTVar theirBaseNonce |
199 | n24 <- atomically $ transportNewNonce crypto | 230 | n24 <- atomically $ transportNewNonce crypto |
@@ -220,6 +251,8 @@ freshCryptoSession sessions | |||
220 | bufstart <- atomically $ newTVar 0 | 251 | bufstart <- atomically $ newTVar 0 |
221 | let netCryptoSession0 = | 252 | let netCryptoSession0 = |
222 | NCrypto { ncState = ncState0 | 253 | NCrypto { ncState = ncState0 |
254 | , ncSessionId = sessionId | ||
255 | , ncTheirPublicKey = remotePublicKey | ||
223 | , ncTheirBaseNonce= ncTheirBaseNonce0 | 256 | , ncTheirBaseNonce= ncTheirBaseNonce0 |
224 | , ncMyPacketNonce = ncMyPacketNonce0 | 257 | , ncMyPacketNonce = ncMyPacketNonce0 |
225 | , ncHandShake = ncHandShake0 | 258 | , ncHandShake = ncHandShake0 |
@@ -235,16 +268,16 @@ freshCryptoSession sessions | |||
235 | , ncView = ncView0 | 268 | , ncView = ncView0 |
236 | , ncPacketQueue = pktq | 269 | , ncPacketQueue = pktq |
237 | , ncBufferStart = bufstart | 270 | , ncBufferStart = bufstart |
238 | , ncDequeueThread = error "you want the NetCrypto-Dequeue thread id, but is it started?" | 271 | , ncDequeueThread = Nothing -- error "you want the NetCrypto-Dequeue thread id, but is it started?" |
239 | } | 272 | } |
240 | threadid <- forkIO $ do | 273 | threadid <- forkIO $ do |
241 | tid <- myThreadId | 274 | tid <- myThreadId |
242 | labelThread tid ("NetCryptoDequeue." ++ show (key2id remotePublicKey)) | 275 | labelThread tid ("NetCryptoDequeue." ++ show (key2id remotePublicKey)) |
243 | fix $ \loop -> do | 276 | fix $ \loop -> do |
244 | cd <- atomically $ PQ.dequeue pktq | 277 | cd <- atomically $ PQ.dequeue pktq |
245 | _ <- runCryptoHook (netCryptoSession0 {ncDequeueThread=tid}) cd | 278 | _ <- runCryptoHook (netCryptoSession0 {ncDequeueThread=Just tid}) cd |
246 | loop | 279 | loop |
247 | let netCryptoSession = netCryptoSession0 {ncDequeueThread=threadid} | 280 | let netCryptoSession = netCryptoSession0 {ncDequeueThread=Just threadid} |
248 | atomically $ do | 281 | atomically $ do |
249 | modifyTVar allsessions (Map.insert addr netCryptoSession) | 282 | modifyTVar allsessions (Map.insert addr netCryptoSession) |
250 | byKeyResult <- readTVar allsessionsByKey >>= return . Map.lookup remotePublicKey | 283 | byKeyResult <- readTVar allsessionsByKey >>= return . Map.lookup remotePublicKey |
@@ -298,7 +331,8 @@ cryptoNetHandler sessions addr (NetHandshake (Handshake (Cookie n24 ecookie) non | |||
298 | digest = hashFinalize hctx' | 331 | digest = hashFinalize hctx' |
299 | guard (cookieHash == digest) | 332 | guard (cookieHash == digest) |
300 | -- known friend? | 333 | -- known friend? |
301 | -- todo | 334 | -- todo TODO, see Roster.hs, |
335 | -- talk to not yet existent Network-Manager to ascertain current permissions | ||
302 | return | 336 | return |
303 | HParam | 337 | HParam |
304 | { hpTheirBaseNonce = Just baseNonce | 338 | { hpTheirBaseNonce = Just baseNonce |