diff options
author | James Crayne <jim.crayne@gmail.com> | 2018-06-14 06:18:04 +0000 |
---|---|---|
committer | James Crayne <jim.crayne@gmail.com> | 2018-06-16 02:27:52 +0000 |
commit | bc5a84c34ba400f3d319387f34a7259923ca64e6 (patch) | |
tree | 5dc429f3417d46a431c5d1f846edf854b79c2caa /src/Network/Tox/Crypto | |
parent | b384cd2e1a806c882359d0cf619e6cce04784d58 (diff) |
Experimental Connection.Tox integration
Diffstat (limited to 'src/Network/Tox/Crypto')
-rw-r--r-- | src/Network/Tox/Crypto/Handlers.hs | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs index 20560a48..61b2ab3c 100644 --- a/src/Network/Tox/Crypto/Handlers.hs +++ b/src/Network/Tox/Crypto/Handlers.hs | |||
@@ -55,9 +55,18 @@ import DPut | |||
55 | import Debug.Trace | 55 | import Debug.Trace |
56 | import Text.Printf | 56 | import Text.Printf |
57 | import Data.Bool | 57 | import Data.Bool |
58 | import Connection (Status(..)) | 58 | import Connection (Status(..), Policy(..)) |
59 | import Connection.Tox.Threads (ToxProgress(..)) | 59 | import Connection.Tox.Threads (ToxProgress(..)) |
60 | 60 | ||
61 | type LookupPolicyFunction = Key -> STM Policy | ||
62 | |||
63 | data Key = Key NodeId{-me-} NodeId{-them-} | ||
64 | deriving (Eq,Ord) | ||
65 | |||
66 | instance Show Key where show = show . showKey_ | ||
67 | |||
68 | showKey_ :: Key -> String | ||
69 | showKey_ (Key me them) = show me ++ ":" ++ show them | ||
61 | 70 | ||
62 | -- * These types are isomorphic to Maybe, but have the advantage of documenting | 71 | -- * These types are isomorphic to Maybe, but have the advantage of documenting |
63 | -- when an item is expected to become known. | 72 | -- when an item is expected to become known. |
@@ -296,6 +305,7 @@ data NetCryptoSession = NCrypto | |||
296 | data NetCryptoSessions = NCSessions | 305 | data NetCryptoSessions = NCSessions |
297 | { netCryptoSessions :: TVar (Map.Map SockAddr NetCryptoSession) | 306 | { netCryptoSessions :: TVar (Map.Map SockAddr NetCryptoSession) |
298 | , netCryptoSessionsByKey :: TVar (Map.Map PublicKey [NetCryptoSession]) | 307 | , netCryptoSessionsByKey :: TVar (Map.Map PublicKey [NetCryptoSession]) |
308 | , netCryptoPolicyByKey :: LookupPolicyFunction | ||
299 | , transportCrypto :: TransportCrypto | 309 | , transportCrypto :: TransportCrypto |
300 | , defaultHooks :: Map.Map MessageType [NetCryptoHook] | 310 | , defaultHooks :: Map.Map MessageType [NetCryptoHook] |
301 | , defaultUnrecognizedHook :: MessageType -> NetCryptoHook | 311 | , defaultUnrecognizedHook :: MessageType -> NetCryptoHook |
@@ -361,6 +371,7 @@ newSessionsState crypto unrechook hooks = do | |||
361 | lsupplyVar <- atomically (newTVar lsupply) | 371 | lsupplyVar <- atomically (newTVar lsupply) |
362 | return NCSessions { netCryptoSessions = x | 372 | return NCSessions { netCryptoSessions = x |
363 | , netCryptoSessionsByKey = x2 | 373 | , netCryptoSessionsByKey = x2 |
374 | , netCryptoPolicyByKey = \_ -> return OpenToConnect | ||
364 | , transportCrypto = crypto | 375 | , transportCrypto = crypto |
365 | , defaultHooks = hooks | 376 | , defaultHooks = hooks |
366 | , defaultUnrecognizedHook = unrechook | 377 | , defaultUnrecognizedHook = unrechook |
@@ -872,7 +883,14 @@ handshakeH sessions addrRaw hshake@(Handshake (Cookie n24 ecookie) nonce24 encry | |||
872 | case Map.lookup addr sessionsmap of | 883 | case Map.lookup addr sessionsmap of |
873 | Nothing -> do | 884 | Nothing -> do |
874 | dmsg $ "sockaddr(" ++ show addr ++ ") not in session map(" ++ show (map (second ncSessionId) (Map.assocs sessionsmap)) ++ "), so freshCryptoSession" | 885 | dmsg $ "sockaddr(" ++ show addr ++ ") not in session map(" ++ show (map (second ncSessionId) (Map.assocs sessionsmap)) ++ "), so freshCryptoSession" |
875 | freshCryptoSession sessions addr newsession timestamp hp -- create new session | 886 | let k = Key (key2id . toPublic $ key) (key2id remotePublicKey) |
887 | policy <- netCryptoPolicyByKey sessions k | ||
888 | case policy of | ||
889 | x | x `elem` [OpenToConnect,TryingToConnect] -> | ||
890 | freshCryptoSession sessions addr newsession timestamp hp -- create new session | ||
891 | x -> do | ||
892 | dmsg $ "Ignoring Handshake from " ++ show (key2id remotePublicKey) ++ " due to policy: " ++ show x | ||
893 | return (Nothing,return ()) | ||
876 | Just session -> do | 894 | Just session -> do |
877 | dmsg "sockaddr ALREADY in session map, so updateCryptoSession" | 895 | dmsg "sockaddr ALREADY in session map, so updateCryptoSession" |
878 | updateCryptoSession sessions addr (ncSessionSecret session) timestamp hp session hshake -- update existing session | 896 | updateCryptoSession sessions addr (ncSessionSecret session) timestamp hp session hshake -- update existing session |