summaryrefslogtreecommitdiff
path: root/Global6.hs
blob: 346be70883e5021003a5e829b602fdb7f8089a30 (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 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