From 718f00853fa3b42940d6544c054bb23fb38ba0c2 Mon Sep 17 00:00:00 2001 From: joe Date: Fri, 15 Sep 2017 03:29:46 -0400 Subject: Moved Global6 to hierarchical name. --- src/System/Global6.hs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/System/Global6.hs (limited to 'src/System') diff --git a/src/System/Global6.hs b/src/System/Global6.hs new file mode 100644 index 00000000..f70a8547 --- /dev/null +++ b/src/System/Global6.hs @@ -0,0 +1,28 @@ +module System.Global6 where + +import Control.Monad +import Data.IP +import Data.List +import Data.Maybe +import Network.Socket +import System.Process +import Text.Read + +parseIpAddr :: String -> Maybe IPv6 +parseIpAddr s = do + let ws = words s + (addr,bs) = splitAt 1 $ drop 1 $ dropWhile (/= "inet6") ws + guard ("global" `elem` bs) + addr <- listToMaybe addr + guard (not $ isPrefixOf "fd" addr) + guard (not $ isPrefixOf "fc" addr) + let (addr',slash) = break (=='/') addr + ip6 <- readMaybe addr' + return $ (ip6 :: IPv6) + + +global6 :: IO (Maybe IPv6) +global6 = do + addrs <- lines <$> readProcess "ip" ["-o","-6","addr"] "" + return $ foldr1 mplus $ map parseIpAddr addrs + -- cgit v1.2.3