diff options
author | James Crayne <jim.crayne@gmail.com> | 2017-11-15 06:47:38 +0000 |
---|---|---|
committer | James Crayne <jim.crayne@gmail.com> | 2017-11-19 23:40:14 +0000 |
commit | 2ebea62ba6e49a653bfa47760e3089799b1bb9a7 (patch) | |
tree | 526189b4c6da91aa64a8a809f474a5f7e67ac020 /src/Network/Tox | |
parent | 43219aa4da88fbf3187230af0b5ad5b2d17f177a (diff) |
netCryptoSessionsByKey
Diffstat (limited to 'src/Network/Tox')
-rw-r--r-- | src/Network/Tox/Crypto/Handlers.hs | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs index b92d4805..dcc63ae0 100644 --- a/src/Network/Tox/Crypto/Handlers.hs +++ b/src/Network/Tox/Crypto/Handlers.hs | |||
@@ -90,6 +90,7 @@ data NetCryptoSession = NCrypto { ncState :: TVar NetCryptoSessionStatus | |||
90 | } | 90 | } |
91 | 91 | ||
92 | data NetCryptoSessions = NCSessions { netCryptoSessions :: TVar (Map.Map SockAddr NetCryptoSession) | 92 | data NetCryptoSessions = NCSessions { netCryptoSessions :: TVar (Map.Map SockAddr NetCryptoSession) |
93 | , netCryptoSessionsByKey :: TVar (Map.Map PublicKey [NetCryptoSession]) | ||
93 | , transportCrypto :: TransportCrypto | 94 | , transportCrypto :: TransportCrypto |
94 | , defaultHooks :: Map.Map MessageType [NetCryptoHook] | 95 | , defaultHooks :: Map.Map MessageType [NetCryptoHook] |
95 | , defaultUnrecognizedHook :: MessageType -> NetCryptoHook | 96 | , defaultUnrecognizedHook :: MessageType -> NetCryptoHook |
@@ -98,12 +99,21 @@ data NetCryptoSessions = NCSessions { netCryptoSessions :: TVar (Map.Map SockAdd | |||
98 | , inboundQueueCapacity :: Word32 | 99 | , inboundQueueCapacity :: Word32 |
99 | } | 100 | } |
100 | 101 | ||
102 | -- | initiate a netcrypto session, blocking | ||
103 | netCrypto :: TransportCrypto -> NetCryptoSessions -> SecretKey -> PublicKey -> IO NetCryptoSession | ||
104 | netCrypto crypto allsessions myseckey theirpubkey = do | ||
105 | -- convert public key to NodeInfo check Roster | ||
106 | -- if no session: | ||
107 | -- 1) send dht key | ||
108 | error "todo" | ||
109 | |||
101 | newSessionsState :: TransportCrypto | 110 | newSessionsState :: TransportCrypto |
102 | -> (MessageType -> NetCryptoHook) -- ^ default hook | 111 | -> (MessageType -> NetCryptoHook) -- ^ default hook |
103 | -> Map.Map MessageType [NetCryptoHook] -- ^ all hooks, can be empty to start | 112 | -> Map.Map MessageType [NetCryptoHook] -- ^ all hooks, can be empty to start |
104 | -> IO NetCryptoSessions | 113 | -> IO NetCryptoSessions |
105 | newSessionsState crypto unrechook hooks = do | 114 | newSessionsState crypto unrechook hooks = do |
106 | x <- atomically $ newTVar Map.empty | 115 | x <- atomically $ newTVar Map.empty |
116 | x2 <- atomically $ newTVar Map.empty | ||
107 | nick <- atomically $ newTVar B.empty | 117 | nick <- atomically $ newTVar B.empty |
108 | status <- atomically $ newTVar Online | 118 | status <- atomically $ newTVar Online |
109 | statusmsg <- atomically $ newTVar B.empty | 119 | statusmsg <- atomically $ newTVar B.empty |
@@ -115,6 +125,7 @@ newSessionsState crypto unrechook hooks = do | |||
115 | homedir <- getHomeDirectory | 125 | homedir <- getHomeDirectory |
116 | svDownloadDir0 <- atomically $ newTVar (homedir </> "Downloads") | 126 | svDownloadDir0 <- atomically $ newTVar (homedir </> "Downloads") |
117 | return NCSessions { netCryptoSessions = x | 127 | return NCSessions { netCryptoSessions = x |
128 | , netCryptoSessionsByKey = x2 | ||
118 | , transportCrypto = crypto | 129 | , transportCrypto = crypto |
119 | , defaultHooks = hooks | 130 | , defaultHooks = hooks |
120 | , defaultUnrecognizedHook = unrechook | 131 | , defaultUnrecognizedHook = unrechook |
@@ -182,6 +193,7 @@ freshCryptoSession sessions | |||
182 | }) = do | 193 | }) = do |
183 | let crypto = transportCrypto sessions | 194 | let crypto = transportCrypto sessions |
184 | allsessions = netCryptoSessions sessions | 195 | allsessions = netCryptoSessions sessions |
196 | allsessionsByKey = netCryptoSessionsByKey sessions | ||
185 | ncState0 <- atomically $ newTVar Accepted | 197 | ncState0 <- atomically $ newTVar Accepted |
186 | ncTheirBaseNonce0 <- atomically $ newTVar theirBaseNonce | 198 | ncTheirBaseNonce0 <- atomically $ newTVar theirBaseNonce |
187 | n24 <- atomically $ transportNewNonce crypto | 199 | n24 <- atomically $ transportNewNonce crypto |
@@ -233,7 +245,12 @@ freshCryptoSession sessions | |||
233 | _ <- runCryptoHook (netCryptoSession0 {ncDequeueThread=tid}) cd | 245 | _ <- runCryptoHook (netCryptoSession0 {ncDequeueThread=tid}) cd |
234 | loop | 246 | loop |
235 | let netCryptoSession = netCryptoSession0 {ncDequeueThread=threadid} | 247 | let netCryptoSession = netCryptoSession0 {ncDequeueThread=threadid} |
236 | atomically $ modifyTVar allsessions (Map.insert addr netCryptoSession) | 248 | atomically $ do |
249 | modifyTVar allsessions (Map.insert addr netCryptoSession) | ||
250 | byKeyResult <- readTVar allsessionsByKey >>= return . Map.lookup remotePublicKey | ||
251 | case byKeyResult of | ||
252 | Nothing -> modifyTVar allsessionsByKey (Map.insert remotePublicKey [netCryptoSession]) | ||
253 | Just xs -> modifyTVar allsessionsByKey (Map.insert remotePublicKey (netCryptoSession:xs)) | ||
237 | 254 | ||
238 | -- | Called when we get a handshake, but there's already a session entry. | 255 | -- | Called when we get a handshake, but there's already a session entry. |
239 | updateCryptoSession :: NetCryptoSessions -> SockAddr -> HandshakeParams -> NetCryptoSession -> IO () | 256 | updateCryptoSession :: NetCryptoSessions -> SockAddr -> HandshakeParams -> NetCryptoSession -> IO () |