diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Network/Tox.hs | 4 | ||||
-rw-r--r-- | src/System/Global6.hs | 20 |
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. |
494 | announceToLan :: Socket -> NodeId -> IO () | 494 | announceToLan :: Socket -> NodeId -> IO () |
495 | announceToLan sock nid = do | 495 | announceToLan 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 @@ | |||
1 | module System.Global6 where | 1 | module System.Global6 where |
2 | 2 | ||
3 | import Control.Monad | 3 | import Control.Monad |
4 | import Control.Applicative | ||
4 | import Data.IP | 5 | import Data.IP |
5 | import Data.List | 6 | import Data.List |
6 | import Data.Maybe | 7 | import Data.Maybe |
7 | import Network.Socket | 8 | import Network.Socket |
8 | import System.Process | 9 | import System.Process |
9 | import Text.Read | 10 | import Text.Read |
11 | import System.IO | ||
10 | 12 | ||
11 | parseIpAddr :: String -> Maybe IPv6 | 13 | parseIpAddr :: String -> Maybe IPv6 |
12 | parseIpAddr s = do | 14 | parseIpAddr 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 | ||
31 | everyOther (x:_:xs) = x : everyOther xs | ||
32 | everyOther xs = xs | ||
33 | |||
34 | |||
35 | -- | Obtain all available IP broadcast addresses (in dotted quad or IPv6 colon | ||
36 | -- format) as Strings. | ||
37 | broadcastAddrs :: IO [String] | ||
38 | broadcastAddrs = parseBroadcastAddrs <$> readProcess "ip" ["-o","addr"] "" | ||
39 | |||
40 | parseBroadcastAddrs :: String -> [String] | ||
41 | parseBroadcastAddrs 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 | ||