summaryrefslogtreecommitdiff
path: root/src/Network
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-07-03 22:59:31 -0400
committerjoe <joe@jerkface.net>2017-07-03 22:59:31 -0400
commitf75d515bc0100e5ca372d592aa2f5f4ff2fc858c (patch)
tree71bc354c8f6b8fed2275eb8c215d99b654c7f473 /src/Network
parent78b05bf38b83b5d46468e1f938bb8c2d9dd0804f (diff)
Fleshed out KRPC instances for Mainline DHT.
Diffstat (limited to 'src/Network')
-rw-r--r--src/Network/BitTorrent/DHT/Query.hs4
-rw-r--r--src/Network/DHT/Mainline.hs44
-rw-r--r--src/Network/DatagramServer.hs4
-rw-r--r--src/Network/DatagramServer/Tox.hs3
4 files changed, 37 insertions, 18 deletions
diff --git a/src/Network/BitTorrent/DHT/Query.hs b/src/Network/BitTorrent/DHT/Query.hs
index ad29adb6..77fede94 100644
--- a/src/Network/BitTorrent/DHT/Query.hs
+++ b/src/Network/BitTorrent/DHT/Query.hs
@@ -133,7 +133,8 @@ nodeHandler :: forall raw dht addr u t q r.
133 IsString t, Functor dht, 133 IsString t, Functor dht,
134 KRPC dht (Query dht q) (Response dht r), 134 KRPC dht (Query dht q) (Response dht r),
135 SerializableTo raw (Response dht r), 135 SerializableTo raw (Response dht r),
136 SerializableTo raw (Query dht q)) => 136 SerializableTo raw (Query dht q),
137 Show (QueryMethod dht)) =>
137 (NodeInfo dht addr u -> Maybe ReflectedIP -> IO ()) 138 (NodeInfo dht addr u -> Maybe ReflectedIP -> IO ())
138 -> (NodeAddr addr -> IO (NodeId dht)) 139 -> (NodeAddr addr -> IO (NodeId dht))
139 -> (Char -> t -> Text -> IO ()) 140 -> (Char -> t -> Text -> IO ())
@@ -150,6 +151,7 @@ nodeHandler insertNode myNodeIdAccordingTo logm dta method action = handler meth
150 case fromSockAddr sockAddr of 151 case fromSockAddr sockAddr of
151 Nothing -> throwIO BadAddress 152 Nothing -> throwIO BadAddress
152 Just naddr -> do 153 Just naddr -> do
154 logm 'D' "nodeHandler" $ "Received query: " <> T.pack (show $ method)
153 me <- myNodeIdAccordingTo naddr 155 me <- myNodeIdAccordingTo naddr
154 rextra <- liftIO $ makeResponseExtra dta me qry resptype 156 rextra <- liftIO $ makeResponseExtra dta me qry resptype
155 let ni = NodeInfo remoteId naddr def 157 let ni = NodeInfo remoteId naddr def
diff --git a/src/Network/DHT/Mainline.hs b/src/Network/DHT/Mainline.hs
index d68755a7..9af42a6d 100644
--- a/src/Network/DHT/Mainline.hs
+++ b/src/Network/DHT/Mainline.hs
@@ -218,11 +218,13 @@ instance Serialize (Response Ping) where
218 218
219-- | \"q\" = \"ping\" 219-- | \"q\" = \"ping\"
220instance KRPC KMessageOf (Query KMessageOf (Ping KMessageOf)) (Response KMessageOf (Ping KMessageOf)) where 220instance KRPC KMessageOf (Query KMessageOf (Ping KMessageOf)) (Response KMessageOf (Ping KMessageOf)) where
221#ifdef VERSION_bencoding 221 method = "ping"
222 method = "ping" 222 makeQueryExtra _ nid _ _ = return $ MainlineQuery nid False -- TODO: check for NAT issues. (BEP 43)
223#else 223 makeResponseExtra _ nid _ _ = return $ MainlineResponse nid
224 method = Method Tox.Ping -- response: Tox.Pong 224
225#endif 225 -- TODO KError Sender/Responder
226 messageSender (Q q) _ = queringNodeId $ queryExtra $ queryArgs q
227 messageResponder _ (R r) = queredNodeId $ responseExtra $ respVals r
226 228
227{----------------------------------------------------------------------- 229{-----------------------------------------------------------------------
228-- find_node method 230-- find_node method
@@ -302,13 +304,14 @@ instance Serialize (Response KMessageOf (NodeFound KMessageOf ip)) where
302-- | \"q\" == \"find_node\" 304-- | \"q\" == \"find_node\"
303instance (Address ip, Typeable ip) 305instance (Address ip, Typeable ip)
304 => KRPC KMessageOf (Query KMessageOf (FindNode KMessageOf ip)) (Response KMessageOf (NodeFound KMessageOf ip)) where 306 => KRPC KMessageOf (Query KMessageOf (FindNode KMessageOf ip)) (Response KMessageOf (NodeFound KMessageOf ip)) where
305#ifdef VERSION_bencoding 307 method = "find_node"
306 method = "find_node" 308 makeQueryExtra _ nid _ _ = return $ MainlineQuery nid False -- TODO: check for NAT issues. (BEP 43)
307#else 309 makeResponseExtra _ nid _ _ = return $ MainlineResponse nid
308 method = Method Tox.GetNodes -- response: Tox.SendNodes 310
309#endif 311 -- TODO KError Sender/Responder
312 messageSender (Q q) _ = queringNodeId $ queryExtra $ queryArgs q
313 messageResponder _ (R r) = queredNodeId $ responseExtra $ respVals r
310 314
311#ifdef VERSION_bencoding
312{----------------------------------------------------------------------- 315{-----------------------------------------------------------------------
313-- get_peers method 316-- get_peers method
314-----------------------------------------------------------------------} 317-----------------------------------------------------------------------}
@@ -371,7 +374,14 @@ instance (Typeable ip, Serialize ip) => BEncode (GotPeers ip) where
371-- | \"q" = \"get_peers\" 374-- | \"q" = \"get_peers\"
372instance (Typeable ip, Serialize ip) => 375instance (Typeable ip, Serialize ip) =>
373 KRPC KMessageOf (Query KMessageOf (GetPeers ip)) (Response KMessageOf (GotPeers ip)) where 376 KRPC KMessageOf (Query KMessageOf (GetPeers ip)) (Response KMessageOf (GotPeers ip)) where
374 method = "get_peers" 377 method = "get_peers"
378 makeQueryExtra _ nid _ _ = return $ MainlineQuery nid False -- TODO: check for NAT issues. (BEP 43)
379 makeResponseExtra _ nid _ _ = return $ MainlineResponse nid
380
381 -- TODO KError Sender/Responder
382 messageSender (Q q) _ = queringNodeId $ queryExtra $ queryArgs q
383 messageResponder _ (R r) = queredNodeId $ responseExtra $ respVals r
384
375 385
376{----------------------------------------------------------------------- 386{-----------------------------------------------------------------------
377-- announce method 387-- announce method
@@ -443,10 +453,14 @@ instance BEncode Announced where
443 453
444-- | \"q" = \"announce\" 454-- | \"q" = \"announce\"
445instance KRPC KMessageOf (Query KMessageOf Announce) (Response KMessageOf Announced) where 455instance KRPC KMessageOf (Query KMessageOf Announce) (Response KMessageOf Announced) where
446 method = "announce_peer" 456 method = "announce_peer"
457 makeQueryExtra _ nid _ _ = return $ MainlineQuery nid False -- TODO: check for NAT issues. (BEP 43)
458 makeResponseExtra _ nid _ _ = return $ MainlineResponse nid
459
460 -- TODO KError Sender/Responder
461 messageSender (Q q) _ = queringNodeId $ queryExtra $ queryArgs q
462 messageResponder _ (R r) = queredNodeId $ responseExtra $ respVals r
447 463
448-- endif VERSION_bencoding
449#endif
450 464
451-- | Yields all 8 DHT neighborhoods available to you given a particular ip 465-- | Yields all 8 DHT neighborhoods available to you given a particular ip
452-- address. 466-- address.
diff --git a/src/Network/DatagramServer.hs b/src/Network/DatagramServer.hs
index b9c78885..1376748f 100644
--- a/src/Network/DatagramServer.hs
+++ b/src/Network/DatagramServer.hs
@@ -481,17 +481,19 @@ runHandler mgr@Manager{..} meth h addr m = Lifted.catches wrapper failbacks
481 481
482 failbacks = 482 failbacks =
483 [ E.Handler $ \ (e :: HandlerFailure) -> do 483 [ E.Handler $ \ (e :: HandlerFailure) -> do
484 logMsg 'D' "handler.failed" signature 484 logMsg 'D' "handler.HandlerFailure" signature
485 return $ Left $ KError ProtocolError (prettyHF e) (envelopeTransaction m) 485 return $ Left $ KError ProtocolError (prettyHF e) (envelopeTransaction m)
486 486
487 487
488 -- may happen if handler makes query and fail 488 -- may happen if handler makes query and fail
489 , E.Handler $ \ (e :: QueryFailure) -> do 489 , E.Handler $ \ (e :: QueryFailure) -> do
490 logMsg 'D' "handler.QueryFailure" signature
490 return $ Left $ KError ServerError (prettyQF e) (envelopeTransaction m) 491 return $ Left $ KError ServerError (prettyQF e) (envelopeTransaction m)
491 492
492 -- since handler thread exit after sendMessage we can safely 493 -- since handler thread exit after sendMessage we can safely
493 -- suppress async exception here 494 -- suppress async exception here
494 , E.Handler $ \ (e :: SomeException) -> do 495 , E.Handler $ \ (e :: SomeException) -> do
496 logMsg 'D' "handler.SomeException" (signature <> T.pack (" "++show e))
495 return $ Left $ KError GenericError (BC.pack (show e)) (envelopeTransaction m) 497 return $ Left $ KError GenericError (BC.pack (show e)) (envelopeTransaction m)
496 ] 498 ]
497 499
diff --git a/src/Network/DatagramServer/Tox.hs b/src/Network/DatagramServer/Tox.hs
index 3a6135cc..f666b951 100644
--- a/src/Network/DatagramServer/Tox.hs
+++ b/src/Network/DatagramServer/Tox.hs
@@ -274,7 +274,8 @@ instance Envelope Message where
274 274
275 buildReply self addr qry payload = (fmap (const payload) qry) { msgClient = self } 275 buildReply self addr qry payload = (fmap (const payload) qry) { msgClient = self }
276 276
277 -- buildQuery = todo 277 -- buildQuery :: NodeId envelope -> SockAddr -> QueryMethod envelope -> TransactionID envelope -> a -> IO (envelope a)
278 -- buildQuery nid addr meth tid q = todo
278 279
279 uniqueTransactionId cnt = do 280 uniqueTransactionId cnt = do
280 return $ either (error "failed to create TransactionId") TID 281 return $ either (error "failed to create TransactionId") TID