diff options
-rw-r--r-- | ToxMessage.hs | 33 |
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 | |||
18 | import Data.Data | 18 | import Data.Data |
19 | import Data.Ord | 19 | import Data.Ord |
20 | import Data.Serialize | 20 | import Data.Serialize |
21 | import Foreign.Ptr | ||
22 | import Foreign.Marshal.Alloc | ||
23 | import System.Endian | ||
24 | import Foreign.Storable | ||
21 | 25 | ||
22 | newtype Auth = Auth Poly1305.Auth | 26 | newtype 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 | ||
69 | newtype Nonce8 = Nonce8 Word64 | 73 | newtype Nonce8 = Nonce8 Word64 |
70 | deriving (Eq, Ord,Data) | 74 | deriving (Eq, Ord, Data, Serialize) |
75 | |||
76 | instance 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 | |||
83 | instance 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 | ||
246 | data PacketClass = | 263 | data 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 | ||
250 | pktClass :: PacketKind -> PacketClass | 270 | pktClass :: PacketKind -> PacketClass |
@@ -257,6 +277,9 @@ pktClass (PacketKind 0x18) = AssymetricClass CookieRequest (\(CookieRequest a) - | |||
257 | pktClass (PacketKind 0x80) = AssymetricClass OnionRequest0 (\(OnionRequest0 a) -> a) | 277 | pktClass (PacketKind 0x80) = AssymetricClass OnionRequest0 (\(OnionRequest0 a) -> a) |
258 | pktClass (PacketKind 0x86) = AssymetricClass (DataToRouteResponse . Aliased) (\(DataToRouteResponse (Aliased a)) -> a) | 278 | pktClass (PacketKind 0x86) = AssymetricClass (DataToRouteResponse . Aliased) (\(DataToRouteResponse (Aliased a)) -> a) |
259 | 279 | ||
280 | pkgClass (PacketKind 0x83) = AliasedClass Announce (\(Announce a)-> a) | ||
281 | pkgClass (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 | ||
262 | instance Serialize Packet where | 285 | instance Serialize Packet where |
@@ -266,12 +289,16 @@ instance Serialize Packet where | |||
266 | getPacket = do | 289 | getPacket = 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 | ||
271 | putPacket p = do | 296 | putPacket 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 | {- |
277 | data Packet' where | 304 | data Packet' where |