summaryrefslogtreecommitdiff
path: root/Global6.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-07-29 00:58:52 -0400
committerjoe <joe@jerkface.net>2017-07-29 01:03:35 -0400
commitf876da224f503542394b3d7614fcc161106ebbb4 (patch)
tree411d1c94ac482a94dcaeb6ff17175673bce0939b /Global6.hs
parentd0ff6c3ac977035f3493b679978da73517550028 (diff)
Detect a global ipv6 address via "ip" command.
Diffstat (limited to 'Global6.hs')
-rw-r--r--Global6.hs28
1 files changed, 28 insertions, 0 deletions
diff --git a/Global6.hs b/Global6.hs
new file mode 100644
index 00000000..346be708
--- /dev/null
+++ b/Global6.hs
@@ -0,0 +1,28 @@
1module Global6 where
2
3import Control.Monad
4import Data.IP
5import Data.List
6import Data.Maybe
7import Network.Socket
8import System.Process
9import Text.Read
10
11parseIpAddr :: String -> Maybe IPv6
12parseIpAddr 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
24global6 :: IO (Maybe IPv6)
25global6 = do
26 addrs <- lines <$> readProcess "ip" ["-o","-6","addr"] ""
27 return $ foldr1 mplus $ map parseIpAddr addrs
28