diff options
author | joe <joe@jerkface.net> | 2018-06-01 21:49:53 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2018-06-01 22:34:04 -0400 |
commit | 11dae7ee45996dd76dff47e20dd51d7da49f0a43 (patch) | |
tree | 4a9603f4366a7a3d6491b6c6fd027a799590b4a1 /src/Network/Tox | |
parent | 45e96346d7ce952f65b44b4e1e3c98287cf4b2da (diff) |
tox: Separate transports for handshakes and crypto-packets.
Diffstat (limited to 'src/Network/Tox')
-rw-r--r-- | src/Network/Tox/Crypto/Transport.hs | 24 | ||||
-rw-r--r-- | src/Network/Tox/Transport.hs | 16 |
2 files changed, 31 insertions, 9 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 |
diff --git a/src/Network/Tox/Transport.hs b/src/Network/Tox/Transport.hs index 30df93c8..57f07aad 100644 --- a/src/Network/Tox/Transport.hs +++ b/src/Network/Tox/Transport.hs | |||
@@ -8,6 +8,7 @@ | |||
8 | {-# LANGUAGE TypeOperators #-} | 8 | {-# LANGUAGE TypeOperators #-} |
9 | module Network.Tox.Transport (toxTransport, RouteId) where | 9 | module Network.Tox.Transport (toxTransport, RouteId) where |
10 | 10 | ||
11 | import Data.ByteString (ByteString) | ||
11 | import Network.QueryResponse | 12 | import Network.QueryResponse |
12 | import Crypto.Tox | 13 | import Crypto.Tox |
13 | import Network.Tox.DHT.Transport | 14 | import Network.Tox.DHT.Transport |
@@ -22,21 +23,24 @@ toxTransport :: | |||
22 | -> OnionRouter | 23 | -> OnionRouter |
23 | -> (PublicKey -> IO (Maybe NodeInfo)) | 24 | -> (PublicKey -> IO (Maybe NodeInfo)) |
24 | -> UDPTransport | 25 | -> UDPTransport |
25 | -> IO ( Transport String NodeInfo (DHTMessage Encrypted8) | 26 | -> IO ( Transport String SockAddr (CryptoPacket Encrypted) |
27 | , Transport String NodeInfo (DHTMessage Encrypted8) | ||
26 | , Transport String (OnionDestination RouteId) (OnionMessage Encrypted) | 28 | , Transport String (OnionDestination RouteId) (OnionMessage Encrypted) |
27 | , Transport String AnnouncedRendezvous (PublicKey,OnionData) | 29 | , Transport String AnnouncedRendezvous (PublicKey,OnionData) |
28 | , Transport String SockAddr NetCrypto ) | 30 | , Transport String SockAddr (Handshake Encrypted)) |
29 | toxTransport crypto orouter closeLookup udp = do | 31 | toxTransport crypto orouter closeLookup udp = do |
30 | (dht,udp1) <- partitionTransportM (parseDHTAddr crypto) (fmap Just . encodeDHTAddr) $ forwardOnions crypto udp | 32 | (netcrypto, udp0) <- partitionTransport parseCrypto encodeCrypto udp |
33 | (dht,udp1) <- partitionTransportM (parseDHTAddr crypto) (fmap Just . encodeDHTAddr) $ forwardOnions crypto udp0 | ||
31 | (onion1,udp2) <- partitionTransportM (parseOnionAddr $ lookupSender orouter) | 34 | (onion1,udp2) <- partitionTransportM (parseOnionAddr $ lookupSender orouter) |
32 | (encodeOnionAddr crypto $ lookupRoute orouter) | 35 | (encodeOnionAddr crypto $ lookupRoute orouter) |
33 | udp1 | 36 | udp1 |
34 | (dta,onion) <- partitionTransportM (parseDataToRoute crypto) (encodeDataToRoute crypto) onion1 | 37 | (dta,onion) <- partitionTransportM (parseDataToRoute crypto) (encodeDataToRoute crypto) onion1 |
35 | let netcrypto = layerTransport parseNetCrypto encodeNetCrypto udp2 | 38 | let handshakes = layerTransport parseHandshakes encodeHandshakes udp2 |
36 | return ( forwardDHTRequests crypto closeLookup dht | 39 | return ( netcrypto |
40 | , forwardDHTRequests crypto closeLookup dht | ||
37 | , onion | 41 | , onion |
38 | , dta | 42 | , dta |
39 | , netcrypto | 43 | , handshakes |
40 | ) | 44 | ) |
41 | 45 | ||
42 | 46 | ||