From 1b7b664d2b3ebbd86511b085e881c7223adcf334 Mon Sep 17 00:00:00 2001 From: joe Date: Thu, 7 Jun 2018 20:51:44 -0400 Subject: Parse ip command output to determine broadcast addresses. --- src/System/Global6.hs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/System/Global6.hs') 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 @@ module System.Global6 where import Control.Monad +import Control.Applicative import Data.IP import Data.List import Data.Maybe import Network.Socket import System.Process import Text.Read +import System.IO parseIpAddr :: String -> Maybe IPv6 parseIpAddr s = do @@ -26,3 +28,21 @@ global6 = do addrs <- lines <$> readProcess "ip" ["-o","-6","addr"] "" return $ foldr1 mplus $ map parseIpAddr addrs +everyOther (x:_:xs) = x : everyOther xs +everyOther xs = xs + + +-- | Obtain all available IP broadcast addresses (in dotted quad or IPv6 colon +-- format) as Strings. +broadcastAddrs :: IO [String] +broadcastAddrs = parseBroadcastAddrs <$> readProcess "ip" ["-o","addr"] "" + +parseBroadcastAddrs :: String -> [String] +parseBroadcastAddrs ipoutput = brds + where + brds = mapMaybe getbrd as + assoclist ws = zip (everyOther ws) (everyOther $ drop 1 ws) + as = map (assoclist . drop 2 . words) $ lines ipoutput + getbrd alist = do + ip <- lookup "inet" alist <|> lookup "inet6" alist + lookup "brd" alist -- cgit v1.2.3