summaryrefslogtreecommitdiff
path: root/src/Data/Tox/Relay.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Data/Tox/Relay.hs')
-rw-r--r--src/Data/Tox/Relay.hs62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/Data/Tox/Relay.hs b/src/Data/Tox/Relay.hs
new file mode 100644
index 00000000..e35f49f0
--- /dev/null
+++ b/src/Data/Tox/Relay.hs
@@ -0,0 +1,62 @@
1{-# LANGUAGE DeriveDataTypeable #-}
2module Data.Tox.Relay where
3
4import Data.ByteString
5import Data.Data
6import Data.Serialize
7import Data.Word
8
9import Crypto.Tox
10import Network.Tox.Onion.Transport
11import Network.Tox.NodeId (key2id,id2key)
12
13data RelayPacket
14 = RoutingRequest PublicKey
15 | RoutingResponse Word8 PublicKey
16 | ConnectNotification Word8
17 | DisconnectNotification Word8
18 | RelayPing Word64
19 | RelayPong Word64
20 | OOBSend PublicKey ByteString
21 | OOBRecv PublicKey ByteString
22 | OnionPacket (OnionRequest N0)
23 | OnionPacketResponse (OnionResponse N1)
24 -- 0x0A through 0x0F reserved for future use.
25 | RelayData Word8 ByteString -- Word8 is a connection id. Encoded as number 16 to 255.
26 deriving (Eq,Ord,Show,Data)
27
28packetNumber :: RelayPacket -> Word8
29packetNumber (RelayData conid _) = conid -- 0 to 15 not allowed.
30packetNumber rp = fromIntegral $ pred $ constrIndex $ toConstr rp
31
32instance Serialize RelayPacket where
33
34 get = do
35 pktid <- getWord8
36 case pktid of
37 0 -> RoutingRequest <$> (id2key <$> get)
38 1 -> RoutingResponse <$> getWord8 <*> (id2key <$> get)
39 2 -> ConnectNotification <$> getWord8
40 3 -> DisconnectNotification <$> getWord8
41 4 -> RelayPing <$> getWord64be
42 5 -> RelayPong <$> getWord64be
43 6 -> OOBSend <$> (id2key <$> get) <*> (remaining >>= getBytes)
44 7 -> OOBRecv <$> (id2key <$> get) <*> (remaining >>= getBytes)
45 8 -> OnionPacket <$> get
46 9 -> OnionPacketResponse <$> get
47 conid -> RelayData conid <$> (remaining >>= getBytes)
48
49 put rp = do
50 putWord8 $ packetNumber rp
51 case rp of
52 RoutingRequest k -> put (key2id k)
53 RoutingResponse rpid k -> putWord8 rpid >> put (key2id k)
54 ConnectNotification conid -> putWord8 conid
55 DisconnectNotification conid -> putWord8 conid
56 RelayPing pingid -> putWord64be pingid
57 RelayPong pingid -> putWord64be pingid
58 OOBSend k bs -> put (key2id k) >> putByteString bs
59 OOBRecv k bs -> put (key2id k) >> putByteString bs
60 OnionPacket query -> put query
61 OnionPacketResponse answer -> put answer
62 RelayData _ bs -> putByteString bs