diff options
author | joe <joe@jerkface.net> | 2017-06-08 03:07:13 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-06-08 03:07:13 -0400 |
commit | 8c33deac14ca92ef67afc7fbcd3f67bc19317f88 (patch) | |
tree | e7636f38ae91ff0ef7c84091ccc65048cc45fea5 /src/Data/Tox.hs | |
parent | d6fac9a8df0ce872ede54d6a71ca6d6c750eadc9 (diff) |
WIP: Adapting DHT to Tox network (part 6).
Diffstat (limited to 'src/Data/Tox.hs')
-rw-r--r-- | src/Data/Tox.hs | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/src/Data/Tox.hs b/src/Data/Tox.hs index 888ca3b6..b9df7582 100644 --- a/src/Data/Tox.hs +++ b/src/Data/Tox.hs | |||
@@ -1,3 +1,6 @@ | |||
1 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
2 | {-# LANGUAGE StandaloneDeriving #-} | ||
3 | {-# LANGUAGE FlexibleInstances #-} | ||
1 | {-# LANGUAGE DeriveDataTypeable #-} | 4 | {-# LANGUAGE DeriveDataTypeable #-} |
2 | {-# LANGUAGE DeriveFunctor #-} | 5 | {-# LANGUAGE DeriveFunctor #-} |
3 | {-# LANGUAGE DeriveGeneric #-} | 6 | {-# LANGUAGE DeriveGeneric #-} |
@@ -10,23 +13,23 @@ | |||
10 | {-# LANGUAGE UnboxedTuples #-} | 13 | {-# LANGUAGE UnboxedTuples #-} |
11 | module Data.Tox where | 14 | module Data.Tox where |
12 | 15 | ||
16 | import Data.Bits | ||
13 | import Data.ByteString (ByteString) | 17 | import Data.ByteString (ByteString) |
14 | import Data.Data (Data) | 18 | import Data.Data (Data) |
15 | import Data.Word | 19 | import Data.Word |
16 | import Data.LargeWord | 20 | import Data.LargeWord |
17 | import Data.IP | 21 | import Data.IP |
18 | import Data.Serialize | 22 | import Data.Serialize |
19 | import Network.BitTorrent.Address (NodeInfo(..)) -- Serialize IP | 23 | -- import Network.BitTorrent.Address (NodeInfo(..)) -- Serialize IP |
20 | import GHC.Generics (Generic) | 24 | import GHC.Generics (Generic) |
21 | import Network.Socket | 25 | import Network.Socket |
22 | import Network.RPC hiding (NodeId) | 26 | import Network.RPC |
23 | import qualified Network.RPC as Envelope (NodeId) | 27 | import qualified Network.RPC as Envelope (NodeId) |
24 | import Crypto.PubKey.ECC.Types | 28 | import Crypto.PubKey.ECC.Types |
25 | 29 | ||
26 | type Key32 = Word256 -- 32 byte key | 30 | type Key32 = Word256 -- 32 byte key |
27 | type Nonce8 = Word64 -- 8 bytes | 31 | type Nonce8 = Word64 -- 8 bytes |
28 | type Nonce24 = Word192 -- 24 bytes | 32 | type Nonce24 = Word192 -- 24 bytes |
29 | type NodeId = Word256 -- 32 bytes (mainline uses only 20-byte node IDs) | ||
30 | 33 | ||
31 | 34 | ||
32 | data NodeFormat = NodeFormat | 35 | data NodeFormat = NodeFormat |
@@ -91,12 +94,14 @@ instance (Serialize a, Serialize b) => Serialize (LargeKey a b) where | |||
91 | -- | Use with 'PingPayload', 'GetNodesPayload', or 'SendNodesPayload' | 94 | -- | Use with 'PingPayload', 'GetNodesPayload', or 'SendNodesPayload' |
92 | data Message a = Message | 95 | data Message a = Message |
93 | { msgType :: MessageType | 96 | { msgType :: MessageType |
94 | , msgClient :: NodeId | 97 | , msgClient :: NodeId Message |
95 | , msgNonce :: Nonce24 | 98 | , msgNonce :: Nonce24 |
96 | , msgPayload :: a | 99 | , msgPayload :: a |
97 | } | 100 | } |
98 | deriving (Show, Generic, Functor, Foldable, Traversable) | 101 | deriving (Show, Generic, Functor, Foldable, Traversable) |
99 | 102 | ||
103 | deriving instance Show (NodeId Message) -- TODO: print as hex | ||
104 | |||
100 | isQuery :: Message a -> Bool | 105 | isQuery :: Message a -> Bool |
101 | isQuery (Message { msgType = SendNodes }) = False | 106 | isQuery (Message { msgType = SendNodes }) = False |
102 | isQuery (Message { msgType = MessageType typ }) | even typ = True | 107 | isQuery (Message { msgType = MessageType typ }) | even typ = True |
@@ -114,7 +119,7 @@ data PingPayload = PingPayload | |||
114 | } | 119 | } |
115 | 120 | ||
116 | data GetNodesPayload = GetNodesPayload | 121 | data GetNodesPayload = GetNodesPayload |
117 | { nodesForWho :: NodeId | 122 | { nodesForWho :: NodeId Message |
118 | , nodesNonce :: Nonce8 | 123 | , nodesNonce :: Nonce8 |
119 | } | 124 | } |
120 | 125 | ||
@@ -249,7 +254,8 @@ curve25519 = CurveFP (CurvePrime prime curvecommon) | |||
249 | 254 | ||
250 | instance Envelope Message where | 255 | instance Envelope Message where |
251 | type TransactionID Message = Nonce24 | 256 | type TransactionID Message = Nonce24 |
252 | type NodeId Message = NodeId | 257 | newtype NodeId Message = NodeId Word256 |
258 | deriving (Serialize, Eq, Ord, Bits, FiniteBits) | ||
253 | 259 | ||
254 | envelopePayload = msgPayload | 260 | envelopePayload = msgPayload |
255 | 261 | ||