diff options
-rw-r--r-- | DHTHandlers.hs | 57 | ||||
-rw-r--r-- | Tox.hs | 2 | ||||
-rw-r--r-- | ToxCrypto.hs | 2 | ||||
-rw-r--r-- | ToxMessage.hs | 450 |
4 files changed, 54 insertions, 457 deletions
diff --git a/DHTHandlers.hs b/DHTHandlers.hs index 2857abf3..e2b4ec05 100644 --- a/DHTHandlers.hs +++ b/DHTHandlers.hs | |||
@@ -1,12 +1,12 @@ | |||
1 | {-# LANGUAGE PatternSynonyms #-} | 1 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} |
2 | {-# LANGUAGE TupleSections #-} | 2 | {-# LANGUAGE PatternSynonyms #-} |
3 | {-# LANGUAGE TupleSections #-} | ||
3 | module DHTHandlers where | 4 | module DHTHandlers where |
4 | 5 | ||
5 | import DHTTransport | 6 | import DHTTransport |
6 | import Network.QueryResponse as QR hiding (Client) | 7 | import Network.QueryResponse as QR hiding (Client) |
7 | import qualified Network.QueryResponse as QR (Client) | 8 | import qualified Network.QueryResponse as QR (Client) |
8 | import ToxCrypto | 9 | import ToxCrypto |
9 | import ToxMessage as Tox (PacketKind(..), pattern PingType, pattern PongType, pattern GetNodesType, pattern SendNodesType, pattern DHTRequestType) | ||
10 | import Network.BitTorrent.DHT.Search | 10 | import Network.BitTorrent.DHT.Search |
11 | import qualified Data.Wrapper.PSQInt as Int | 11 | import qualified Data.Wrapper.PSQInt as Int |
12 | import Kademlia | 12 | import Kademlia |
@@ -28,6 +28,8 @@ import Data.IP | |||
28 | import Data.Ord | 28 | import Data.Ord |
29 | import Data.Maybe | 29 | import Data.Maybe |
30 | import Data.Bits | 30 | import Data.Bits |
31 | import Data.Serialize (Serialize) | ||
32 | import Data.Word | ||
31 | import System.IO | 33 | import System.IO |
32 | 34 | ||
33 | data TransactionId = TransactionId | 35 | data TransactionId = TransactionId |
@@ -36,6 +38,53 @@ data TransactionId = TransactionId | |||
36 | } | 38 | } |
37 | deriving (Eq,Ord,Show) | 39 | deriving (Eq,Ord,Show) |
38 | 40 | ||
41 | newtype PacketKind = PacketKind Word8 | ||
42 | deriving (Eq, Ord, Serialize) | ||
43 | |||
44 | pattern OnionRequest0Type = PacketKind 128 -- 0x80 Onion Request 0 | ||
45 | pattern OnionRequest1Type = PacketKind 129 -- 0x81 Onion Request 1 | ||
46 | pattern OnionRequest2Type = PacketKind 130 -- 0x82 Onion Request 2 | ||
47 | pattern AnnounceType = PacketKind 131 -- 0x83 Announce Request | ||
48 | pattern AnnounceResponseType = PacketKind 132 -- 0x84 Announce Response | ||
49 | |||
50 | pattern DataRequestType = PacketKind 133 -- 0x85 Onion Data Request (data to route request packet) | ||
51 | pattern DataResponseType = PacketKind 134 -- 0x86 Onion Data Response (data to route response packet) | ||
52 | -- 0x8c Onion Response 3 | ||
53 | -- 0x8d Onion Response 2 | ||
54 | pattern OnionResponse3Type = PacketKind 140 -- 0x8c Onion Response 3 | ||
55 | pattern OnionResponse2Type = PacketKind 141 -- 0x8d Onion Response 2 | ||
56 | pattern OnionResponse1Type = PacketKind 142 -- 0x8e Onion Response 1 | ||
57 | -- 0xf0 Bootstrap Info | ||
58 | |||
59 | pattern DHTRequestType = PacketKind 32 -- 0x20 DHT Request | ||
60 | |||
61 | pattern CookieRequestType = PacketKind 0x18 | ||
62 | pattern CookieResponseType = PacketKind 0x19 | ||
63 | |||
64 | pattern PingType = PacketKind 0 -- 0x00 Ping Request | ||
65 | pattern PongType = PacketKind 1 -- 0x01 Ping Response | ||
66 | pattern GetNodesType = PacketKind 2 -- 0x02 Nodes Request | ||
67 | pattern SendNodesType = PacketKind 4 -- 0x04 Nodes Response | ||
68 | |||
69 | |||
70 | instance Show PacketKind where | ||
71 | showsPrec d PingType = mappend "PingType" | ||
72 | showsPrec d PongType = mappend "PongType" | ||
73 | showsPrec d GetNodesType = mappend "GetNodesType" | ||
74 | showsPrec d SendNodesType = mappend "SendNodesType" | ||
75 | showsPrec d DHTRequestType = mappend "DHTRequestType" | ||
76 | showsPrec d OnionRequest0Type = mappend "OnionRequest0Type" | ||
77 | showsPrec d OnionResponse1Type = mappend "OnionResponse1Type" | ||
78 | showsPrec d OnionResponse3Type = mappend "OnionResponse3Type" | ||
79 | showsPrec d AnnounceType = mappend "AnnounceType" | ||
80 | showsPrec d AnnounceResponseType = mappend "AnnounceResponseType" | ||
81 | showsPrec d DataRequestType = mappend "DataRequestType" | ||
82 | showsPrec d DataResponseType = mappend "DataResponseType" | ||
83 | showsPrec d CookieRequestType = mappend "CookieRequestType" | ||
84 | showsPrec d CookieResponseType = mappend "CookieResponseType" | ||
85 | showsPrec d (PacketKind x) = mappend "PacketKind " . showsPrec (d+1) x | ||
86 | |||
87 | |||
39 | classify :: Message -> MessageClass String PacketKind TransactionId | 88 | classify :: Message -> MessageClass String PacketKind TransactionId |
40 | classify msg = mapMessage (\nonce24 (nonce8,_) -> go msg (TransactionId nonce8 nonce24)) msg | 89 | classify msg = mapMessage (\nonce24 (nonce8,_) -> go msg (TransactionId nonce8 nonce24)) msg |
41 | where | 90 | where |
@@ -245,7 +294,7 @@ isGetNodes _ _ = Left "Bad GetNodes" | |||
245 | mkSendNodes :: TransactionId -> NodeInfo -> NodeInfo -> SendNodes -> DHTMessage ((,) Nonce8) | 294 | mkSendNodes :: TransactionId -> NodeInfo -> NodeInfo -> SendNodes -> DHTMessage ((,) Nonce8) |
246 | mkSendNodes tid src dst sendnodes = DHTSendNodes $ wrapAssym tid src dst (, sendnodes) | 295 | mkSendNodes tid src dst sendnodes = DHTSendNodes $ wrapAssym tid src dst (, sendnodes) |
247 | 296 | ||
248 | handlers :: Routing -> Tox.PacketKind -> Maybe Handler | 297 | handlers :: Routing -> PacketKind -> Maybe Handler |
249 | handlers routing PingType = Just $ MethodHandler (isPing snd) mkPong pingH | 298 | handlers routing PingType = Just $ MethodHandler (isPing snd) mkPong pingH |
250 | handlers routing GetNodesType = Just $ MethodHandler (isGetNodes snd) mkSendNodes $ getNodesH routing | 299 | handlers routing GetNodesType = Just $ MethodHandler (isGetNodes snd) mkSendNodes $ getNodesH routing |
251 | 300 | ||
@@ -79,8 +79,6 @@ import System.IO | |||
79 | import qualified Text.ParserCombinators.ReadP as RP | 79 | import qualified Text.ParserCombinators.ReadP as RP |
80 | import Text.Printf | 80 | import Text.Printf |
81 | import Text.Read | 81 | import Text.Read |
82 | import ToxMessage as Tox hiding (Ping,Pong,SendNodes,GetNodes,AnnounceResponse,Nonce24,Nonce8) | ||
83 | ;import ToxMessage (bin2hex, quoted) | ||
84 | import TriadCommittee | 82 | import TriadCommittee |
85 | import Network.BitTorrent.DHT.Token as Token | 83 | import Network.BitTorrent.DHT.Token as Token |
86 | import GHC.TypeLits | 84 | import GHC.TypeLits |
diff --git a/ToxCrypto.hs b/ToxCrypto.hs index 7797da70..9f39f1e1 100644 --- a/ToxCrypto.hs +++ b/ToxCrypto.hs | |||
@@ -87,7 +87,7 @@ instance Data Auth where | |||
87 | -- Well, this is a little wonky... XXX | 87 | -- Well, this is a little wonky... XXX |
88 | gunfold k z c = k (z (Auth . Poly1305.Auth . (BA.convert :: ByteString -> Bytes))) | 88 | gunfold k z c = k (z (Auth . Poly1305.Auth . (BA.convert :: ByteString -> Bytes))) |
89 | toConstr _ = con_Auth | 89 | toConstr _ = con_Auth |
90 | dataTypeOf _ = mkDataType "ToxMessage" [con_Auth] | 90 | dataTypeOf _ = mkDataType "ToxCrypto" [con_Auth] |
91 | con_Auth :: Constr | 91 | con_Auth :: Constr |
92 | con_Auth = mkConstr (dataTypeOf (Auth (error "con_Auth"))) "Auth" [] Prefix | 92 | con_Auth = mkConstr (dataTypeOf (Auth (error "con_Auth"))) "Auth" [] Prefix |
93 | instance Serialize Auth where | 93 | instance Serialize Auth where |
diff --git a/ToxMessage.hs b/ToxMessage.hs deleted file mode 100644 index 41204697..00000000 --- a/ToxMessage.hs +++ /dev/null | |||
@@ -1,450 +0,0 @@ | |||
1 | {-# LANGUAGE PatternSynonyms #-} | ||
2 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
3 | {-# LANGUAGE DeriveFunctor, DeriveTraversable,DeriveDataTypeable #-} | ||
4 | {-# LANGUAGE GADTs #-} | ||
5 | {-# LANGUAGE FlexibleInstances #-} | ||
6 | {-# LANGUAGE ScopedTypeVariables #-} | ||
7 | {-# LANGUAGE TypeApplications #-} | ||
8 | {-# LANGUAGE ExistentialQuantification #-} | ||
9 | {-# LANGUAGE DataKinds, KindSignatures #-} | ||
10 | module ToxMessage where | ||
11 | |||
12 | import Debug.Trace | ||
13 | import Data.ByteString (ByteString) | ||
14 | import qualified Crypto.MAC.Poly1305 as Poly1305 (Auth(..)) | ||
15 | import qualified Crypto.PubKey.Curve25519 as Curve25519 | ||
16 | import Data.ByteArray as BA (ByteArrayAccess, Bytes) | ||
17 | import qualified Data.ByteArray as BA | ||
18 | import qualified Data.ByteString as B | ||
19 | import qualified Data.ByteString.Char8 as C8 | ||
20 | import qualified Data.ByteString.Base16 as Base16 | ||
21 | import Data.Bits | ||
22 | import Data.Hashable | ||
23 | import Data.Bits.ByteString () | ||
24 | import Data.Word | ||
25 | import Data.Data | ||
26 | import Data.Ord | ||
27 | import Data.Serialize | ||
28 | import Foreign.Ptr | ||
29 | import Foreign.Marshal.Alloc | ||
30 | import System.Endian | ||
31 | import Foreign.Storable | ||
32 | import GHC.TypeLits | ||
33 | import Data.Tuple | ||
34 | |||
35 | newtype Auth = Auth Poly1305.Auth | ||
36 | deriving (Eq, ByteArrayAccess) | ||
37 | |||
38 | instance Ord Auth where | ||
39 | compare (Auth a) (Auth b) = comparing (BA.convert :: Poly1305.Auth -> Bytes) a b | ||
40 | |||
41 | instance Data Auth where | ||
42 | gfoldl k z x = z x | ||
43 | |||
44 | -- Well, this is a little wonky... XXX | ||
45 | gunfold k z c = k (z (Auth . Poly1305.Auth . (BA.convert :: ByteString -> Bytes))) | ||
46 | |||
47 | toConstr _ = con_Auth | ||
48 | |||
49 | dataTypeOf _ = mkDataType "ToxMessage" [con_Auth] | ||
50 | |||
51 | con_Auth :: Constr | ||
52 | con_Auth = mkConstr (dataTypeOf (Auth (error "con_Auth"))) "Auth" [] Prefix | ||
53 | |||
54 | instance Serialize Auth where | ||
55 | get = Auth . Poly1305.Auth . BA.convert <$> getBytes 16 | ||
56 | put (Auth (Poly1305.Auth bs)) = putByteString $ BA.convert bs | ||
57 | -- | ||
58 | -- | An 'Aliased' 'PubKey' is one that is not the DHT key and so should not go | ||
59 | -- into the kademlia routing table buckets. | ||
60 | -- | ||
61 | -- Note: This includes the long-term tox-id key that humans use to friend each | ||
62 | -- other and is often refered to as your "real public key" by the Tox | ||
63 | -- documents. For the purposes of the DHT, it is an alias. | ||
64 | newtype Aliased a = Aliased a | ||
65 | deriving (Eq,Ord,Show,Data,Functor,Foldable,Traversable) | ||
66 | |||
67 | newtype Nonce24 = Nonce24 ByteString | ||
68 | deriving (Eq, Ord, ByteArrayAccess,Data) | ||
69 | |||
70 | quoted :: ShowS -> ShowS | ||
71 | quoted shows s = '"':shows ('"':s) | ||
72 | |||
73 | bin2hex :: ByteArrayAccess bs => bs -> String | ||
74 | bin2hex = C8.unpack . Base16.encode . BA.convert | ||
75 | |||
76 | instance Show Nonce24 where | ||
77 | showsPrec d nonce = quoted (mappend $ bin2hex nonce) | ||
78 | |||
79 | instance Serialize Nonce24 where | ||
80 | get = Nonce24 <$> getBytes 24 | ||
81 | put (Nonce24 bs) = putByteString bs | ||
82 | |||
83 | newtype Nonce8 = Nonce8 Word64 | ||
84 | deriving (Eq, Ord, Data, Serialize) | ||
85 | |||
86 | instance ByteArrayAccess Nonce8 where | ||
87 | length _ = 8 | ||
88 | withByteArray (Nonce8 w64) kont = | ||
89 | allocaBytes 8 $ \p -> do | ||
90 | poke (castPtr p :: Ptr Word64) $ toBE64 w64 | ||
91 | kont p | ||
92 | |||
93 | instance Show Nonce8 where | ||
94 | showsPrec d nonce = quoted (mappend $ bin2hex nonce) | ||
95 | |||
96 | |||
97 | |||
98 | |||
99 | -- TODO: This should probably be represented by Curve25519.PublicKey, but | ||
100 | -- ByteString has more instances... | ||
101 | newtype PubKey = PubKey ByteString | ||
102 | deriving (Eq,Ord,Data, ByteArrayAccess, Bits, Hashable) | ||
103 | |||
104 | instance Serialize PubKey where | ||
105 | get = PubKey <$> getBytes 32 | ||
106 | put (PubKey bs) = putByteString bs | ||
107 | |||
108 | instance Show PubKey where | ||
109 | show (PubKey bs) = C8.unpack $ Base16.encode bs | ||
110 | |||
111 | instance FiniteBits PubKey where | ||
112 | finiteBitSize _ = 256 | ||
113 | |||
114 | instance Read PubKey where | ||
115 | readsPrec _ str | ||
116 | | (bs, xs) <- Base16.decode $ C8.pack str | ||
117 | , B.length bs == 32 | ||
118 | = [ (PubKey bs, drop 64 str) ] | ||
119 | | otherwise = [] | ||
120 | |||
121 | |||
122 | |||
123 | -- | A chunk of data encrypted with public-key cryptography. | ||
124 | data ImplicitAssymetric = ImplicitAssymetric | ||
125 | { assymetricAuth :: Auth | ||
126 | , assymetricBytes :: ByteString | ||
127 | } | ||
128 | deriving (Eq, Ord,Data) | ||
129 | |||
130 | getRemaining :: Get ByteString | ||
131 | getRemaining = remaining >>= getBytes | ||
132 | |||
133 | instance Serialize ImplicitAssymetric where | ||
134 | get = ImplicitAssymetric <$> get <*> getRemaining | ||
135 | put (ImplicitAssymetric auth bs) = put auth >> putByteString bs | ||
136 | |||
137 | -- | Like ImplicitAssymetric, but includes the nonce used to encrypt. | ||
138 | data UnclaimedAssymetric = UnclaimedAssymetric | ||
139 | { assymetricNonce :: Nonce24 | ||
140 | , assymetricData :: !ImplicitAssymetric | ||
141 | } | ||
142 | deriving (Eq, Ord, Data) | ||
143 | |||
144 | instance Serialize UnclaimedAssymetric where | ||
145 | get = UnclaimedAssymetric <$> get <*> get | ||
146 | put (UnclaimedAssymetric nonce dta) = put nonce >> put dta | ||
147 | |||
148 | -- | Like UnclaimedAssymetric, but includes the public key of the sender. | ||
149 | data Assymetric = Assymetric | ||
150 | { senderKey :: PubKey | ||
151 | , sent :: !UnclaimedAssymetric | ||
152 | } | ||
153 | deriving (Eq, Ord,Data) | ||
154 | |||
155 | -- get requires isolate. | ||
156 | -- sender key, then nonce | ||
157 | instance Serialize Assymetric where | ||
158 | get = Assymetric <$> get <*> get | ||
159 | put (Assymetric key dta) = put key >> put dta | ||
160 | |||
161 | -- Aliased packets have the sender key and nonce reversed. | ||
162 | instance Serialize (Aliased Assymetric) where | ||
163 | get = do | ||
164 | nonce <- get | ||
165 | key <- get | ||
166 | dta <- get | ||
167 | return $ Aliased (Assymetric key (UnclaimedAssymetric nonce dta)) | ||
168 | |||
169 | put (Aliased (Assymetric key (UnclaimedAssymetric nonce dta))) = do | ||
170 | put nonce | ||
171 | put key | ||
172 | put dta | ||
173 | |||
174 | |||
175 | newtype Cookie = Cookie UnclaimedAssymetric | ||
176 | deriving (Eq, Ord,Data) | ||
177 | |||
178 | newtype ReturnPath (n::Nat) = ReturnPath ByteString | ||
179 | deriving (Eq, Ord,Data) | ||
180 | |||
181 | emptyReturnPath :: ReturnPath 0 | ||
182 | emptyReturnPath = ReturnPath B.empty | ||
183 | |||
184 | instance KnownNat n => Serialize (ReturnPath n) where | ||
185 | -- Size: 59 = 1(family) + 16(ip) + 2(port) +16(mac) + 24(nonce) | ||
186 | get = ReturnPath <$> getBytes ( 59 * (fromIntegral $ natVal $ Proxy @n) ) | ||
187 | put (ReturnPath bs) = putByteString bs | ||
188 | |||
189 | data Symmetric = Symmetric | ||
190 | { symmetricNonce :: Nonce24 | ||
191 | , symmetricAuth :: Auth | ||
192 | , symmetricBytes :: ByteString | ||
193 | } | ||
194 | deriving (Eq, Ord,Data) | ||
195 | |||
196 | -- Test run histogram: | ||
197 | -- 377 PongType | ||
198 | -- 387 DataRequestType | ||
199 | -- 3238 PingType | ||
200 | -- 9231 DHTRequestType | ||
201 | -- 10299 PacketKind 130 | ||
202 | -- 12626 PacketKind 129 | ||
203 | -- 16596 OnionRequest0 | ||
204 | -- 16742 SendNodesType | ||
205 | -- 41877 Announce | ||
206 | -- 81793 GetNodesType | ||
207 | data Packet where | ||
208 | Ping :: Assymetric -> Packet -- 0x00 -- Assymetric query | ||
209 | Pong :: Assymetric -> Packet -- 0x01 -- Assymetric response | ||
210 | |||
211 | GetNodes :: Assymetric -> Packet -- 0x02 -- Assymetric query | ||
212 | SendNodes :: Assymetric -> Packet -- 0x04 -- Assymetric response | ||
213 | |||
214 | CookieRequest :: Assymetric -> Packet -- 0x18 | ||
215 | CookieResponse :: UnclaimedAssymetric -> Packet -- 0x19 | ||
216 | |||
217 | OnionRequest0 :: Assymetric -> Packet -- 0x80 | ||
218 | |||
219 | |||
220 | CryptoHandshake :: Cookie -> UnclaimedAssymetric -> Packet -- 0x1a | ||
221 | |||
222 | CryptoData :: Word16 -> ImplicitAssymetric -> Packet -- 0x1b | ||
223 | |||
224 | DHTRequest :: PubKey -> Assymetric -> Packet -- 0x20 -- Sometimes Assymetric query | ||
225 | |||
226 | DataToRoute :: PubKey -> Aliased Assymetric -> ReturnPath 3 -> Packet | ||
227 | DataToRouteResponse :: Aliased Assymetric -> Packet | ||
228 | |||
229 | LanDiscovery :: PubKey -> Packet -- 0x21 | ||
230 | |||
231 | OnionRequest1 :: Aliased Assymetric -> Symmetric -> Packet -- 0x81 | ||
232 | OnionRequest2 :: Aliased Assymetric -> Symmetric -> Packet -- 0x82 | ||
233 | |||
234 | OnionRequest3 :: ByteString -> Symmetric -> Packet -- 0x82 | ||
235 | |||
236 | Announce :: Aliased Assymetric -> ReturnPath 3 -> Packet --0x83 | ||
237 | AnnounceResponse :: Nonce8 -> UnclaimedAssymetric -> Packet -- 0x84 | ||
238 | |||
239 | OnionResponse3 :: ReturnPath 3 -> Packet -> Packet -- 0x8c | ||
240 | OnionResponse2 :: Symmetric -> ByteString -> Packet -- 0x8d | ||
241 | OnionResponse1 :: Symmetric -> ByteString -> Packet -- 0x8e | ||
242 | |||
243 | |||
244 | GetBootstrapInfo :: ByteString -> Packet -- 0xf0 + 77 bytes -- ByteString query | ||
245 | BootstrapInfo :: Word32 -> ByteString -> Packet -- 0xf0 + version + (256 byte motd) -- ByteSTring response | ||
246 | |||
247 | deriving (Eq, Ord,Data) | ||
248 | |||
249 | class KnownNat n => OnionPacket n where | ||
250 | mkOnion :: ReturnPath n -> Packet -> Packet | ||
251 | |||
252 | instance OnionPacket 0 where mkOnion _ = id | ||
253 | instance OnionPacket 3 where mkOnion = OnionResponse3 | ||
254 | |||
255 | newtype PacketKind = PacketKind Word8 | ||
256 | deriving (Eq, Ord, Serialize) | ||
257 | |||
258 | -- TODO: Auth fail: | ||
259 | pattern OnionRequest0Type = PacketKind 128 -- 0x80 Onion Request 0 | ||
260 | pattern OnionRequest1Type = PacketKind 129 -- 0x81 Onion Request 1 | ||
261 | pattern OnionRequest2Type = PacketKind 130 -- 0x82 Onion Request 2 | ||
262 | pattern AnnounceType = PacketKind 131 -- 0x83 Announce Request | ||
263 | pattern AnnounceResponseType = PacketKind 132 -- 0x84 Announce Response | ||
264 | |||
265 | pattern DataRequestType = PacketKind 133 -- 0x85 Onion Data Request (data to route request packet) | ||
266 | pattern DataResponseType = PacketKind 134 -- 0x86 Onion Data Response (data to route response packet) | ||
267 | -- 0x8c Onion Response 3 | ||
268 | -- 0x8d Onion Response 2 | ||
269 | pattern OnionResponse3Type = PacketKind 140 -- 0x8c Onion Response 3 | ||
270 | pattern OnionResponse2Type = PacketKind 141 -- 0x8d Onion Response 2 | ||
271 | pattern OnionResponse1Type = PacketKind 142 -- 0x8e Onion Response 1 | ||
272 | -- 0xf0 Bootstrap Info | ||
273 | |||
274 | -- TODO Fix these fails... | ||
275 | -- GetNodesType decipherAndAuth: auth fail | ||
276 | -- MessageType 128 decipherAndAuth: auth fail | ||
277 | -- MessageType 129 decipherAndAuth: auth fail | ||
278 | -- MessageType 130 decipherAndAuth: auth fail | ||
279 | -- MessageType 131 decipherAndAuth: auth fail | ||
280 | -- MessageType 32 decipherAndAuth: auth fail | ||
281 | |||
282 | -- TODO: Auth fail: | ||
283 | pattern DHTRequestType = PacketKind 32 -- 0x20 DHT Request | ||
284 | |||
285 | pattern PingType = PacketKind 0 -- 0x00 Ping Request | ||
286 | pattern PongType = PacketKind 1 -- 0x01 Ping Response | ||
287 | pattern GetNodesType = PacketKind 2 -- 0x02 Nodes Request | ||
288 | pattern SendNodesType = PacketKind 4 -- 0x04 Nodes Response | ||
289 | |||
290 | |||
291 | instance Show PacketKind where | ||
292 | showsPrec d PingType = mappend "PingType" | ||
293 | showsPrec d PongType = mappend "PongType" | ||
294 | showsPrec d GetNodesType = mappend "GetNodesType" | ||
295 | showsPrec d SendNodesType = mappend "SendNodesType" | ||
296 | showsPrec d DHTRequestType = mappend "DHTRequestType" | ||
297 | showsPrec d OnionRequest0Type = mappend "OnionRequest0" | ||
298 | showsPrec d OnionResponse1Type = mappend "OnionResponse1" | ||
299 | showsPrec d OnionResponse3Type = mappend "OnionResponse3" | ||
300 | showsPrec d AnnounceType = mappend "Announce" | ||
301 | showsPrec d AnnounceResponseType = mappend "AnnounceResponse" | ||
302 | showsPrec d DataRequestType = mappend "DataRequestType" | ||
303 | showsPrec d DataResponseType = mappend "DataResponseType" | ||
304 | showsPrec d (PacketKind x) = mappend "PacketKind " . showsPrec (d+1) x | ||
305 | |||
306 | pktKind :: Packet -> PacketKind | ||
307 | |||
308 | -- These are (Assymetric -> Assymetric) queries. | ||
309 | pktKind Ping {} = PacketKind 0x00 | ||
310 | pktKind Pong {} = PacketKind 0x01 | ||
311 | pktKind GetNodes {} = PacketKind 0x02 | ||
312 | pktKind SendNodes {} = PacketKind 0x04 | ||
313 | |||
314 | |||
315 | -- This is a (Assymetric -> UnclaimedAssymetric) query | ||
316 | pktKind CookieRequest {} = PacketKind 0x18 | ||
317 | pktKind CookieResponse {} = PacketKind 0x19 | ||
318 | |||
319 | -- Query (Assymetric -> (Nonce8,UnclaimedAssymetric)) | ||
320 | pktKind Announce {} = PacketKind 0x83 | ||
321 | pktKind AnnounceResponse {} = PacketKind 0x84 | ||
322 | |||
323 | -- Query (Assymetric -> ByteString) | ||
324 | pktKind OnionRequest0 {} = PacketKind 0x80 | ||
325 | |||
326 | |||
327 | -- This is a (ByteString -> ByteString) query | ||
328 | pktKind GetBootstrapInfo {} = PacketKind 0xf0 | ||
329 | pktKind BootstrapInfo {} = PacketKind 0xf0 | ||
330 | |||
331 | |||
332 | -- Trigering event. No direct response. (PubKey -> ()) | ||
333 | pktKind LanDiscovery {} = PacketKind 0x21 | ||
334 | |||
335 | -- Two cases: | ||
336 | -- Half-established: (Cookie,UnclaimedAssymetric) -> (Cookie,UnclaimedAssymetric) | ||
337 | -- Session established: (Cookie,UnclaimedAssymetric) -> (Word16,ImplicitAssymetric) | ||
338 | pktKind CryptoHandshake {} = PacketKind 0x1a | ||
339 | |||
340 | -- Session data, no direct response. | ||
341 | -- (reponse to CryptoHandshake, or other data) | ||
342 | pktKind CryptoData {} = PacketKind 0x1b | ||
343 | |||
344 | -- Two cases: | ||
345 | -- ( (PubKey, Assymetric) -> response ) | ||
346 | -- ( (PubKey, Assymetric) -> () ) | ||
347 | pktKind DHTRequest {} = PacketKind 0x20 | ||
348 | |||
349 | |||
350 | -- Query ( (PubKey,Aliased Assymetric) -> Aliased Assymetric) | ||
351 | pktKind DataToRoute {} = PacketKind 0x85 | ||
352 | pktKind DataToRouteResponse {} = PacketKind 0x86 | ||
353 | |||
354 | -- 3 Queries ( (Aliased Assymetric, Symmetric ) | ||
355 | -- -> ( Symmetric, ByteString ) ) | ||
356 | pktKind OnionRequest1 {} = PacketKind 0x81 | ||
357 | pktKind OnionResponse1 {} = PacketKind 0x8e | ||
358 | |||
359 | pktKind OnionRequest2 {} = PacketKind 0x82 | ||
360 | pktKind OnionResponse2 {} = PacketKind 0x8d | ||
361 | |||
362 | pktKind OnionRequest3 {} = PacketKind 0x82 | ||
363 | pktKind OnionResponse3 {} = PacketKind 0x8c | ||
364 | |||
365 | data PacketClass = | ||
366 | AssymetricClass (Assymetric -> Packet) (Packet -> Assymetric) | ||
367 | | forall n. OnionPacket n => AliasedClass ((Aliased Assymetric,ReturnPath n) -> Packet) (Packet -> (Aliased Assymetric,ReturnPath n)) | ||
368 | | forall n. OnionPacket n => ToRouteClass ((PubKey,(Aliased Assymetric,ReturnPath n)) -> Packet) (Packet -> (PubKey,(Aliased Assymetric,ReturnPath n))) | ||
369 | | forall n. OnionPacket n => OnionClass ((Packet,ReturnPath n) -> Packet) (Packet -> (Packet,ReturnPath n)) | ||
370 | | NoncedUnclaimedClass (Nonce8 -> UnclaimedAssymetric -> Packet) | ||
371 | (Packet -> (Nonce8, UnclaimedAssymetric)) | ||
372 | | Unclassified | ||
373 | {- | ||
374 | data Packet' where | ||
375 | :: Assymetric -> Packet | ||
376 | :: UnclaimedAssymetric -> Packet | ||
377 | :: Word16 -> ImplicitAssymetric -> Packet | ||
378 | :: PubKey -> Assymetric -> Packet | ||
379 | :: PubKey -> Packet | ||
380 | :: Aliased Assymetric -> Symmetric -> Packet | ||
381 | :: ByteString -> Symmetric -> Packet | ||
382 | :: Aliased Assymetric -> Packet | ||
383 | :: Symmetric -> ByteString -> Packet | ||
384 | :: ByteString -> Packet | ||
385 | :: Word32 -> ByteString -> Packet | ||
386 | -} | ||
387 | |||
388 | pktClass :: PacketKind -> PacketClass | ||
389 | pktClass (PacketKind 0) = AssymetricClass Ping (\(Ping a) -> a) | ||
390 | pktClass (PacketKind 1) = AssymetricClass Pong (\(Pong a) -> a) | ||
391 | pktClass (PacketKind 2) = AssymetricClass GetNodes (\(GetNodes a) -> a) | ||
392 | pktClass (PacketKind 4) = AssymetricClass SendNodes (\(SendNodes a) -> a) | ||
393 | |||
394 | pktClass (PacketKind 0x18) = AssymetricClass CookieRequest (\(CookieRequest a) -> a) | ||
395 | pktClass (PacketKind 0x80) = AssymetricClass OnionRequest0 (\(OnionRequest0 a) -> a) | ||
396 | pktClass (PacketKind 0x86) = AssymetricClass (DataToRouteResponse . Aliased) (\(DataToRouteResponse (Aliased a)) -> a) | ||
397 | |||
398 | pktClass (PacketKind 0x83) = AliasedClass (uncurry Announce) (\(Announce a r)-> (a,r)) | ||
399 | pktClass (PacketKind 0x84) = NoncedUnclaimedClass AnnounceResponse (\(AnnounceResponse n8 uncl)-> (n8,uncl)) | ||
400 | |||
401 | pktClass (PacketKind 0x8c) = OnionClass (uncurry OnionResponse3 . swap) (\(OnionResponse3 r a)-> (a,r)) | ||
402 | |||
403 | pktClass DataRequestType = ToRouteClass (\(k,(a,r))-> DataToRoute k a r) | ||
404 | (\(DataToRoute k a r) -> (k,(a,r))) | ||
405 | |||
406 | pktClass _ = Unclassified | ||
407 | |||
408 | |||
409 | instance Serialize Packet where | ||
410 | get = getPacket | ||
411 | put = putPacket | ||
412 | |||
413 | getPacket :: Get Packet | ||
414 | getPacket = do | ||
415 | typ <- get | ||
416 | case pktClass typ of | ||
417 | AssymetricClass toPacket _ -> toPacket <$> get | ||
418 | AliasedClass toPacket _ -> do | ||
419 | trace ("PARSE "++show typ) $ return () | ||
420 | cnt <- remaining | ||
421 | a <- isolate (cnt - 59*3) get | ||
422 | r <- get | ||
423 | trace ("PARSED "++show typ) $ return () | ||
424 | return $ toPacket (a,r) | ||
425 | ToRouteClass toPacket _ -> do | ||
426 | trace ("R-PARSE "++show typ) $ return () | ||
427 | cnt <- remaining | ||
428 | (pub,a) <- isolate (cnt - 59*3) get | ||
429 | r <- get | ||
430 | trace ("R-PARSED "++show typ) $ return () | ||
431 | return $ toPacket (pub,(a,r)) | ||
432 | OnionClass toPacket _ -> do | ||
433 | trace ("ONION-PARSE "++show typ) $ return () | ||
434 | p <- get | ||
435 | trace ("ONION-PARSED "++show typ) $ return () | ||
436 | return $ toPacket p | ||
437 | NoncedUnclaimedClass toPacket _ -> toPacket <$> get <*> get | ||
438 | Unclassified -> fail $ "todo: unserialize packet "++show typ | ||
439 | |||
440 | putPacket :: Packet -> PutM () | ||
441 | putPacket p = do | ||
442 | put $ pktKind p | ||
443 | case pktClass (pktKind p) of | ||
444 | AssymetricClass _ fromPacket -> put $ fromPacket p | ||
445 | AliasedClass _ fromPacket -> put $ fromPacket p | ||
446 | ToRouteClass _ fromPacket -> put $ fromPacket p | ||
447 | OnionClass _ fromPacket -> put $ swap $ fromPacket p | ||
448 | NoncedUnclaimedClass _ fromPacket -> put $ fromPacket p -- putting a pair. | ||
449 | Unclassified -> fail $ "todo: serialize packet "++show (pktKind p) | ||
450 | |||