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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
{-# OPTIONS_GHC -fno-cse #-}
{-| Obtain a MAC address for the host system, on *NIX and Windows.
-}
module System.Info.MAC
( mac
, macs
, nic
, nics
, refresh
) where
import Data.MAC
import System.Info.MAC.Fetch
import Data.IORef
import System.IO
import System.IO.Unsafe
import Data.Maybe
import Control.Applicative
{-| Fetch MAC address, using a cached value if it is available.
-}
mac :: IO (Maybe MAC)
mac = listToMaybe <$> macs
{-| Fetch MAC addresses, using a cached value if it is available.
-}
macs :: IO [MAC]
macs = map snd <$> nics
{-| Fetch a name-MAC pair, using a cached value if it is available.
-}
nic :: IO (Maybe (String, MAC))
nic = listToMaybe <$> nics
{-| Fetch name-MAC pairs, using a cached value if it is available.
-}
nics :: IO [(String, MAC)]
nics = do
val <- readIORef fetched
case val of [ ] -> refresh
_:_ -> return val
{-| Explicitly re-run the MAC reading operation.
-}
refresh :: IO [(String, MAC)]
refresh = do
res <- fetchNICs
writeIORef fetched res
return res
{-# NOINLINE fetched #-}
fetched = unsafePerformIO $ newIORef []
|