summaryrefslogtreecommitdiff
path: root/ToxMessage.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-08-06 07:04:08 -0400
committerjoe <joe@jerkface.net>2017-08-06 07:04:08 -0400
commitad8622a7ec62eb273bd8c3480ca9686f84fd66d7 (patch)
tree5b03b07939c126e7cd23ab90ef93aee6df789e9d /ToxMessage.hs
parentaa5ea9e2049c741140773d2adf0f0daea236d913 (diff)
Fix typo, fix Aliased Assymetric serialization.
Diffstat (limited to 'ToxMessage.hs')
-rw-r--r--ToxMessage.hs29
1 files changed, 24 insertions, 5 deletions
diff --git a/ToxMessage.hs b/ToxMessage.hs
index 6248facf..1d139c93 100644
--- a/ToxMessage.hs
+++ b/ToxMessage.hs
@@ -1,6 +1,7 @@
1{-# LANGUAGE GeneralizedNewtypeDeriving #-} 1{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2{-# LANGUAGE DeriveFunctor, DeriveTraversable,DeriveDataTypeable #-} 2{-# LANGUAGE DeriveFunctor, DeriveTraversable,DeriveDataTypeable #-}
3{-# LANGUAGE GADTs #-} 3{-# LANGUAGE GADTs #-}
4{-# LANGUAGE FlexibleInstances #-}
4module ToxMessage where 5module ToxMessage where
5 6
6import Data.ByteString (ByteString) 7import Data.ByteString (ByteString)
@@ -52,7 +53,8 @@ instance Serialize Auth where
52-- other and is often refered to as your "real public key" by the Tox 53-- other and is often refered to as your "real public key" by the Tox
53-- documents. For the purposes of the DHT, it is an alias. 54-- documents. For the purposes of the DHT, it is an alias.
54newtype Aliased a = Aliased a 55newtype Aliased a = Aliased a
55 deriving (Eq,Ord,Show,Data,Functor,Foldable,Traversable,Serialize) 56 deriving (Eq,Ord,Show,Data,Functor,Foldable,Traversable)
57
56 58
57newtype Nonce24 = Nonce24 ByteString 59newtype Nonce24 = Nonce24 ByteString
58 deriving (Eq, Ord, ByteArrayAccess,Data) 60 deriving (Eq, Ord, ByteArrayAccess,Data)
@@ -142,10 +144,25 @@ data Assymetric = Assymetric
142 deriving (Eq, Ord,Data) 144 deriving (Eq, Ord,Data)
143 145
144-- get requires isolate. 146-- get requires isolate.
147-- sender key, then nonce
145instance Serialize Assymetric where 148instance Serialize Assymetric where
146 get = Assymetric <$> get <*> get 149 get = Assymetric <$> get <*> get
147 put (Assymetric key dta) = put key >> put dta 150 put (Assymetric key dta) = put key >> put dta
148 151
152-- Aliased packets have the sender key and nonce reversed.
153instance Serialize (Aliased Assymetric) where
154 get = do
155 nonce <- get
156 key <- get
157 dta <- get
158 return $ Aliased (Assymetric key (UnclaimedAssymetric nonce dta))
159
160 put (Aliased (Assymetric key (UnclaimedAssymetric nonce dta))) = do
161 put nonce
162 put key
163 put dta
164
165
149newtype Cookie = Cookie UnclaimedAssymetric 166newtype Cookie = Cookie UnclaimedAssymetric
150 deriving (Eq, Ord,Data) 167 deriving (Eq, Ord,Data)
151 168
@@ -185,6 +202,7 @@ data Packet where
185 202
186 OnionRequest3 :: ByteString -> Symmetric -> Packet -- 0x82 203 OnionRequest3 :: ByteString -> Symmetric -> Packet -- 0x82
187 204
205 -- reverse of typical serialization, nonce and then sender key
188 Announce :: Aliased Assymetric -> Packet --0x83 206 Announce :: Aliased Assymetric -> Packet --0x83
189 AnnounceResponse :: Nonce8 -> UnclaimedAssymetric -> Packet -- 0x84 207 AnnounceResponse :: Nonce8 -> UnclaimedAssymetric -> Packet -- 0x84
190 208
@@ -275,12 +293,13 @@ pktClass (PacketKind 4) = AssymetricClass SendNodes (\(SendNodes a) -> a)
275 293
276pktClass (PacketKind 0x18) = AssymetricClass CookieRequest (\(CookieRequest a) -> a) 294pktClass (PacketKind 0x18) = AssymetricClass CookieRequest (\(CookieRequest a) -> a)
277pktClass (PacketKind 0x80) = AssymetricClass OnionRequest0 (\(OnionRequest0 a) -> a) 295pktClass (PacketKind 0x80) = AssymetricClass OnionRequest0 (\(OnionRequest0 a) -> a)
278pktClass (PacketKind 0x86) = AssymetricClass (DataToRouteResponse . Aliased) (\(DataToRouteResponse (Aliased a)) -> a) 296-- pktClass (PacketKind 0x86) = AssymetricClass (DataToRouteResponse . Aliased) (\(DataToRouteResponse (Aliased a)) -> a)
297
298pktClass (PacketKind 0x83) = AliasedClass Announce (\(Announce a)-> a)
299pktClass (PacketKind 0x84) = NoncedUnclaimedClass AnnounceResponse (\(AnnounceResponse n8 uncl)-> (n8,uncl))
279 300
280pkgClass (PacketKind 0x83) = AliasedClass Announce (\(Announce a)-> a) 301pktClass _ = Unclassified
281pkgClass (PacketKind 0x84) = NoncedUnclaimedClass AnnounceResponse (\(AnnounceResponse n8 uncl)-> (n8,uncl))
282 302
283 -- (indexConstr (dataTypeOf (error "dataTypeOf Packet" :: Packet)) 0) -- Ping
284 303
285instance Serialize Packet where 304instance Serialize Packet where
286 get = getPacket 305 get = getPacket