summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Network/Tox.hs38
-rw-r--r--src/Network/Tox/Crypto/Handlers.hs75
-rw-r--r--src/Network/Tox/NodeId.hs80
3 files changed, 97 insertions, 96 deletions
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs
index 515f155d..1bf6efc5 100644
--- a/src/Network/Tox.hs
+++ b/src/Network/Tox.hs
@@ -47,7 +47,6 @@ import System.Endian
47import Network.BitTorrent.DHT.Token as Token 47import Network.BitTorrent.DHT.Token as Token
48 48
49import Connection 49import Connection
50import Connection.Tox
51import Crypto.Tox 50import Crypto.Tox
52import Data.Word64Map (fitsInInt) 51import Data.Word64Map (fitsInInt)
53import qualified Data.Word64Map (empty) 52import qualified Data.Word64Map (empty)
@@ -207,7 +206,6 @@ data Tox extra = Tox
207 , toxOnionRoutes :: OnionRouter 206 , toxOnionRoutes :: OnionRouter
208 , toxContactInfo :: ContactInfo extra 207 , toxContactInfo :: ContactInfo extra
209 , toxAnnounceToLan :: IO () 208 , toxAnnounceToLan :: IO ()
210 , toxMgr :: Manager ToxProgress Key
211 } 209 }
212 210
213-- | initiate a netcrypto session, blocking 211-- | initiate a netcrypto session, blocking
@@ -438,25 +436,22 @@ newToxOverTransport keydb addr mbSessionsState suppliedDHTKey udp = do
438 tbl6 = DHT.routing6 $ mkrouting (error "missing client") 436 tbl6 = DHT.routing6 $ mkrouting (error "missing client")
439 dhtclient <- newClient drg dhtnet0 DHT.classify (myAddr tbl4 tbl6) (DHT.handlers crypto . mkrouting) id 437 dhtclient <- newClient drg dhtnet0 DHT.classify (myAddr tbl4 tbl6) (DHT.handlers crypto . mkrouting) id
440 $ \client net -> onInbound (DHT.updateRouting client (mkrouting client) orouter) net 438 $ \client net -> onInbound (DHT.updateRouting client (mkrouting client) orouter) net
441 -- TODO: Refactor so this recursive do is unnecessary. 439
442 rec (mgr,sessionsState) <- do 440 let sessionsState = sessionsState0 { sendHandshake = sendMessage handshakes
443 mgr <- toxManager (Parameters { dhtRouting = mkrouting dhtclient 441 , sendSessionPacket = sendMessage cryptonet
444 , roster = roster 442 , transportCrypto = crypto
445 , sessions = sessionsState 443 -- ToxContact -> STM Policy
446 , dhtClient = dhtclient 444 , netCryptoPolicyByKey = policylookup
447 , onToxSession = return () -- TODO 445 }
448 }) 446 policylookup (ToxContact me them) = do
449 let policylookup key = do 447 macnt <- HashMap.lookup me <$> readTVar (accounts roster)
450 mp <- connections mgr 448 case macnt of
451 case Map.lookup key mp of 449 Nothing -> return RefusingToConnect
452 Nothing -> return OpenToConnect 450 Just acnt -> do
453 Just conn -> Connection.connPolicy conn 451 mc <- HashMap.lookup them <$> readTVar (contacts acnt)
454 452 case mc of
455 return (mgr, sessionsState0 { sendHandshake = sendMessage handshakes 453 Nothing -> return RefusingToConnect
456 , sendSessionPacket = sendMessage cryptonet 454 Just c -> fromMaybe RefusingToConnect <$> readTVar (contactPolicy c)
457 , transportCrypto = crypto
458 , netCryptoPolicyByKey = policylookup
459 })
460 455
461 orouter' <- forkRouteBuilder orouter 456 orouter' <- forkRouteBuilder orouter
462 $ \nid ni -> fmap (\(_,ns,_)->ns) 457 $ \nid ni -> fmap (\(_,ns,_)->ns)
@@ -487,7 +482,6 @@ newToxOverTransport keydb addr mbSessionsState suppliedDHTKey udp = do
487 , toxOnionRoutes = orouter 482 , toxOnionRoutes = orouter
488 , toxContactInfo = roster 483 , toxContactInfo = roster
489 , toxAnnounceToLan = return () 484 , toxAnnounceToLan = return ()
490 , toxMgr = mgr
491 } 485 }
492 486
493onionTimeout :: Tox extra -> DHT.TransactionId -> Onion.OnionDestination RouteId -> STM (Onion.OnionDestination RouteId, Int) 487onionTimeout :: Tox extra -> DHT.TransactionId -> Onion.OnionDestination RouteId -> STM (Onion.OnionDestination RouteId, Int)
diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs
index 07c033b6..3269f5dd 100644
--- a/src/Network/Tox/Crypto/Handlers.hs
+++ b/src/Network/Tox/Crypto/Handlers.hs
@@ -52,80 +52,9 @@ import Data.InOrOut
52import DPut 52import DPut
53import Text.Printf 53import Text.Printf
54import Data.Bool 54import Data.Bool
55import Connection (Status(..), Policy(..))
56import Network.Tox.Handshake 55import Network.Tox.Handshake
57 56
58-- | This type indicates the progress of a tox encrypted friend link 57type LookupPolicyFunction = ToxContact -> STM Policy
59-- connection. Two scenarios are illustrated below. The parenthesis show the
60-- current 'G.Status' 'ToxProgress' of the session.
61--
62--
63-- Perfect handshake scenario:
64--
65-- Peer 1 Peer 2
66-- (InProgress AcquiringCookie) (Dormant/InProgress AcquiringCookie)
67-- Cookie request ->
68-- <- Cookie response
69-- (InProgress AwaitingHandshake) (Dormant/InProgress AcquiringCookie)
70-- Handshake packet ->
71-- * accepts connection
72-- (InProgress AwaitingSessionPacket)
73-- <- Handshake packet
74-- *accepts connection
75-- (InProgress AwaitingSessionPacket)
76-- Encrypted packet -> <- Encrypted packet
77-- *confirms connection *confirms connection
78-- (Established) (Established)
79--
80-- Connection successful.
81--
82-- Encrypted packets -> <- Encrypted packets
83--
84--
85--
86--
87-- More realistic handshake scenario:
88-- Peer 1 Peer 2
89-- (InProgress AcquiringCookie) (Dormant/InProgress AcquiringCookie)
90-- Cookie request -> *packet lost*
91-- Cookie request ->
92-- <- Cookie response
93-- (InProgress AwaitingHandshake) (Dormant/InProgress AcquiringCookie)
94--
95-- *Peer 2 randomly starts new connection to peer 1
96-- (InProgress AcquiringCookie)
97-- <- Cookie request
98-- Cookie response ->
99-- (InProgress AwaitingHandshake)
100--
101-- Handshake packet -> <- Handshake packet
102-- *accepts connection * accepts connection
103-- (InProgress AwaitingSessionPacket) (InProgress AwaitingSessionPacket)
104--
105-- Encrypted packet -> <- Encrypted packet
106-- *confirms connection *confirms connection
107-- (Established) (Established)
108--
109-- Connection successful.
110--
111-- Encrypted packets -> <- Encrypted packets
112data ToxProgress
113 = AwaitingDHTKey -- ^ Waiting to receive their DHT key.
114 | AcquiringIPAddress -- ^ Searching DHT to obtain their node's IP & port.
115 | AcquiringCookie -- ^ Attempting to obtain a cookie.
116 | AwaitingHandshake -- ^ Waiting to receive a handshake.
117 | AwaitingSessionPacket -- ^ Connection is "accepted" but not yet "confirmed".
118 deriving (Eq,Ord,Enum,Show)
119
120type LookupPolicyFunction = Key -> STM Policy
121
122data Key = Key NodeId{-me-} NodeId{-them-}
123 deriving (Eq,Ord)
124
125instance Show Key where show = show . showKey_
126
127showKey_ :: Key -> String
128showKey_ (Key me them) = show me ++ ":" ++ show them
129 58
130-- * These types are isomorphic to Maybe, but have the advantage of documenting 59-- * These types are isomorphic to Maybe, but have the advantage of documenting
131-- when an item is expected to become known. 60-- when an item is expected to become known.
@@ -1075,7 +1004,7 @@ handshakeH sessions addrRaw hshake@(Handshake (Cookie n24 ecookie) nonce24 encry
1075 case Map.lookup addr sessionsmap of 1004 case Map.lookup addr sessionsmap of
1076 Nothing -> do 1005 Nothing -> do
1077 dmsg $ "sockaddr(" ++ show addr ++ ") not in session map(" ++ show (map (second ncSessionId) (Map.assocs sessionsmap)) ++ "), so freshCryptoSession" 1006 dmsg $ "sockaddr(" ++ show addr ++ ") not in session map(" ++ show (map (second ncSessionId) (Map.assocs sessionsmap)) ++ "), so freshCryptoSession"
1078 let k = Key (key2id . toPublic $ key) (key2id remotePublicKey) 1007 let k = ToxContact (key2id . toPublic $ key) (key2id remotePublicKey)
1079 policy <- netCryptoPolicyByKey sessions k 1008 policy <- netCryptoPolicyByKey sessions k
1080 case policy of 1009 case policy of
1081 x | x `elem` [OpenToConnect,TryingToConnect] -> 1010 x | x `elem` [OpenToConnect,TryingToConnect] ->
diff --git a/src/Network/Tox/NodeId.hs b/src/Network/Tox/NodeId.hs
index 3a732b43..d5da692a 100644
--- a/src/Network/Tox/NodeId.hs
+++ b/src/Network/Tox/NodeId.hs
@@ -33,7 +33,10 @@ module Network.Tox.NodeId
33 , parseNoSpamId 33 , parseNoSpamId
34 , nospam64 34 , nospam64
35 , nospam16 35 , nospam16
36 , verifyChecksum) where 36 , verifyChecksum
37 , ToxContact(..)
38 , ToxProgress(..)
39 ) where
37 40
38import Control.Applicative 41import Control.Applicative
39import Control.Arrow 42import Control.Arrow
@@ -617,3 +620,78 @@ solveBase64NoSpamID b64digits pub = do
617 let nospam' = fromIntegral (n64' `shiftR` 32) 620 let nospam' = fromIntegral (n64' `shiftR` 32)
618 cksum' = fromIntegral (n64' `shiftR` 16) 621 cksum' = fromIntegral (n64' `shiftR` 16)
619 return $ NoSpamId (NoSpam nospam' (Just cksum')) pub 622 return $ NoSpamId (NoSpam nospam' (Just cksum')) pub
623
624-- | This type indicates a roster-link relationship between a local toxid and a
625-- remote toxid. Note that these toxids are represented as the type 'NodeId'
626-- even though they are long-term keys rather than the public keys of Tox DHT
627-- nodes.
628data ToxContact = ToxContact NodeId{-me-} NodeId{-them-}
629 deriving (Eq,Ord)
630
631instance Show ToxContact where show = show . showToxContact_
632
633showToxContact_ :: ToxContact -> String
634showToxContact_ (ToxContact me them) = show me ++ ":" ++ show them
635
636-- | This type indicates the progress of a tox encrypted friend link
637-- connection. Two scenarios are illustrated below. The parenthesis show the
638-- current 'G.Status' 'ToxProgress' of the session.
639--
640--
641-- Perfect handshake scenario:
642--
643-- Peer 1 Peer 2
644-- (InProgress AcquiringCookie) (Dormant/InProgress AcquiringCookie)
645-- Cookie request ->
646-- <- Cookie response
647-- (InProgress AwaitingHandshake) (Dormant/InProgress AcquiringCookie)
648-- Handshake packet ->
649-- * accepts connection
650-- (InProgress AwaitingSessionPacket)
651-- <- Handshake packet
652-- *accepts connection
653-- (InProgress AwaitingSessionPacket)
654-- Encrypted packet -> <- Encrypted packet
655-- *confirms connection *confirms connection
656-- (Established) (Established)
657--
658-- Connection successful.
659--
660-- Encrypted packets -> <- Encrypted packets
661--
662--
663--
664--
665-- More realistic handshake scenario:
666-- Peer 1 Peer 2
667-- (InProgress AcquiringCookie) (Dormant/InProgress AcquiringCookie)
668-- Cookie request -> *packet lost*
669-- Cookie request ->
670-- <- Cookie response
671-- (InProgress AwaitingHandshake) (Dormant/InProgress AcquiringCookie)
672--
673-- *Peer 2 randomly starts new connection to peer 1
674-- (InProgress AcquiringCookie)
675-- <- Cookie request
676-- Cookie response ->
677-- (InProgress AwaitingHandshake)
678--
679-- Handshake packet -> <- Handshake packet
680-- *accepts connection * accepts connection
681-- (InProgress AwaitingSessionPacket) (InProgress AwaitingSessionPacket)
682--
683-- Encrypted packet -> <- Encrypted packet
684-- *confirms connection *confirms connection
685-- (Established) (Established)
686--
687-- Connection successful.
688--
689-- Encrypted packets -> <- Encrypted packets
690data ToxProgress
691 = AwaitingDHTKey -- ^ Waiting to receive their DHT key.
692 | AcquiringIPAddress -- ^ Searching DHT to obtain their node's IP & port.
693 | AcquiringCookie -- ^ Attempting to obtain a cookie.
694 | AwaitingHandshake -- ^ Waiting to receive a handshake.
695 | AwaitingSessionPacket -- ^ Connection is "accepted" but not yet "confirmed".
696 deriving (Eq,Ord,Enum,Show)
697