From ab1aaab49ab6a4a13c4416201b261a69155f2eec Mon Sep 17 00:00:00 2001 From: joe Date: Mon, 12 Jun 2017 21:49:13 -0400 Subject: Evacuated the Network.DHT.Mainline code. Most of it went to Network.BitTorrent.DHT.Message. Some of it went to Network.DatagramServer.Mainline. --- src/Network/DHT/Mainline.hs | 83 --------------------------------------------- 1 file changed, 83 deletions(-) delete mode 100644 src/Network/DHT/Mainline.hs (limited to 'src/Network/DHT') diff --git a/src/Network/DHT/Mainline.hs b/src/Network/DHT/Mainline.hs deleted file mode 100644 index 2ecb9845..00000000 --- a/src/Network/DHT/Mainline.hs +++ /dev/null @@ -1,83 +0,0 @@ -{-# LANGUAGE LambdaCase #-} -{-# LANGUAGE MultiParamTypeClasses #-} -{-# LANGUAGE FlexibleInstances #-} -{-# LANGUAGE DeriveDataTypeable #-} -{-# LANGUAGE TypeFamilies #-} -{-# LANGUAGE GeneralizedNewtypeDeriving #-} -module Network.DHT.Mainline where - -import Data.Digest.CRC32C -import Control.Applicative -import Data.Maybe -import Data.Monoid -import Data.Word -import Data.IP -import Data.BEncode as BE -import Data.Bits -import Data.ByteString (ByteString) -import Data.ByteString.Base16 as Base16 -import qualified Data.ByteString.Char8 as Char8 -import qualified Data.ByteString as BS -import qualified Data.ByteString.Lazy as L -import Data.Default -import Data.LargeWord -import Data.Serialize as S -import Data.String -import Data.Typeable -import Network.DatagramServer.Mainline (NodeId(..)) -import Network.DatagramServer.Mainline as KRPC -import Network.DatagramServer.Types as RPC -import Text.PrettyPrint as PP hiding ((<>)) -import Text.PrettyPrint.HughesPJClass hiding (($$), (<>)) - --- | KRPC 'compact list' compatible encoding: contact information for --- nodes is encoded as a 26-byte string. Also known as "Compact node --- info" the 20-byte Node ID in network byte order has the compact --- IP-address/port info concatenated to the end. -instance Serialize a => Serialize (NodeInfo KMessageOf a ()) where - get = (\a b -> NodeInfo a b ()) <$> get <*> get - put NodeInfo {..} = put nodeId >> put nodeAddr - -instance Pretty ip => Pretty (NodeInfo KMessageOf ip ()) where - pPrint NodeInfo {..} = pPrint nodeId <> "@(" <> pPrint nodeAddr <> ")" - -instance Pretty ip => Pretty [NodeInfo KMessageOf ip ()] where - pPrint = PP.vcat . PP.punctuate "," . map pPrint - - --- | Yields all 8 DHT neighborhoods available to you given a particular ip --- address. -bep42s :: Address a => a -> NodeId KMessageOf -> [NodeId KMessageOf] -bep42s addr (NodeId r) = mapMaybe (bep42 addr) rs - where - rs = map (NodeId . change3bits r) [0..7] - --- change3bits :: ByteString -> Word8 -> ByteString --- change3bits bs n = BS.snoc (BS.init bs) (BS.last bs .&. 0xF8 .|. n) - -change3bits :: (Num b, Bits b) => b -> b -> b -change3bits bs n = (bs .&. complement 7) .|. n - --- | Modifies a purely random 'NodeId' to one that is related to a given --- routable address in accordance with BEP 42. -bep42 :: Address a => a -> NodeId KMessageOf -> Maybe (NodeId KMessageOf) -bep42 addr (NodeId r) - | Just ip <- fmap S.encode (fromAddr addr :: Maybe IPv4) - <|> fmap S.encode (fromAddr addr :: Maybe IPv6) - = genBucketSample' retr (NodeId $ crc $ applyMask ip) (3,0x07,0) - | otherwise - = Nothing - where - ip4mask = "\x03\x0f\x3f\xff" :: ByteString - ip6mask = "\x01\x03\x07\x0f\x1f\x3f\x7f\xff" :: ByteString - nbhood_select = (fromIntegral r :: Word8) .&. 7 - retr n = pure $ BS.drop (nodeIdSize - n) $ S.encode r - crc = flip shiftL (finiteBitSize (NodeId undefined) - 32) . fromIntegral . crc32c . BS.pack - applyMask ip = case BS.zipWith (.&.) msk ip of - (b:bs) -> (b .|. shiftL nbhood_select 5) : bs - bs -> bs - where msk | BS.length ip == 4 = ip4mask - | otherwise = ip6mask - - - -- cgit v1.2.3