From f876da224f503542394b3d7614fcc161106ebbb4 Mon Sep 17 00:00:00 2001 From: joe Date: Sat, 29 Jul 2017 00:58:52 -0400 Subject: Detect a global ipv6 address via "ip" command. --- Global6.hs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Global6.hs (limited to 'Global6.hs') diff --git a/Global6.hs b/Global6.hs new file mode 100644 index 00000000..346be708 --- /dev/null +++ b/Global6.hs @@ -0,0 +1,28 @@ +module 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