summaryrefslogtreecommitdiff
path: root/src/System
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2018-06-07 20:51:44 -0400
committerjoe <joe@jerkface.net>2018-06-07 20:51:44 -0400
commit1b7b664d2b3ebbd86511b085e881c7223adcf334 (patch)
tree4a3cedf1c8074945f263a64cc3ee5a7d77f9b868 /src/System
parent3948b015e5bd1537154213cf115c4b88e95cdefe (diff)
Parse ip command output to determine broadcast addresses.
Diffstat (limited to 'src/System')
-rw-r--r--src/System/Global6.hs20
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 @@
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