summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/dhtd.hs17
-rw-r--r--src/Network/Tox.hs20
2 files changed, 26 insertions, 11 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs
index 6756b14b..04b8c064 100644
--- a/examples/dhtd.hs
+++ b/examples/dhtd.hs
@@ -1055,7 +1055,7 @@ readExternals nodeAddr vars = do
1055 1055
1056data Options = Options 1056data Options = Options
1057 { portbt :: String 1057 { portbt :: String
1058 , porttox :: String 1058 , porttox :: [String]
1059 , portxmpp :: String -- client-to-server 1059 , portxmpp :: String -- client-to-server
1060 , portxmppS :: String -- server-to-server 1060 , portxmppS :: String -- server-to-server
1061 , ip6bt :: Bool 1061 , ip6bt :: Bool
@@ -1076,7 +1076,7 @@ data Options = Options
1076sensibleDefaults :: Options 1076sensibleDefaults :: Options
1077sensibleDefaults = Options 1077sensibleDefaults = Options
1078 { portbt = "6881" 1078 { portbt = "6881"
1079 , porttox = "33445" 1079 , porttox = ["33445"]
1080 , portxmpp = "5222" 1080 , portxmpp = "5222"
1081 , portxmppS = "5269" 1081 , portxmppS = "5269"
1082 , ip6bt = True 1082 , ip6bt = True
@@ -1107,10 +1107,13 @@ parseArgs ("-v":tags:args) opts = parseArgs args opts
1107 } 1107 }
1108parseArgs (arg:args) opts = parseArgs args opts 1108parseArgs (arg:args) opts = parseArgs args opts
1109 { portbt = fromMaybe (portbt opts) $ Prelude.lookup "bt" ports 1109 { portbt = fromMaybe (portbt opts) $ Prelude.lookup "bt" ports
1110 , porttox = fromMaybe (porttox opts) $ Prelude.lookup "tox" ports 1110 , porttox = fromMaybe (porttox opts) $ lookupAll "tox" ports
1111 , portxmpp = fromMaybe (portxmpp opts) $ Prelude.lookup "xmpp" ports 1111 , portxmpp = fromMaybe (portxmpp opts) $ Prelude.lookup "xmpp" ports
1112 , portxmppS = fromMaybe (portxmppS opts) $ Prelude.lookup "xmpp.s2s" ports } 1112 , portxmppS = fromMaybe (portxmppS opts) $ Prelude.lookup "xmpp.s2s" ports }
1113 where 1113 where
1114 lookupAll seeking kvs = case filter (\(k,v) -> k == seeking) kvs of
1115 [] -> Nothing
1116 xs -> Just $ map snd xs
1114 ports = map ( (dropWhile (==',') *** dropWhile (=='=')) 1117 ports = map ( (dropWhile (==',') *** dropWhile (=='='))
1115 . break (=='=') ) 1118 . break (=='=') )
1116 $ groupBy (const (/= ',')) arg 1119 $ groupBy (const (/= ',')) arg
@@ -1298,17 +1301,17 @@ initTox :: Options
1298 , IO [SockAddr] 1301 , IO [SockAddr]
1299 , [SockAddr]) 1302 , [SockAddr])
1300initTox opts ssvar keysdb mbxmpp invc = case porttox opts of 1303initTox opts ssvar keysdb mbxmpp invc = case porttox opts of
1301 "" -> return (Nothing,return (), Map.empty, return [],[]) 1304 [""] -> return (Nothing,return (), Map.empty, return [],[])
1302 toxport -> do 1305 toxport -> do
1303 addrTox <- getBindAddress toxport (ip6tox opts)
1304 dput XMisc $ "Supplied key: " ++ show (fmap (Tox.key2id . toPublic) (dhtkey opts)) 1306 dput XMisc $ "Supplied key: " ++ show (fmap (Tox.key2id . toPublic) (dhtkey opts))
1305 tox <- Tox.newTox keysdb 1307 tox <- Tox.newTox keysdb
1306 addrTox 1308 toxport
1307 (case mbxmpp of 1309 (case mbxmpp of
1308 Nothing -> \_ _ _ -> return () 1310 Nothing -> \_ _ _ -> return ()
1309 Just xmpp -> onNewToxSession xmpp ssvar invc) 1311 Just xmpp -> onNewToxSession xmpp ssvar invc)
1310 (dhtkey opts) 1312 (dhtkey opts)
1311 (\_ _ -> return ()) -- TODO: TCP relay send 1313 (\_ _ -> return ()) -- TODO: TCP relay send
1314 -- addrTox <- getBindAddress toxport (ip6tox opts)
1312 (quitTox, toxStrap4, toxStrap6) <- Tox.forkTox tox True 1315 (quitTox, toxStrap4, toxStrap6) <- Tox.forkTox tox True
1313 1316
1314 toxSearches <- atomically $ newTVar Map.empty 1317 toxSearches <- atomically $ newTVar Map.empty
@@ -1491,7 +1494,7 @@ initTox opts ssvar keysdb mbxmpp invc = case porttox opts of
1491 ips :: IO [SockAddr] 1494 ips :: IO [SockAddr]
1492 ips = readExternals Tox.nodeAddr [ Tox.routing4 $ Tox.toxRouting tox 1495 ips = readExternals Tox.nodeAddr [ Tox.routing4 $ Tox.toxRouting tox
1493 , Tox.routing6 $ Tox.toxRouting tox ] 1496 , Tox.routing6 $ Tox.toxRouting tox ]
1494 return (Just tox, quitTox, dhts, ips, [addrTox]) 1497 return (Just tox, quitTox, dhts, ips, [Tox.toxBindAddress tox])
1495 1498
1496initJabber :: Options 1499initJabber :: Options
1497 -> TVar (Map.Map Uniq24 AggregateSession) 1500 -> TVar (Map.Map Uniq24 AggregateSession)
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs
index b22cfdf3..30efefa8 100644
--- a/src/Network/Tox.hs
+++ b/src/Network/Tox.hs
@@ -23,6 +23,7 @@ import Control.Concurrent.Lifted.Instrument
23import Control.Concurrent.Lifted 23import Control.Concurrent.Lifted
24#endif 24#endif
25import Control.Concurrent.STM 25import Control.Concurrent.STM
26import Control.Exception (throwIO)
26import Control.Monad 27import Control.Monad
27import Crypto.PubKey.Curve25519 28import Crypto.PubKey.Curve25519
28import Crypto.Random 29import Crypto.Random
@@ -39,11 +40,12 @@ import Data.Time.Clock.POSIX (getPOSIXTime)
39import Data.Word 40import Data.Word
40import Network.Socket 41import Network.Socket
41import System.Endian 42import System.Endian
43import System.IO.Error
42 44
43import Network.BitTorrent.DHT.Token as Token 45import Network.BitTorrent.DHT.Token as Token
44import qualified Data.Wrapper.PSQ as PSQ 46import qualified Data.Wrapper.PSQ as PSQ
45import System.Global6 47import System.Global6
46import Network.Address (WantIP (..),IP) 48import Network.Address (WantIP (..),IP,getBindAddress)
47import qualified Network.Kademlia.Routing as R 49import qualified Network.Kademlia.Routing as R
48import Network.QueryResponse 50import Network.QueryResponse
49import Crypto.Tox 51import Crypto.Tox
@@ -209,6 +211,7 @@ data Tox extra = Tox
209 , toxOnionRoutes :: OnionRouter 211 , toxOnionRoutes :: OnionRouter
210 , toxContactInfo :: ContactInfo extra 212 , toxContactInfo :: ContactInfo extra
211 , toxAnnounceToLan :: IO () 213 , toxAnnounceToLan :: IO ()
214 , toxBindAddress :: SockAddr
212 } 215 }
213 216
214 217
@@ -268,13 +271,21 @@ getOnionAlias crypto dhtself remoteNode = atomically $ do
268 return $ Onion.OnionDestination Onion.SearchingAlias alias Nothing 271 return $ Onion.OnionDestination Onion.SearchingAlias alias Nothing
269 272
270newTox :: TVar Onion.AnnouncedKeys -- ^ Store of announced keys we are a rendezvous for. 273newTox :: TVar Onion.AnnouncedKeys -- ^ Store of announced keys we are a rendezvous for.
271 -> SockAddr -- ^ Bind-address to listen on. 274 -> [String] -- ^ Bind-address to listen on. Must provide at least one.
272 -> ( ContactInfo extra -> SockAddr -> Session -> IO () ) 275 -> ( ContactInfo extra -> SockAddr -> Session -> IO () )
273 -> Maybe SecretKey -- ^ Optional DHT secret key to use. 276 -> Maybe SecretKey -- ^ Optional DHT secret key to use.
274 -> ( Int -> Onion.OnionResponse Onion.N1 -> IO () ) -- ^ TCP-bound onion responses. 277 -> ( Int -> Onion.OnionResponse Onion.N1 -> IO () ) -- ^ TCP-bound onion responses.
275 -> IO (Tox extra) 278 -> IO (Tox extra)
276newTox keydb addr onsess suppliedDHTKey tcp = do 279newTox keydb bindspecs onsess suppliedDHTKey tcp = do
277 (udp,sock) <- {- addVerbosity <$> -} udpTransport' addr 280 addrs <- mapM (`getBindAddress` True) bindspecs
281 let tryBind addr next _ = udpTransport' addr `catchIOError` (next . Just)
282 failedBind mbe = do
283 forM_ mbe $ \e -> do
284 dput XDHT $ "tox udp bind error: " ++ show addrs ++ " " ++ show e
285 throwIO e
286 throwIO $ userError "Tox UDP listen port?"
287 (udp,sock) <- foldr tryBind failedBind addrs Nothing
288 addr <- getSocketName sock
278 tox <- newToxOverTransport keydb addr onsess suppliedDHTKey udp tcp 289 tox <- newToxOverTransport keydb addr onsess suppliedDHTKey udp tcp
279 return tox { toxAnnounceToLan = announceToLan sock (key2id $ transportPublic $ toxCryptoKeys tox) } 290 return tox { toxAnnounceToLan = announceToLan sock (key2id $ transportPublic $ toxCryptoKeys tox) }
280 291
@@ -354,6 +365,7 @@ newToxOverTransport keydb addr onNewSession suppliedDHTKey udp tcp = do
354 , toxOnionRoutes = orouter 365 , toxOnionRoutes = orouter
355 , toxContactInfo = roster 366 , toxContactInfo = roster
356 , toxAnnounceToLan = return () 367 , toxAnnounceToLan = return ()
368 , toxBindAddress = addr
357 } 369 }
358 370
359onionTimeout :: Tox extra -> DHT.TransactionId -> Onion.OnionDestination RouteId -> STM (Onion.OnionDestination RouteId, Int) 371onionTimeout :: Tox extra -> DHT.TransactionId -> Onion.OnionDestination RouteId -> STM (Onion.OnionDestination RouteId, Int)