summaryrefslogtreecommitdiff
path: root/dht/src
diff options
context:
space:
mode:
Diffstat (limited to 'dht/src')
-rw-r--r--dht/src/Network/Tox.hs38
1 files changed, 24 insertions, 14 deletions
diff --git a/dht/src/Network/Tox.hs b/dht/src/Network/Tox.hs
index 34e63ad8..746d8667 100644
--- a/dht/src/Network/Tox.hs
+++ b/dht/src/Network/Tox.hs
@@ -23,6 +23,7 @@ import Control.Concurrent.Lifted.Instrument
23#else 23#else
24import Control.Concurrent.Lifted 24import Control.Concurrent.Lifted
25#endif 25#endif
26import Control.Arrow
26import Control.Concurrent.STM 27import Control.Concurrent.STM
27import Control.Exception (throwIO) 28import Control.Exception (throwIO)
28import Control.Monad 29import Control.Monad
@@ -281,9 +282,10 @@ newTox :: TVar Onion.AnnouncedKeys -- ^ Store of announced keys we are a rende
281 -> [String] -- ^ Bind-address to listen on. Must provide at least one. 282 -> [String] -- ^ Bind-address to listen on. Must provide at least one.
282 -> ( ContactInfo extra -> SockAddr -> Session -> IO () ) 283 -> ( ContactInfo extra -> SockAddr -> Session -> IO () )
283 -> (TransportCrypto, ContactInfo extra) 284 -> (TransportCrypto, ContactInfo extra)
284 -> ( Int -> Onion.OnionMessage Encrypted -> IO () ) -- ^ TCP-bound onion responses. 285 -> Bool -- Enable TCP messages.
286 -- ( Int -> Onion.OnionMessage Encrypted -> IO () ) -- ^ TCP-bound onion responses. XXX ignored
285 -> IO (Tox extra) 287 -> IO (Tox extra)
286newTox keydb bindspecs onsess crypto tcp = do 288newTox keydb bindspecs onsess crypto usetcp = do
287 addrs <- mapM (`getBindAddress` True) bindspecs 289 addrs <- mapM (`getBindAddress` True) bindspecs
288 let tryBind addr next _ = udpTransport' addr `catchIOError` (next . Just) 290 let tryBind addr next _ = udpTransport' addr `catchIOError` (next . Just)
289 failedBind mbe = do 291 failedBind mbe = do
@@ -294,14 +296,17 @@ newTox keydb bindspecs onsess crypto tcp = do
294 (udp,sock) <- foldr tryBind failedBind addrs Nothing 296 (udp,sock) <- foldr tryBind failedBind addrs Nothing
295 addr <- getSocketName sock 297 addr <- getSocketName sock
296 dput XOnion $ "UDP bind address: " ++ show addr 298 dput XOnion $ "UDP bind address: " ++ show addr
297 (relay,sendTCP) <- tcpRelay (fst crypto) addr $ \a x -> do 299 (relay,sendTCP) <-
298 let bs = S.runPut $ Onion.putRequest x 300 if usetcp then do
299 dput XOnion $ "Sending onion(0x" ++ (C8.unpack . Base16.encode) (B.take 1 bs) ++ ") from tcp-client to " ++ show a 301 fmap (Just *** Just) $ tcpRelay (fst crypto) addr $ \a x -> do
300 -- mapM_ (dput XOnion) (xxd2 0 bs) 302 let bs = S.runPut $ Onion.putRequest x
301 sendMessage udp (substituteLoopback addr a) bs 303 dput XOnion $ "Sending onion(0x" ++ (C8.unpack . Base16.encode) (B.take 1 bs) ++ ") from tcp-client to " ++ show a
304 -- mapM_ (dput XOnion) (xxd2 0 bs)
305 sendMessage udp (substituteLoopback addr a) bs
306 else return (Nothing, Nothing)
302 tox <- newToxOverTransport keydb addr onsess crypto udp sendTCP 307 tox <- newToxOverTransport keydb addr onsess crypto udp sendTCP
303 return tox { toxAnnounceToLan = announceToLan sock (key2id $ transportPublic $ toxCryptoKeys tox) 308 return tox { toxAnnounceToLan = announceToLan sock (key2id $ transportPublic $ toxCryptoKeys tox)
304 , toxRelayServer = Just relay 309 , toxRelayServer = relay
305 } 310 }
306 311
307newToxCrypto :: Maybe SecretKey -> IO (TransportCrypto, ContactInfo extra) 312newToxCrypto :: Maybe SecretKey -> IO (TransportCrypto, ContactInfo extra)
@@ -328,18 +333,18 @@ newToxOverTransport :: TVar Onion.AnnouncedKeys
328 -> ( ContactInfo extra -> SockAddr -> Session -> IO () ) 333 -> ( ContactInfo extra -> SockAddr -> Session -> IO () )
329 -> (TransportCrypto, ContactInfo extra) 334 -> (TransportCrypto, ContactInfo extra)
330 -> Onion.UDPTransport 335 -> Onion.UDPTransport
331 -> ( Int -> Onion.OnionMessage Encrypted -> IO () ) -- ^ TCP-bound onion responses. 336 -> Maybe ( Int -> Onion.OnionMessage Encrypted -> IO () ) -- ^ TCP-bound onion responses.
332 -> IO (Tox extra) 337 -> IO (Tox extra)
333newToxOverTransport keydb addr onNewSession (crypto,roster) udp tcp = do 338newToxOverTransport keydb addr onNewSession (crypto,roster) udp tcp = do
334 drg <- drgNew 339 drg <- drgNew
335 let lookupClose _ = return Nothing 340 let lookupClose _ = return Nothing
336 341
337 mkrouting <- DHT.newRouting addr crypto updateIP updateIP 342 mkrouting <- DHT.newRouting addr crypto updateIP updateIP
338 (orouter,otbl) <- newOnionRouter crypto (dput XRoutes) 343 (orouter,otbl) <- newOnionRouter crypto (dput XRoutes) (maybe False (const True) tcp)
339 (cryptonet,dhtcrypt,onioncrypt,dtacrypt,handshakes) 344 (cryptonet,dhtcrypt,onioncrypt,dtacrypt,handshakes)
340 <- toxTransport crypto orouter lookupClose addr udp 345 <- toxTransport crypto orouter lookupClose addr udp
341 (\dst x -> sendMessage (clientNet $ tcpClient $ tcpKademliaClient orouter) dst (True,x)) 346 (\dst x -> sendMessage (clientNet $ tcpClient $ tcpKademliaClient orouter) dst (True,x))
342 tcp 347 (fromMaybe (\_ _ -> return ()) tcp)
343 sessions <- initSessions (sendMessage cryptonet) 348 sessions <- initSessions (sendMessage cryptonet)
344 349
345 let dhtnet0 = layerTransportM (DHT.decrypt crypto) (DHT.encrypt crypto) dhtcrypt 350 let dhtnet0 = layerTransportM (DHT.decrypt crypto) (DHT.encrypt crypto) dhtcrypt
@@ -420,14 +425,19 @@ dnssdDiscover tox ni toxid = do
420-- * action to bootstrap an IPv4 Kademlia table. 425-- * action to bootstrap an IPv4 Kademlia table.
421-- 426--
422-- * action to bootstrap an IPv6 Kademlia table. 427-- * action to bootstrap an IPv6 Kademlia table.
423forkTox :: Tox extra -> Bool -> IO (IO (), [NodeInfo] -> [NodeInfo] -> IO (), [NodeInfo] -> [NodeInfo] -> IO ()) 428forkTox :: Tox extra
424forkTox tox with_avahi = do 429 -> Bool -- avahi
430 -> Bool -- tcp
431 -> IO (IO (), [NodeInfo] -> [NodeInfo] -> IO (), [NodeInfo] -> [NodeInfo] -> IO ())
432forkTox tox with_avahi with_tcp = do
425 quitHs <- forkListener "toxHandshakes" (toxHandshakes tox) 433 quitHs <- forkListener "toxHandshakes" (toxHandshakes tox)
426 quitToRoute <- forkListener "toxToRoute" (toxToRoute tox) 434 quitToRoute <- forkListener "toxToRoute" (toxToRoute tox)
427 quitOnion <- forkListener "toxOnion" (clientNet $ toxOnion tox) 435 quitOnion <- forkListener "toxOnion" (clientNet $ toxOnion tox)
428 quitDHT <- forkListener "toxDHT" (clientNet $ toxDHT tox) 436 quitDHT <- forkListener "toxDHT" (clientNet $ toxDHT tox)
429 quitNC <- forkListener "toxCrypto" (toxCrypto tox) 437 quitNC <- forkListener "toxCrypto" (toxCrypto tox)
430 quitTCP <- forkListener "relay-client" (clientNet $ tcpClient $ tcpKademliaClient $ toxOnionRoutes tox) 438 quitTCP <- if with_tcp
439 then forkListener "relay-client" (clientNet $ tcpClient $ tcpKademliaClient $ toxOnionRoutes tox)
440 else return $ return ()
431 refresher4 <- forkPollForRefresh (DHT.refresher4 $ toxRouting tox) 441 refresher4 <- forkPollForRefresh (DHT.refresher4 $ toxRouting tox)
432 refresher6 <- forkPollForRefresh (DHT.refresher6 $ toxRouting tox) 442 refresher6 <- forkPollForRefresh (DHT.refresher6 $ toxRouting tox)
433 quitAvahi <- if with_avahi then do 443 quitAvahi <- if with_avahi then do