summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Network/Tox.hs27
-rw-r--r--src/Network/Tox/Crypto/Transport.hs24
-rw-r--r--src/Network/Tox/Transport.hs16
3 files changed, 53 insertions, 14 deletions
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs
index ce9939a9..6df239b5 100644
--- a/src/Network/Tox.hs
+++ b/src/Network/Tox.hs
@@ -240,6 +240,7 @@ data Tox = Tox
240 , toxOnion :: Onion.Client RouteId 240 , toxOnion :: Onion.Client RouteId
241 , toxToRoute :: Transport String Onion.AnnouncedRendezvous (PublicKey,Onion.OnionData) 241 , toxToRoute :: Transport String Onion.AnnouncedRendezvous (PublicKey,Onion.OnionData)
242 , toxCrypto :: Transport String SockAddr NetCrypto 242 , toxCrypto :: Transport String SockAddr NetCrypto
243 , toxHandshakes :: Transport String SockAddr NetCrypto
243 , toxCryptoSessions :: NetCryptoSessions 244 , toxCryptoSessions :: NetCryptoSessions
244 , toxCryptoKeys :: TransportCrypto 245 , toxCryptoKeys :: TransportCrypto
245 , toxRouting :: DHT.Routing 246 , toxRouting :: DHT.Routing
@@ -439,8 +440,22 @@ newTox keydb addr mbSessionsState suppliedDHTKey = do
439 mkrouting <- DHT.newRouting addr crypto updateIP updateIP 440 mkrouting <- DHT.newRouting addr crypto updateIP updateIP
440 let ignoreErrors _ = return () -- Set this to (hPutStrLn stderr) to debug onion route building. 441 let ignoreErrors _ = return () -- Set this to (hPutStrLn stderr) to debug onion route building.
441 orouter <- newOnionRouter ignoreErrors 442 orouter <- newOnionRouter ignoreErrors
442 (dhtcrypt,onioncrypt,dtacrypt,cryptonet) <- toxTransport crypto orouter lookupClose udp 443 (cryptonet,dhtcrypt,onioncrypt,dtacrypt,handshakes) <- toxTransport crypto orouter lookupClose udp
443 let sessionsState = sessionsState0 { sessionTransport = cryptonet 444
445 -- TODO: Avoid adapting these transports here.
446 let nc_cryptonet = layerTransport (\x addr -> Right (NetCrypto x,addr))
447 (\(NetCrypto x) addr -> (x,addr))
448 cryptonet
449 nc_handshakes = layerTransport (\x addr -> Right (NetHandshake x,addr))
450 (\(NetHandshake x) addr -> (x,addr))
451 handshakes
452 nc_combo = nc_handshakes
453 { sendMessage = \addr -> \case
454 NetCrypto x -> sendMessage cryptonet addr x
455 NetHandshake x -> sendMessage handshakes addr x
456 }
457
458 let sessionsState = sessionsState0 { sessionTransport = nc_combo
444 , transportCrypto = crypto } 459 , transportCrypto = crypto }
445 let dhtnet0 = layerTransportM (DHT.decrypt crypto) (DHT.encrypt crypto) dhtcrypt 460 let dhtnet0 = layerTransportM (DHT.decrypt crypto) (DHT.encrypt crypto) dhtcrypt
446 tbl4 = DHT.routing4 $ mkrouting (error "missing client") 461 tbl4 = DHT.routing4 $ mkrouting (error "missing client")
@@ -465,7 +480,8 @@ newTox keydb addr mbSessionsState suppliedDHTKey = do
465 { toxDHT = dhtclient 480 { toxDHT = dhtclient
466 , toxOnion = onionclient 481 , toxOnion = onionclient
467 , toxToRoute = onInbound (updateContactInfo roster) dtacrypt 482 , toxToRoute = onInbound (updateContactInfo roster) dtacrypt
468 , toxCrypto = addHandler (hPutStrLn stderr) (cryptoNetHandler sessionsState) cryptonet 483 , toxCrypto = addHandler (hPutStrLn stderr) (cryptoNetHandler sessionsState) nc_cryptonet
484 , toxHandshakes = addHandler (hPutStrLn stderr) (cryptoNetHandler sessionsState) nc_handshakes
469 , toxCryptoSessions = sessionsState 485 , toxCryptoSessions = sessionsState
470 , toxCryptoKeys = crypto 486 , toxCryptoKeys = crypto
471 , toxRouting = mkrouting dhtclient 487 , toxRouting = mkrouting dhtclient
@@ -481,10 +497,11 @@ onionTimeout Tox { toxOnionRoutes = or } (DHT.TransactionId n8 _) od = lookupTim
481 497
482forkTox :: Tox -> IO (IO (), [NodeInfo] -> [NodeInfo] -> IO (), [NodeInfo] -> [NodeInfo] -> IO ()) 498forkTox :: Tox -> IO (IO (), [NodeInfo] -> [NodeInfo] -> IO (), [NodeInfo] -> [NodeInfo] -> IO ())
483forkTox tox = do 499forkTox tox = do
484 _ <- forkListener "toxCrypto" (toxCrypto tox) 500 _ <- forkListener "toxHandshakes" (toxHandshakes tox)
485 _ <- forkListener "toxToRoute" (toxToRoute tox) 501 _ <- forkListener "toxToRoute" (toxToRoute tox)
486 _ <- forkListener "toxOnion" (clientNet $ toxOnion tox) 502 _ <- forkListener "toxOnion" (clientNet $ toxOnion tox)
487 quit <- forkListener "toxDHT" (clientNet $ toxDHT tox) 503 _ <- forkListener "toxDHT" (clientNet $ toxDHT tox)
504 quit <- forkListener "toxCrypto" (toxCrypto tox)
488 forkPollForRefresh (DHT.refresher4 $ toxRouting tox) 505 forkPollForRefresh (DHT.refresher4 $ toxRouting tox)
489 forkPollForRefresh (DHT.refresher6 $ toxRouting tox) 506 forkPollForRefresh (DHT.refresher6 $ toxRouting tox)
490 return ( quit 507 return ( quit
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
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 #-}
9module Network.Tox.Transport (toxTransport, RouteId) where 9module Network.Tox.Transport (toxTransport, RouteId) where
10 10
11import Data.ByteString (ByteString)
11import Network.QueryResponse 12import Network.QueryResponse
12import Crypto.Tox 13import Crypto.Tox
13import Network.Tox.DHT.Transport 14import 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))
29toxTransport crypto orouter closeLookup udp = do 31toxTransport 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