diff options
Diffstat (limited to 'src/Network/Tox/Crypto/Transport.hs')
-rw-r--r-- | src/Network/Tox/Crypto/Transport.hs | 24 |
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 #-} |
7 | module Network.Tox.Crypto.Transport | 7 | module 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 | ||
84 | parseCrypto :: (ByteString, SockAddr) -> Either (CryptoPacket Encrypted, SockAddr) (ByteString, SockAddr) | ||
85 | parseCrypto ((B.uncons -> Just (0x1b,pkt)),saddr) = either (\_ -> Right (pkt,saddr)) | ||
86 | (\x -> Left (x ,saddr)) | ||
87 | $ runGet get pkt | ||
88 | parseCrypto not_mine = Right not_mine | ||
89 | |||
90 | encodeCrypto :: (CryptoPacket Encrypted, SockAddr) -> Maybe (ByteString, SockAddr) | ||
91 | encodeCrypto (x,saddr) = Just (B.cons 0x1b (runPut $ put x),saddr) | ||
92 | |||
93 | parseHandshakes :: ByteString -> SockAddr -> Either String (Handshake Encrypted, SockAddr) | ||
94 | parseHandshakes (B.uncons -> Just (0x1a,pkt)) saddr = left ("parseNetCrypto: "++) $ (,saddr) <$> runGet get pkt | ||
95 | parseHandshakes _ _ = Left "parseHandshakes_: ?" | ||
96 | |||
97 | encodeHandshakes :: Handshake Encrypted -> SockAddr -> (ByteString, SockAddr) | ||
98 | encodeHandshakes x saddr = (B.cons 0x1a (runPut $ put x),saddr) | ||
99 | |||
100 | |||
83 | parseNetCrypto :: ByteString -> SockAddr -> Either String (NetCrypto, SockAddr) | 101 | parseNetCrypto :: ByteString -> SockAddr -> Either String (NetCrypto, SockAddr) |
84 | parseNetCrypto (B.uncons -> Just (0x1a,pkt)) saddr = left ("parseNetCrypto: "++) $ (,saddr) . NetHandshake <$> runGet get pkt | 102 | parseNetCrypto (B.uncons -> Just (0x1a,pkt)) saddr = left ("parseNetCrypto: "++) $ (,saddr) . NetHandshake <$> runGet get pkt |
85 | parseNetCrypto (B.uncons -> Just (0x1b,pkt)) saddr = left ("parseNetCrypto: "++) $ (,saddr) . NetCrypto <$> runGet get pkt | 103 | parseNetCrypto (B.uncons -> Just (0x1b,pkt)) saddr = left ("parseNetCrypto: "++) $ (,saddr) . NetCrypto <$> runGet get pkt |