summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Network/Tox.hs4
-rw-r--r--src/System/Global6.hs20
2 files changed, 23 insertions, 1 deletions
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs
index a9b4f2e6..5bd23da8 100644
--- a/src/Network/Tox.hs
+++ b/src/Network/Tox.hs
@@ -493,8 +493,10 @@ forkTox tox = do
493-- TODO: Don't export this. 493-- TODO: Don't export this.
494announceToLan :: Socket -> NodeId -> IO () 494announceToLan :: Socket -> NodeId -> IO ()
495announceToLan sock nid = do 495announceToLan sock nid = do
496 addrs <- broadcastAddrs
497 forM_ addrs $ \addr -> do
496 (broadcast_info:_) <- getAddrInfo (Just defaultHints { addrFlags = [AI_NUMERICHOST], addrSocketType = Datagram }) 498 (broadcast_info:_) <- getAddrInfo (Just defaultHints { addrFlags = [AI_NUMERICHOST], addrSocketType = Datagram })
497 (Just "192.168.1.255") -- TODO: Detect broadcast address. 499 (Just addr) -- TODO: Detect broadcast address.
498 (Just "33445") 500 (Just "33445")
499 let broadcast = addrAddress broadcast_info 501 let broadcast = addrAddress broadcast_info
500 bs = S.runPut $ DHT.putMessage (DHT.DHTLanDiscovery nid) 502 bs = S.runPut $ DHT.putMessage (DHT.DHTLanDiscovery nid)
diff --git a/src/System/Global6.hs b/src/System/Global6.hs
index f70a8547..cc8ed020 100644
--- a/src/System/Global6.hs
+++ b/src/System/Global6.hs
@@ -1,12 +1,14 @@
1module System.Global6 where 1module System.Global6 where
2 2
3import Control.Monad 3import Control.Monad
4import Control.Applicative
4import Data.IP 5import Data.IP
5import Data.List 6import Data.List
6import Data.Maybe 7import Data.Maybe
7import Network.Socket 8import Network.Socket
8import System.Process 9import System.Process
9import Text.Read 10import Text.Read
11import System.IO
10 12
11parseIpAddr :: String -> Maybe IPv6 13parseIpAddr :: String -> Maybe IPv6
12parseIpAddr s = do 14parseIpAddr s = do
@@ -26,3 +28,21 @@ global6 = do
26 addrs <- lines <$> readProcess "ip" ["-o","-6","addr"] "" 28 addrs <- lines <$> readProcess "ip" ["-o","-6","addr"] ""
27 return $ foldr1 mplus $ map parseIpAddr addrs 29 return $ foldr1 mplus $ map parseIpAddr addrs
28 30
31everyOther (x:_:xs) = x : everyOther xs
32everyOther xs = xs
33
34
35-- | Obtain all available IP broadcast addresses (in dotted quad or IPv6 colon
36-- format) as Strings.
37broadcastAddrs :: IO [String]
38broadcastAddrs = parseBroadcastAddrs <$> readProcess "ip" ["-o","addr"] ""
39
40parseBroadcastAddrs :: String -> [String]
41parseBroadcastAddrs ipoutput = brds
42 where
43 brds = mapMaybe getbrd as
44 assoclist ws = zip (everyOther ws) (everyOther $ drop 1 ws)
45 as = map (assoclist . drop 2 . words) $ lines ipoutput
46 getbrd alist = do
47 ip <- lookup "inet" alist <|> lookup "inet6" alist
48 lookup "brd" alist