diff options
Diffstat (limited to 'src/System/Global6.hs')
-rw-r--r-- | src/System/Global6.hs | 20 |
1 files changed, 20 insertions, 0 deletions
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 | ||