diff options
author | joe <joe@jerkface.net> | 2017-09-15 03:29:46 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-09-15 03:29:46 -0400 |
commit | 718f00853fa3b42940d6544c054bb23fb38ba0c2 (patch) | |
tree | 2ce8e263cd46bcecf86350d7d450fddfbbbe7f7d /src/System | |
parent | de0296e5c8563ea2dc7216142e474eb4e4cb46d6 (diff) |
Moved Global6 to hierarchical name.
Diffstat (limited to 'src/System')
-rw-r--r-- | src/System/Global6.hs | 28 |
1 files changed, 28 insertions, 0 deletions
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 @@ | |||
1 | module System.Global6 where | ||
2 | |||
3 | import Control.Monad | ||
4 | import Data.IP | ||
5 | import Data.List | ||
6 | import Data.Maybe | ||
7 | import Network.Socket | ||
8 | import System.Process | ||
9 | import Text.Read | ||
10 | |||
11 | parseIpAddr :: String -> Maybe IPv6 | ||
12 | parseIpAddr s = do | ||
13 | let ws = words s | ||
14 | (addr,bs) = splitAt 1 $ drop 1 $ dropWhile (/= "inet6") ws | ||
15 | guard ("global" `elem` bs) | ||
16 | addr <- listToMaybe addr | ||
17 | guard (not $ isPrefixOf "fd" addr) | ||
18 | guard (not $ isPrefixOf "fc" addr) | ||
19 | let (addr',slash) = break (=='/') addr | ||
20 | ip6 <- readMaybe addr' | ||
21 | return $ (ip6 :: IPv6) | ||
22 | |||
23 | |||
24 | global6 :: IO (Maybe IPv6) | ||
25 | global6 = do | ||
26 | addrs <- lines <$> readProcess "ip" ["-o","-6","addr"] "" | ||
27 | return $ foldr1 mplus $ map parseIpAddr addrs | ||
28 | |||