diff options
author | joe <joe@jerkface.net> | 2018-06-16 02:59:42 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2018-06-16 02:59:42 -0400 |
commit | 8d6a50dde334a2dc4039c0c718a992e4df36402f (patch) | |
tree | 58558561314a49a29ea6a0ab82a6a4cb9550cf81 /Connection | |
parent | 939c23d57365a49d366b6534c6f343f0a12a770a (diff) |
more persueContact wip
Diffstat (limited to 'Connection')
-rw-r--r-- | Connection/Tox/Threads.hs | 85 |
1 files changed, 12 insertions, 73 deletions
diff --git a/Connection/Tox/Threads.hs b/Connection/Tox/Threads.hs index 12ac9682..3cf9486d 100644 --- a/Connection/Tox/Threads.hs +++ b/Connection/Tox/Threads.hs | |||
@@ -13,12 +13,14 @@ module Connection.Tox.Threads where | |||
13 | 13 | ||
14 | import Connection | 14 | import Connection |
15 | -- import Connection.Tox | 15 | -- import Connection.Tox |
16 | import Crypto.Tox | ||
16 | import Data.IP (IP) | 17 | import Data.IP (IP) |
17 | import Network.Tox.Crypto.Transport | 18 | import Network.Tox.Crypto.Transport |
18 | import Network.Tox.Crypto.Handlers | 19 | import Network.Tox.Crypto.Handlers |
19 | import Network.Tox.NodeId | 20 | import Network.Tox.NodeId |
20 | import Network.Tox.ContactInfo | 21 | import Network.Tox.ContactInfo |
21 | import Network.Tox.DHT.Handlers {- (nodeSearch) -} as DHT | 22 | import Network.Tox.DHT.Handlers {- (nodeSearch) -} as DHT |
23 | import Network.Tox.DHT.Transport as DHT (dhtpk) | ||
22 | import Network.Socket | 24 | import Network.Socket |
23 | import Network.Kademlia.Search | 25 | import Network.Kademlia.Search |
24 | import Network.Kademlia.Routing (BucketList) | 26 | import Network.Kademlia.Routing (BucketList) |
@@ -30,68 +32,6 @@ import Data.Functor.Identity | |||
30 | import System.Timeout | 32 | import System.Timeout |
31 | 33 | ||
32 | 34 | ||
33 | -- | This type indicates the progress of a tox encrypted friend link | ||
34 | -- connection. Two scenarios are illustrated below. The parenthesis show the | ||
35 | -- current 'G.Status' 'ToxProgress' of the session. | ||
36 | -- | ||
37 | -- | ||
38 | -- Perfect handshake scenario: | ||
39 | -- | ||
40 | -- Peer 1 Peer 2 | ||
41 | -- (InProgress AcquiringCookie) (Dormant/InProgress AcquiringCookie) | ||
42 | -- Cookie request -> | ||
43 | -- <- Cookie response | ||
44 | -- (InProgress AwaitingHandshake) (Dormant/InProgress AcquiringCookie) | ||
45 | -- Handshake packet -> | ||
46 | -- * accepts connection | ||
47 | -- (InProgress AwaitingSessionPacket) | ||
48 | -- <- Handshake packet | ||
49 | -- *accepts connection | ||
50 | -- (InProgress AwaitingSessionPacket) | ||
51 | -- Encrypted packet -> <- Encrypted packet | ||
52 | -- *confirms connection *confirms connection | ||
53 | -- (Established) (Established) | ||
54 | -- | ||
55 | -- Connection successful. | ||
56 | -- | ||
57 | -- Encrypted packets -> <- Encrypted packets | ||
58 | -- | ||
59 | -- | ||
60 | -- | ||
61 | -- | ||
62 | -- More realistic handshake scenario: | ||
63 | -- Peer 1 Peer 2 | ||
64 | -- (InProgress AcquiringCookie) (Dormant/InProgress AcquiringCookie) | ||
65 | -- Cookie request -> *packet lost* | ||
66 | -- Cookie request -> | ||
67 | -- <- Cookie response | ||
68 | -- (InProgress AwaitingHandshake) (Dormant/InProgress AcquiringCookie) | ||
69 | -- | ||
70 | -- *Peer 2 randomly starts new connection to peer 1 | ||
71 | -- (InProgress AcquiringCookie) | ||
72 | -- <- Cookie request | ||
73 | -- Cookie response -> | ||
74 | -- (InProgress AwaitingHandshake) | ||
75 | -- | ||
76 | -- Handshake packet -> <- Handshake packet | ||
77 | -- *accepts connection * accepts connection | ||
78 | -- (InProgress AwaitingSessionPacket) (InProgress AwaitingSessionPacket) | ||
79 | -- | ||
80 | -- Encrypted packet -> <- Encrypted packet | ||
81 | -- *confirms connection *confirms connection | ||
82 | -- (Established) (Established) | ||
83 | -- | ||
84 | -- Connection successful. | ||
85 | -- | ||
86 | -- Encrypted packets -> <- Encrypted packets | ||
87 | data ToxProgress | ||
88 | = AwaitingDHTKey -- ^ Waiting to receive their DHT key. | ||
89 | | AcquiringIPAddress -- ^ Searching DHT to obtain their node's IP & port. | ||
90 | | AcquiringCookie -- ^ Attempting to obtain a cookie. | ||
91 | | AwaitingHandshake -- ^ Waiting to receive a handshake. | ||
92 | | AwaitingSessionPacket -- ^ Connection is "accepted" but not yet "confirmed". | ||
93 | deriving (Eq,Ord,Enum,Show) | ||
94 | |||
95 | 35 | ||
96 | type NodeSearch = Search NodeId (IP,PortNumber) () NodeInfo NodeInfo | 36 | type NodeSearch = Search NodeId (IP,PortNumber) () NodeInfo NodeInfo |
97 | 37 | ||
@@ -150,7 +90,8 @@ data PersueContactMethods params = PersueContactMethods | |||
150 | , myseckey :: SecretKey | 90 | , myseckey :: SecretKey |
151 | , theirpubkey :: PublicKey | 91 | , theirpubkey :: PublicKey |
152 | , client :: DHT.Client | 92 | , client :: DHT.Client |
153 | , retryInterval :: Int | 93 | , shortRetryInterval :: Int -- successful cookie, try again soon. |
94 | , longRetryInterval :: Int -- no cookie, he's offline, give it some time. | ||
154 | , contact :: Contact | 95 | , contact :: Contact |
155 | } | 96 | } |
156 | 97 | ||
@@ -185,19 +126,16 @@ persueContact getPolicy getStatus PersueContactMethods{..} writeStatus | |||
185 | either retry return $ nodeInfo (key2id theirDhtKey) saddr | 126 | either retry return $ nodeInfo (key2id theirDhtKey) saddr |
186 | let mykeyAsId = key2id (toPublic myseckey) | 127 | let mykeyAsId = key2id (toPublic myseckey) |
187 | theirkeyAsId = key2id theirpubkey | 128 | theirkeyAsId = key2id theirpubkey |
129 | crypto = transportCrypto allsessions | ||
130 | -- AcquiringCookie | ||
188 | atomically $ writeStatus (InProgress AcquiringCookie) | 131 | atomically $ writeStatus (InProgress AcquiringCookie) |
189 | -- if no session: | 132 | mbCookie <- -- TODO: Check for recent cached cookie. |
190 | -- Convert to NodeInfo, so we can send cookieRequest | 133 | DHT.cookieRequest crypto client (toPublic myseckey) ni |
191 | let crypto = transportCrypto allsessions | 134 | interval <- case mbCookie of |
192 | case nodeInfo (key2id theirDhtKey) saddr of | ||
193 | Left e -> hPutStrLn stderr ("persueContact: nodeInfo fail... " ++ e) | ||
194 | Right ni -> do | ||
195 | -- AcquiringCookie | ||
196 | mbCookie <- DHT.cookieRequest crypto client (toPublic myseckey) ni | ||
197 | case mbCookie of | ||
198 | Nothing -> do | 135 | Nothing -> do |
199 | hPutStrLn stderr ("persueContact: (" ++ show mykeyAsId ++") <--> (" ++ show theirkeyAsId ++ ").") | 136 | hPutStrLn stderr ("persueContact: (" ++ show mykeyAsId ++") <--> (" ++ show theirkeyAsId ++ ").") |
200 | hPutStrLn stderr ("persueContact: CookieRequest failed. TODO: dhtpkNodes thingy") | 137 | hPutStrLn stderr ("persueContact: CookieRequest failed. TODO: dhtpkNodes thingy") |
138 | return longRetryInterval | ||
201 | Just cookie -> do | 139 | Just cookie -> do |
202 | hPutStrLn stderr "Have cookie, creating handshake packet..." | 140 | hPutStrLn stderr "Have cookie, creating handshake packet..." |
203 | let hp = HParam { hpOtherCookie = cookie | 141 | let hp = HParam { hpOtherCookie = cookie |
@@ -216,9 +154,10 @@ persueContact getPolicy getStatus PersueContactMethods{..} writeStatus | |||
216 | forM myhandshake $ \response_handshake -> do | 154 | forM myhandshake $ \response_handshake -> do |
217 | sendHandshake (toxCryptoSessions tox) saddr response_handshake | 155 | sendHandshake (toxCryptoSessions tox) saddr response_handshake |
218 | atomically $ writeStatus $ InProgress AwaitingHandshake | 156 | atomically $ writeStatus $ InProgress AwaitingHandshake |
157 | return shortRetryInterval | ||
219 | -- AwaitingHandshake | 158 | -- AwaitingHandshake |
220 | -- AwaitingSessionPacket | 159 | -- AwaitingSessionPacket |
221 | retryAfterTimeout retryInterval | 160 | retryAfterTimeout interval |
222 | 161 | ||
223 | data FreshenContactMethods = FreshenContactMethods | 162 | data FreshenContactMethods = FreshenContactMethods |
224 | { dhtkeyInterval :: Int | 163 | { dhtkeyInterval :: Int |