summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--network-bittorrent.cabal9
-rw-r--r--src/Data/Kademlia/RoutingTable.hs28
-rw-r--r--src/Network/DHT/Kademlia.hs68
3 files changed, 103 insertions, 2 deletions
diff --git a/network-bittorrent.cabal b/network-bittorrent.cabal
index 53cadcd8..da4e6818 100644
--- a/network-bittorrent.cabal
+++ b/network-bittorrent.cabal
@@ -22,8 +22,8 @@ source-repository head
22 22
23library 23library
24 exposed-modules: Data.Torrent 24 exposed-modules: Data.Torrent
25 , Data.Bitfield
26 , Data.Torrent.InfoHash 25 , Data.Torrent.InfoHash
26 , Data.Bitfield
27 27
28 , Network.BitTorrent 28 , Network.BitTorrent
29 , Network.BitTorrent.Extension 29 , Network.BitTorrent.Extension
@@ -44,6 +44,10 @@ library
44 , Network.BitTorrent.PeerWire.Message 44 , Network.BitTorrent.PeerWire.Message
45 , Network.BitTorrent.PeerWire.Handshake 45 , Network.BitTorrent.PeerWire.Handshake
46 46
47 -- DHT
48 , Data.Kademlia.RoutingTable
49 , Network.DHT.Kademlia
50
47 other-modules: 51 other-modules:
48 52
49 53
@@ -69,8 +73,9 @@ library
69 -- network related packages 73 -- network related packages
70 , network >= 2.4 74 , network >= 2.4
71 , HTTP >= 4000.2 75 , HTTP >= 4000.2
72 , cryptohash 76 , krpc
73 77
78 , cryptohash
74 , filepath == 1.* 79 , filepath == 1.*
75 80
76 81
diff --git a/src/Data/Kademlia/RoutingTable.hs b/src/Data/Kademlia/RoutingTable.hs
new file mode 100644
index 00000000..98e15a0b
--- /dev/null
+++ b/src/Data/Kademlia/RoutingTable.hs
@@ -0,0 +1,28 @@
1module Data.Kademlia.RoutingTable
2 (
3 ) where
4
5import Data.ByteString
6
7type NodeID = ByteString
8type InfoHash = ByteString
9
10type Bucket = [NodeID]
11
12data Tree
13 = Tip Bucket
14 | Bin Table Table
15
16data Table = Table {
17 tree :: Tree
18 , bucketSize :: Int
19 }
20
21closest :: InfoHash -> Table -> [NodeID]
22closest = undefined
23
24insert :: NodeID -> Table -> Table
25insert x t = undefined
26
27-- TODO table serialization: usually we need to save table between
28-- target program executions
diff --git a/src/Network/DHT/Kademlia.hs b/src/Network/DHT/Kademlia.hs
new file mode 100644
index 00000000..d5418beb
--- /dev/null
+++ b/src/Network/DHT/Kademlia.hs
@@ -0,0 +1,68 @@
1-- TODO move to Network.DHT.Kademlia
2{-# LANGUAGE OverloadedStrings #-}
3module Network.DHT.Kademlia
4 (
5 ) where
6
7import Data.ByteString
8import Network
9import Remote.KRPC
10
11
12
13-- | Global unique identifier of the node. Size of the identifier
14-- should(!) be equal to the size of DHT keys. This limitation arises
15-- from the design of Kademlia: we should estimate distance between
16-- keys and NodeId in routing algorithms.
17--
18type NodeID = ByteString
19
20type NodeAddr = ByteString
21type InfoHash = ByteString
22type Token = ByteString
23
24-- | Used to check out if a node is alive or not. This has a tow-fold effect:
25--
26-- * If(!) caller get response it should update the bucket
27-- corresponding to the callee.
28--
29-- * Callee of the ping should update the bucket corresponding
30-- to the caller as well.
31--
32ping :: Method NodeID NodeID
33ping = method "ping" ["id"] ["id"]
34
35type PeerContact = ()
36data NodeContact = NodeContact {
37 peerContact :: PeerContact
38 , nodeID :: NodeID
39 }
40
41-- | Used to lookup peer ID from node ID.
42--
43find_node :: Method (NodeID, NodeID) (NodeID, NodeContact)
44find_node = method "find_node" ["id", "target"] ["id", "nodes"]
45
46-- |
47announce_peer :: Method (NodeID, InfoHash, PortNumber, Token) NodeID
48announce_peer = undefined
49
50genNodeID :: IO NodeID
51genNodeID = undefined
52
53{-
54type InfoHash = Int
55type Token = Int
56
57ping :: Method NodeId NodeId
58ping = method "ping" ["id"] ["id"]
59
60get_peers :: Method (NodeId :*: InfoHash) (NodeId, Token, NodeAddr :|: NodeAddr)
61get_peers = method "get_peers"
62 ("id", "target")
63 ("id", "token", view ("values" :|: "nodes"))
64
65
66
67
68-} \ No newline at end of file