summaryrefslogtreecommitdiff
path: root/src/System/Global6.hs
blob: f70a8547f1726d00a268ba2d69570e8d60ac9ebb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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