summaryrefslogtreecommitdiff
path: root/src/Network/Tox/Onion
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2018-11-27 22:32:32 -0500
committerJoe Crayne <joe@jerkface.net>2018-11-27 22:47:28 -0500
commitc2807c309c32e86ff199d06ea411d6619c92e597 (patch)
tree6e87daa698104bea86aa0b499d9e4d16cdefa49b /src/Network/Tox/Onion
parentb6f84bd5bca03a5493c038c3e9bb26892224a41a (diff)
TCP relay packet serialization.
Diffstat (limited to 'src/Network/Tox/Onion')
-rw-r--r--src/Network/Tox/Onion/Transport.hs48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/Network/Tox/Onion/Transport.hs b/src/Network/Tox/Onion/Transport.hs
index 10bd5a44..ef9121f2 100644
--- a/src/Network/Tox/Onion/Transport.hs
+++ b/src/Network/Tox/Onion/Transport.hs
@@ -1,5 +1,6 @@
1{-# LANGUAGE CPP #-} 1{-# LANGUAGE CPP #-}
2{-# LANGUAGE DataKinds #-} 2{-# LANGUAGE DataKinds #-}
3{-# LANGUAGE DeriveDataTypeable #-}
3{-# LANGUAGE FlexibleContexts #-} 4{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleInstances #-} 5{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE GADTs #-} 6{-# LANGUAGE GADTs #-}
@@ -41,6 +42,8 @@ module Network.Tox.Onion.Transport
41 , decrypt 42 , decrypt
42 , peelSymmetric 43 , peelSymmetric
43 , OnionRoute(..) 44 , OnionRoute(..)
45 , N0
46 , N1
44 , N3 47 , N3
45 , onionKey 48 , onionKey
46 , onionAliasSelector 49 , onionAliasSelector
@@ -62,6 +65,7 @@ import Control.Concurrent.STM
62import Control.Monad 65import Control.Monad
63import qualified Data.ByteString as B 66import qualified Data.ByteString as B
64 ;import Data.ByteString (ByteString) 67 ;import Data.ByteString (ByteString)
68import Data.Data
65import Data.Function 69import Data.Function
66import Data.Functor.Contravariant 70import Data.Functor.Contravariant
67import Data.Functor.Identity 71import Data.Functor.Identity
@@ -101,6 +105,16 @@ data OnionMessage (f :: * -> *)
101 | OnionToRoute PublicKey (Asymm (Encrypted DataToRoute)) -- destination key, aliased Asymm 105 | OnionToRoute PublicKey (Asymm (Encrypted DataToRoute)) -- destination key, aliased Asymm
102 | OnionToRouteResponse (Asymm (Encrypted DataToRoute)) 106 | OnionToRouteResponse (Asymm (Encrypted DataToRoute))
103 107
108deriving instance ( Eq (f (AnnounceRequest, Nonce8))
109 , Eq (f AnnounceResponse)
110 , Eq (f DataToRoute)
111 ) => Eq (OnionMessage f)
112
113deriving instance ( Ord (f (AnnounceRequest, Nonce8))
114 , Ord (f AnnounceResponse)
115 , Ord (f DataToRoute)
116 ) => Ord (OnionMessage f)
117
104deriving instance ( Show (f (AnnounceRequest, Nonce8)) 118deriving instance ( Show (f (AnnounceRequest, Nonce8))
105 , Show (f AnnounceResponse) 119 , Show (f AnnounceResponse)
106 , Show (f DataToRoute) 120 , Show (f DataToRoute)
@@ -304,6 +318,30 @@ data OnionRequest n = OnionRequest
304 , onionForward :: Forwarding (ThreeMinus n) (OnionMessage Encrypted) 318 , onionForward :: Forwarding (ThreeMinus n) (OnionMessage Encrypted)
305 , pathFromOwner :: ReturnPath n 319 , pathFromOwner :: ReturnPath n
306 } 320 }
321 deriving (Eq,Ord)
322
323
324instance (Typeable n, Sized (ReturnPath n), Serialize (ReturnPath n)
325 , Serialize (Forwarding (ThreeMinus n) (OnionMessage Encrypted))
326 ) => Data (OnionRequest n) where
327 gfoldl f z txt = z (either error id . S.decode) `f` S.encode txt
328 toConstr _ = error "OnionRequest.toConstr"
329 gunfold _ _ = error "OnionRequest.gunfold"
330#if MIN_VERSION_base(4,2,0)
331 dataTypeOf _ = mkNoRepType "Network.Tox.Onion.Transport.OnionRequest"
332#else
333 dataTypeOf _ = mkNorepType "Network.Tox.Onion.Transport.OnionRequest"
334#endif
335
336instance (Typeable n, Serialize (ReturnPath n)) => Data (OnionResponse n) where
337 gfoldl f z txt = z (either error id . S.decode) `f` S.encode txt
338 toConstr _ = error "OnionResponse.toConstr"
339 gunfold _ _ = error "OnionResponse.gunfold"
340#if MIN_VERSION_base(4,2,0)
341 dataTypeOf _ = mkNoRepType "Network.Tox.Onion.Transport.OnionResponse"
342#else
343 dataTypeOf _ = mkNorepType "Network.Tox.Onion.Transport.OnionResponse"
344#endif
307 345
308deriving instance ( Show (Forwarding (ThreeMinus n) (OnionMessage Encrypted)) 346deriving instance ( Show (Forwarding (ThreeMinus n) (OnionMessage Encrypted))
309 , KnownNat (PeanoNat n) 347 , KnownNat (PeanoNat n)
@@ -336,6 +374,7 @@ data OnionResponse n = OnionResponse
336 { pathToOwner :: ReturnPath n 374 { pathToOwner :: ReturnPath n
337 , msgToOwner :: OnionMessage Encrypted 375 , msgToOwner :: OnionMessage Encrypted
338 } 376 }
377 deriving (Eq,Ord)
339 378
340deriving instance KnownNat (PeanoNat n) => Show (OnionResponse n) 379deriving instance KnownNat (PeanoNat n) => Show (OnionResponse n)
341 380
@@ -383,6 +422,9 @@ type N1 = S N0
383type N2 = S N1 422type N2 = S N1
384type N3 = S N2 423type N3 = S N2
385 424
425deriving instance Data N0
426deriving instance Data n => Data (S n)
427
386class KnownPeanoNat n where 428class KnownPeanoNat n where
387 peanoVal :: p n -> Int 429 peanoVal :: p n -> Int
388 430
@@ -399,6 +441,9 @@ data ReturnPath n where
399 NoReturnPath :: ReturnPath N0 441 NoReturnPath :: ReturnPath N0
400 ReturnPath :: Nonce24 -> Encrypted (Addressed (ReturnPath n)) -> ReturnPath (S n) 442 ReturnPath :: Nonce24 -> Encrypted (Addressed (ReturnPath n)) -> ReturnPath (S n)
401 443
444deriving instance Eq (ReturnPath n)
445deriving instance Ord (ReturnPath n)
446
402-- Size: 59 = 1(family) + 16(ip) + 2(port) +16(mac) + 24(nonce) 447-- Size: 59 = 1(family) + 16(ip) + 2(port) +16(mac) + 24(nonce)
403instance Sized (ReturnPath N0) where size = ConstSize 0 448instance Sized (ReturnPath N0) where size = ConstSize 0
404instance Sized (ReturnPath n) => Sized (ReturnPath (S n)) where 449instance Sized (ReturnPath n) => Sized (ReturnPath (S n)) where
@@ -444,6 +489,9 @@ data Forwarding n msg where
444 NotForwarded :: msg -> Forwarding N0 msg 489 NotForwarded :: msg -> Forwarding N0 msg
445 Forwarding :: PublicKey -> Encrypted (Addressed (Forwarding n msg)) -> Forwarding (S n) msg 490 Forwarding :: PublicKey -> Encrypted (Addressed (Forwarding n msg)) -> Forwarding (S n) msg
446 491
492deriving instance Eq msg => Eq (Forwarding n msg)
493deriving instance Ord msg => Ord (Forwarding n msg)
494
447instance Show msg => Show (Forwarding N0 msg) where 495instance Show msg => Show (Forwarding N0 msg) where
448 show (NotForwarded x) = "NotForwarded "++show x 496 show (NotForwarded x) = "NotForwarded "++show x
449 497