diff options
-rw-r--r-- | examples/dhtd.hs | 17 | ||||
-rw-r--r-- | src/Network/Tox.hs | 20 |
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 | ||
1056 | data Options = Options | 1056 | data 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 | |||
1076 | sensibleDefaults :: Options | 1076 | sensibleDefaults :: Options |
1077 | sensibleDefaults = Options | 1077 | sensibleDefaults = 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 | } |
1108 | parseArgs (arg:args) opts = parseArgs args opts | 1108 | parseArgs (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]) |
1300 | initTox opts ssvar keysdb mbxmpp invc = case porttox opts of | 1303 | initTox 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 | ||
1496 | initJabber :: Options | 1499 | initJabber :: 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 | |||
23 | import Control.Concurrent.Lifted | 23 | import Control.Concurrent.Lifted |
24 | #endif | 24 | #endif |
25 | import Control.Concurrent.STM | 25 | import Control.Concurrent.STM |
26 | import Control.Exception (throwIO) | ||
26 | import Control.Monad | 27 | import Control.Monad |
27 | import Crypto.PubKey.Curve25519 | 28 | import Crypto.PubKey.Curve25519 |
28 | import Crypto.Random | 29 | import Crypto.Random |
@@ -39,11 +40,12 @@ import Data.Time.Clock.POSIX (getPOSIXTime) | |||
39 | import Data.Word | 40 | import Data.Word |
40 | import Network.Socket | 41 | import Network.Socket |
41 | import System.Endian | 42 | import System.Endian |
43 | import System.IO.Error | ||
42 | 44 | ||
43 | import Network.BitTorrent.DHT.Token as Token | 45 | import Network.BitTorrent.DHT.Token as Token |
44 | import qualified Data.Wrapper.PSQ as PSQ | 46 | import qualified Data.Wrapper.PSQ as PSQ |
45 | import System.Global6 | 47 | import System.Global6 |
46 | import Network.Address (WantIP (..),IP) | 48 | import Network.Address (WantIP (..),IP,getBindAddress) |
47 | import qualified Network.Kademlia.Routing as R | 49 | import qualified Network.Kademlia.Routing as R |
48 | import Network.QueryResponse | 50 | import Network.QueryResponse |
49 | import Crypto.Tox | 51 | import 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 | ||
270 | newTox :: TVar Onion.AnnouncedKeys -- ^ Store of announced keys we are a rendezvous for. | 273 | newTox :: 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) |
276 | newTox keydb addr onsess suppliedDHTKey tcp = do | 279 | newTox 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 | ||
359 | onionTimeout :: Tox extra -> DHT.TransactionId -> Onion.OnionDestination RouteId -> STM (Onion.OnionDestination RouteId, Int) | 371 | onionTimeout :: Tox extra -> DHT.TransactionId -> Onion.OnionDestination RouteId -> STM (Onion.OnionDestination RouteId, Int) |