summaryrefslogtreecommitdiff
path: root/src/Network
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network')
-rw-r--r--src/Network/Tox.hs20
-rw-r--r--src/Network/Tox/Avahi.hs39
2 files changed, 36 insertions, 23 deletions
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs
index 93d42eca..00dfcf9f 100644
--- a/src/Network/Tox.hs
+++ b/src/Network/Tox.hs
@@ -486,12 +486,20 @@ routing4nodeInfo :: DHT.Routing -> IO NodeInfo
486routing4nodeInfo (DHT.routing4 -> tv) = R.thisNode <$> readTVarIO tv 486routing4nodeInfo (DHT.routing4 -> tv) = R.thisNode <$> readTVarIO tv
487 487
488dnssdAnnounce :: Tox extra -> IO () 488dnssdAnnounce :: Tox extra -> IO ()
489dnssdAnnounce (toxRouting -> r) = do 489dnssdAnnounce tox = do
490 ni <- routing4nodeInfo r 490 ni <- routing4nodeInfo (toxRouting tox)
491 announceToxService (nodePort ni) (nodeId ni) 491 keys <- fmap (key2id . snd) <$> atomically (userKeys $ toxCryptoKeys tox)
492 492 announceToxService (nodePort ni) (nodeId ni) (listToMaybe keys)
493dnssdDiscover :: Tox extra -> NodeInfo -> IO () 493
494dnssdDiscover (toxDHT -> client) ni = void $ DHT.ping client ni 494dnssdDiscover :: Tox extra -> NodeInfo -> (Maybe NodeId) -> IO ()
495dnssdDiscover tox ni toxid = do
496 acts <- atomically $ readTVar $ accounts $ toxContactInfo tox
497 now <- getPOSIXTime
498 forM toxid $ \tid ->
499 forM acts $ \act ->
500 atomically $ setContactAddr now (id2key tid) ni act
501
502 void $ DHT.ping (toxDHT tox) ni
495 503
496forkTox :: Tox extra -> IO (IO (), [NodeInfo] -> [NodeInfo] -> IO (), [NodeInfo] -> [NodeInfo] -> IO ()) 504forkTox :: Tox extra -> IO (IO (), [NodeInfo] -> [NodeInfo] -> IO (), [NodeInfo] -> [NodeInfo] -> IO ())
497forkTox tox = do 505forkTox tox = do
diff --git a/src/Network/Tox/Avahi.hs b/src/Network/Tox/Avahi.hs
index 7fe87db2..559e5fd7 100644
--- a/src/Network/Tox/Avahi.hs
+++ b/src/Network/Tox/Avahi.hs
@@ -1,16 +1,18 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# LANGUAGE RecordWildCards #-} 2{-# LANGUAGE RecordWildCards #-}
2{-# LANGUAGE ViewPatterns #-} 3{-# LANGUAGE ViewPatterns #-}
3module Network.Tox.Avahi 4module Network.Tox.Avahi
4 ( module Network.Tox.Avahi 5 ( module Network.Tox.Avahi
5 , NodeInfo(..) 6 , NodeInfo(..)
6 , NodeId(..) 7 , NodeId
7 ) where 8 ) where
8 9
9import Data.Foldable 10import Control.Applicative
10import Network.Address 11import Data.Foldable
11import Network.Avahi 12import Network.Address
12import Network.Tox.NodeId 13import Network.Avahi
13import Text.Read 14import Network.Tox.NodeId
15import Text.Read
14 16
15toxServiceName :: String 17toxServiceName :: String
16toxServiceName = "_tox_dht._udp" 18toxServiceName = "_tox_dht._udp"
@@ -21,8 +23,8 @@ toxServiceDomain = "local"
21(<.>) :: String -> String -> String 23(<.>) :: String -> String -> String
22a <.> b = a ++ "." ++ b 24a <.> b = a ++ "." ++ b
23 25
24toxService :: String -> PortNumber -> NodeId -> Service 26toxService :: String -> PortNumber -> NodeId -> (Maybe NodeId) -> Service
25toxService hostname (fromIntegral -> port) (show -> extra) = 27toxService hostname (fromIntegral -> port) dhtkey toxid =
26 Service { 28 Service {
27 serviceProtocol = PROTO_UNSPEC, 29 serviceProtocol = PROTO_UNSPEC,
28 serviceName = "Tox DHT @ " ++ hostname, 30 serviceName = "Tox DHT @ " ++ hostname,
@@ -31,16 +33,17 @@ toxService hostname (fromIntegral -> port) (show -> extra) =
31 serviceHost = if null hostname then "" else hostname <.> toxServiceDomain, 33 serviceHost = if null hostname then "" else hostname <.> toxServiceDomain,
32 serviceAddress = Nothing, 34 serviceAddress = Nothing,
33 servicePort = port, 35 servicePort = port,
34 serviceText = extra 36 serviceText = maybe (show dhtkey) (show . ((,) dhtkey)) toxid
35 } 37 }
36 38
37announceToxServiceWithHostname :: String -> PortNumber -> NodeId -> IO () 39announceToxServiceWithHostname :: String -> PortNumber -> NodeId -> (Maybe NodeId) -> IO ()
38announceToxServiceWithHostname = ((.).(.).(.)) announce toxService 40announceToxServiceWithHostname = (boobs.boobs) announce toxService
41 where boobs = ((.).(.))
39 42
40announceToxService :: PortNumber -> NodeId -> IO () 43announceToxService :: PortNumber -> NodeId -> (Maybe NodeId) -> IO ()
41announceToxService = announceToxServiceWithHostname "" 44announceToxService = announceToxServiceWithHostname ""
42 45
43queryToxService :: (NodeInfo -> IO ()) -> IO () 46queryToxService :: (NodeInfo -> Maybe NodeId -> IO ()) -> IO ()
44queryToxService cb = 47queryToxService cb =
45 browse $ 48 browse $
46 BrowseQuery 49 BrowseQuery
@@ -50,8 +53,10 @@ queryToxService cb =
50 , lookupCallback = runCallback 53 , lookupCallback = runCallback
51 } 54 }
52 where 55 where
53 runCallback x@Service {..} = do 56 runCallback Service {..} = do
54 let nid = readMaybe serviceText 57 let both :: Maybe (NodeId, NodeId)
58 both = readMaybe serviceText
59 nid = (fst <$> both) <|> readMaybe serviceText
55 addr = readMaybe =<< serviceAddress 60 addr = readMaybe =<< serviceAddress
56 p = fromIntegral servicePort 61 p = fromIntegral servicePort
57 forM_ nid $ \n -> forM_ addr $ \a -> cb (NodeInfo n a p) 62 forM_ nid $ \n -> forM_ addr $ \a -> cb (NodeInfo n a p) (snd <$> both)