summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/MainlineDHT.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-10-31 15:28:59 -0400
committerjoe <joe@jerkface.net>2017-10-31 15:28:59 -0400
commit7fe3c9e04ecd7aec962bcc83bc4cf5139cf732f0 (patch)
treeb0bdfd844adced80317b622778be5921fa0f79e2 /src/Network/BitTorrent/MainlineDHT.hs
parent3fdfec6b917016a2013be03bd3ec5682cc31893c (diff)
Added missing signatures for MainlineDHT.
Diffstat (limited to 'src/Network/BitTorrent/MainlineDHT.hs')
-rw-r--r--src/Network/BitTorrent/MainlineDHT.hs52
1 files changed, 21 insertions, 31 deletions
diff --git a/src/Network/BitTorrent/MainlineDHT.hs b/src/Network/BitTorrent/MainlineDHT.hs
index c9abf003..f4a5ade1 100644
--- a/src/Network/BitTorrent/MainlineDHT.hs
+++ b/src/Network/BitTorrent/MainlineDHT.hs
@@ -52,6 +52,7 @@ import Data.Typeable
52import Data.Word 52import Data.Word
53import qualified Data.Wrapper.PSQInt as Int 53import qualified Data.Wrapper.PSQInt as Int
54import Debug.Trace 54import Debug.Trace
55import Network.BitTorrent.MainlineDHT.Symbols
55import Network.Kademlia 56import Network.Kademlia
56import Network.Address (Address, fromAddr, fromSockAddr, 57import Network.Address (Address, fromAddr, fromSockAddr,
57 setPort, sockAddrPort, testIdBit, 58 setPort, sockAddrPort, testIdBit,
@@ -303,6 +304,7 @@ data Message a = Q { msgOrigin :: NodeId
303 , rspPayload :: Either Error a 304 , rspPayload :: Either Error a
304 , rspReflectedIP :: Maybe SockAddr } 305 , rspReflectedIP :: Maybe SockAddr }
305 306
307showBE :: BValue -> String
306showBE bval = L8.unpack (showBEncode bval) 308showBE bval = L8.unpack (showBEncode bval)
307 309
308instance BE.BEncode (Message BValue) where 310instance BE.BEncode (Message BValue) where
@@ -409,6 +411,7 @@ encodeAny tid key val aux = toDict $
409 .: endDict 411 .: endDict
410 412
411 413
414showPacket :: ([L8.ByteString] -> [L8.ByteString]) -> SockAddr -> L8.ByteString -> ByteString -> String
412showPacket f addr flow bs = L8.unpack $ L8.unlines es 415showPacket f addr flow bs = L8.unpack $ L8.unlines es
413 where 416 where
414 es = map (L8.append prefix) (f $ L8.lines pp) 417 es = map (L8.append prefix) (f $ L8.lines pp)
@@ -418,6 +421,7 @@ showPacket f addr flow bs = L8.unpack $ L8.unlines es
418 pp = either L8.pack showBEncode $ BE.decode bs 421 pp = either L8.pack showBEncode $ BE.decode bs
419 422
420-- Add detailed printouts for every packet. 423-- Add detailed printouts for every packet.
424addVerbosity :: Transport err SockAddr ByteString -> Transport err SockAddr ByteString
421addVerbosity tr = 425addVerbosity tr =
422 tr { awaitMessage = \kont -> awaitMessage tr $ \m -> do 426 tr { awaitMessage = \kont -> awaitMessage tr $ \m -> do
423 forM_ m $ mapM_ $ \(msg,addr) -> do 427 forM_ m $ mapM_ $ \(msg,addr) -> do
@@ -429,7 +433,7 @@ addVerbosity tr =
429 } 433 }
430 434
431 435
432 436showParseError :: ByteString -> SockAddr -> String -> String
433showParseError bs addr err = showPacket (L8.pack err :) addr " --> " bs 437showParseError bs addr err = showPacket (L8.pack err :) addr " --> " bs
434 438
435parsePacket :: ByteString -> SockAddr -> Either String (Message BValue, NodeInfo) 439parsePacket :: ByteString -> SockAddr -> Either String (Message BValue, NodeInfo)
@@ -918,36 +922,6 @@ mkAnnounce portnum info token = Announce
918 , impliedPort = False 922 , impliedPort = False
919 } 923 }
920 924
921peer_ip_key = "ip"
922peer_id_key = "peer id"
923peer_port_key = "port"
924msg_type_key = "msg_type"
925piece_key = "piece"
926total_size_key = "total_size"
927node_id_key :: BKey
928node_id_key = "id"
929read_only_key :: BKey
930read_only_key = "ro"
931want_key :: BKey
932want_key = "want"
933target_key :: BKey
934target_key = "target"
935nodes_key :: BKey
936nodes_key = "nodes"
937nodes6_key :: BKey
938nodes6_key = "nodes6"
939info_hash_key :: BKey
940info_hash_key = "info_hash"
941peers_key :: BKey
942peers_key = "values"
943token_key :: BKey
944token_key = "token"
945name_key :: BKey
946name_key = "name"
947port_key :: BKey
948port_key = "port"
949implied_port_key :: BKey
950implied_port_key = "implied_port"
951 925
952instance BEncode Announce where 926instance BEncode Announce where
953 toBEncode Announce {..} = toDict $ 927 toBEncode Announce {..} = toDict $
@@ -1004,8 +978,18 @@ announceH (SwarmsDatabase peers toks _) naddr announcement = do
1004 } 978 }
1005 return Announced 979 return Announced
1006 980
981isReadonlyClient :: MainlineClient -> Bool
1007isReadonlyClient client = False -- TODO 982isReadonlyClient client = False -- TODO
1008 983
984mainlineSend :: ( BEncode a
985 , BEncode a2
986 ) => Method
987 -> (a2 -> b)
988 -> (t -> a)
989 -> MainlineClient
990 -> t
991 -> NodeInfo
992 -> IO (Maybe b)
1009mainlineSend meth unwrap msg client nid addr = do 993mainlineSend meth unwrap msg client nid addr = do
1010 reply <- sendQuery client serializer (msg nid) addr 994 reply <- sendQuery client serializer (msg nid) addr
1011 -- sendQuery will return (Just (Left _)) on a parse error. We're going to 995 -- sendQuery will return (Just (Left _)) on a parse error. We're going to
@@ -1032,21 +1016,27 @@ ping client addr =
1032getNodes :: MainlineClient -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[NodeInfo],())) 1016getNodes :: MainlineClient -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[NodeInfo],()))
1033getNodes = mainlineSend (Method "find_node") unwrapNodes $ flip FindNode (Just Want_Both) 1017getNodes = mainlineSend (Method "find_node") unwrapNodes $ flip FindNode (Just Want_Both)
1034 1018
1019unwrapNodes :: NodeFound -> ([NodeInfo], [NodeInfo], ())
1035unwrapNodes (NodeFound ns4 ns6) = (ns4++ns6, ns4++ns6,()) 1020unwrapNodes (NodeFound ns4 ns6) = (ns4++ns6, ns4++ns6,())
1036 1021
1037getPeers :: MainlineClient -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[PeerAddr],Token)) 1022getPeers :: MainlineClient -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[PeerAddr],Token))
1038getPeers = mainlineSend (Method "get_peers") unwrapPeers $ flip GetPeers (Just Want_Both) . coerce 1023getPeers = mainlineSend (Method "get_peers") unwrapPeers $ flip GetPeers (Just Want_Both) . coerce
1039 1024
1025unwrapPeers :: GotPeers -> ([NodeInfo], [PeerAddr], Token)
1040unwrapPeers (GotPeers ps (NodeFound ns4 ns6) tok) = (ns4++ns6, ps, tok) 1026unwrapPeers (GotPeers ps (NodeFound ns4 ns6) tok) = (ns4++ns6, ps, tok)
1041 1027
1028mainlineSearch :: (NodeId -> NodeInfo -> IO (Maybe ([NodeInfo], [r], tok)))
1029 -> Search NodeId (IP, PortNumber) tok NodeInfo r
1042mainlineSearch qry = Search 1030mainlineSearch qry = Search
1043 { searchSpace = mainlineSpace 1031 { searchSpace = mainlineSpace
1044 , searchNodeAddress = nodeIP &&& nodePort 1032 , searchNodeAddress = nodeIP &&& nodePort
1045 , searchQuery = qry 1033 , searchQuery = qry
1046 } 1034 }
1047 1035
1036nodeSearch :: MainlineClient -> Search NodeId (IP, PortNumber) () NodeInfo NodeInfo
1048nodeSearch client = mainlineSearch (getNodes client) 1037nodeSearch client = mainlineSearch (getNodes client)
1049 1038
1039peerSearch :: MainlineClient -> Search NodeId (IP, PortNumber) Token NodeInfo PeerAddr
1050peerSearch client = mainlineSearch (getPeers client) 1040peerSearch client = mainlineSearch (getPeers client)
1051 1041
1052-- | List of bootstrap nodes maintained by different bittorrent 1042-- | List of bootstrap nodes maintained by different bittorrent