diff options
author | joe <joe@jerkface.net> | 2017-07-02 16:41:11 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-07-02 16:41:11 -0400 |
commit | be15a1ceba3b87f845d3b56915207457a94394ee (patch) | |
tree | e0d0bc153dab2ebbcb23a8a4a16b03efe2e43de5 /src/Network/BitTorrent | |
parent | de4a5fd744b4744a80e3403f914886ff7df10526 (diff) |
Abstract facility to represent Tox encrypted nonces.
Diffstat (limited to 'src/Network/BitTorrent')
-rw-r--r-- | src/Network/BitTorrent/DHT/Query.hs | 43 |
1 files changed, 26 insertions, 17 deletions
diff --git a/src/Network/BitTorrent/DHT/Query.hs b/src/Network/BitTorrent/DHT/Query.hs index 4c980e22..87081d38 100644 --- a/src/Network/BitTorrent/DHT/Query.hs +++ b/src/Network/BitTorrent/DHT/Query.hs | |||
@@ -127,32 +127,37 @@ nodeHandler :: ( Address ip | |||
127 | ) | 127 | ) |
128 | => (NodeInfo KMessageOf ip () -> Maybe ReflectedIP -> IO ()) -> (NodeAddr ip -> IO (NodeId KMessageOf)) -> (Char -> String -> Text -> IO ()) -> QueryMethod KMessageOf -> (NodeAddr ip -> a -> IO b) -> NodeHandler | 128 | => (NodeInfo KMessageOf ip () -> Maybe ReflectedIP -> IO ()) -> (NodeAddr ip -> IO (NodeId KMessageOf)) -> (Char -> String -> Text -> IO ()) -> QueryMethod KMessageOf -> (NodeAddr ip -> a -> IO b) -> NodeHandler |
129 | -} | 129 | -} |
130 | nodeHandler :: | 130 | nodeHandler :: forall raw dht addr u t q r. |
131 | (Address addr, WireFormat raw msg, Pretty (NodeInfo dht addr u), | 131 | (Address addr, WireFormat raw dht, Pretty (NodeInfo dht addr u), |
132 | Default u, | 132 | Default u, |
133 | IsString t, Functor msg, | 133 | IsString t, Functor dht, |
134 | KRPC dht (Query dht q) (Response dht r), | ||
134 | SerializableTo raw (Response dht r), | 135 | SerializableTo raw (Response dht r), |
135 | SerializableTo raw (Query dht q)) => | 136 | SerializableTo raw (Query dht q)) => |
136 | (NodeInfo dht addr u -> Maybe ReflectedIP -> IO ()) | 137 | (NodeInfo dht addr u -> Maybe ReflectedIP -> IO ()) |
137 | -> (NodeAddr addr -> IO (NodeId dht)) | 138 | -> (NodeAddr addr -> IO (NodeId dht)) |
138 | -> (Char -> t -> Text -> IO ()) | 139 | -> (Char -> t -> Text -> IO ()) |
139 | -> QueryMethod msg | 140 | -> DHTData dht addr |
141 | -> QueryMethod dht | ||
140 | -> (NodeAddr addr -> q -> IO r) | 142 | -> (NodeAddr addr -> q -> IO r) |
141 | -> Handler IO msg raw | 143 | -> Handler IO dht raw |
142 | nodeHandler insertNode myNodeIdAccordingTo logm method action = handler method $ \ sockAddr qry -> do | 144 | nodeHandler insertNode myNodeIdAccordingTo logm dta method action = handler method $ \ sockAddr qry -> do |
143 | let remoteId = queringNodeId qry | 145 | let remoteId = queringNodeId qry |
144 | read_only = queryIsReadOnly qry | 146 | qextra = queryExtra qry |
145 | q = queryParams qry | 147 | resptype = Proxy :: Proxy (Response dht r) |
148 | q = queryParams qry | ||
146 | case fromSockAddr sockAddr of | 149 | case fromSockAddr sockAddr of |
147 | Nothing -> throwIO BadAddress | 150 | Nothing -> throwIO BadAddress |
148 | Just naddr -> do | 151 | Just naddr -> do |
152 | rextra <- liftIO $ makeResponseExtra dta qry resptype | ||
149 | let ni = NodeInfo remoteId naddr def | 153 | let ni = NodeInfo remoteId naddr def |
150 | -- Do not route read-only nodes. (bep 43) | 154 | -- Do not route read-only nodes. (bep 43) |
151 | if read_only | 155 | if fromRoutableNode qextra |
152 | then logm 'W' "nodeHandler" $ "READ-ONLY " <> T.pack (show $ pPrint ni) | 156 | then insertNode ni Nothing >> return () -- TODO need to block. why? |
153 | else insertNode ni Nothing >> return () -- TODO need to block. why? | 157 | else logm 'W' "nodeHandler" $ "READ-ONLY " <> T.pack (show $ pPrint ni) |
154 | Response | 158 | Response |
155 | <$> myNodeIdAccordingTo naddr | 159 | <$> myNodeIdAccordingTo naddr |
160 | <*> pure rextra | ||
156 | <*> action naddr q | 161 | <*> action naddr q |
157 | 162 | ||
158 | -- | Default 'Ping' handler. | 163 | -- | Default 'Ping' handler. |
@@ -208,11 +213,12 @@ kademliaHandlers :: forall raw dht u ip. (Eq ip, Ord ip, Address ip | |||
208 | kademliaHandlers logger = do | 213 | kademliaHandlers logger = do |
209 | groknode <- insertNode1 | 214 | groknode <- insertNode1 |
210 | mynid <- myNodeIdAccordingTo1 | 215 | mynid <- myNodeIdAccordingTo1 |
216 | dta <- asks dhtData | ||
211 | let handler :: ( KRPC dht (Query dht a) (Response dht b) | 217 | let handler :: ( KRPC dht (Query dht a) (Response dht b) |
212 | , SerializableTo raw (Response dht b) | 218 | , SerializableTo raw (Response dht b) |
213 | , SerializableTo raw (Query dht a) | 219 | , SerializableTo raw (Query dht a) |
214 | ) => QueryMethod dht -> (NodeAddr ip -> a -> IO b) -> Handler IO dht raw | 220 | ) => QueryMethod dht -> (NodeAddr ip -> a -> IO b) -> Handler IO dht raw |
215 | handler = nodeHandler groknode mynid (logt logger) | 221 | handler = nodeHandler groknode mynid (logt logger) dta |
216 | dht = Proxy :: Proxy dht | 222 | dht = Proxy :: Proxy dht |
217 | getclosest <- getClosest1 | 223 | getclosest <- getClosest1 |
218 | return [ handler (namePing dht) $ pingH dht | 224 | return [ handler (namePing dht) $ pingH dht |
@@ -248,6 +254,7 @@ bthandlers getclosest dta = | |||
248 | data MethodHandler raw dht ip = | 254 | data MethodHandler raw dht ip = |
249 | forall a b. ( SerializableTo raw (Response dht b) | 255 | forall a b. ( SerializableTo raw (Response dht b) |
250 | , SerializableTo raw (Query dht a) | 256 | , SerializableTo raw (Query dht a) |
257 | , KRPC dht (Query dht a) (Response dht b) | ||
251 | ) => MethodHandler (QueryMethod dht) (NodeAddr ip -> a -> IO b) | 258 | ) => MethodHandler (QueryMethod dht) (NodeAddr ip -> a -> IO b) |
252 | 259 | ||
253 | -- | Includes all default query handlers. | 260 | -- | Includes all default query handlers. |
@@ -275,9 +282,9 @@ defaultHandlers :: forall raw dht u ip. | |||
275 | defaultHandlers logger = do | 282 | defaultHandlers logger = do |
276 | groknode <- insertNode1 | 283 | groknode <- insertNode1 |
277 | mynid <- myNodeIdAccordingTo1 | 284 | mynid <- myNodeIdAccordingTo1 |
278 | let handler :: MethodHandler raw dht ip -> Handler IO dht raw | ||
279 | handler (MethodHandler name action) = nodeHandler groknode mynid (logt logger) name action | ||
280 | dta <- asks dhtData | 285 | dta <- asks dhtData |
286 | let handler :: MethodHandler raw dht ip -> Handler IO dht raw | ||
287 | handler (MethodHandler name action) = nodeHandler groknode mynid (logt logger) dta name action | ||
281 | getclosest <- getClosest1 | 288 | getclosest <- getClosest1 |
282 | hs <- kademliaHandlers logger | 289 | hs <- kademliaHandlers logger |
283 | return $ hs ++ L.map handler (dataHandlers (fmap (fmap (fmap (const ()))) . getclosest) dta) | 290 | return $ hs ++ L.map handler (dataHandlers (fmap (fmap (fmap (const ()))) . getclosest) dta) |
@@ -664,10 +671,12 @@ queryNode' :: forall raw dht u a b ip. | |||
664 | ) => NodeAddr ip -> a -> DHT raw dht u ip (NodeId dht, b, Maybe ReflectedIP) | 671 | ) => NodeAddr ip -> a -> DHT raw dht u ip (NodeId dht, b, Maybe ReflectedIP) |
665 | queryNode' addr q = do | 672 | queryNode' addr q = do |
666 | nid <- myNodeIdAccordingTo addr | 673 | nid <- myNodeIdAccordingTo addr |
674 | dta <- asks dhtData | ||
675 | qextra <- liftIO $ makeQueryExtra dta (Proxy :: Proxy (Query dht q)) (Proxy :: Proxy (Response dht b)) | ||
667 | let read_only = False -- TODO: check for NAT issues. (BEP 43) | 676 | let read_only = False -- TODO: check for NAT issues. (BEP 43) |
668 | let KRPC.Method name = KRPC.method :: KRPC.Method dht (Query dht a) (Response dht b) | 677 | -- let KRPC.Method name = KRPC.method :: KRPC.Method dht (Query dht a) (Response dht b) |
669 | mgr <- asks manager | 678 | mgr <- asks manager |
670 | (Response remoteId r, witnessed_ip) <- liftIO $ query' mgr name (toSockAddr addr) (Query nid read_only q) | 679 | (Response remoteId rextra r , witnessed_ip) <- liftIO $ query' mgr (toSockAddr addr) (Query nid qextra q) |
671 | -- \$(logDebugS) "queryNode" $ "Witnessed address: " <> T.pack (show witnessed_ip) | 680 | -- \$(logDebugS) "queryNode" $ "Witnessed address: " <> T.pack (show witnessed_ip) |
672 | -- <> " by " <> T.pack (show (toSockAddr addr)) | 681 | -- <> " by " <> T.pack (show (toSockAddr addr)) |
673 | _ <- insertNode (NodeInfo remoteId addr def) witnessed_ip | 682 | _ <- insertNode (NodeInfo remoteId addr def) witnessed_ip |