diff options
author | joe <joe@jerkface.net> | 2018-06-13 01:30:12 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2018-06-13 01:30:12 -0400 |
commit | 7cdb8da4f1c6df5d4b2755498e79c9886fd0750f (patch) | |
tree | 8e02f15c4bf3143e6db70dd16727b59d736951c1 /src | |
parent | 66ee00b2b74eea4258314a66b7599da7606a6539 (diff) |
tox: Node-address notifications.
Diffstat (limited to 'src')
-rw-r--r-- | src/Network/Tox/DHT/Handlers.hs | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/src/Network/Tox/DHT/Handlers.hs b/src/Network/Tox/DHT/Handlers.hs index e6986bf4..2bce382a 100644 --- a/src/Network/Tox/DHT/Handlers.hs +++ b/src/Network/Tox/DHT/Handlers.hs | |||
@@ -30,6 +30,8 @@ import Control.Concurrent.STM | |||
30 | import Data.Time.Clock.POSIX (POSIXTime, getPOSIXTime) | 30 | import Data.Time.Clock.POSIX (POSIXTime, getPOSIXTime) |
31 | import Network.Socket | 31 | import Network.Socket |
32 | import Data.Hashable | 32 | import Data.Hashable |
33 | import qualified Data.HashMap.Strict as HashMap | ||
34 | ;import Data.HashMap.Strict (HashMap) | ||
33 | import Data.IP | 35 | import Data.IP |
34 | import Data.Ord | 36 | import Data.Ord |
35 | import Data.Maybe | 37 | import Data.Maybe |
@@ -112,12 +114,20 @@ classify client msg = fromMaybe (IsUnknown "unknown") | |||
112 | go (DHTCookie {}) = IsResponse | 114 | go (DHTCookie {}) = IsResponse |
113 | go (DHTDHTRequest {}) = IsQuery DHTRequestType | 115 | go (DHTDHTRequest {}) = IsQuery DHTRequestType |
114 | 116 | ||
117 | data NodeInfoCallback = NodeInfoCallback | ||
118 | { interestingNodeId :: NodeId | ||
119 | , listenerId :: Int | ||
120 | , observedAddress :: NodeInfo -> STM () | ||
121 | , rumoredAddress :: SockAddr -> NodeInfo -> STM () | ||
122 | } | ||
123 | |||
115 | data Routing = Routing | 124 | data Routing = Routing |
116 | { tentativeId :: NodeInfo | 125 | { tentativeId :: NodeInfo |
117 | , committee4 :: TriadCommittee NodeId SockAddr | 126 | , committee4 :: TriadCommittee NodeId SockAddr |
118 | , committee6 :: TriadCommittee NodeId SockAddr | 127 | , committee6 :: TriadCommittee NodeId SockAddr |
119 | , refresher4 :: BucketRefresher NodeId NodeInfo | 128 | , refresher4 :: BucketRefresher NodeId NodeInfo |
120 | , refresher6 :: BucketRefresher NodeId NodeInfo | 129 | , refresher6 :: BucketRefresher NodeId NodeInfo |
130 | , nodesOfInterest :: TVar (HashMap NodeId [NodeInfoCallback]) | ||
121 | } | 131 | } |
122 | 132 | ||
123 | sched4 :: Routing -> TVar (Int.PSQ POSIXTime) | 133 | sched4 :: Routing -> TVar (Int.PSQ POSIXTime) |
@@ -166,10 +176,17 @@ newRouting addr crypto update4 update6 = do | |||
166 | tbl6 = refreshBuckets refresher6 | 176 | tbl6 = refreshBuckets refresher6 |
167 | committee4 <- newTriadCommittee (update4 tbl4) -- updateIPVote tbl4 addr4 | 177 | committee4 <- newTriadCommittee (update4 tbl4) -- updateIPVote tbl4 addr4 |
168 | committee6 <- newTriadCommittee (update6 tbl6) -- updateIPVote tbl6 addr6 | 178 | committee6 <- newTriadCommittee (update6 tbl6) -- updateIPVote tbl6 addr6 |
179 | cbvar <- newTVar HashMap.empty | ||
169 | return $ \client -> | 180 | return $ \client -> |
170 | -- Now we have a client, so tell the BucketRefresher how to search and ping. | 181 | -- Now we have a client, so tell the BucketRefresher how to search and ping. |
171 | let updIO r = updateRefresherIO (nodeSearch client) (ping client) r | 182 | let updIO r = updateRefresherIO (nodeSearch client) (ping client) r |
172 | in Routing tentative_info committee4 committee6 (updIO refresher4) (updIO refresher6) | 183 | in Routing { tentativeId = tentative_info |
184 | , committee4 = committee4 | ||
185 | , committee6 = committee6 | ||
186 | , refresher4 = updIO refresher4 | ||
187 | , refresher6 = updIO refresher6 | ||
188 | , nodesOfInterest = cbvar | ||
189 | } | ||
173 | 190 | ||
174 | 191 | ||
175 | -- TODO: This should cover more cases | 192 | -- TODO: This should cover more cases |
@@ -370,6 +387,11 @@ updateRouting :: Client -> Routing -> OnionRouter -> NodeInfo -> Message -> IO ( | |||
370 | updateRouting client routing orouter naddr msg | 387 | updateRouting client routing orouter naddr msg |
371 | | PacketKind 0x21 <- msgType msg = hPutStrLn stderr "(tox)updateRouting: ignoring lan discovery" -- ignore lan discovery | 388 | | PacketKind 0x21 <- msgType msg = hPutStrLn stderr "(tox)updateRouting: ignoring lan discovery" -- ignore lan discovery |
372 | | otherwise = do | 389 | | otherwise = do |
390 | atomically $ do | ||
391 | m <- HashMap.lookup (nodeId naddr) <$> readTVar (nodesOfInterest routing) | ||
392 | forM_ m $ mapM_ $ \NodeInfoCallback{interestingNodeId,observedAddress} -> do | ||
393 | when (interestingNodeId == nodeId naddr) | ||
394 | $ observedAddress naddr | ||
373 | case prefer4or6 naddr Nothing of | 395 | case prefer4or6 naddr Nothing of |
374 | Want_IP4 -> updateTable client naddr orouter (committee4 routing) (refresher4 routing) | 396 | Want_IP4 -> updateTable client naddr orouter (committee4 routing) (refresher4 routing) |
375 | Want_IP6 -> updateTable client naddr orouter (committee6 routing) (refresher6 routing) | 397 | Want_IP6 -> updateTable client naddr orouter (committee6 routing) (refresher6 routing) |