summaryrefslogtreecommitdiff
path: root/DHTHandlers.hs
diff options
context:
space:
mode:
Diffstat (limited to 'DHTHandlers.hs')
-rw-r--r--DHTHandlers.hs36
1 files changed, 32 insertions, 4 deletions
diff --git a/DHTHandlers.hs b/DHTHandlers.hs
index 41a4bc06..437b05f3 100644
--- a/DHTHandlers.hs
+++ b/DHTHandlers.hs
@@ -6,19 +6,24 @@ import DHTTransport
6import Network.QueryResponse as QR hiding (Client) 6import Network.QueryResponse as QR hiding (Client)
7import qualified Network.QueryResponse as QR (Client) 7import qualified Network.QueryResponse as QR (Client)
8import ToxCrypto 8import ToxCrypto
9import ToxMessage as Tox (PacketKind(..), pattern PingType, pattern GetNodesType, pattern DHTRequestType) 9import ToxMessage as Tox (PacketKind(..), pattern PingType, pattern PongType, pattern GetNodesType, pattern SendNodesType, pattern DHTRequestType)
10import Network.BitTorrent.DHT.Search
10 11
12import Control.Arrow
11import qualified Data.Wrapper.PSQInt as Int 13import qualified Data.Wrapper.PSQInt as Int
12import Kademlia 14import Kademlia
13import Network.Address (WantIP (..), ipFamily, testIdBit) 15import Network.Address (WantIP (..), ipFamily, testIdBit,fromSockAddr, sockAddrPort)
14import qualified Network.DHT.Routing as R 16import qualified Network.DHT.Routing as R
15import TriadCommittee 17import TriadCommittee
18import Global6
16 19
17import Control.Monad 20import Control.Monad
18import Control.Concurrent.STM 21import Control.Concurrent.STM
19import Data.Time.Clock.POSIX (POSIXTime, getPOSIXTime) 22import Data.Time.Clock.POSIX (POSIXTime, getPOSIXTime)
20import Network.Socket 23import Network.Socket
24import Data.Hashable
21import Data.IP 25import Data.IP
26import Data.Ord
22import Data.Maybe 27import Data.Maybe
23import Data.Bits 28import Data.Bits
24 29
@@ -77,9 +82,11 @@ newRouting addr crypto update4 update6 = do
77 82
78 83
79-- TODO: This should cover more cases 84-- TODO: This should cover more cases
85isLocal :: IP -> Bool
80isLocal (IPv6 ip6) = (ip6 == toEnum 0) 86isLocal (IPv6 ip6) = (ip6 == toEnum 0)
81isLocal (IPv4 ip4) = (ip4 == toEnum 0) 87isLocal (IPv4 ip4) = (ip4 == toEnum 0)
82 88
89isGlobal :: IP -> Bool
83isGlobal = not . isLocal 90isGlobal = not . isLocal
84 91
85prefer4or6 :: NodeInfo -> Maybe WantIP -> WantIP 92prefer4or6 :: NodeInfo -> Maybe WantIP -> WantIP
@@ -210,6 +217,27 @@ transitionCommittee committee _ = return $ return ()
210 217
211type Handler = MethodHandler String TransactionId NodeInfo Message 218type Handler = MethodHandler String TransactionId NodeInfo Message
212 219
220isPing :: (f Ping -> Ping) -> DHTMessage f -> Either String Ping
221isPing unpack (DHTPing a) = Right $ unpack $ assymData a
222isPing _ _ = Left "Bad ping"
223
224mkPong :: TransactionId -> NodeInfo -> NodeInfo -> Pong -> DHTMessage ((,) Nonce8)
225mkPong tid src dst pong = DHTPong $ wrapAssym tid src dst (, pong)
226
227isGetNodes :: (f GetNodes -> GetNodes) -> DHTMessage f -> Either String GetNodes
228isGetNodes unpack (DHTGetNodes a) = Right $ unpack $ assymData a
229isGetNodes _ _ = Left "Bad GetNodes"
230
231mkSendNodes :: TransactionId -> NodeInfo -> NodeInfo -> SendNodes -> DHTMessage ((,) Nonce8)
232mkSendNodes tid src dst sendnodes = DHTSendNodes $ wrapAssym tid src dst (, sendnodes)
233
213handlers :: Routing -> Tox.PacketKind -> Maybe Handler 234handlers :: Routing -> Tox.PacketKind -> Maybe Handler
214handlers routing PingType = handler PongType pingH 235handlers routing PingType = Just $ MethodHandler (isPing snd) mkPong pingH
215handlers routing GetNodesType = handler SendNodesType $ getNodesH routing 236handlers routing GetNodesType = Just $ MethodHandler (isGetNodes snd) mkSendNodes $ getNodesH routing
237
238nodeSearch :: Client -> Search NodeId (IP,PortNumber) () NodeInfo NodeInfo
239nodeSearch client = Search
240 { searchSpace = toxSpace
241 , searchNodeAddress = nodeIP &&& nodePort
242 , searchQuery = getNodes client
243 }