summaryrefslogtreecommitdiff
path: root/src/Network/Tox.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/Tox.hs')
-rw-r--r--src/Network/Tox.hs20
1 files changed, 16 insertions, 4 deletions
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)