summaryrefslogtreecommitdiff
path: root/src/Network/Tox.hs
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2018-12-15 02:34:00 -0500
committerJoe Crayne <joe@jerkface.net>2018-12-16 14:08:27 -0500
commit0403b3426c268409969eb517dce86e9c2ce12988 (patch)
tree2d12967dd1c68d8fc7943d94685f67cb84493ec9 /src/Network/Tox.hs
parenta599a465072409a428ea5973083844090d270968 (diff)
WIP: Support for sending onion queries to TCP relays.
Diffstat (limited to 'src/Network/Tox.hs')
-rw-r--r--src/Network/Tox.hs56
1 files changed, 47 insertions, 9 deletions
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs
index ef74b9c6..6e2a42c5 100644
--- a/src/Network/Tox.hs
+++ b/src/Network/Tox.hs
@@ -1,4 +1,5 @@
1{-# LANGUAGE CPP #-} 1{-# LANGUAGE CPP #-}
2{-# LANGUAGE FlexibleContexts #-}
2{-# LANGUAGE DeriveDataTypeable #-} 3{-# LANGUAGE DeriveDataTypeable #-}
3{-# LANGUAGE DeriveFoldable #-} 4{-# LANGUAGE DeriveFoldable #-}
4{-# LANGUAGE DeriveFunctor #-} 5{-# LANGUAGE DeriveFunctor #-}
@@ -32,6 +33,7 @@ import qualified Data.ByteString as B
32 ;import Data.ByteString (ByteString) 33 ;import Data.ByteString (ByteString)
33import qualified Data.ByteString.Char8 as C8 34import qualified Data.ByteString.Char8 as C8
34import Data.Data 35import Data.Data
36import Data.Functor.Identity
35import Data.Functor.Contravariant 37import Data.Functor.Contravariant
36import Data.Maybe 38import Data.Maybe
37import qualified Data.MinMaxPSQ as MinMaxPSQ 39import qualified Data.MinMaxPSQ as MinMaxPSQ
@@ -42,6 +44,7 @@ import Network.Socket
42import System.Endian 44import System.Endian
43import System.IO.Error 45import System.IO.Error
44 46
47import qualified Data.Word64Map
45import Network.BitTorrent.DHT.Token as Token 48import Network.BitTorrent.DHT.Token as Token
46import qualified Data.Wrapper.PSQ as PSQ 49import qualified Data.Wrapper.PSQ as PSQ
47import System.Global6 50import System.Global6
@@ -68,6 +71,7 @@ import DebugTag
68import TCPProber 71import TCPProber
69import Network.Tox.Avahi 72import Network.Tox.Avahi
70import Network.Tox.Session 73import Network.Tox.Session
74import qualified Data.Tox.Relay as TCP
71import Network.Tox.Relay 75import Network.Tox.Relay
72import Network.SessionTransports 76import Network.SessionTransports
73import Network.Kademlia.Search 77import Network.Kademlia.Search
@@ -238,6 +242,37 @@ getOnionAlias crypto dhtself remoteNode = atomically $ do
238 _ -> ni { nodeId = key2id (onionAliasPublic crypto) } 242 _ -> ni { nodeId = key2id (onionAliasPublic crypto) }
239 return $ Onion.OnionDestination Onion.SearchingAlias alias Nothing 243 return $ Onion.OnionDestination Onion.SearchingAlias alias Nothing
240 244
245newOnionClient :: DRG g =>
246 TransportCrypto
247 -> Transport String (Onion.OnionDestination RouteId) Onion.Message
248 -> DHT.Routing
249 -> TVar SessionTokens
250 -> TVar Onion.AnnouncedKeys
251 -> OnionRouter
252 -> TVar (g, Data.Word64Map.Word64Map a)
253 -> (MVar Onion.Message -> a)
254 -> (a -> Onion.Message -> IO void)
255 -> Client String
256 DHT.PacketKind
257 DHT.TransactionId
258 (Onion.OnionDestination RouteId)
259 Onion.Message
260newOnionClient crypto net r toks keydb orouter map_var store load = Client
261 { clientNet = net
262 , clientDispatcher = DispatchMethods
263 { classifyInbound = Onion.classify
264 , lookupHandler = Onion.handlers net r toks keydb
265 , tableMethods = hookQueries orouter DHT.transactionKey
266 $ transactionMethods' store load (contramap w64Key w64MapMethods) gen
267 }
268 , clientErrorReporter = logErrors { reportTimeout = reportTimeout ignoreErrors }
269 , clientPending = map_var
270 , clientAddress = getOnionAlias crypto $ R.thisNode <$> readTVar (DHT.routing4 r)
271 , clientResponseId = genNonce24 map_var
272 , clientEnterQuery = \_ -> return ()
273 , clientLeaveQuery = \_ _ -> return ()
274 }
275
241newTox :: TVar Onion.AnnouncedKeys -- ^ Store of announced keys we are a rendezvous for. 276newTox :: TVar Onion.AnnouncedKeys -- ^ Store of announced keys we are a rendezvous for.
242 -> [String] -- ^ Bind-address to listen on. Must provide at least one. 277 -> [String] -- ^ Bind-address to listen on. Must provide at least one.
243 -> ( ContactInfo extra -> SockAddr -> Session -> IO () ) 278 -> ( ContactInfo extra -> SockAddr -> Session -> IO () )
@@ -287,8 +322,11 @@ newToxOverTransport keydb addr onNewSession suppliedDHTKey udp tcp = do
287 let lookupClose _ = return Nothing 322 let lookupClose _ = return Nothing
288 323
289 mkrouting <- DHT.newRouting addr crypto updateIP updateIP 324 mkrouting <- DHT.newRouting addr crypto updateIP updateIP
290 orouter <- newOnionRouter crypto $ dput XRoutes 325 (orouter,otbl) <- newOnionRouter crypto (dput XRoutes)
291 (cryptonet,dhtcrypt,onioncrypt,dtacrypt,handshakes) <- toxTransport crypto orouter lookupClose udp tcp 326 (cryptonet,dhtcrypt,onioncrypt,dtacrypt,handshakes)
327 <- toxTransport crypto orouter lookupClose udp
328 (sendMessage (clientNet $ tcpClient $ tcpKademliaClient orouter))
329 tcp
292 sessions <- initSessions (sendMessage cryptonet) 330 sessions <- initSessions (sendMessage cryptonet)
293 331
294 let dhtnet0 = layerTransportM (DHT.decrypt crypto) (DHT.encrypt crypto) dhtcrypt 332 let dhtnet0 = layerTransportM (DHT.decrypt crypto) (DHT.encrypt crypto) dhtcrypt
@@ -296,7 +334,7 @@ newToxOverTransport keydb addr onNewSession suppliedDHTKey udp tcp = do
296 tbl6 = DHT.routing6 $ mkrouting (error "missing client") 334 tbl6 = DHT.routing6 $ mkrouting (error "missing client")
297 updateOnion bkts tr = hookBucketList DHT.toxSpace bkts orouter (trampolinesUDP orouter) tr 335 updateOnion bkts tr = hookBucketList DHT.toxSpace bkts orouter (trampolinesUDP orouter) tr
298 dhtclient <- newClient drg dhtnet0 DHT.classify (myAddr tbl4 tbl6) (DHT.handlers crypto . mkrouting) id 336 dhtclient <- newClient drg dhtnet0 DHT.classify (myAddr tbl4 tbl6) (DHT.handlers crypto . mkrouting) id
299 $ \client net -> onInbound (DHT.updateRouting client (mkrouting client) updateOnion) net 337 (\client net -> onInbound (DHT.updateRouting client (mkrouting client) updateOnion) net)
300 338
301 hscache <- newHandshakeCache crypto (sendMessage handshakes) 339 hscache <- newHandshakeCache crypto (sendMessage handshakes)
302 let sparams = SessionParams 340 let sparams = SessionParams
@@ -315,13 +353,13 @@ newToxOverTransport keydb addr onNewSession suppliedDHTKey udp tcp = do
315 toks <- do 353 toks <- do
316 nil <- nullSessionTokens 354 nil <- nullSessionTokens
317 atomically $ newTVar nil { maxInterval = 20 } -- 20 second timeout on announce ping-ids. 355 atomically $ newTVar nil { maxInterval = 20 } -- 20 second timeout on announce ping-ids.
318 oniondrg <- drgNew
319 let onionnet = layerTransportM (Onion.decrypt crypto) (Onion.encrypt crypto) onioncrypt 356 let onionnet = layerTransportM (Onion.decrypt crypto) (Onion.encrypt crypto) onioncrypt
320 onionclient <- newClient oniondrg onionnet (const Onion.classify) 357 let onionclient = newOnionClient crypto onionnet (mkrouting dhtclient) toks keydb orouter' otbl
321 (getOnionAlias crypto $ R.thisNode <$> readTVar (DHT.routing4 $ mkrouting dhtclient)) 358 Right $ \case
322 (const $ Onion.handlers onionnet (mkrouting dhtclient) toks keydb) 359 Right v -> tryPutMVar v
323 (hookQueries orouter' DHT.transactionKey) 360 Left v -> \_ -> do
324 (const id) 361 dput XUnexpected "TCP-sent onion query got response over UDP?"
362 return False
325 363
326 return Tox 364 return Tox
327 { toxDHT = dhtclient 365 { toxDHT = dhtclient