summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ToxMessage.hs33
1 files changed, 30 insertions, 3 deletions
diff --git a/ToxMessage.hs b/ToxMessage.hs
index 6853a4a1..6248facf 100644
--- a/ToxMessage.hs
+++ b/ToxMessage.hs
@@ -18,6 +18,10 @@ import Data.Word
18import Data.Data 18import Data.Data
19import Data.Ord 19import Data.Ord
20import Data.Serialize 20import Data.Serialize
21import Foreign.Ptr
22import Foreign.Marshal.Alloc
23import System.Endian
24import Foreign.Storable
21 25
22newtype Auth = Auth Poly1305.Auth 26newtype Auth = Auth Poly1305.Auth
23 deriving (Eq, ByteArrayAccess) 27 deriving (Eq, ByteArrayAccess)
@@ -67,7 +71,20 @@ instance Serialize Nonce24 where
67 put (Nonce24 bs) = putByteString bs 71 put (Nonce24 bs) = putByteString bs
68 72
69newtype Nonce8 = Nonce8 Word64 73newtype Nonce8 = Nonce8 Word64
70 deriving (Eq, Ord,Data) 74 deriving (Eq, Ord, Data, Serialize)
75
76instance ByteArrayAccess Nonce8 where
77 length _ = 8
78 withByteArray (Nonce8 w64) kont =
79 allocaBytes 8 $ \p -> do
80 poke (castPtr p :: Ptr Word64) $ toBE64 w64
81 kont p
82
83instance Show Nonce8 where
84 showsPrec d nonce = quoted (mappend $ bin2hex nonce)
85
86
87
71 88
72-- TODO: This should probably be represented by Curve25519.PublicKey, but 89-- TODO: This should probably be represented by Curve25519.PublicKey, but
73-- ByteString has more instances... 90-- ByteString has more instances...
@@ -245,6 +262,9 @@ pktKind OnionResponse3 {} = PacketKind 0x8c
245 262
246data PacketClass = 263data PacketClass =
247 AssymetricClass (Assymetric -> Packet) (Packet -> Assymetric) 264 AssymetricClass (Assymetric -> Packet) (Packet -> Assymetric)
265 | AliasedClass (Aliased Assymetric -> Packet) (Packet -> Aliased Assymetric)
266 | NoncedUnclaimedClass (Nonce8 -> UnclaimedAssymetric -> Packet)
267 (Packet -> (Nonce8, UnclaimedAssymetric))
248 | Unclassified 268 | Unclassified
249 269
250pktClass :: PacketKind -> PacketClass 270pktClass :: PacketKind -> PacketClass
@@ -257,6 +277,9 @@ pktClass (PacketKind 0x18) = AssymetricClass CookieRequest (\(CookieRequest a) -
257pktClass (PacketKind 0x80) = AssymetricClass OnionRequest0 (\(OnionRequest0 a) -> a) 277pktClass (PacketKind 0x80) = AssymetricClass OnionRequest0 (\(OnionRequest0 a) -> a)
258pktClass (PacketKind 0x86) = AssymetricClass (DataToRouteResponse . Aliased) (\(DataToRouteResponse (Aliased a)) -> a) 278pktClass (PacketKind 0x86) = AssymetricClass (DataToRouteResponse . Aliased) (\(DataToRouteResponse (Aliased a)) -> a)
259 279
280pkgClass (PacketKind 0x83) = AliasedClass Announce (\(Announce a)-> a)
281pkgClass (PacketKind 0x84) = NoncedUnclaimedClass AnnounceResponse (\(AnnounceResponse n8 uncl)-> (n8,uncl))
282
260 -- (indexConstr (dataTypeOf (error "dataTypeOf Packet" :: Packet)) 0) -- Ping 283 -- (indexConstr (dataTypeOf (error "dataTypeOf Packet" :: Packet)) 0) -- Ping
261 284
262instance Serialize Packet where 285instance Serialize Packet where
@@ -266,12 +289,16 @@ instance Serialize Packet where
266getPacket = do 289getPacket = do
267 typ <- get 290 typ <- get
268 case pktClass typ of 291 case pktClass typ of
269 AssymetricClass toPacket fromPacket -> toPacket <$> get 292 AssymetricClass toPacket _ -> toPacket <$> get
293 AliasedClass toPacket _ -> toPacket <$> get
294 NoncedUnclaimedClass toPacket _ -> toPacket <$> get <*> get
270 295
271putPacket p = do 296putPacket p = do
272 put $ pktKind p 297 put $ pktKind p
273 case pktClass (pktKind p) of 298 case pktClass (pktKind p) of
274 AssymetricClass toPacket fromPacket -> put $ fromPacket p 299 AssymetricClass _ fromPacket -> put $ fromPacket p
300 AliasedClass _ fromPacket -> put $ fromPacket p
301 NoncedUnclaimedClass _ fromPacket -> put $ fromPacket p -- putting a pair.
275 302
276{- 303{-
277data Packet' where 304data Packet' where