summaryrefslogtreecommitdiff
path: root/src/Network/Tox/Crypto/Transport.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/Tox/Crypto/Transport.hs')
-rw-r--r--src/Network/Tox/Crypto/Transport.hs24
1 files changed, 21 insertions, 3 deletions
diff --git a/src/Network/Tox/Crypto/Transport.hs b/src/Network/Tox/Crypto/Transport.hs
index 1444ffca..2c998006 100644
--- a/src/Network/Tox/Crypto/Transport.hs
+++ b/src/Network/Tox/Crypto/Transport.hs
@@ -5,9 +5,10 @@
5{-# LANGUAGE LambdaCase #-} 5{-# LANGUAGE LambdaCase #-}
6{-# LANGUAGE TupleSections #-} 6{-# LANGUAGE TupleSections #-}
7module Network.Tox.Crypto.Transport 7module Network.Tox.Crypto.Transport
8 ( parseNetCrypto 8 ( parseCrypto
9 , encodeNetCrypto 9 , encodeCrypto
10 -- CryptoTransport 10 , parseHandshakes
11 , encodeHandshakes
11 , NetCrypto(..) 12 , NetCrypto(..)
12 , CryptoData(..) 13 , CryptoData(..)
13 , CryptoMessage(..) 14 , CryptoMessage(..)
@@ -80,6 +81,23 @@ data NetCrypto
80 = NetHandshake (Handshake Encrypted) 81 = NetHandshake (Handshake Encrypted)
81 | NetCrypto (CryptoPacket Encrypted) 82 | NetCrypto (CryptoPacket Encrypted)
82 83
84parseCrypto :: (ByteString, SockAddr) -> Either (CryptoPacket Encrypted, SockAddr) (ByteString, SockAddr)
85parseCrypto ((B.uncons -> Just (0x1b,pkt)),saddr) = either (\_ -> Right (pkt,saddr))
86 (\x -> Left (x ,saddr))
87 $ runGet get pkt
88parseCrypto not_mine = Right not_mine
89
90encodeCrypto :: (CryptoPacket Encrypted, SockAddr) -> Maybe (ByteString, SockAddr)
91encodeCrypto (x,saddr) = Just (B.cons 0x1b (runPut $ put x),saddr)
92
93parseHandshakes :: ByteString -> SockAddr -> Either String (Handshake Encrypted, SockAddr)
94parseHandshakes (B.uncons -> Just (0x1a,pkt)) saddr = left ("parseNetCrypto: "++) $ (,saddr) <$> runGet get pkt
95parseHandshakes _ _ = Left "parseHandshakes_: ?"
96
97encodeHandshakes :: Handshake Encrypted -> SockAddr -> (ByteString, SockAddr)
98encodeHandshakes x saddr = (B.cons 0x1a (runPut $ put x),saddr)
99
100
83parseNetCrypto :: ByteString -> SockAddr -> Either String (NetCrypto, SockAddr) 101parseNetCrypto :: ByteString -> SockAddr -> Either String (NetCrypto, SockAddr)
84parseNetCrypto (B.uncons -> Just (0x1a,pkt)) saddr = left ("parseNetCrypto: "++) $ (,saddr) . NetHandshake <$> runGet get pkt 102parseNetCrypto (B.uncons -> Just (0x1a,pkt)) saddr = left ("parseNetCrypto: "++) $ (,saddr) . NetHandshake <$> runGet get pkt
85parseNetCrypto (B.uncons -> Just (0x1b,pkt)) saddr = left ("parseNetCrypto: "++) $ (,saddr) . NetCrypto <$> runGet get pkt 103parseNetCrypto (B.uncons -> Just (0x1b,pkt)) saddr = left ("parseNetCrypto: "++) $ (,saddr) . NetCrypto <$> runGet get pkt